Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-nds32.c
Line
Count
Source
1
/* NDS32-specific support for 32-bit ELF.
2
   Copyright (C) 2012-2026 Free Software Foundation, Inc.
3
   Contributed by Andes Technology Corporation.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20
   02110-1301, USA.  */
21
22
23
#include "sysdep.h"
24
#include "bfd.h"
25
#include "bfdlink.h"
26
#include "libbfd.h"
27
#include "elf-bfd.h"
28
#include "libiberty.h"
29
#include "elf/nds32.h"
30
#include "opcode/nds32.h"
31
#include "elf32-nds32.h"
32
#include "opcode/cgen.h"
33
#include "../opcodes/nds32-opc.h"
34
35
/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
36
20
#define OCTETS_PER_BYTE(ABFD, SEC) 1
37
38
/* Relocation HOWTO functions.  */
39
static bfd_reloc_status_type nds32_elf_ignore_reloc
40
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41
static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
42
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43
static bfd_reloc_status_type nds32_elf_hi20_reloc
44
  (bfd *, arelent *, asymbol *, void *,
45
   asection *, bfd *, char **);
46
static bfd_reloc_status_type nds32_elf_lo12_reloc
47
  (bfd *, arelent *, asymbol *, void *,
48
   asection *, bfd *, char **);
49
static bfd_reloc_status_type nds32_elf_generic_reloc
50
  (bfd *, arelent *, asymbol *, void *,
51
   asection *, bfd *, char **);
52
static bfd_reloc_status_type nds32_elf_sda15_reloc
53
  (bfd *, arelent *, asymbol *, void *,
54
   asection *, bfd *, char **);
55
56
/* Helper functions for HOWTO.  */
57
static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
58
  (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
59
   asection *, bfd_vma, bfd_vma);
60
61
/* Nds32 helper functions.  */
62
static bfd_vma calculate_memory_address
63
  (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
64
static int nds32_get_section_contents (bfd *, asection *,
65
               bfd_byte **, bool);
66
static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
67
         Elf_Internal_Sym **);
68
static bool  nds32_relax_fp_as_gp
69
  (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
70
   Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
71
   Elf_Internal_Sym *isymbuf);
72
static bool nds32_fag_remove_unused_fpbase
73
  (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
74
   Elf_Internal_Rela *irelend);
75
76
enum
77
{
78
  MACH_V1 = bfd_mach_n1h,
79
  MACH_V2 = bfd_mach_n1h_v2,
80
  MACH_V3 = bfd_mach_n1h_v3,
81
  MACH_V3M = bfd_mach_n1h_v3m
82
};
83
84
#define MIN(a, b) ((a) > (b) ? (b) : (a))
85
#define MAX(a, b) ((a) > (b) ? (a) : (b))
86
87
/* The name of the dynamic interpreter.  This is put in the .interp
88
   section.  */
89
0
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
90
91
0
#define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
92
0
          && (flags) & SEC_LOAD \
93
0
          && (flags) & SEC_READONLY)
94
95
/* The nop opcode we use.  */
96
0
#define NDS32_NOP32 0x40000009
97
0
#define NDS32_NOP16 0x9200
98
99
/* The size in bytes of an entry in the procedure linkage table.  */
100
0
#define PLT_ENTRY_SIZE 24
101
#define PLT_HEADER_SIZE 24
102
103
/* The first entry in a procedure linkage table are reserved,
104
   and the initial contents are unimportant (we zero them out).
105
   Subsequent entries look like this.  */
106
0
#define PLT0_ENTRY_WORD0  0x46f00000    /* sethi   r15, HI20(.got+4)    */
107
0
#define PLT0_ENTRY_WORD1  0x58f78000    /* ori     r15, r25, LO12(.got+4) */
108
0
#define PLT0_ENTRY_WORD2  0x05178000    /* lwi     r17, [r15+0]     */
109
0
#define PLT0_ENTRY_WORD3  0x04f78001    /* lwi     r15, [r15+4]     */
110
0
#define PLT0_ENTRY_WORD4  0x4a003c00    /* jr    r15        */
111
112
/* $ta is change to $r15 (from $r25).  */
113
0
#define PLT0_PIC_ENTRY_WORD0  0x46f00000  /* sethi   r15, HI20(got[1]@GOT)  */
114
0
#define PLT0_PIC_ENTRY_WORD1  0x58f78000  /* ori     r15, r15, LO12(got[1]@GOT) */
115
0
#define PLT0_PIC_ENTRY_WORD2  0x40f7f400  /* add     r15, gp, r15     */
116
0
#define PLT0_PIC_ENTRY_WORD3  0x05178000  /* lwi     r17, [r15+0]     */
117
0
#define PLT0_PIC_ENTRY_WORD4  0x04f78001  /* lwi     r15, [r15+4]     */
118
0
#define PLT0_PIC_ENTRY_WORD5  0x4a003c00  /* jr    r15        */
119
120
0
#define PLT_ENTRY_WORD0  0x46f00000    /* sethi   r15, HI20(&got[n+3])      */
121
0
#define PLT_ENTRY_WORD1  0x04f78000    /* lwi     r15, r15, LO12(&got[n+3]) */
122
0
#define PLT_ENTRY_WORD2  0x4a003c00    /* jr    r15           */
123
0
#define PLT_ENTRY_WORD3  0x45000000    /* movi    r16, sizeof(RELA) * n     */
124
0
#define PLT_ENTRY_WORD4  0x48000000    /* j    .plt0.         */
125
126
0
#define PLT_PIC_ENTRY_WORD0  0x46f00000    /* sethi  r15, HI20(got[n+3]@GOT)    */
127
0
#define PLT_PIC_ENTRY_WORD1  0x58f78000    /* ori    r15, r15,    LO12(got[n+3]@GOT) */
128
0
#define PLT_PIC_ENTRY_WORD2  0x38febc02    /* lw   r15, [gp+r15]        */
129
0
#define PLT_PIC_ENTRY_WORD3  0x4a003c00    /* jr   r15          */
130
0
#define PLT_PIC_ENTRY_WORD4  0x45000000    /* movi   r16, sizeof(RELA) * n      */
131
0
#define PLT_PIC_ENTRY_WORD5  0x48000000    /* j    .plt0          */
132
133
/* These are macros used to get the relocation accurate value.  */
134
4
#define ACCURATE_8BIT_S1  (0x100)
135
0
#define ACCURATE_U9BIT_S1 (0x400)
136
#define ACCURATE_12BIT_S1 (0x2000)
137
0
#define ACCURATE_14BIT_S1 (0x4000)
138
#define ACCURATE_19BIT    (0x40000)
139
140
/* These are macros used to get the relocation conservative value.  */
141
0
#define CONSERVATIVE_8BIT_S1  (0x100 - 4)
142
0
#define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
143
0
#define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
144
0
#define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
145
/* These must be more conservative because the address may be in
146
   different segment.  */
147
#define CONSERVATIVE_15BIT  (0x4000 - 0x1000)
148
#define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
149
#define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
150
#define CONSERVATIVE_19BIT  (0x40000 - 0x1000)
151
0
#define CONSERVATIVE_20BIT  (0x80000 - 0x1000)
152
153
static int is_SDA_BASE_set = 0;
154
155
/* Convert ELF-VER in eflags to string for debugging purpose.  */
156
static const char *const nds32_elfver_strtab[] =
157
{
158
  "ELF-1.2",
159
  "ELF-1.3",
160
  "ELF-1.4",
161
};
162
163
/* The nds32 linker needs to keep track of the number of relocs that it
164
   decides to copy in check_relocs for each symbol.  This is so that
165
   it can discard PC relative relocs if it doesn't need them when
166
   linking with -Bsymbolic.  We store the information in a field
167
   extending the regular ELF linker hash table.  */
168
169
/* This structure keeps track of the number of PC relative relocs we
170
   have copied for a given symbol.  */
171
172
struct elf_nds32_pcrel_relocs_copied
173
{
174
  /* Next section.  */
175
  struct elf_nds32_pcrel_relocs_copied *next;
176
  /* A section in dynobj.  */
177
  asection *section;
178
  /* Number of relocs copied in this section.  */
179
  bfd_size_type count;
180
};
181
182
enum elf_nds32_tls_type
183
{
184
  GOT_UNKNOWN = (0),
185
  GOT_NORMAL = (1 << 0),
186
  GOT_TLS_LE = (1 << 1),
187
  GOT_TLS_IE = (1 << 2),
188
  GOT_TLS_IEGP = (1 << 3),
189
  GOT_TLS_LD = (1 << 4),
190
  GOT_TLS_GD = (1 << 5),
191
  GOT_TLS_DESC = (1 << 6),
192
};
193
194
/* Nds32 ELF linker hash entry.  */
195
196
struct elf_nds32_link_hash_entry
197
{
198
  struct elf_link_hash_entry root;
199
200
  /* For checking relocation type.  */
201
  enum elf_nds32_tls_type tls_type;
202
203
  int offset_to_gp;
204
};
205
206
/* Get the nds32 ELF linker hash table from a link_info structure.  */
207
208
0
#define FP_BASE_NAME "_FP_BASE_"
209
static int check_start_export_sym = 0;
210
211
/* The offset for executable tls relaxation.  */
212
0
#define TP_OFFSET 0x0
213
214
typedef struct
215
{
216
  int min_id;
217
  int max_id;
218
  int count;
219
  int bias;
220
  int init;
221
} elf32_nds32_relax_group_t;
222
223
struct elf_nds32_obj_tdata
224
{
225
  struct elf_obj_tdata root;
226
227
  /* tls_type for each local got entry.  */
228
  char *local_got_tls_type;
229
230
  /* GOTPLT entries for TLS descriptors.  */
231
  bfd_vma *local_tlsdesc_gotent;
232
233
  /* for R_NDS32_RELAX_GROUP handling.  */
234
  elf32_nds32_relax_group_t relax_group;
235
236
  unsigned int hdr_size;
237
  int* offset_to_gp;
238
};
239
240
#define elf_nds32_tdata(bfd) \
241
0
  ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
242
243
#define elf32_nds32_local_got_tls_type(bfd) \
244
0
  (elf_nds32_tdata (bfd)->local_got_tls_type)
245
246
#define elf32_nds32_local_gp_offset(bfd) \
247
0
  (elf_nds32_tdata (bfd)->offset_to_gp)
248
249
0
#define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
250
251
#define elf32_nds32_relax_group_ptr(bfd) \
252
0
  &(elf_nds32_tdata (bfd)->relax_group)
253
254
static bool
255
nds32_elf_mkobject (bfd *abfd)
256
95.5k
{
257
95.5k
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata));
258
95.5k
}
259
260
/* Relocations used for relocation.  */
261
/* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
262
   initialize array nds32_elf_howto_table in any order. The benefit
263
   is that we can add any new relocations with any numbers and don't
264
   need to fill the gap by lots of EMPTY_HOWTO. */
265
#define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
266
  [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
267
268
static reloc_howto_type nds32_elf_howto_table[] =
269
{
270
  /* This reloc does nothing.  */
271
  HOWTO2 (R_NDS32_NONE,   /* type  */
272
   0,     /* rightshift  */
273
   4,     /* size  */
274
   32,      /* bitsize  */
275
   false,     /* pc_relative  */
276
   0,     /* bitpos  */
277
   complain_overflow_bitfield,/* complain_on_overflow  */
278
   bfd_elf_generic_reloc, /* special_function  */
279
   "R_NDS32_NONE",  /* name  */
280
   false,     /* partial_inplace  */
281
   0,     /* src_mask  */
282
   0,     /* dst_mask  */
283
   false),    /* pcrel_offset  */
284
285
  /* A 16 bit absolute relocation.  */
286
  HOWTO2 (R_NDS32_16,   /* type  */
287
   0,     /* rightshift  */
288
   2,     /* size  */
289
   16,      /* bitsize  */
290
   false,     /* pc_relative  */
291
   0,     /* bitpos  */
292
   complain_overflow_bitfield,/* complain_on_overflow  */
293
   nds32_elf_generic_reloc,/* special_function  */
294
   "R_NDS32_16",    /* name  */
295
   false,     /* partial_inplace  */
296
   0xffff,    /* src_mask  */
297
   0xffff,    /* dst_mask  */
298
   false),    /* pcrel_offset  */
299
300
  /* A 32 bit absolute relocation.  */
301
  HOWTO2 (R_NDS32_32,   /* type  */
302
   0,     /* rightshift  */
303
   4,     /* size  */
304
   32,      /* bitsize  */
305
   false,     /* pc_relative  */
306
   0,     /* bitpos  */
307
   complain_overflow_bitfield,/* complain_on_overflow  */
308
   nds32_elf_generic_reloc,/* special_function  */
309
   "R_NDS32_32",    /* name  */
310
   false,     /* partial_inplace  */
311
   0xffffffff,    /* src_mask  */
312
   0xffffffff,    /* dst_mask  */
313
   false),    /* pcrel_offset  */
314
315
  /* A 20 bit address.  */
316
  HOWTO2 (R_NDS32_20,   /* type  */
317
   0,     /* rightshift  */
318
   4,     /* size  */
319
   20,      /* bitsize  */
320
   false,     /* pc_relative  */
321
   0,     /* bitpos  */
322
   complain_overflow_unsigned,/* complain_on_overflow  */
323
   nds32_elf_generic_reloc,/* special_function  */
324
   "R_NDS32_20",    /* name  */
325
   false,     /* partial_inplace  */
326
   0xfffff,   /* src_mask  */
327
   0xfffff,   /* dst_mask  */
328
   false),    /* pcrel_offset  */
329
330
  /* An PC Relative 9-bit relocation, shifted by 2.
331
     This reloc is complicated because relocations are relative to pc & -4.
332
     i.e. branches in the right insn slot use the address of the left insn
333
     slot for pc.  */
334
  /* It's not clear whether this should have partial_inplace set or not.
335
     Branch relaxing in the assembler can store the addend in the insn,
336
     and if bfd_install_relocation gets called the addend may get added
337
     again.  */
338
  HOWTO2 (R_NDS32_9_PCREL,  /* type  */
339
   1,     /* rightshift  */
340
   2,     /* size  */
341
   8,     /* bitsize  */
342
   true,      /* pc_relative  */
343
   0,     /* bitpos  */
344
   complain_overflow_signed,/* complain_on_overflow  */
345
   nds32_elf_9_pcrel_reloc,/* special_function  */
346
   "R_NDS32_9_PCREL", /* name  */
347
   false,     /* partial_inplace  */
348
   0xff,      /* src_mask  */
349
   0xff,      /* dst_mask  */
350
   true),     /* pcrel_offset  */
351
352
  /* A relative 15 bit relocation, right shifted by 1.  */
353
  HOWTO2 (R_NDS32_15_PCREL, /* type  */
354
   1,     /* rightshift  */
355
   4,     /* size  */
356
   14,      /* bitsize  */
357
   true,      /* pc_relative  */
358
   0,     /* bitpos  */
359
   complain_overflow_signed,/* complain_on_overflow  */
360
   bfd_elf_generic_reloc, /* special_function  */
361
   "R_NDS32_15_PCREL",  /* name  */
362
   false,     /* partial_inplace  */
363
   0x3fff,    /* src_mask  */
364
   0x3fff,    /* dst_mask  */
365
   true),     /* pcrel_offset  */
366
367
  /* A relative 17 bit relocation, right shifted by 1.  */
368
  HOWTO2 (R_NDS32_17_PCREL, /* type  */
369
   1,     /* rightshift  */
370
   4,     /* size  */
371
   16,      /* bitsize  */
372
   true,      /* pc_relative  */
373
   0,     /* bitpos  */
374
   complain_overflow_signed,/* complain_on_overflow  */
375
   bfd_elf_generic_reloc, /* special_function  */
376
   "R_NDS32_17_PCREL",  /* name  */
377
   false,     /* partial_inplace  */
378
   0xffff,    /* src_mask  */
379
   0xffff,    /* dst_mask  */
380
   true),     /* pcrel_offset  */
381
382
  /* A relative 25 bit relocation, right shifted by 1.  */
383
  /* It's not clear whether this should have partial_inplace set or not.
384
     Branch relaxing in the assembler can store the addend in the insn,
385
     and if bfd_install_relocation gets called the addend may get added
386
     again.  */
387
  HOWTO2 (R_NDS32_25_PCREL, /* type  */
388
   1,     /* rightshift  */
389
   4,     /* size  */
390
   24,      /* bitsize  */
391
   true,      /* pc_relative  */
392
   0,     /* bitpos  */
393
   complain_overflow_signed,/* complain_on_overflow  */
394
   bfd_elf_generic_reloc, /* special_function  */
395
   "R_NDS32_25_PCREL",  /* name  */
396
   false,     /* partial_inplace  */
397
   0xffffff,    /* src_mask  */
398
   0xffffff,    /* dst_mask  */
399
   true),     /* pcrel_offset  */
400
401
  /* High 20 bits of address when lower 12 is or'd in.  */
402
  HOWTO2 (R_NDS32_HI20,   /* type  */
403
   12,      /* rightshift  */
404
   4,     /* size  */
405
   20,      /* bitsize  */
406
   false,     /* pc_relative  */
407
   0,     /* bitpos  */
408
   complain_overflow_dont,/* complain_on_overflow  */
409
   nds32_elf_hi20_reloc,  /* special_function  */
410
   "R_NDS32_HI20",  /* name  */
411
   false,     /* partial_inplace  */
412
   0x000fffff,    /* src_mask  */
413
   0x000fffff,    /* dst_mask  */
414
   false),    /* pcrel_offset  */
415
416
  /* Lower 12 bits of address.  */
417
  HOWTO2 (R_NDS32_LO12S3, /* type  */
418
   3,     /* rightshift  */
419
   4,     /* size  */
420
   9,     /* bitsize  */
421
   false,     /* pc_relative  */
422
   0,     /* bitpos  */
423
   complain_overflow_dont,/* complain_on_overflow  */
424
   nds32_elf_lo12_reloc,  /* special_function  */
425
   "R_NDS32_LO12S3",  /* name  */
426
   false,     /* partial_inplace  */
427
   0x000001ff,    /* src_mask  */
428
   0x000001ff,    /* dst_mask  */
429
   false),    /* pcrel_offset  */
430
431
  /* Lower 12 bits of address.  */
432
  HOWTO2 (R_NDS32_LO12S2, /* type  */
433
   2,     /* rightshift  */
434
   4,     /* size  */
435
   10,      /* bitsize  */
436
   false,     /* pc_relative  */
437
   0,     /* bitpos  */
438
   complain_overflow_dont,/* complain_on_overflow  */
439
   nds32_elf_lo12_reloc,  /* special_function  */
440
   "R_NDS32_LO12S2",  /* name  */
441
   false,     /* partial_inplace  */
442
   0x000003ff,    /* src_mask  */
443
   0x000003ff,    /* dst_mask  */
444
   false),    /* pcrel_offset  */
445
446
  /* Lower 12 bits of address.  */
447
  HOWTO2 (R_NDS32_LO12S1, /* type  */
448
   1,     /* rightshift  */
449
   4,     /* size  */
450
   11,      /* bitsize  */
451
   false,     /* pc_relative  */
452
   0,     /* bitpos  */
453
   complain_overflow_dont,/* complain_on_overflow  */
454
   nds32_elf_lo12_reloc,  /* special_function  */
455
   "R_NDS32_LO12S1",  /* name  */
456
   false,     /* partial_inplace  */
457
   0x000007ff,    /* src_mask  */
458
   0x000007ff,    /* dst_mask  */
459
   false),    /* pcrel_offset  */
460
461
  /* Lower 12 bits of address.  */
462
  HOWTO2 (R_NDS32_LO12S0, /* type  */
463
   0,     /* rightshift  */
464
   4,     /* size  */
465
   12,      /* bitsize  */
466
   false,     /* pc_relative  */
467
   0,     /* bitpos  */
468
   complain_overflow_dont,/* complain_on_overflow  */
469
   nds32_elf_lo12_reloc,  /* special_function  */
470
   "R_NDS32_LO12S0",  /* name  */
471
   false,     /* partial_inplace  */
472
   0x00000fff,    /* src_mask  */
473
   0x00000fff,    /* dst_mask  */
474
   false),    /* pcrel_offset  */
475
476
  /* Small data area 15 bits offset.  */
477
  HOWTO2 (R_NDS32_SDA15S3,  /* type  */
478
   3,     /* rightshift  */
479
   4,     /* size  */
480
   15,      /* bitsize  */
481
   false,     /* pc_relative  */
482
   0,     /* bitpos  */
483
   complain_overflow_signed,/* complain_on_overflow  */
484
   nds32_elf_sda15_reloc, /* special_function  */
485
   "R_NDS32_SDA15S3", /* name  */
486
   false,     /* partial_inplace  */
487
   0x00007fff,    /* src_mask  */
488
   0x00007fff,    /* dst_mask  */
489
   false),    /* pcrel_offset  */
490
491
  /* Small data area 15 bits offset.  */
492
  HOWTO2 (R_NDS32_SDA15S2,  /* type  */
493
   2,     /* rightshift  */
494
   4,     /* size  */
495
   15,      /* bitsize  */
496
   false,     /* pc_relative  */
497
   0,     /* bitpos  */
498
   complain_overflow_signed,/* complain_on_overflow  */
499
   nds32_elf_sda15_reloc, /* special_function  */
500
   "R_NDS32_SDA15S2", /* name  */
501
   false,     /* partial_inplace  */
502
   0x00007fff,    /* src_mask  */
503
   0x00007fff,    /* dst_mask  */
504
   false),    /* pcrel_offset  */
505
506
  /* Small data area 15 bits offset.  */
507
  HOWTO2 (R_NDS32_SDA15S1,  /* type  */
508
   1,     /* rightshift  */
509
   4,     /* size  */
510
   15,      /* bitsize  */
511
   false,     /* pc_relative  */
512
   0,     /* bitpos  */
513
   complain_overflow_signed,/* complain_on_overflow  */
514
   nds32_elf_sda15_reloc, /* special_function  */
515
   "R_NDS32_SDA15S1", /* name  */
516
   false,     /* partial_inplace  */
517
   0x00007fff,    /* src_mask  */
518
   0x00007fff,    /* dst_mask  */
519
   false),    /* pcrel_offset  */
520
521
  /* Small data area 15 bits offset.  */
522
  HOWTO2 (R_NDS32_SDA15S0,  /* type  */
523
   0,     /* rightshift  */
524
   4,     /* size  */
525
   15,      /* bitsize  */
526
   false,     /* pc_relative  */
527
   0,     /* bitpos  */
528
   complain_overflow_signed,/* complain_on_overflow  */
529
   nds32_elf_sda15_reloc, /* special_function  */
530
   "R_NDS32_SDA15S0", /* name  */
531
   false,     /* partial_inplace  */
532
   0x00007fff,    /* src_mask  */
533
   0x00007fff,    /* dst_mask  */
534
   false),    /* pcrel_offset  */
535
536
  /* GNU extension to record C++ vtable hierarchy  */
537
  HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type  */
538
   0,     /* rightshift  */
539
   4,     /* size  */
540
   0,     /* bitsize  */
541
   false,     /* pc_relative  */
542
   0,     /* bitpos  */
543
   complain_overflow_dont,/* complain_on_overflow  */
544
   NULL,      /* special_function  */
545
   "R_NDS32_GNU_VTINHERIT",/* name  */
546
   false,     /* partial_inplace  */
547
   0,     /* src_mask  */
548
   0,     /* dst_mask  */
549
   false),    /* pcrel_offset  */
550
551
  /* GNU extension to record C++ vtable member usage  */
552
  HOWTO2 (R_NDS32_GNU_VTENTRY,  /* type  */
553
   0,     /* rightshift  */
554
   4,     /* size  */
555
   0,     /* bitsize  */
556
   false,     /* pc_relative  */
557
   0,     /* bitpos  */
558
   complain_overflow_dont,/* complain_on_overflow  */
559
   _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
560
   "R_NDS32_GNU_VTENTRY", /* name  */
561
   false,     /* partial_inplace  */
562
   0,     /* src_mask  */
563
   0,     /* dst_mask  */
564
   false),    /* pcrel_offset  */
565
566
  /* A 16 bit absolute relocation.  */
567
  HOWTO2 (R_NDS32_16_RELA,  /* type  */
568
   0,     /* rightshift  */
569
   2,     /* size  */
570
   16,      /* bitsize  */
571
   false,     /* pc_relative  */
572
   0,     /* bitpos  */
573
   complain_overflow_bitfield,/* complain_on_overflow  */
574
   bfd_elf_generic_reloc, /* special_function  */
575
   "R_NDS32_16_RELA", /* name  */
576
   false,     /* partial_inplace  */
577
   0xffff,    /* src_mask  */
578
   0xffff,    /* dst_mask  */
579
   false),    /* pcrel_offset  */
580
581
  /* A 32 bit absolute relocation.  */
582
  HOWTO2 (R_NDS32_32_RELA,  /* type  */
583
   0,     /* rightshift  */
584
   4,     /* size  */
585
   32,      /* bitsize  */
586
   false,     /* pc_relative  */
587
   0,     /* bitpos  */
588
   complain_overflow_bitfield,/* complain_on_overflow  */
589
   bfd_elf_generic_reloc, /* special_function  */
590
   "R_NDS32_32_RELA", /* name  */
591
   false,     /* partial_inplace  */
592
   0xffffffff,    /* src_mask  */
593
   0xffffffff,    /* dst_mask  */
594
   false),    /* pcrel_offset  */
595
596
  /* A 20 bit address.  */
597
  HOWTO2 (R_NDS32_20_RELA,  /* type  */
598
   0,     /* rightshift  */
599
   4,     /* size  */
600
   20,      /* bitsize  */
601
   false,     /* pc_relative  */
602
   0,     /* bitpos  */
603
   complain_overflow_signed,/* complain_on_overflow  */
604
   bfd_elf_generic_reloc, /* special_function  */
605
   "R_NDS32_20_RELA", /* name  */
606
   false,     /* partial_inplace  */
607
   0xfffff,   /* src_mask  */
608
   0xfffff,   /* dst_mask  */
609
   false),    /* pcrel_offset  */
610
611
  HOWTO2 (R_NDS32_9_PCREL_RELA, /* type  */
612
   1,     /* rightshift  */
613
   2,     /* size  */
614
   8,     /* bitsize  */
615
   true,      /* pc_relative  */
616
   0,     /* bitpos  */
617
   complain_overflow_signed,/* complain_on_overflow  */
618
   bfd_elf_generic_reloc, /* special_function  */
619
   "R_NDS32_9_PCREL_RELA",/* name  */
620
   false,     /* partial_inplace  */
621
   0xff,      /* src_mask  */
622
   0xff,      /* dst_mask  */
623
   true),     /* pcrel_offset  */
624
625
  /* A relative 15 bit relocation, right shifted by 1.  */
626
  HOWTO2 (R_NDS32_15_PCREL_RELA,/* type  */
627
   1,     /* rightshift  */
628
   4,     /* size  */
629
   14,      /* bitsize  */
630
   true,      /* pc_relative  */
631
   0,     /* bitpos  */
632
   complain_overflow_signed,/* complain_on_overflow  */
633
   bfd_elf_generic_reloc, /* special_function  */
634
   "R_NDS32_15_PCREL_RELA",/* name  */
635
   false,     /* partial_inplace  */
636
   0x3fff,    /* src_mask  */
637
   0x3fff,    /* dst_mask  */
638
   true),     /* pcrel_offset  */
639
640
  /* A relative 17 bit relocation, right shifted by 1.  */
641
  HOWTO2 (R_NDS32_17_PCREL_RELA,/* type  */
642
   1,     /* rightshift  */
643
   4,     /* size  */
644
   16,      /* bitsize  */
645
   true,      /* pc_relative  */
646
   0,     /* bitpos  */
647
   complain_overflow_signed,/* complain_on_overflow  */
648
   bfd_elf_generic_reloc, /* special_function  */
649
   "R_NDS32_17_PCREL_RELA",/* name  */
650
   false,     /* partial_inplace  */
651
   0xffff,    /* src_mask  */
652
   0xffff,    /* dst_mask  */
653
   true),     /* pcrel_offset  */
654
655
  /* A relative 25 bit relocation, right shifted by 2.  */
656
  HOWTO2 (R_NDS32_25_PCREL_RELA,/* type  */
657
   1,     /* rightshift  */
658
   4,     /* size  */
659
   24,      /* bitsize  */
660
   true,      /* pc_relative  */
661
   0,     /* bitpos  */
662
   complain_overflow_signed,/* complain_on_overflow  */
663
   bfd_elf_generic_reloc, /* special_function  */
664
   "R_NDS32_25_PCREL_RELA",/* name  */
665
   false,     /* partial_inplace  */
666
   0xffffff,    /* src_mask  */
667
   0xffffff,    /* dst_mask  */
668
   true),     /* pcrel_offset  */
669
670
  /* High 20 bits of address when lower 16 is or'd in.  */
671
  HOWTO2 (R_NDS32_HI20_RELA,  /* type  */
672
   12,      /* rightshift  */
673
   4,     /* size  */
674
   20,      /* bitsize  */
675
   false,     /* pc_relative  */
676
   0,     /* bitpos  */
677
   complain_overflow_dont,/* complain_on_overflow  */
678
   bfd_elf_generic_reloc, /* special_function  */
679
   "R_NDS32_HI20_RELA", /* name  */
680
   false,     /* partial_inplace  */
681
   0x000fffff,    /* src_mask  */
682
   0x000fffff,    /* dst_mask  */
683
   false),    /* pcrel_offset  */
684
685
  /* Lower 12 bits of address.  */
686
  HOWTO2 (R_NDS32_LO12S3_RELA,  /* type  */
687
   3,     /* rightshift  */
688
   4,     /* size  */
689
   9,     /* bitsize  */
690
   false,     /* pc_relative  */
691
   0,     /* bitpos  */
692
   complain_overflow_dont,/* complain_on_overflow  */
693
   bfd_elf_generic_reloc, /* special_function  */
694
   "R_NDS32_LO12S3_RELA", /* name  */
695
   false,     /* partial_inplace  */
696
   0x000001ff,    /* src_mask  */
697
   0x000001ff,    /* dst_mask  */
698
   false),    /* pcrel_offset  */
699
700
  /* Lower 12 bits of address.  */
701
  HOWTO2 (R_NDS32_LO12S2_RELA,  /* type  */
702
   2,     /* rightshift  */
703
   4,     /* size  */
704
   10,      /* bitsize  */
705
   false,     /* pc_relative  */
706
   0,     /* bitpos  */
707
   complain_overflow_dont,/* complain_on_overflow  */
708
   bfd_elf_generic_reloc, /* special_function  */
709
   "R_NDS32_LO12S2_RELA", /* name  */
710
   false,     /* partial_inplace  */
711
   0x000003ff,    /* src_mask  */
712
   0x000003ff,    /* dst_mask  */
713
   false),    /* pcrel_offset  */
714
715
  /* Lower 12 bits of address.  */
716
  HOWTO2 (R_NDS32_LO12S1_RELA,  /* type  */
717
   1,     /* rightshift  */
718
   4,     /* size  */
719
   11,      /* bitsize  */
720
   false,     /* pc_relative  */
721
   0,     /* bitpos  */
722
   complain_overflow_dont,/* complain_on_overflow  */
723
   bfd_elf_generic_reloc, /* special_function  */
724
   "R_NDS32_LO12S1_RELA", /* name  */
725
   false,     /* partial_inplace  */
726
   0x000007ff,    /* src_mask  */
727
   0x000007ff,    /* dst_mask  */
728
   false),    /* pcrel_offset  */
729
730
  /* Lower 12 bits of address.  */
731
  HOWTO2 (R_NDS32_LO12S0_RELA,  /* type  */
732
   0,     /* rightshift  */
733
   4,     /* size  */
734
   12,      /* bitsize  */
735
   false,     /* pc_relative  */
736
   0,     /* bitpos  */
737
   complain_overflow_dont,/* complain_on_overflow  */
738
   bfd_elf_generic_reloc, /* special_function  */
739
   "R_NDS32_LO12S0_RELA", /* name  */
740
   false,     /* partial_inplace  */
741
   0x00000fff,    /* src_mask  */
742
   0x00000fff,    /* dst_mask  */
743
   false),    /* pcrel_offset  */
744
745
  /* Small data area 15 bits offset.  */
746
  HOWTO2 (R_NDS32_SDA15S3_RELA, /* type  */
747
   3,     /* rightshift  */
748
   4,     /* size  */
749
   15,      /* bitsize  */
750
   false,     /* pc_relative  */
751
   0,     /* bitpos  */
752
   complain_overflow_signed,/* complain_on_overflow  */
753
   bfd_elf_generic_reloc, /* special_function  */
754
   "R_NDS32_SDA15S3_RELA",/* name  */
755
   false,     /* partial_inplace  */
756
   0x00007fff,    /* src_mask  */
757
   0x00007fff,    /* dst_mask  */
758
   false),    /* pcrel_offset  */
759
760
  /* Small data area 15 bits offset.  */
761
  HOWTO2 (R_NDS32_SDA15S2_RELA, /* type  */
762
   2,     /* rightshift  */
763
   4,     /* size  */
764
   15,      /* bitsize  */
765
   false,     /* pc_relative  */
766
   0,     /* bitpos  */
767
   complain_overflow_signed,/* complain_on_overflow  */
768
   bfd_elf_generic_reloc, /* special_function  */
769
   "R_NDS32_SDA15S2_RELA",/* name  */
770
   false,     /* partial_inplace  */
771
   0x00007fff,    /* src_mask  */
772
   0x00007fff,    /* dst_mask  */
773
   false),    /* pcrel_offset  */
774
775
  HOWTO2 (R_NDS32_SDA15S1_RELA, /* type  */
776
   1,     /* rightshift  */
777
   4,     /* size  */
778
   15,      /* bitsize  */
779
   false,     /* pc_relative  */
780
   0,     /* bitpos  */
781
   complain_overflow_signed,/* complain_on_overflow  */
782
   bfd_elf_generic_reloc, /* special_function  */
783
   "R_NDS32_SDA15S1_RELA",/* name  */
784
   false,     /* partial_inplace  */
785
   0x00007fff,    /* src_mask  */
786
   0x00007fff,    /* dst_mask  */
787
   false),    /* pcrel_offset  */
788
789
  HOWTO2 (R_NDS32_SDA15S0_RELA, /* type  */
790
   0,     /* rightshift  */
791
   4,     /* size  */
792
   15,      /* bitsize  */
793
   false,     /* pc_relative  */
794
   0,     /* bitpos  */
795
   complain_overflow_signed,/* complain_on_overflow  */
796
   bfd_elf_generic_reloc, /* special_function  */
797
   "R_NDS32_SDA15S0_RELA",/* name  */
798
   false,     /* partial_inplace  */
799
   0x00007fff,    /* src_mask  */
800
   0x00007fff,    /* dst_mask  */
801
   false),    /* pcrel_offset  */
802
803
  /* GNU extension to record C++ vtable hierarchy  */
804
  HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type  */
805
   0,     /* rightshift  */
806
   4,     /* size  */
807
   0,     /* bitsize  */
808
   false,     /* pc_relative  */
809
   0,     /* bitpos  */
810
   complain_overflow_dont,/* complain_on_overflow  */
811
   NULL,      /* special_function  */
812
   "R_NDS32_RELA_GNU_VTINHERIT",/* name  */
813
   false,     /* partial_inplace  */
814
   0,     /* src_mask  */
815
   0,     /* dst_mask  */
816
   false),    /* pcrel_offset  */
817
818
  /* GNU extension to record C++ vtable member usage  */
819
  HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type  */
820
   0,     /* rightshift  */
821
   4,     /* size  */
822
   0,     /* bitsize  */
823
   false,     /* pc_relative  */
824
   0,     /* bitpos  */
825
   complain_overflow_dont,/* complain_on_overflow  */
826
   _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
827
   "R_NDS32_RELA_GNU_VTENTRY",/* name  */
828
   false,     /* partial_inplace  */
829
   0,     /* src_mask  */
830
   0,     /* dst_mask  */
831
   false),    /* pcrel_offset  */
832
833
  /* Like R_NDS32_20, but referring to the GOT table entry for
834
     the symbol.  */
835
  HOWTO2 (R_NDS32_GOT20,  /* type  */
836
   0,     /* rightshift  */
837
   4,     /* size  */
838
   20,      /* bitsize  */
839
   false,     /* pc_relative  */
840
   0,     /* bitpos  */
841
   complain_overflow_signed,/* complain_on_overflow  */
842
   bfd_elf_generic_reloc, /* special_function  */
843
   "R_NDS32_GOT20", /* name  */
844
   false,     /* partial_inplace  */
845
   0xfffff,   /* src_mask  */
846
   0xfffff,   /* dst_mask  */
847
   false),    /* pcrel_offset  */
848
849
  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
850
     entry for the symbol.  */
851
  HOWTO2 (R_NDS32_25_PLTREL,  /* type  */
852
   1,     /* rightshift  */
853
   4,     /* size  */
854
   24,      /* bitsize  */
855
   true,      /* pc_relative  */
856
   0,     /* bitpos  */
857
   complain_overflow_signed,/* complain_on_overflow  */
858
   bfd_elf_generic_reloc, /* special_function  */
859
   "R_NDS32_25_PLTREL", /* name  */
860
   false,     /* partial_inplace  */
861
   0xffffff,    /* src_mask  */
862
   0xffffff,    /* dst_mask  */
863
   true),     /* pcrel_offset  */
864
865
  /* This is used only by the dynamic linker.  The symbol should exist
866
     both in the object being run and in some shared library.  The
867
     dynamic linker copies the data addressed by the symbol from the
868
     shared library into the object, because the object being
869
     run has to have the data at some particular address.  */
870
  HOWTO2 (R_NDS32_COPY,   /* type  */
871
   0,     /* rightshift  */
872
   4,     /* size  */
873
   32,      /* bitsize  */
874
   false,     /* pc_relative  */
875
   0,     /* bitpos  */
876
   complain_overflow_bitfield,/* complain_on_overflow  */
877
   bfd_elf_generic_reloc, /* special_function  */
878
   "R_NDS32_COPY",  /* name  */
879
   false,     /* partial_inplace  */
880
   0xffffffff,    /* src_mask  */
881
   0xffffffff,    /* dst_mask  */
882
   false),    /* pcrel_offset  */
883
884
  /* Like R_NDS32_20, but used when setting global offset table
885
     entries.  */
886
  HOWTO2 (R_NDS32_GLOB_DAT, /* type  */
887
   0,     /* rightshift  */
888
   4,     /* size  */
889
   32,      /* bitsize  */
890
   false,     /* pc_relative  */
891
   0,     /* bitpos  */
892
   complain_overflow_bitfield,/* complain_on_overflow  */
893
   bfd_elf_generic_reloc, /* special_function  */
894
   "R_NDS32_GLOB_DAT",  /* name  */
895
   false,     /* partial_inplace  */
896
   0xffffffff,    /* src_mask  */
897
   0xffffffff,    /* dst_mask  */
898
   false),    /* pcrel_offset  */
899
900
  /* Marks a procedure linkage table entry for a symbol.  */
901
  HOWTO2 (R_NDS32_JMP_SLOT, /* type  */
902
   0,     /* rightshift  */
903
   4,     /* size  */
904
   32,      /* bitsize  */
905
   false,     /* pc_relative  */
906
   0,     /* bitpos  */
907
   complain_overflow_bitfield,/* complain_on_overflow  */
908
   bfd_elf_generic_reloc, /* special_function  */
909
   "R_NDS32_JMP_SLOT",  /* name  */
910
   false,     /* partial_inplace  */
911
   0xffffffff,    /* src_mask  */
912
   0xffffffff,    /* dst_mask  */
913
   false),    /* pcrel_offset  */
914
915
  /* Used only by the dynamic linker.  When the object is run, this
916
     longword is set to the load address of the object, plus the
917
     addend.  */
918
  HOWTO2 (R_NDS32_RELATIVE, /* type  */
919
   0,     /* rightshift  */
920
   4,     /* size  */
921
   32,      /* bitsize  */
922
   false,     /* pc_relative  */
923
   0,     /* bitpos  */
924
   complain_overflow_bitfield,/* complain_on_overflow  */
925
   bfd_elf_generic_reloc, /* special_function  */
926
   "R_NDS32_RELATIVE",  /* name  */
927
   false,     /* partial_inplace  */
928
   0xffffffff,    /* src_mask  */
929
   0xffffffff,    /* dst_mask  */
930
   false),    /* pcrel_offset  */
931
932
  HOWTO2 (R_NDS32_GOTOFF, /* type  */
933
   0,     /* rightshift  */
934
   4,     /* size  */
935
   20,      /* bitsize  */
936
   false,     /* pc_relative  */
937
   0,     /* bitpos  */
938
   complain_overflow_signed,/* complain_on_overflow  */
939
   bfd_elf_generic_reloc, /* special_function  */
940
   "R_NDS32_GOTOFF",  /* name  */
941
   false,     /* partial_inplace  */
942
   0xfffff,   /* src_mask  */
943
   0xfffff,   /* dst_mask  */
944
   false),    /* pcrel_offset  */
945
946
  /* An PC Relative 20-bit relocation used when setting PIC offset
947
     table register.  */
948
  HOWTO2 (R_NDS32_GOTPC20,  /* type  */
949
   0,     /* rightshift  */
950
   4,     /* size  */
951
   20,      /* bitsize  */
952
   true,      /* pc_relative  */
953
   0,     /* bitpos  */
954
   complain_overflow_signed,/* complain_on_overflow  */
955
   bfd_elf_generic_reloc, /* special_function  */
956
   "R_NDS32_GOTPC20", /* name  */
957
   false,     /* partial_inplace  */
958
   0xfffff,   /* src_mask  */
959
   0xfffff,   /* dst_mask  */
960
   true),     /* pcrel_offset  */
961
962
  /* Like R_NDS32_HI20, but referring to the GOT table entry for
963
     the symbol.  */
964
  HOWTO2 (R_NDS32_GOT_HI20, /* type  */
965
   12,      /* rightshift  */
966
   4,     /* size  */
967
   20,      /* bitsize  */
968
   false,     /* pc_relative  */
969
   0,     /* bitpos  */
970
   complain_overflow_dont,/* complain_on_overflow  */
971
   bfd_elf_generic_reloc, /* special_function  */
972
   "R_NDS32_GOT_HI20",  /* name  */
973
   false,     /* partial_inplace  */
974
   0x000fffff,    /* src_mask  */
975
   0x000fffff,    /* dst_mask  */
976
   false),    /* pcrel_offset  */
977
978
  HOWTO2 (R_NDS32_GOT_LO12, /* type  */
979
   0,     /* rightshift  */
980
   4,     /* size  */
981
   12,      /* bitsize  */
982
   false,     /* pc_relative  */
983
   0,     /* bitpos  */
984
   complain_overflow_dont,/* complain_on_overflow  */
985
   bfd_elf_generic_reloc, /* special_function  */
986
   "R_NDS32_GOT_LO12",  /* name  */
987
   false,     /* partial_inplace  */
988
   0x00000fff,    /* src_mask  */
989
   0x00000fff,    /* dst_mask  */
990
   false),    /* pcrel_offset  */
991
992
  /* An PC Relative relocation used when setting PIC offset table register.
993
     Like R_NDS32_HI20, but referring to the GOT table entry for
994
     the symbol.  */
995
  HOWTO2 (R_NDS32_GOTPC_HI20, /* type  */
996
   12,      /* rightshift  */
997
   4,     /* size  */
998
   20,      /* bitsize  */
999
   false,     /* pc_relative  */
1000
   0,     /* bitpos  */
1001
   complain_overflow_dont,/* complain_on_overflow  */
1002
   bfd_elf_generic_reloc, /* special_function  */
1003
   "R_NDS32_GOTPC_HI20",  /* name  */
1004
   false,     /* partial_inplace  */
1005
   0x000fffff,    /* src_mask  */
1006
   0x000fffff,    /* dst_mask  */
1007
   true),     /* pcrel_offset  */
1008
1009
  HOWTO2 (R_NDS32_GOTPC_LO12, /* type  */
1010
   0,     /* rightshift  */
1011
   4,     /* size  */
1012
   12,      /* bitsize  */
1013
   false,     /* pc_relative  */
1014
   0,     /* bitpos  */
1015
   complain_overflow_dont,/* complain_on_overflow  */
1016
   bfd_elf_generic_reloc, /* special_function  */
1017
   "R_NDS32_GOTPC_LO12",  /* name  */
1018
   false,     /* partial_inplace  */
1019
   0x00000fff,    /* src_mask  */
1020
   0x00000fff,    /* dst_mask  */
1021
   true),     /* pcrel_offset  */
1022
1023
  HOWTO2 (R_NDS32_GOTOFF_HI20,  /* type  */
1024
   12,      /* rightshift  */
1025
   4,     /* size  */
1026
   20,      /* bitsize  */
1027
   false,     /* pc_relative  */
1028
   0,     /* bitpos  */
1029
   complain_overflow_dont,/* complain_on_overflow  */
1030
   bfd_elf_generic_reloc, /* special_function  */
1031
   "R_NDS32_GOTOFF_HI20", /* name  */
1032
   false,     /* partial_inplace  */
1033
   0x000fffff,    /* src_mask  */
1034
   0x000fffff,    /* dst_mask  */
1035
   false),    /* pcrel_offset  */
1036
1037
  HOWTO2 (R_NDS32_GOTOFF_LO12,  /* type  */
1038
   0,     /* rightshift  */
1039
   4,     /* size  */
1040
   12,      /* bitsize  */
1041
   false,     /* pc_relative  */
1042
   0,     /* bitpos  */
1043
   complain_overflow_dont,/* complain_on_overflow  */
1044
   bfd_elf_generic_reloc, /* special_function  */
1045
   "R_NDS32_GOTOFF_LO12", /* name  */
1046
   false,     /* partial_inplace  */
1047
   0x00000fff,    /* src_mask  */
1048
   0x00000fff,    /* dst_mask  */
1049
   false),    /* pcrel_offset  */
1050
1051
  /* Alignment hint for relaxable instruction.  This is used with
1052
     R_NDS32_LABEL as a pair.  Relax this instruction from 4 bytes to 2
1053
     in order to make next label aligned on word boundary.  */
1054
  HOWTO2 (R_NDS32_INSN16, /* type  */
1055
   0,     /* rightshift  */
1056
   4,     /* size  */
1057
   32,      /* bitsize  */
1058
   false,     /* pc_relative  */
1059
   0,     /* bitpos  */
1060
   complain_overflow_dont,/* complain_on_overflow  */
1061
   nds32_elf_ignore_reloc,/* special_function  */
1062
   "R_NDS32_INSN16",  /* name  */
1063
   false,     /* partial_inplace  */
1064
   0x00000fff,    /* src_mask  */
1065
   0x00000fff,    /* dst_mask  */
1066
   false),    /* pcrel_offset  */
1067
1068
  /* Alignment hint for label.  */
1069
  HOWTO2 (R_NDS32_LABEL,  /* type  */
1070
   0,     /* rightshift  */
1071
   4,     /* size  */
1072
   32,      /* bitsize  */
1073
   false,     /* pc_relative  */
1074
   0,     /* bitpos  */
1075
   complain_overflow_dont,/* complain_on_overflow  */
1076
   nds32_elf_ignore_reloc,/* special_function  */
1077
   "R_NDS32_LABEL", /* name  */
1078
   false,     /* partial_inplace  */
1079
   0xffffffff,    /* src_mask  */
1080
   0xffffffff,    /* dst_mask  */
1081
   false),    /* pcrel_offset  */
1082
1083
  /* Relax hint for unconditional call sequence  */
1084
  HOWTO2 (R_NDS32_LONGCALL1,  /* type  */
1085
   0,     /* rightshift  */
1086
   4,     /* size  */
1087
   32,      /* bitsize  */
1088
   false,     /* pc_relative  */
1089
   0,     /* bitpos  */
1090
   complain_overflow_dont,/* complain_on_overflow  */
1091
   nds32_elf_ignore_reloc,/* special_function  */
1092
   "R_NDS32_LONGCALL1", /* name  */
1093
   false,     /* partial_inplace  */
1094
   0xffffffff,    /* src_mask  */
1095
   0xffffffff,    /* dst_mask  */
1096
   false),    /* pcrel_offset  */
1097
1098
  /* Relax hint for conditional call sequence.  */
1099
  HOWTO2 (R_NDS32_LONGCALL2,  /* type  */
1100
   0,     /* rightshift  */
1101
   4,     /* size  */
1102
   32,      /* bitsize  */
1103
   false,     /* pc_relative  */
1104
   0,     /* bitpos  */
1105
   complain_overflow_dont,/* complain_on_overflow  */
1106
   nds32_elf_ignore_reloc,/* special_function  */
1107
   "R_NDS32_LONGCALL2", /* name  */
1108
   false,     /* partial_inplace  */
1109
   0xffffffff,    /* src_mask  */
1110
   0xffffffff,    /* dst_mask  */
1111
   false),    /* pcrel_offset  */
1112
1113
  /* Relax hint for conditional call sequence.  */
1114
  HOWTO2 (R_NDS32_LONGCALL3,  /* type  */
1115
   0,     /* rightshift  */
1116
   4,     /* size  */
1117
   32,      /* bitsize  */
1118
   false,     /* pc_relative  */
1119
   0,     /* bitpos  */
1120
   complain_overflow_dont,/* complain_on_overflow  */
1121
   nds32_elf_ignore_reloc,/* special_function  */
1122
   "R_NDS32_LONGCALL3", /* name  */
1123
   false,     /* partial_inplace  */
1124
   0xffffffff,    /* src_mask  */
1125
   0xffffffff,    /* dst_mask  */
1126
   false),    /* pcrel_offset  */
1127
1128
  /* Relax hint for unconditional branch sequence.  */
1129
  HOWTO2 (R_NDS32_LONGJUMP1,  /* type  */
1130
   0,     /* rightshift  */
1131
   4,     /* size  */
1132
   32,      /* bitsize  */
1133
   false,     /* pc_relative  */
1134
   0,     /* bitpos  */
1135
   complain_overflow_dont,/* complain_on_overflow  */
1136
   nds32_elf_ignore_reloc,/* special_function  */
1137
   "R_NDS32_LONGJUMP1", /* name  */
1138
   false,     /* partial_inplace  */
1139
   0xffffffff,    /* src_mask  */
1140
   0xffffffff,    /* dst_mask  */
1141
   false),    /* pcrel_offset  */
1142
1143
  /* Relax hint for conditional branch sequence.  */
1144
  HOWTO2 (R_NDS32_LONGJUMP2,  /* type  */
1145
   0,     /* rightshift  */
1146
   4,     /* size  */
1147
   32,      /* bitsize  */
1148
   false,     /* pc_relative  */
1149
   0,     /* bitpos  */
1150
   complain_overflow_dont,/* complain_on_overflow  */
1151
   nds32_elf_ignore_reloc,/* special_function  */
1152
   "R_NDS32_LONGJUMP2", /* name  */
1153
   false,     /* partial_inplace  */
1154
   0xffffffff,    /* src_mask  */
1155
   0xffffffff,    /* dst_mask  */
1156
   false),    /* pcrel_offset  */
1157
1158
  /* Relax hint for conditional branch sequence.  */
1159
  HOWTO2 (R_NDS32_LONGJUMP3,  /* type  */
1160
   0,     /* rightshift  */
1161
   4,     /* size  */
1162
   32,      /* bitsize  */
1163
   false,     /* pc_relative  */
1164
   0,     /* bitpos  */
1165
   complain_overflow_dont,/* complain_on_overflow  */
1166
   nds32_elf_ignore_reloc,/* special_function  */
1167
   "R_NDS32_LONGJUMP3", /* name  */
1168
   false,     /* partial_inplace  */
1169
   0xffffffff,    /* src_mask  */
1170
   0xffffffff,    /* dst_mask  */
1171
   false),    /* pcrel_offset  */
1172
1173
  /* Relax hint for load/store sequence.   */
1174
  HOWTO2 (R_NDS32_LOADSTORE,  /* type  */
1175
   0,     /* rightshift  */
1176
   4,     /* size  */
1177
   32,      /* bitsize  */
1178
   false,     /* pc_relative  */
1179
   0,     /* bitpos  */
1180
   complain_overflow_dont,/* complain_on_overflow  */
1181
   nds32_elf_ignore_reloc,/* special_function  */
1182
   "R_NDS32_LOADSTORE", /* name  */
1183
   false,     /* partial_inplace  */
1184
   0xffffffff,    /* src_mask  */
1185
   0xffffffff,    /* dst_mask  */
1186
   false),    /* pcrel_offset  */
1187
1188
  /* Relax hint for load/store sequence.  */
1189
  HOWTO2 (R_NDS32_9_FIXED_RELA, /* type  */
1190
   0,     /* rightshift  */
1191
   2,     /* size  */
1192
   16,      /* bitsize  */
1193
   false,     /* pc_relative  */
1194
   0,     /* bitpos  */
1195
   complain_overflow_dont,/* complain_on_overflow  */
1196
   nds32_elf_ignore_reloc,/* special_function  */
1197
   "R_NDS32_9_FIXED_RELA",/* name  */
1198
   false,     /* partial_inplace  */
1199
   0x000000ff,    /* src_mask  */
1200
   0x000000ff,    /* dst_mask  */
1201
   false),    /* pcrel_offset  */
1202
1203
  /* Relax hint for load/store sequence.  */
1204
  HOWTO2 (R_NDS32_15_FIXED_RELA,/* type  */
1205
   0,     /* rightshift  */
1206
   4,     /* size  */
1207
   32,      /* bitsize  */
1208
   false,     /* pc_relative  */
1209
   0,     /* bitpos  */
1210
   complain_overflow_dont,/* complain_on_overflow  */
1211
   nds32_elf_ignore_reloc,/* special_function  */
1212
   "R_NDS32_15_FIXED_RELA",/* name  */
1213
   false,     /* partial_inplace  */
1214
   0x00003fff,    /* src_mask  */
1215
   0x00003fff,    /* dst_mask  */
1216
   false),    /* pcrel_offset  */
1217
1218
  /* Relax hint for load/store sequence.  */
1219
  HOWTO2 (R_NDS32_17_FIXED_RELA,/* type  */
1220
   0,     /* rightshift  */
1221
   4,     /* size  */
1222
   32,      /* bitsize  */
1223
   false,     /* pc_relative  */
1224
   0,     /* bitpos  */
1225
   complain_overflow_dont,/* complain_on_overflow  */
1226
   nds32_elf_ignore_reloc,/* special_function  */
1227
   "R_NDS32_17_FIXED_RELA",/* name  */
1228
   false,     /* partial_inplace  */
1229
   0x0000ffff,    /* src_mask  */
1230
   0x0000ffff,    /* dst_mask  */
1231
   false),    /* pcrel_offset  */
1232
1233
  /* Relax hint for load/store sequence.  */
1234
  HOWTO2 (R_NDS32_25_FIXED_RELA,/* type  */
1235
   0,     /* rightshift  */
1236
   4,     /* size  */
1237
   32,      /* bitsize  */
1238
   false,     /* pc_relative  */
1239
   0,     /* bitpos  */
1240
   complain_overflow_dont,/* complain_on_overflow  */
1241
   nds32_elf_ignore_reloc,/* special_function  */
1242
   "R_NDS32_25_FIXED_RELA",/* name  */
1243
   false,     /* partial_inplace  */
1244
   0x00ffffff,    /* src_mask  */
1245
   0x00ffffff,    /* dst_mask  */
1246
   false),    /* pcrel_offset  */
1247
1248
  /* High 20 bits of PLT symbol offset relative to PC.  */
1249
  HOWTO2 (R_NDS32_PLTREL_HI20,  /* type  */
1250
   12,      /* rightshift  */
1251
   4,     /* size  */
1252
   20,      /* bitsize  */
1253
   false,     /* pc_relative  */
1254
   0,     /* bitpos  */
1255
   complain_overflow_dont,/* complain_on_overflow  */
1256
   bfd_elf_generic_reloc, /* special_function  */
1257
   "R_NDS32_PLTREL_HI20", /* name  */
1258
   false,     /* partial_inplace  */
1259
   0x000fffff,    /* src_mask  */
1260
   0x000fffff,    /* dst_mask  */
1261
   false),    /* pcrel_offset  */
1262
1263
  /* Low 12 bits of PLT symbol offset relative to PC.  */
1264
  HOWTO2 (R_NDS32_PLTREL_LO12,  /* type  */
1265
   0,     /* rightshift  */
1266
   4,     /* size  */
1267
   12,      /* bitsize  */
1268
   false,     /* pc_relative  */
1269
   0,     /* bitpos  */
1270
   complain_overflow_dont,/* complain_on_overflow  */
1271
   bfd_elf_generic_reloc, /* special_function  */
1272
   "R_NDS32_PLTREL_LO12", /* name  */
1273
   false,     /* partial_inplace  */
1274
   0x00000fff,    /* src_mask  */
1275
   0x00000fff,    /* dst_mask  */
1276
   false),    /* pcrel_offset  */
1277
1278
  /* High 20 bits of PLT symbol offset relative to GOT (GP).  */
1279
  HOWTO2 (R_NDS32_PLT_GOTREL_HI20,  /* type  */
1280
   12,      /* rightshift  */
1281
   4,     /* size  */
1282
   20,      /* bitsize  */
1283
   false,     /* pc_relative  */
1284
   0,     /* bitpos  */
1285
   complain_overflow_dont,/* complain_on_overflow  */
1286
   bfd_elf_generic_reloc, /* special_function  */
1287
   "R_NDS32_PLT_GOTREL_HI20",/* name  */
1288
   false,     /* partial_inplace  */
1289
   0x000fffff,    /* src_mask  */
1290
   0x000fffff,    /* dst_mask  */
1291
   false),    /* pcrel_offset  */
1292
1293
  /* Low 12 bits of PLT symbol offset relative to GOT (GP).  */
1294
  HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type  */
1295
   0,     /* rightshift  */
1296
   4,     /* size  */
1297
   12,      /* bitsize  */
1298
   false,     /* pc_relative  */
1299
   0,     /* bitpos  */
1300
   complain_overflow_dont,/* complain_on_overflow  */
1301
   bfd_elf_generic_reloc, /* special_function  */
1302
   "R_NDS32_PLT_GOTREL_LO12",/* name  */
1303
   false,     /* partial_inplace  */
1304
   0x00000fff,    /* src_mask  */
1305
   0x00000fff,    /* dst_mask  */
1306
   false),    /* pcrel_offset  */
1307
1308
  /* Small data area 12 bits offset.  */
1309
  HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type  */
1310
   2,     /* rightshift  */
1311
   4,     /* size  */
1312
   12,      /* bitsize  */
1313
   false,     /* pc_relative  */
1314
   0,     /* bitpos  */
1315
   complain_overflow_signed,/* complain_on_overflow  */
1316
   bfd_elf_generic_reloc, /* special_function  */
1317
   "R_NDS32_SDA12S2_DP_RELA",/* name  */
1318
   false,     /* partial_inplace  */
1319
   0x00000fff,    /* src_mask  */
1320
   0x00000fff,    /* dst_mask  */
1321
   false),    /* pcrel_offset  */
1322
1323
  /* Small data area 12 bits offset.  */
1324
  HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type  */
1325
   2,     /* rightshift  */
1326
   4,     /* size  */
1327
   12,      /* bitsize  */
1328
   false,     /* pc_relative  */
1329
   0,     /* bitpos  */
1330
   complain_overflow_signed,/* complain_on_overflow  */
1331
   bfd_elf_generic_reloc, /* special_function  */
1332
   "R_NDS32_SDA12S2_SP_RELA",/* name  */
1333
   false,     /* partial_inplace  */
1334
   0x00000fff,    /* src_mask  */
1335
   0x00000fff,    /* dst_mask  */
1336
   false),    /* pcrel_offset  */
1337
  /* Lower 12 bits of address.  */
1338
1339
  HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type  */
1340
   2,     /* rightshift  */
1341
   4,     /* size  */
1342
   10,      /* bitsize  */
1343
   false,     /* pc_relative  */
1344
   0,     /* bitpos  */
1345
   complain_overflow_dont,/* complain_on_overflow  */
1346
   bfd_elf_generic_reloc, /* special_function  */
1347
   "R_NDS32_LO12S2_DP_RELA",/* name  */
1348
   false,     /* partial_inplace  */
1349
   0x000003ff,    /* src_mask  */
1350
   0x000003ff,    /* dst_mask  */
1351
   false),    /* pcrel_offset  */
1352
1353
  /* Lower 12 bits of address.  */
1354
  HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type  */
1355
   2,     /* rightshift  */
1356
   4,     /* size  */
1357
   10,      /* bitsize  */
1358
   false,     /* pc_relative  */
1359
   0,     /* bitpos  */
1360
   complain_overflow_dont,/* complain_on_overflow  */
1361
   bfd_elf_generic_reloc, /* special_function  */
1362
   "R_NDS32_LO12S2_SP_RELA",/* name  */
1363
   false,     /* partial_inplace  */
1364
   0x000003ff,    /* src_mask  */
1365
   0x000003ff,    /* dst_mask  */
1366
   false),    /* pcrel_offset  */
1367
1368
  /* Lower 12 bits of address.  Special identity for or case.  */
1369
  HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type  */
1370
   0,     /* rightshift  */
1371
   4,     /* size  */
1372
   12,      /* bitsize  */
1373
   false,     /* pc_relative  */
1374
   0,     /* bitpos  */
1375
   complain_overflow_dont,/* complain_on_overflow  */
1376
   bfd_elf_generic_reloc, /* special_function  */
1377
   "R_NDS32_LO12S0_ORI_RELA",/* name  */
1378
   false,     /* partial_inplace  */
1379
   0x00000fff,    /* src_mask  */
1380
   0x00000fff,    /* dst_mask  */
1381
   false),    /* pcrel_offset  */
1382
1383
  /* Small data area 19 bits offset.  */
1384
  HOWTO2 (R_NDS32_SDA16S3_RELA, /* type  */
1385
   3,     /* rightshift  */
1386
   4,     /* size  */
1387
   16,      /* bitsize  */
1388
   false,     /* pc_relative  */
1389
   0,     /* bitpos  */
1390
   complain_overflow_signed,/* complain_on_overflow  */
1391
   bfd_elf_generic_reloc, /* special_function  */
1392
   "R_NDS32_SDA16S3_RELA",/* name  */
1393
   false,     /* partial_inplace  */
1394
   0x0000ffff,    /* src_mask  */
1395
   0x0000ffff,    /* dst_mask  */
1396
   false),    /* pcrel_offset  */
1397
1398
  /* Small data area 15 bits offset.  */
1399
  HOWTO2 (R_NDS32_SDA17S2_RELA, /* type  */
1400
   2,     /* rightshift  */
1401
   4,     /* size  */
1402
   17,      /* bitsize  */
1403
   false,     /* pc_relative  */
1404
   0,     /* bitpos  */
1405
   complain_overflow_signed,/* complain_on_overflow  */
1406
   bfd_elf_generic_reloc, /* special_function  */
1407
   "R_NDS32_SDA17S2_RELA",/* name  */
1408
   false,     /* partial_inplace  */
1409
   0x0001ffff,    /* src_mask  */
1410
   0x0001ffff,    /* dst_mask  */
1411
   false),    /* pcrel_offset  */
1412
1413
  HOWTO2 (R_NDS32_SDA18S1_RELA, /* type  */
1414
   1,     /* rightshift  */
1415
   4,     /* size  */
1416
   18,      /* bitsize  */
1417
   false,     /* pc_relative  */
1418
   0,     /* bitpos  */
1419
   complain_overflow_signed,/* complain_on_overflow  */
1420
   bfd_elf_generic_reloc, /* special_function  */
1421
   "R_NDS32_SDA18S1_RELA",/* name  */
1422
   false,     /* partial_inplace  */
1423
   0x0003ffff,    /* src_mask  */
1424
   0x0003ffff,    /* dst_mask  */
1425
   false),    /* pcrel_offset  */
1426
1427
  HOWTO2 (R_NDS32_SDA19S0_RELA, /* type  */
1428
   0,     /* rightshift  */
1429
   4,     /* size  */
1430
   19,      /* bitsize  */
1431
   false,     /* pc_relative  */
1432
   0,     /* bitpos  */
1433
   complain_overflow_signed,/* complain_on_overflow  */
1434
   bfd_elf_generic_reloc, /* special_function  */
1435
   "R_NDS32_SDA19S0_RELA",/* name  */
1436
   false,     /* partial_inplace  */
1437
   0x0007ffff,    /* src_mask  */
1438
   0x0007ffff,    /* dst_mask  */
1439
   false),    /* pcrel_offset  */
1440
  HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type  */
1441
   0,     /* rightshift  */
1442
   1,     /* size  */
1443
   8,     /* bitsize  */
1444
   false,     /* pc_relative  */
1445
   0,     /* bitpos  */
1446
   complain_overflow_dont,/* complain_on_overflow  */
1447
   nds32_elf_ignore_reloc,/* special_function  */
1448
   "R_NDS32_DWARF2_OP1_RELA",/* name  */
1449
   false,     /* partial_inplace  */
1450
   0xff,      /* src_mask  */
1451
   0xff,      /* dst_mask  */
1452
   false),    /* pcrel_offset  */
1453
1454
  HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type  */
1455
   0,     /* rightshift  */
1456
   2,     /* size  */
1457
   16,      /* bitsize  */
1458
   false,     /* pc_relative  */
1459
   0,     /* bitpos  */
1460
   complain_overflow_dont,/* complain_on_overflow  */
1461
   nds32_elf_ignore_reloc,/* special_function  */
1462
   "R_NDS32_DWARF2_OP2_RELA",/* name  */
1463
   false,     /* partial_inplace  */
1464
   0xffff,    /* src_mask  */
1465
   0xffff,    /* dst_mask  */
1466
   false),    /* pcrel_offset  */
1467
1468
  HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type  */
1469
   0,     /* rightshift  */
1470
   4,     /* size  */
1471
   32,      /* bitsize  */
1472
   false,     /* pc_relative  */
1473
   0,     /* bitpos  */
1474
   complain_overflow_dont,/* complain_on_overflow  */
1475
   nds32_elf_ignore_reloc,/* special_function  */
1476
   "R_NDS32_DWARF2_LEB_RELA",/* name  */
1477
   false,     /* partial_inplace  */
1478
   0xffffffff,    /* src_mask  */
1479
   0xffffffff,    /* dst_mask  */
1480
   false),    /* pcrel_offset  */
1481
1482
  HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type  */
1483
   0,     /* rightshift  */
1484
   2,     /* size  */
1485
   16,      /* bitsize  */
1486
   false,     /* pc_relative  */
1487
   0,     /* bitpos  */
1488
   complain_overflow_dont,/* complain_on_overflow  */
1489
   nds32_elf_ignore_reloc,/* special_function  */
1490
   "R_NDS32_UPDATE_TA_RELA",/* name  */
1491
   false,     /* partial_inplace  */
1492
   0xffff,    /* src_mask  */
1493
   0xffff,    /* dst_mask  */
1494
   false),    /* pcrel_offset  */
1495
1496
  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1497
     entry for the symbol.  */
1498
  HOWTO2 (R_NDS32_9_PLTREL, /* type  */
1499
   1,     /* rightshift  */
1500
   2,     /* size  */
1501
   8,     /* bitsize  */
1502
   true,      /* pc_relative  */
1503
   0,     /* bitpos  */
1504
   complain_overflow_signed,/* complain_on_overflow  */
1505
   bfd_elf_generic_reloc, /* special_function  */
1506
   "R_NDS32_9_PLTREL",  /* name  */
1507
   false,     /* partial_inplace  */
1508
   0xff,      /* src_mask  */
1509
   0xff,      /* dst_mask  */
1510
   true),     /* pcrel_offset  */
1511
1512
  /* Low 20 bits of PLT symbol offset relative to GOT (GP).  */
1513
  HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type  */
1514
   0,     /* rightshift  */
1515
   4,     /* size  */
1516
   20,      /* bitsize  */
1517
   false,     /* pc_relative  */
1518
   0,     /* bitpos  */
1519
   complain_overflow_dont,/* complain_on_overflow  */
1520
   bfd_elf_generic_reloc, /* special_function  */
1521
   "R_NDS32_PLT_GOTREL_LO20",/* name  */
1522
   false,     /* partial_inplace  */
1523
   0x000fffff,    /* src_mask  */
1524
   0x000fffff,    /* dst_mask  */
1525
   false),    /* pcrel_offset  */
1526
1527
  /* low 15 bits of PLT symbol offset relative to GOT (GP)  */
1528
  HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type  */
1529
   0,     /* rightshift  */
1530
   4,     /* size  */
1531
   15,      /* bitsize  */
1532
   false,     /* pc_relative  */
1533
   0,     /* bitpos  */
1534
   complain_overflow_dont,/* complain_on_overflow  */
1535
   bfd_elf_generic_reloc, /* special_function  */
1536
   "R_NDS32_PLT_GOTREL_LO15",/* name  */
1537
   false,     /* partial_inplace  */
1538
   0x00007fff,    /* src_mask  */
1539
   0x00007fff,    /* dst_mask  */
1540
   false),    /* pcrel_offset  */
1541
1542
  /* Low 19 bits of PLT symbol offset relative to GOT (GP).  */
1543
  HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type  */
1544
   0,     /* rightshift  */
1545
   4,     /* size  */
1546
   19,      /* bitsize  */
1547
   false,     /* pc_relative  */
1548
   0,     /* bitpos  */
1549
   complain_overflow_dont,/* complain_on_overflow  */
1550
   bfd_elf_generic_reloc, /* special_function  */
1551
   "R_NDS32_PLT_GOTREL_LO19",/* name  */
1552
   false,     /* partial_inplace  */
1553
   0x0007ffff,    /* src_mask  */
1554
   0x0007ffff,    /* dst_mask  */
1555
   false),    /* pcrel_offset  */
1556
1557
  HOWTO2 (R_NDS32_GOT_LO15, /* type  */
1558
   0,     /* rightshift  */
1559
   4,     /* size  */
1560
   15,      /* bitsize  */
1561
   false,     /* pc_relative  */
1562
   0,     /* bitpos  */
1563
   complain_overflow_dont,/* complain_on_overflow  */
1564
   bfd_elf_generic_reloc, /* special_function  */
1565
   "R_NDS32_GOT_LO15",  /* name  */
1566
   false,     /* partial_inplace  */
1567
   0x00007fff,    /* src_mask  */
1568
   0x00007fff,    /* dst_mask  */
1569
   false),    /* pcrel_offset  */
1570
1571
  HOWTO2 (R_NDS32_GOT_LO19, /* type  */
1572
   0,     /* rightshift  */
1573
   4,     /* size  */
1574
   19,      /* bitsize  */
1575
   false,     /* pc_relative  */
1576
   0,     /* bitpos  */
1577
   complain_overflow_dont,/* complain_on_overflow  */
1578
   bfd_elf_generic_reloc, /* special_function  */
1579
   "R_NDS32_GOT_LO19",  /* name  */
1580
   false,     /* partial_inplace  */
1581
   0x0007ffff,    /* src_mask  */
1582
   0x0007ffff,    /* dst_mask  */
1583
   false),    /* pcrel_offset  */
1584
1585
  HOWTO2 (R_NDS32_GOTOFF_LO15,  /* type  */
1586
   0,     /* rightshift  */
1587
   4,     /* size  */
1588
   15,      /* bitsize  */
1589
   false,     /* pc_relative  */
1590
   0,     /* bitpos  */
1591
   complain_overflow_dont,/* complain_on_overflow  */
1592
   bfd_elf_generic_reloc, /* special_function  */
1593
   "R_NDS32_GOTOFF_LO15", /* name  */
1594
   false,     /* partial_inplace  */
1595
   0x00007fff,    /* src_mask  */
1596
   0x00007fff,    /* dst_mask  */
1597
   false),    /* pcrel_offset  */
1598
1599
  HOWTO2 (R_NDS32_GOTOFF_LO19,  /* type  */
1600
   0,     /* rightshift  */
1601
   4,     /* size  */
1602
   19,      /* bitsize  */
1603
   false,     /* pc_relative  */
1604
   0,     /* bitpos  */
1605
   complain_overflow_dont,/* complain_on_overflow  */
1606
   bfd_elf_generic_reloc, /* special_function  */
1607
   "R_NDS32_GOTOFF_LO19", /* name  */
1608
   false,     /* partial_inplace  */
1609
   0x0007ffff,    /* src_mask  */
1610
   0x0007ffff,    /* dst_mask  */
1611
   false),    /* pcrel_offset  */
1612
1613
  /* GOT 15 bits offset.  */
1614
  HOWTO2 (R_NDS32_GOT15S2_RELA, /* type  */
1615
   2,     /* rightshift  */
1616
   4,     /* size  */
1617
   15,      /* bitsize  */
1618
   false,     /* pc_relative  */
1619
   0,     /* bitpos  */
1620
   complain_overflow_signed,/* complain_on_overflow  */
1621
   bfd_elf_generic_reloc, /* special_function  */
1622
   "R_NDS32_GOT15S2_RELA",/* name  */
1623
   false,     /* partial_inplace  */
1624
   0x00007fff,    /* src_mask  */
1625
   0x00007fff,    /* dst_mask  */
1626
   false),    /* pcrel_offset  */
1627
1628
  /* GOT 17 bits offset.  */
1629
  HOWTO2 (R_NDS32_GOT17S2_RELA, /* type  */
1630
   2,     /* rightshift  */
1631
   4,     /* size  */
1632
   17,      /* bitsize  */
1633
   false,     /* pc_relative  */
1634
   0,     /* bitpos  */
1635
   complain_overflow_signed,/* complain_on_overflow  */
1636
   bfd_elf_generic_reloc, /* special_function  */
1637
   "R_NDS32_GOT17S2_RELA",/* name  */
1638
   false,     /* partial_inplace  */
1639
   0x0001ffff,    /* src_mask  */
1640
   0x0001ffff,    /* dst_mask  */
1641
   false),    /* pcrel_offset  */
1642
1643
  /* A 5 bit address.  */
1644
  HOWTO2 (R_NDS32_5_RELA, /* type  */
1645
   0,     /* rightshift  */
1646
   2,     /* size  */
1647
   5,     /* bitsize  */
1648
   false,     /* pc_relative  */
1649
   0,     /* bitpos  */
1650
   complain_overflow_signed,/* complain_on_overflow  */
1651
   bfd_elf_generic_reloc, /* special_function  */
1652
   "R_NDS32_5_RELA",  /* name  */
1653
   false,     /* partial_inplace  */
1654
   0x1f,      /* src_mask  */
1655
   0x1f,      /* dst_mask  */
1656
   false),    /* pcrel_offset  */
1657
1658
  HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type  */
1659
   1,     /* rightshift  */
1660
   2,     /* size  */
1661
   9,     /* bitsize  */
1662
   true,      /* pc_relative  */
1663
   0,     /* bitpos  */
1664
   complain_overflow_unsigned,/* complain_on_overflow  */
1665
   bfd_elf_generic_reloc, /* special_function  */
1666
   "R_NDS32_10_UPCREL_RELA",/* name  */
1667
   false,     /* partial_inplace  */
1668
   0x1ff,     /* src_mask  */
1669
   0x1ff,     /* dst_mask  */
1670
   true),     /* pcrel_offset  */
1671
1672
  HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type  */
1673
   2,     /* rightshift  */
1674
   2,     /* size  */
1675
   7,     /* bitsize  */
1676
   false,     /* pc_relative  */
1677
   0,     /* bitpos  */
1678
   complain_overflow_unsigned,/* complain_on_overflow  */
1679
   bfd_elf_generic_reloc, /* special_function  */
1680
   "R_NDS32_SDA_FP7U2_RELA",/* name  */
1681
   false,     /* partial_inplace  */
1682
   0x0000007f,    /* src_mask  */
1683
   0x0000007f,    /* dst_mask  */
1684
   false),    /* pcrel_offset  */
1685
1686
  HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type  */
1687
   1,     /* rightshift  */
1688
   4,     /* size  */
1689
   8,     /* bitsize  */
1690
   true,      /* pc_relative  */
1691
   0,     /* bitpos  */
1692
   complain_overflow_signed,/* complain_on_overflow  */
1693
   bfd_elf_generic_reloc, /* special_function  */
1694
   "R_NDS32_WORD_9_PCREL_RELA",/* name  */
1695
   false,     /* partial_inplace  */
1696
   0xff,      /* src_mask  */
1697
   0xff,      /* dst_mask  */
1698
   true),     /* pcrel_offset  */
1699
1700
  HOWTO2 (R_NDS32_25_ABS_RELA,  /* type  */
1701
   1,     /* rightshift  */
1702
   4,     /* size  */
1703
   24,      /* bitsize  */
1704
   false,     /* pc_relative  */
1705
   0,     /* bitpos  */
1706
   complain_overflow_dont,/* complain_on_overflow  */
1707
   bfd_elf_generic_reloc, /* special_function  */
1708
   "R_NDS32_25_ABS_RELA", /* name  */
1709
   false,     /* partial_inplace  */
1710
   0xffffff,    /* src_mask  */
1711
   0xffffff,    /* dst_mask  */
1712
   false),    /* pcrel_offset  */
1713
1714
  /* A relative 17 bit relocation for ifc, right shifted by 1.  */
1715
  HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type  */
1716
   1,     /* rightshift  */
1717
   4,     /* size  */
1718
   16,      /* bitsize  */
1719
   true,      /* pc_relative  */
1720
   0,     /* bitpos  */
1721
   complain_overflow_signed,/* complain_on_overflow  */
1722
   bfd_elf_generic_reloc, /* special_function  */
1723
   "R_NDS32_17IFC_PCREL_RELA",/* name  */
1724
   false,     /* partial_inplace  */
1725
   0xffff,    /* src_mask  */
1726
   0xffff,    /* dst_mask  */
1727
   true),     /* pcrel_offset  */
1728
1729
  /* A relative unsigned 10 bit relocation for ifc, right shifted by 1.  */
1730
  HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type  */
1731
   1,     /* rightshift  */
1732
   2,     /* size  */
1733
   9,     /* bitsize  */
1734
   true,      /* pc_relative  */
1735
   0,     /* bitpos  */
1736
   complain_overflow_unsigned,/* complain_on_overflow  */
1737
   bfd_elf_generic_reloc, /* special_function  */
1738
   "R_NDS32_10IFCU_PCREL_RELA",/* name  */
1739
   false,     /* partial_inplace  */
1740
   0x1ff,     /* src_mask  */
1741
   0x1ff,     /* dst_mask  */
1742
   true),     /* pcrel_offset  */
1743
1744
  /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol.  */
1745
  HOWTO2 (R_NDS32_TLS_LE_HI20,  /* type  */
1746
   12,      /* rightshift  */
1747
   4,     /* size  */
1748
   20,      /* bitsize  */
1749
   false,     /* pc_relative  */
1750
   0,     /* bitpos  */
1751
   complain_overflow_dont,/* complain_on_overflow  */
1752
   bfd_elf_generic_reloc, /* special_function  */
1753
   "R_NDS32_TLS_LE_HI20", /* name  */
1754
   false,     /* partial_inplace  */
1755
   0x000fffff,    /* src_mask  */
1756
   0x000fffff,    /* dst_mask  */
1757
   false),    /* pcrel_offset  */
1758
1759
  HOWTO2 (R_NDS32_TLS_LE_LO12,  /* type  */
1760
   0,     /* rightshift  */
1761
   4,     /* size  */
1762
   12,      /* bitsize  */
1763
   false,     /* pc_relative  */
1764
   0,     /* bitpos  */
1765
   complain_overflow_dont,/* complain_on_overflow  */
1766
   bfd_elf_generic_reloc, /* special_function  */
1767
   "R_NDS32_TLS_LE_LO12", /* name  */
1768
   false,     /* partial_inplace  */
1769
   0x00000fff,    /* src_mask  */
1770
   0x00000fff,    /* dst_mask  */
1771
   false),    /* pcrel_offset  */
1772
1773
  /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol.  */
1774
  HOWTO2 (R_NDS32_TLS_IE_HI20,  /* type  */
1775
   12,      /* rightshift  */
1776
   4,     /* size  */
1777
   20,      /* bitsize  */
1778
   false,     /* pc_relative  */
1779
   0,     /* bitpos  */
1780
   complain_overflow_dont,/* complain_on_overflow  */
1781
   bfd_elf_generic_reloc, /* special_function  */
1782
   "R_NDS32_TLS_IE_HI20", /* name  */
1783
   false,     /* partial_inplace  */
1784
   0x000fffff,    /* src_mask  */
1785
   0x000fffff,    /* dst_mask  */
1786
   false),    /* pcrel_offset  */
1787
1788
  HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type  */
1789
   2,     /* rightshift  */
1790
   4,     /* size  */
1791
   10,      /* bitsize  */
1792
   false,     /* pc_relative  */
1793
   0,     /* bitpos  */
1794
   complain_overflow_dont,/* complain_on_overflow  */
1795
   bfd_elf_generic_reloc, /* special_function  */
1796
   "R_NDS32_TLS_IE_LO12S2",/* name  */
1797
   false,     /* partial_inplace  */
1798
   0x000003ff,    /* src_mask  */
1799
   0x000003ff,    /* dst_mask  */
1800
   false),    /* pcrel_offset  */
1801
1802
  /* TLS LE TP offset relocation  */
1803
  HOWTO2 (R_NDS32_TLS_TPOFF,  /* type  */
1804
   0,     /* rightshift  */
1805
   4,     /* size  */
1806
   32,      /* bitsize  */
1807
   false,     /* pc_relative  */
1808
   0,     /* bitpos  */
1809
   complain_overflow_bitfield,/* complain_on_overflow  */
1810
   bfd_elf_generic_reloc, /* special_function  */
1811
   "R_NDS32_TLS_TPOFF", /* name  */
1812
   false,     /* partial_inplace  */
1813
   0xffffffff,    /* src_mask  */
1814
   0xffffffff,    /* dst_mask  */
1815
   false),    /* pcrel_offset  */
1816
1817
  /* A 20 bit address.  */
1818
  HOWTO2 (R_NDS32_TLS_LE_20,  /* type  */
1819
   0,     /* rightshift  */
1820
   4,     /* size  */
1821
   20,      /* bitsize  */
1822
   false,     /* pc_relative  */
1823
   0,     /* bitpos  */
1824
   complain_overflow_signed,/* complain_on_overflow  */
1825
   bfd_elf_generic_reloc, /* special_function  */
1826
   "R_NDS32_TLS_LE_20", /* name  */
1827
   false,     /* partial_inplace  */
1828
   0xfffff,   /* src_mask  */
1829
   0xfffff,   /* dst_mask  */
1830
   false),    /* pcrel_offset  */
1831
1832
  HOWTO2 (R_NDS32_TLS_LE_15S0,  /* type  */
1833
   0,     /* rightshift  */
1834
   4,     /* size  */
1835
   15,      /* bitsize  */
1836
   false,     /* pc_relative  */
1837
   0,     /* bitpos  */
1838
   complain_overflow_signed,/* complain_on_overflow  */
1839
   bfd_elf_generic_reloc, /* special_function  */
1840
   "R_NDS32_TLS_LE_15S0", /* name  */
1841
   false,     /* partial_inplace  */
1842
   0x7fff,    /* src_mask  */
1843
   0x7fff,    /* dst_mask  */
1844
   false),    /* pcrel_offset  */
1845
1846
  HOWTO2 (R_NDS32_TLS_LE_15S1,  /* type  */
1847
   1,     /* rightshift  */
1848
   4,     /* size  */
1849
   15,      /* bitsize  */
1850
   false,     /* pc_relative  */
1851
   0,     /* bitpos  */
1852
   complain_overflow_signed,/* complain_on_overflow  */
1853
   bfd_elf_generic_reloc, /* special_function  */
1854
   "R_NDS32_TLS_LE_15S1", /* name  */
1855
   false,     /* partial_inplace  */
1856
   0x7fff,    /* src_mask  */
1857
   0x7fff,    /* dst_mask  */
1858
   false),    /* pcrel_offset  */
1859
1860
  HOWTO2 (R_NDS32_TLS_LE_15S2,  /* type  */
1861
   2,     /* rightshift  */
1862
   4,     /* size  */
1863
   15,      /* bitsize  */
1864
   false,     /* pc_relative  */
1865
   0,     /* bitpos  */
1866
   complain_overflow_signed,/* complain_on_overflow  */
1867
   bfd_elf_generic_reloc, /* special_function  */
1868
   "R_NDS32_TLS_LE_15S2", /* name  */
1869
   false,     /* partial_inplace  */
1870
   0x7fff,    /* src_mask  */
1871
   0x7fff,    /* dst_mask  */
1872
   false),    /* pcrel_offset  */
1873
1874
  /* Relax hint for unconditional call sequence  */
1875
  HOWTO2 (R_NDS32_LONGCALL4,  /* type  */
1876
   0,     /* rightshift  */
1877
   4,     /* size  */
1878
   32,      /* bitsize  */
1879
   false,     /* pc_relative  */
1880
   0,     /* bitpos  */
1881
   complain_overflow_dont,/* complain_on_overflow  */
1882
   nds32_elf_ignore_reloc,/* special_function  */
1883
   "R_NDS32_LONGCALL4", /* name  */
1884
   false,     /* partial_inplace  */
1885
   0xffffffff,    /* src_mask  */
1886
   0xffffffff,    /* dst_mask  */
1887
   false),    /* pcrel_offset  */
1888
1889
  /* Relax hint for conditional call sequence.  */
1890
  HOWTO2 (R_NDS32_LONGCALL5,  /* type  */
1891
   0,     /* rightshift  */
1892
   4,     /* size  */
1893
   32,      /* bitsize  */
1894
   false,     /* pc_relative  */
1895
   0,     /* bitpos  */
1896
   complain_overflow_dont,/* complain_on_overflow  */
1897
   nds32_elf_ignore_reloc,/* special_function  */
1898
   "R_NDS32_LONGCALL5", /* name  */
1899
   false,     /* partial_inplace  */
1900
   0xffffffff,    /* src_mask  */
1901
   0xffffffff,    /* dst_mask  */
1902
   false),    /* pcrel_offset  */
1903
1904
  /* Relax hint for conditional call sequence.  */
1905
  HOWTO2 (R_NDS32_LONGCALL6,  /* type  */
1906
   0,     /* rightshift  */
1907
   4,     /* size  */
1908
   32,      /* bitsize  */
1909
   false,     /* pc_relative  */
1910
   0,     /* bitpos  */
1911
   complain_overflow_dont,/* complain_on_overflow  */
1912
   nds32_elf_ignore_reloc,/* special_function  */
1913
   "R_NDS32_LONGCALL6", /* name  */
1914
   false,     /* partial_inplace  */
1915
   0xffffffff,    /* src_mask  */
1916
   0xffffffff,    /* dst_mask  */
1917
   false),    /* pcrel_offset  */
1918
1919
  /* Relax hint for unconditional branch sequence.  */
1920
  HOWTO2 (R_NDS32_LONGJUMP4,  /* type  */
1921
   0,     /* rightshift  */
1922
   4,     /* size  */
1923
   32,      /* bitsize  */
1924
   false,     /* pc_relative  */
1925
   0,     /* bitpos  */
1926
   complain_overflow_dont,/* complain_on_overflow  */
1927
   nds32_elf_ignore_reloc,/* special_function  */
1928
   "R_NDS32_LONGJUMP4", /* name  */
1929
   false,     /* partial_inplace  */
1930
   0xffffffff,    /* src_mask  */
1931
   0xffffffff,    /* dst_mask  */
1932
   false),    /* pcrel_offset  */
1933
1934
  /* Relax hint for conditional branch sequence.  */
1935
  HOWTO2 (R_NDS32_LONGJUMP5,  /* type  */
1936
   0,     /* rightshift  */
1937
   4,     /* size  */
1938
   32,      /* bitsize  */
1939
   false,     /* pc_relative  */
1940
   0,     /* bitpos  */
1941
   complain_overflow_dont,/* complain_on_overflow  */
1942
   nds32_elf_ignore_reloc,/* special_function  */
1943
   "R_NDS32_LONGJUMP5", /* name  */
1944
   false,     /* partial_inplace  */
1945
   0xffffffff,    /* src_mask  */
1946
   0xffffffff,    /* dst_mask  */
1947
   false),    /* pcrel_offset  */
1948
1949
  /* Relax hint for conditional branch sequence.  */
1950
  HOWTO2 (R_NDS32_LONGJUMP6,  /* type  */
1951
   0,     /* rightshift  */
1952
   4,     /* size  */
1953
   32,      /* bitsize  */
1954
   false,     /* pc_relative  */
1955
   0,     /* bitpos  */
1956
   complain_overflow_dont,/* complain_on_overflow  */
1957
   nds32_elf_ignore_reloc,/* special_function  */
1958
   "R_NDS32_LONGJUMP6", /* name  */
1959
   false,     /* partial_inplace  */
1960
   0xffffffff,    /* src_mask  */
1961
   0xffffffff,    /* dst_mask  */
1962
   false),    /* pcrel_offset  */
1963
1964
  /* Relax hint for conditional branch sequence.  */
1965
  HOWTO2 (R_NDS32_LONGJUMP7,  /* type  */
1966
   0,     /* rightshift  */
1967
   4,     /* size  */
1968
   32,      /* bitsize  */
1969
   false,     /* pc_relative  */
1970
   0,     /* bitpos  */
1971
   complain_overflow_dont,/* complain_on_overflow  */
1972
   nds32_elf_ignore_reloc,/* special_function  */
1973
   "R_NDS32_LONGJUMP7", /* name  */
1974
   false,     /* partial_inplace  */
1975
   0xffffffff,    /* src_mask  */
1976
   0xffffffff,    /* dst_mask  */
1977
   false),    /* pcrel_offset  */
1978
1979
  EMPTY_HOWTO (114),
1980
1981
  HOWTO2 (R_NDS32_TLS_IE_LO12,  /* type  */
1982
   0,     /* rightshift  */
1983
   4,     /* size  */
1984
   12,      /* bitsize  */
1985
   false,     /* pc_relative  */
1986
   0,     /* bitpos  */
1987
   complain_overflow_dont,/* complain_on_overflow  */
1988
   bfd_elf_generic_reloc, /* special_function  */
1989
   "R_NDS32_TLS_IE_LO12", /* name  */
1990
   false,     /* partial_inplace  */
1991
   0x00000fff,    /* src_mask  */
1992
   0x00000fff,    /* dst_mask  */
1993
   false),    /* pcrel_offset  */
1994
1995
  /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1996
     entry for the symbol.  */
1997
  HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type  */
1998
   12,      /* rightshift  */
1999
   4,     /* size  */
2000
   20,      /* bitsize  */
2001
   false,     /* pc_relative  */
2002
   0,     /* bitpos  */
2003
   complain_overflow_dont,/* complain_on_overflow  */
2004
   bfd_elf_generic_reloc, /* special_function  */
2005
   "R_NDS32_TLS_IEGP_HI20",/* name  */
2006
   false,     /* partial_inplace  */
2007
   0x000fffff,    /* src_mask  */
2008
   0x000fffff,    /* dst_mask  */
2009
   false),    /* pcrel_offset  */
2010
2011
  HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type  */
2012
   0,     /* rightshift  */
2013
   4,     /* size  */
2014
   12,      /* bitsize  */
2015
   false,     /* pc_relative  */
2016
   0,     /* bitpos  */
2017
   complain_overflow_dont,/* complain_on_overflow  */
2018
   bfd_elf_generic_reloc, /* special_function  */
2019
   "R_NDS32_TLS_IEGP_LO12",/* name  */
2020
   false,     /* partial_inplace  */
2021
   0x00000fff,    /* src_mask  */
2022
   0x00000fff,    /* dst_mask  */
2023
   false),    /* pcrel_offset  */
2024
2025
  HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type  */
2026
   2,     /* rightshift  */
2027
   4,     /* size  */
2028
   10,      /* bitsize  */
2029
   false,     /* pc_relative  */
2030
   0,     /* bitpos  */
2031
   complain_overflow_dont,/* complain_on_overflow  */
2032
   bfd_elf_generic_reloc, /* special_function  */
2033
   "R_NDS32_TLS_IEGP_LO12S2",/* name  */
2034
   false,     /* partial_inplace  */
2035
   0x000003ff,    /* src_mask  */
2036
   0x000003ff,    /* dst_mask  */
2037
   false),    /* pcrel_offset  */
2038
2039
  /* TLS description relocation  */
2040
  HOWTO2 (R_NDS32_TLS_DESC, /* type  */
2041
   12,      /* rightshift  */
2042
   4,     /* size  */
2043
   20,      /* bitsize  */
2044
   false,     /* pc_relative  */
2045
   0,     /* bitpos  */
2046
   complain_overflow_dont,/* complain_on_overflow  */
2047
   nds32_elf_hi20_reloc,  /* special_function  */
2048
   "R_NDS32_TLS_DESC_HI20",/* name  */
2049
   false,     /* partial_inplace  */
2050
   0x000fffff,    /* src_mask  */
2051
   0x000fffff,    /* dst_mask  */
2052
   false),    /* pcrel_offset  */
2053
2054
  /* TLS GD/LD description offset high part.  */
2055
  HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type  */
2056
   12,      /* rightshift  */
2057
   4,     /* size  */
2058
   20,      /* bitsize  */
2059
   false,     /* pc_relative  */
2060
   0,     /* bitpos  */
2061
   complain_overflow_dont,/* complain_on_overflow  */
2062
   nds32_elf_hi20_reloc,  /* special_function  */
2063
   "R_NDS32_TLS_DESC_HI20",/* name  */
2064
   false,     /* partial_inplace  */
2065
   0x000fffff,    /* src_mask  */
2066
   0x000fffff,    /* dst_mask  */
2067
   false),    /* pcrel_offset  */
2068
2069
  /* TLS GD/LD description offset low part.  */
2070
  HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type  */
2071
   0,     /* rightshift  */
2072
   4,     /* size  */
2073
   12,      /* bitsize  */
2074
   false,     /* pc_relative  */
2075
   0,     /* bitpos  */
2076
   complain_overflow_dont,/* complain_on_overflow  */
2077
   nds32_elf_lo12_reloc,  /* special_function  */
2078
   "R_NDS32_TLS_DESC_LO12",/* name  */
2079
   false,     /* partial_inplace  */
2080
   0x00000fff,    /* src_mask  */
2081
   0x00000fff,    /* dst_mask  */
2082
   false),    /* pcrel_offset  */
2083
2084
  /* TLS GD/LD description offset set (movi).  */
2085
  HOWTO2 (R_NDS32_TLS_DESC_20,  /* type  */
2086
   0,     /* rightshift  */
2087
   4,     /* size  */
2088
   20,      /* bitsize  */
2089
   false,     /* pc_relative  */
2090
   0,     /* bitpos  */
2091
   complain_overflow_signed,/* complain_on_overflow  */
2092
   bfd_elf_generic_reloc, /* special_function  */
2093
   "R_NDS32_TLS_DESC_20", /* name  */
2094
   false,     /* partial_inplace  */
2095
   0x000fffff,    /* src_mask  */
2096
   0x000fffff,    /* dst_mask  */
2097
   false),    /* pcrel_offset  */
2098
2099
  /* TLS GD/LD description offset set (lwi.gp).  */
2100
  HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type  */
2101
   2,     /* rightshift  */
2102
   4,     /* size  */
2103
   17,      /* bitsize  */
2104
   false,     /* pc_relative  */
2105
   0,     /* bitpos  */
2106
   complain_overflow_signed,/* complain_on_overflow  */
2107
   bfd_elf_generic_reloc, /* special_function  */
2108
   "R_NDS32_TLS_DESC_SDA17S2",/* name  */
2109
   false,     /* partial_inplace  */
2110
   0x0001ffff,    /* src_mask  */
2111
   0x0001ffff,    /* dst_mask  */
2112
   false),    /* pcrel_offset  */
2113
};
2114
2115
/* Relocations used for relaxation.  */
2116
#define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2117
  [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2118
2119
static reloc_howto_type nds32_elf_relax_howto_table[] = {
2120
  HOWTO3 (R_NDS32_RELAX_ENTRY,  /* type  */
2121
   0,     /* rightshift  */
2122
   4,     /* size  */
2123
   32,      /* bitsize  */
2124
   false,     /* pc_relative  */
2125
   0,     /* bitpos  */
2126
   complain_overflow_dont,/* complain_on_overflow  */
2127
   nds32_elf_ignore_reloc,/* special_function  */
2128
   "R_NDS32_RELAX_ENTRY", /* name  */
2129
   false,     /* partial_inplace  */
2130
   0xffffffff,    /* src_mask  */
2131
   0xffffffff,    /* dst_mask  */
2132
   false),    /* pcrel_offset  */
2133
2134
  HOWTO3 (R_NDS32_GOT_SUFF, /* type  */
2135
   0,     /* rightshift  */
2136
   4,     /* size  */
2137
   32,      /* bitsize  */
2138
   false,     /* pc_relative  */
2139
   0,     /* bitpos  */
2140
   complain_overflow_dont,/* complain_on_overflow  */
2141
   nds32_elf_ignore_reloc,/* special_function  */
2142
   "R_NDS32_GOT_SUFF",  /* name  */
2143
   false,     /* partial_inplace  */
2144
   0xffffffff,    /* src_mask  */
2145
   0xffffffff,    /* dst_mask  */
2146
   false),    /* pcrel_offset  */
2147
2148
  HOWTO3 (R_NDS32_GOTOFF_SUFF,  /* type  */
2149
   0,     /* rightshift  */
2150
   4,     /* size  */
2151
   32,      /* bitsize  */
2152
   false,     /* pc_relative  */
2153
   0,     /* bitpos  */
2154
   complain_overflow_bitfield,/* complain_on_overflow  */
2155
   nds32_elf_ignore_reloc,/* special_function  */
2156
   "R_NDS32_GOTOFF_SUFF", /* name  */
2157
   false,     /* partial_inplace  */
2158
   0xffffffff,    /* src_mask  */
2159
   0xffffffff,    /* dst_mask  */
2160
   false),    /* pcrel_offset  */
2161
2162
  HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type  */
2163
   0,     /* rightshift  */
2164
   4,     /* size  */
2165
   32,      /* bitsize  */
2166
   false,     /* pc_relative  */
2167
   0,     /* bitpos  */
2168
   complain_overflow_dont,/* complain_on_overflow  */
2169
   nds32_elf_ignore_reloc,/* special_function  */
2170
   "R_NDS32_PLT_GOT_SUFF",/* name  */
2171
   false,     /* partial_inplace  */
2172
   0xffffffff,    /* src_mask  */
2173
   0xffffffff,    /* dst_mask  */
2174
   false),    /* pcrel_offset  */
2175
2176
  HOWTO3 (R_NDS32_MULCALL_SUFF, /* type  */
2177
   0,     /* rightshift  */
2178
   4,     /* size  */
2179
   32,      /* bitsize  */
2180
   false,     /* pc_relative  */
2181
   0,     /* bitpos  */
2182
   complain_overflow_dont,/* complain_on_overflow  */
2183
   nds32_elf_ignore_reloc,/* special_function  */
2184
   "R_NDS32_MULCALL_SUFF",/* name  */
2185
   false,     /* partial_inplace  */
2186
   0xffffffff,    /* src_mask  */
2187
   0xffffffff,    /* dst_mask  */
2188
   false),    /* pcrel_offset  */
2189
2190
  HOWTO3 (R_NDS32_PTR,    /* type  */
2191
   0,     /* rightshift  */
2192
   4,     /* size  */
2193
   32,      /* bitsize  */
2194
   false,     /* pc_relative  */
2195
   0,     /* bitpos  */
2196
   complain_overflow_dont,/* complain_on_overflow  */
2197
   nds32_elf_ignore_reloc,/* special_function  */
2198
   "R_NDS32_PTR",   /* name  */
2199
   false,     /* partial_inplace  */
2200
   0xffffffff,    /* src_mask  */
2201
   0xffffffff,    /* dst_mask  */
2202
   false),    /* pcrel_offset  */
2203
2204
  HOWTO3 (R_NDS32_PTR_COUNT,  /* type  */
2205
   0,     /* rightshift  */
2206
   4,     /* size  */
2207
   32,      /* bitsize  */
2208
   false,     /* pc_relative  */
2209
   0,     /* bitpos  */
2210
   complain_overflow_dont,/* complain_on_overflow  */
2211
   nds32_elf_ignore_reloc,/* special_function  */
2212
   "R_NDS32_PTR_COUNT", /* name  */
2213
   false,     /* partial_inplace  */
2214
   0xffffffff,    /* src_mask  */
2215
   0xffffffff,    /* dst_mask  */
2216
   false),    /* pcrel_offset  */
2217
2218
  HOWTO3 (R_NDS32_PTR_RESOLVED, /* type  */
2219
   0,     /* rightshift  */
2220
   4,     /* size  */
2221
   32,      /* bitsize  */
2222
   false,     /* pc_relative  */
2223
   0,     /* bitpos  */
2224
   complain_overflow_dont,/* complain_on_overflow  */
2225
   nds32_elf_ignore_reloc,/* special_function  */
2226
   "R_NDS32_PTR_RESOLVED",/* name  */
2227
   false,     /* partial_inplace  */
2228
   0xffffffff,    /* src_mask  */
2229
   0xffffffff,    /* dst_mask  */
2230
   false),    /* pcrel_offset  */
2231
2232
  HOWTO3 (R_NDS32_PLTBLOCK, /* type  */
2233
   0,     /* rightshift  */
2234
   4,     /* size  */
2235
   32,      /* bitsize  */
2236
   false,     /* pc_relative  */
2237
   0,     /* bitpos  */
2238
   complain_overflow_dont,/* complain_on_overflow  */
2239
   nds32_elf_ignore_reloc,/* special_function  */
2240
   "R_NDS32_PLTBLOCK",  /* name  */
2241
   false,     /* partial_inplace  */
2242
   0xffffffff,    /* src_mask  */
2243
   0xffffffff,    /* dst_mask  */
2244
   false),    /* pcrel_offset  */
2245
2246
  HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type  */
2247
   0,     /* rightshift  */
2248
   4,     /* size  */
2249
   32,      /* bitsize  */
2250
   false,     /* pc_relative  */
2251
   0,     /* bitpos  */
2252
   complain_overflow_dont,/* complain_on_overflow  */
2253
   nds32_elf_ignore_reloc,/* special_function  */
2254
   "R_NDS32_RELAX_REGION_BEGIN",/* name  */
2255
   false,     /* partial_inplace  */
2256
   0xffffffff,    /* src_mask  */
2257
   0xffffffff,    /* dst_mask  */
2258
   false),    /* pcrel_offset  */
2259
2260
  HOWTO3 (R_NDS32_RELAX_REGION_END,/* type  */
2261
   0,     /* rightshift  */
2262
   4,     /* size  */
2263
   32,      /* bitsize  */
2264
   false,     /* pc_relative  */
2265
   0,     /* bitpos  */
2266
   complain_overflow_dont,/* complain_on_overflow  */
2267
   nds32_elf_ignore_reloc,/* special_function  */
2268
   "R_NDS32_RELAX_REGION_END",/* name  */
2269
   false,     /* partial_inplace  */
2270
   0xffffffff,    /* src_mask  */
2271
   0xffffffff,    /* dst_mask  */
2272
   false),    /* pcrel_offset  */
2273
2274
  HOWTO3 (R_NDS32_MINUEND,  /* type  */
2275
   0,     /* rightshift  */
2276
   4,     /* size  */
2277
   32,      /* bitsize  */
2278
   false,     /* pc_relative  */
2279
   0,     /* bitpos  */
2280
   complain_overflow_dont,/* complain_on_overflow  */
2281
   nds32_elf_ignore_reloc,/* special_function  */
2282
   "R_NDS32_MINUEND", /* name  */
2283
   false,     /* partial_inplace  */
2284
   0xffffffff,    /* src_mask  */
2285
   0xffffffff,    /* dst_mask  */
2286
   false),    /* pcrel_offset  */
2287
2288
  HOWTO3 (R_NDS32_SUBTRAHEND, /* type  */
2289
   0,     /* rightshift  */
2290
   4,     /* size  */
2291
   32,      /* bitsize  */
2292
   false,     /* pc_relative  */
2293
   0,     /* bitpos  */
2294
   complain_overflow_dont,/* complain_on_overflow  */
2295
   nds32_elf_ignore_reloc,/* special_function  */
2296
   "R_NDS32_SUBTRAHEND",  /* name  */
2297
   false,     /* partial_inplace  */
2298
   0xffffffff,    /* src_mask  */
2299
   0xffffffff,    /* dst_mask  */
2300
   false),    /* pcrel_offset  */
2301
2302
  HOWTO3 (R_NDS32_DIFF8,  /* type  */
2303
   0,     /* rightshift  */
2304
   1,     /* size  */
2305
   8,     /* bitsize  */
2306
   false,     /* pc_relative  */
2307
   0,     /* bitpos  */
2308
   complain_overflow_dont,/* complain_on_overflow  */
2309
   nds32_elf_ignore_reloc,/* special_function  */
2310
   "R_NDS32_DIFF8", /* name  */
2311
   false,     /* partial_inplace  */
2312
   0x000000ff,    /* src_mask  */
2313
   0x000000ff,    /* dst_mask  */
2314
   false),    /* pcrel_offset  */
2315
2316
  HOWTO3 (R_NDS32_DIFF16, /* type  */
2317
   0,     /* rightshift  */
2318
   2,     /* size  */
2319
   16,      /* bitsize  */
2320
   false,     /* pc_relative  */
2321
   0,     /* bitpos  */
2322
   complain_overflow_dont,/* complain_on_overflow  */
2323
   nds32_elf_ignore_reloc,/* special_function  */
2324
   "R_NDS32_DIFF16",  /* name  */
2325
   false,     /* partial_inplace  */
2326
   0x0000ffff,    /* src_mask  */
2327
   0x0000ffff,    /* dst_mask  */
2328
   false),    /* pcrel_offset  */
2329
2330
  HOWTO3 (R_NDS32_DIFF32, /* type  */
2331
   0,     /* rightshift  */
2332
   4,     /* size  */
2333
   32,      /* bitsize  */
2334
   false,     /* pc_relative  */
2335
   0,     /* bitpos  */
2336
   complain_overflow_dont,/* complain_on_overflow  */
2337
   nds32_elf_ignore_reloc,/* special_function  */
2338
   "R_NDS32_DIFF32",  /* name  */
2339
   false,     /* partial_inplace  */
2340
   0xffffffff,    /* src_mask  */
2341
   0xffffffff,    /* dst_mask  */
2342
   false),    /* pcrel_offset  */
2343
2344
  HOWTO3 (R_NDS32_DIFF_ULEB128, /* type  */
2345
   0,     /* rightshift  */
2346
   1,     /* size  */
2347
   0,     /* bitsize  */
2348
   false,     /* pc_relative  */
2349
   0,     /* bitpos  */
2350
   complain_overflow_dont,/* complain_on_overflow  */
2351
   nds32_elf_ignore_reloc,/* special_function  */
2352
   "R_NDS32_DIFF_ULEB128",/* name  */
2353
   false,     /* partial_inplace  */
2354
   0xffffffff,    /* src_mask  */
2355
   0xffffffff,    /* dst_mask  */
2356
   false),    /* pcrel_offset  */
2357
2358
  HOWTO3 (R_NDS32_DATA,   /* type  */
2359
   0,     /* rightshift  */
2360
   4,     /* size  */
2361
   32,      /* bitsize  */
2362
   false,     /* pc_relative  */
2363
   0,     /* bitpos  */
2364
   complain_overflow_dont,/* complain_on_overflow  */
2365
   nds32_elf_ignore_reloc,/* special_function  */
2366
   "R_NDS32_DATA",  /* name  */
2367
   false,     /* partial_inplace  */
2368
   0xffffffff,    /* src_mask  */
2369
   0xffffffff,    /* dst_mask  */
2370
   false),    /* pcrel_offset  */
2371
2372
  HOWTO3 (R_NDS32_TRAN,   /* type  */
2373
   0,     /* rightshift  */
2374
   4,     /* size  */
2375
   32,      /* bitsize  */
2376
   false,     /* pc_relative  */
2377
   0,     /* bitpos  */
2378
   complain_overflow_dont,/* complain_on_overflow  */
2379
   nds32_elf_ignore_reloc,/* special_function  */
2380
   "R_NDS32_TRAN",  /* name  */
2381
   false,     /* partial_inplace  */
2382
   0xffffffff,    /* src_mask  */
2383
   0xffffffff,    /* dst_mask  */
2384
   false),    /* pcrel_offset  */
2385
2386
  HOWTO3 (R_NDS32_TLS_LE_ADD, /* type  */
2387
   0,     /* rightshift  */
2388
   4,     /* size  */
2389
   32,      /* bitsize  */
2390
   false,     /* pc_relative  */
2391
   0,     /* bitpos  */
2392
   complain_overflow_dont,/* complain_on_overflow  */
2393
   nds32_elf_ignore_reloc,/* special_function  */
2394
   "R_NDS32_TLS_LE_ADD",  /* name  */
2395
   false,     /* partial_inplace  */
2396
   0xffffffff,    /* src_mask  */
2397
   0xffffffff,    /* dst_mask  */
2398
   false),    /* pcrel_offset  */
2399
2400
  HOWTO3 (R_NDS32_TLS_LE_LS,  /* type  */
2401
   0,     /* rightshift  */
2402
   4,     /* size  */
2403
   32,      /* bitsize  */
2404
   false,     /* pc_relative  */
2405
   0,     /* bitpos  */
2406
   complain_overflow_dont,/* complain_on_overflow  */
2407
   nds32_elf_ignore_reloc,/* special_function  */
2408
   "R_NDS32_TLS_LE_LS", /* name  */
2409
   false,     /* partial_inplace  */
2410
   0xffffffff,    /* src_mask  */
2411
   0xffffffff,    /* dst_mask  */
2412
   false),    /* pcrel_offset  */
2413
2414
  HOWTO3 (R_NDS32_EMPTY,  /* type  */
2415
   0,     /* rightshift  */
2416
   4,     /* size  */
2417
   32,      /* bitsize  */
2418
   false,     /* pc_relative  */
2419
   0,     /* bitpos  */
2420
   complain_overflow_dont,/* complain_on_overflow  */
2421
   nds32_elf_ignore_reloc,/* special_function  */
2422
   "R_NDS32_EMPTY", /* name  */
2423
   false,     /* partial_inplace  */
2424
   0xffffffff,    /* src_mask  */
2425
   0xffffffff,    /* dst_mask  */
2426
   false),    /* pcrel_offset  */
2427
2428
  /* TLS GD/LD description address base addition.  */
2429
  HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type  */
2430
   0,     /* rightshift  */
2431
   4,     /* size  */
2432
   32,      /* bitsize  */
2433
   false,     /* pc_relative  */
2434
   0,     /* bitpos  */
2435
   complain_overflow_dont,/* complain_on_overflow  */
2436
   nds32_elf_ignore_reloc,/* special_function  */
2437
   "R_NDS32_TLS_DESC_ADD",/* name  */
2438
   false,     /* partial_inplace  */
2439
   0xffffffff,    /* src_mask  */
2440
   0xffffffff,    /* dst_mask  */
2441
   false),    /* pcrel_offset  */
2442
2443
  /* TLS GD/LD description function load.  */
2444
  HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type  */
2445
   0,     /* rightshift  */
2446
   4,     /* size  */
2447
   32,      /* bitsize  */
2448
   false,     /* pc_relative  */
2449
   0,     /* bitpos  */
2450
   complain_overflow_dont,/* complain_on_overflow  */
2451
   nds32_elf_ignore_reloc,/* special_function  */
2452
   "R_NDS32_TLS_DESC_FUNC",/* name  */
2453
   false,     /* partial_inplace  */
2454
   0xffffffff,    /* src_mask  */
2455
   0xffffffff,    /* dst_mask  */
2456
   false),    /* pcrel_offset  */
2457
2458
  /* TLS DESC resolve function call.  */
2459
  HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type  */
2460
   0,     /* rightshift  */
2461
   4,     /* size  */
2462
   32,      /* bitsize  */
2463
   false,     /* pc_relative  */
2464
   0,     /* bitpos  */
2465
   complain_overflow_dont,/* complain_on_overflow  */
2466
   nds32_elf_ignore_reloc,/* special_function  */
2467
   "R_NDS32_TLS_DESC_CALL",/* name  */
2468
   false,     /* partial_inplace  */
2469
   0xffffffff,    /* src_mask  */
2470
   0xffffffff,    /* dst_mask  */
2471
   false),    /* pcrel_offset  */
2472
2473
  /* TLS DESC variable access.  */
2474
  HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type  */
2475
   0,     /* rightshift  */
2476
   4,     /* size  */
2477
   32,      /* bitsize  */
2478
   false,     /* pc_relative  */
2479
   0,     /* bitpos  */
2480
   complain_overflow_dont,/* complain_on_overflow  */
2481
   nds32_elf_ignore_reloc,/* special_function  */
2482
   "R_NDS32_TLS_DESC_MEM",/* name  */
2483
   false,     /* partial_inplace  */
2484
   0xffffffff,    /* src_mask  */
2485
   0xffffffff,    /* dst_mask  */
2486
   false),    /* pcrel_offset  */
2487
2488
  /* TLS GD/LD description mark (@tlsdec).  */
2489
  HOWTO3 (R_NDS32_RELAX_REMOVE, /* type  */
2490
   0,     /* rightshift  */
2491
   4,     /* size  */
2492
   32,      /* bitsize  */
2493
   false,     /* pc_relative  */
2494
   0,     /* bitpos  */
2495
   complain_overflow_dont,/* complain_on_overflow  */
2496
   nds32_elf_ignore_reloc,/* special_function  */
2497
   "R_NDS32_REMOVE",  /* name  */
2498
   false,     /* partial_inplace  */
2499
   0xffffffff,    /* src_mask  */
2500
   0xffffffff,    /* dst_mask  */
2501
   false),    /* pcrel_offset  */
2502
2503
  /* TLS GD/LD description mark (@tlsdec).  */
2504
  HOWTO3 (R_NDS32_RELAX_GROUP,  /* type  */
2505
   0,     /* rightshift  */
2506
   4,     /* size  */
2507
   32,      /* bitsize  */
2508
   false,     /* pc_relative  */
2509
   0,     /* bitpos  */
2510
   complain_overflow_dont,/* complain_on_overflow  */
2511
   nds32_elf_ignore_reloc,/* special_function  */
2512
   "R_NDS32_GROUP", /* name  */
2513
   false,     /* partial_inplace  */
2514
   0xffffffff,    /* src_mask  */
2515
   0xffffffff,    /* dst_mask  */
2516
   false),    /* pcrel_offset  */
2517
2518
  HOWTO3 (R_NDS32_TLS_IEGP_LW,  /* type  */
2519
   0,     /* rightshift  */
2520
   4,     /* size  */
2521
   32,      /* bitsize  */
2522
   false,     /* pc_relative  */
2523
   0,     /* bitpos  */
2524
   complain_overflow_dont,/* complain_on_overflow  */
2525
   nds32_elf_ignore_reloc,/* special_function  */
2526
   "R_NDS32_TLS_IEGP_LW", /* name  */
2527
   false,     /* partial_inplace  */
2528
   0xffffffff,    /* src_mask  */
2529
   0xffffffff,    /* dst_mask  */
2530
   false),    /* pcrel_offset  */
2531
2532
  /* LA and FLSI relaxation.  */
2533
  HOWTO3 (R_NDS32_LSI,    /* type  */
2534
   0,     /* rightshift  */
2535
   4,     /* size  */
2536
   32,      /* bitsize  */
2537
   false,     /* pc_relative  */
2538
   0,     /* bitpos  */
2539
   complain_overflow_dont,/* complain_on_overflow  */
2540
   nds32_elf_ignore_reloc,/* special_function  */
2541
   "R_NDS32_LSI",   /* name  */
2542
   false,     /* partial_inplace  */
2543
   0xffffffff,    /* src_mask  */
2544
   0xffffffff,    /* dst_mask  */
2545
   false),
2546
};
2547
2548
static unsigned long dl_tlsdesc_lazy_trampoline[] =
2549
{
2550
  0x46200000,     /* sethi $r2,#0x0      */
2551
  0x58210000,     /* ori $r2,$r2,#0x0    */
2552
  0x40217400,     /* add $r2,$r2,$gp     */
2553
  0x04210000,     /* lwi $r2,[$r2+#0x0]  */
2554
  0x46300000,     /* sethi $r3,#0x0      */
2555
  0x58318000,     /* ori $r3,$r3,#0x0    */
2556
  0x4031f400,     /* add $r3,$r3,$gp     */
2557
  0x4a000800,     /* jr $r2              */
2558
};
2559
2560
static void
2561
nds32_put_trampoline (void *contents, const unsigned long *template,
2562
          unsigned count)
2563
0
{
2564
0
  unsigned ix;
2565
2566
0
  for (ix = 0; ix != count; ix++)
2567
0
    {
2568
0
      unsigned long insn = template[ix];
2569
0
      bfd_putb32 (insn, (char *) contents + ix * 4);
2570
0
    }
2571
0
}
2572
2573
/* nds32_insertion_sort sorts an array with nmemb elements of size size.
2574
   This prototype is the same as qsort ().  */
2575
2576
static void
2577
nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2578
          int (*compar) (const void *lhs, const void *rhs))
2579
0
{
2580
0
  char *ptr = (char *) base;
2581
0
  int i, j;
2582
0
  char tmp[sizeof (Elf_Internal_Rela)];
2583
2584
0
  BFD_ASSERT (size <= sizeof (tmp));
2585
2586
  /* If i is less than j, i is inserted before j.
2587
2588
     |---- j ----- i --------------|
2589
      \    / \      /
2590
   sorted   unsorted
2591
   */
2592
2593
0
  for (i = 1; i < (int) nmemb; i++)
2594
0
    {
2595
0
      for (j = (i - 1); j >= 0; j--)
2596
0
  if (compar (ptr + i * size, ptr + j * size) >= 0)
2597
0
    break;
2598
2599
0
      j++;
2600
2601
0
      if (i == j)
2602
0
  continue; /* i is in order.  */
2603
2604
0
      memcpy (tmp, ptr + i * size, size);
2605
0
      memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2606
0
      memcpy (ptr + j * size, tmp, size);
2607
0
    }
2608
0
}
2609
2610
/* Sort relocation by r_offset.
2611
2612
   We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2613
   algorithm.  Relocations at the same r_offset must keep their order.
2614
   For example, RELAX_ENTRY must be the very first relocation entry.
2615
2616
   Currently, this function implements insertion-sort.
2617
2618
   FIXME: If we already sort them in assembler, why bother sort them
2619
    here again?  */
2620
2621
static int
2622
compar_reloc (const void *lhs, const void *rhs)
2623
0
{
2624
0
  const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2625
0
  const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2626
2627
0
  if (l->r_offset > r->r_offset)
2628
0
    return 1;
2629
0
  else if (l->r_offset == r->r_offset)
2630
0
    return 0;
2631
0
  else
2632
0
    return -1;
2633
0
}
2634
2635
/* Functions listed below are only used for old relocs.
2636
     nds32_elf_9_pcrel_reloc
2637
     nds32_elf_do_9_pcrel_reloc
2638
     nds32_elf_hi20_reloc
2639
     nds32_elf_relocate_hi20
2640
     nds32_elf_lo12_reloc
2641
     nds32_elf_sda15_reloc
2642
     nds32_elf_generic_reloc.  */
2643
2644
/* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc.  */
2645
2646
static bfd_reloc_status_type
2647
nds32_elf_9_pcrel_reloc (bfd *       abfd,
2648
       arelent *   reloc_entry,
2649
       asymbol *   symbol,
2650
       void *      data,
2651
       asection *  input_section,
2652
       bfd *       output_bfd,
2653
       char **     error_message ATTRIBUTE_UNUSED)
2654
3
{
2655
  /* This part is from bfd_elf_generic_reloc.  */
2656
3
  if (output_bfd != (bfd *) NULL
2657
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
2658
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2659
0
    {
2660
0
      reloc_entry->address += input_section->output_offset;
2661
0
      return bfd_reloc_ok;
2662
0
    }
2663
2664
3
  if (output_bfd != NULL)
2665
0
    {
2666
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
2667
0
      return bfd_reloc_continue;
2668
0
    }
2669
2670
3
  return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2671
3
             input_section,
2672
3
             data, reloc_entry->address,
2673
3
             symbol->section,
2674
3
             (symbol->value
2675
3
              + symbol->section->output_section->vma
2676
3
              + symbol->section->output_offset),
2677
3
             reloc_entry->addend);
2678
3
}
2679
2680
/* Utility to actually perform an R_NDS32_9_PCREL reloc.  */
2681
0
#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2682
2683
static bfd_reloc_status_type
2684
nds32_elf_do_9_pcrel_reloc (bfd *               abfd,
2685
          reloc_howto_type *  howto,
2686
          asection *          input_section,
2687
          bfd_byte *          data,
2688
          bfd_vma             offset,
2689
          asection *          symbol_section ATTRIBUTE_UNUSED,
2690
          bfd_vma             symbol_value,
2691
          bfd_vma             addend)
2692
3
{
2693
3
  bfd_vma relocation;
2694
3
  unsigned short x;
2695
3
  bfd_reloc_status_type status;
2696
2697
  /* Sanity check the address (offset in section).  */
2698
3
  bfd_vma octet = offset * OCTETS_PER_BYTE (abfd, input_section);
2699
3
  if (!bfd_reloc_offset_in_range (howto, abfd, input_section, octet))
2700
1
    return bfd_reloc_outofrange;
2701
2702
2
  relocation = symbol_value + addend;
2703
  /* Make it pc relative.  */
2704
2
  relocation -= (input_section->output_section->vma
2705
2
     + input_section->output_offset);
2706
  /* These jumps mask off the lower two bits of the current address
2707
     before doing pcrel calculations.  */
2708
2
  relocation -= (offset & -(bfd_vma) 2);
2709
2710
2
  if (relocation + ACCURATE_8BIT_S1 >= 2 * ACCURATE_8BIT_S1)
2711
1
    status = bfd_reloc_overflow;
2712
1
  else
2713
1
    status = bfd_reloc_ok;
2714
2715
2
  x = bfd_getb16 (data + offset);
2716
2717
2
  relocation >>= howto->rightshift;
2718
2
  relocation <<= howto->bitpos;
2719
2
  x = (x & ~howto->dst_mask)
2720
2
      | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2721
2722
2
  bfd_putb16 ((bfd_vma) x, data + offset);
2723
2724
2
  return status;
2725
3
}
2726
2727
/* Handle the R_NDS32_HI20_[SU]LO relocs.
2728
   HI20_SLO is for the add3 and load/store with displacement instructions.
2729
   HI20 is for the or3 instruction.
2730
   For R_NDS32_HI20_SLO, the lower 12 bits are sign extended when added to
2731
   the high 20 bits so if the lower 12 bits are negative (bit 11 set) then
2732
   we must add one to the high 20 bits (which will get subtracted off when
2733
   the low 12 bits are added).
2734
   These relocs have to be done in combination with an R_NDS32_LO12 reloc
2735
   because there is a carry from the LO12 to the HI20.  Here we just save
2736
   the information we need; we do the actual relocation when we see the LO12.
2737
   This code is copied from the elf32-mips.c.  We also support an arbitrary
2738
   number of HI20 relocs to be associated with a single LO12 reloc.  The
2739
   assembler sorts the relocs to ensure each HI20 immediately precedes its
2740
   LO12.  However if there are multiple copies, the assembler may not find
2741
   the real LO12 so it picks the first one it finds.  */
2742
2743
struct nds32_hi20
2744
{
2745
  struct nds32_hi20 *next;
2746
  bfd_byte *addr;
2747
  bfd_vma addend;
2748
};
2749
2750
struct _nds32_elf_section_data
2751
{
2752
  struct bfd_elf_section_data elf;
2753
  struct nds32_hi20 *nds32_hi20_list;
2754
};
2755
2756
#define nds32_elf_section_data(sec) \
2757
9.68k
  ((struct _nds32_elf_section_data *) elf_section_data (sec))
2758
2759
static bool
2760
nds32_elf_new_section_hook (bfd *abfd, asection *sec)
2761
28.3k
{
2762
28.3k
  struct _nds32_elf_section_data *sdata;
2763
2764
28.3k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
2765
28.3k
  if (sdata == NULL)
2766
0
    return false;
2767
28.3k
  sec->used_by_bfd = sdata;
2768
2769
28.3k
  return _bfd_elf_new_section_hook (abfd, sec);
2770
28.3k
}
2771
2772
static void
2773
nds32_elf_free_hi20_list (asection *sec)
2774
9.68k
{
2775
9.68k
  struct _nds32_elf_section_data *sdata = nds32_elf_section_data (sec);
2776
9.68k
  while (sdata->nds32_hi20_list != NULL)
2777
2
    {
2778
2
      struct nds32_hi20 *hi = sdata->nds32_hi20_list;
2779
2
      sdata->nds32_hi20_list = hi->next;
2780
2
      free (hi);
2781
2
    }
2782
9.68k
}
2783
2784
static bool
2785
nds32_elf_free_cached_info (bfd *abfd)
2786
165k
{
2787
165k
  if (bfd_get_format (abfd) == bfd_object
2788
164k
      || bfd_get_format (abfd) == bfd_core)
2789
648
    {
2790
10.3k
      for (asection *sec = abfd->sections; sec; sec = sec->next)
2791
9.68k
  nds32_elf_free_hi20_list (sec);
2792
648
    }
2793
165k
  return _bfd_elf_free_cached_info (abfd);
2794
165k
}
2795
2796
static bfd_reloc_status_type
2797
nds32_elf_hi20_reloc (bfd *abfd,
2798
          arelent *reloc_entry,
2799
          asymbol *symbol,
2800
          void *data,
2801
          asection *input_section,
2802
          bfd *output_bfd,
2803
          char **error_message ATTRIBUTE_UNUSED)
2804
3
{
2805
3
  bfd_reloc_status_type ret;
2806
3
  bfd_vma relocation;
2807
3
  struct nds32_hi20 *n;
2808
3
  struct _nds32_elf_section_data *sdata;
2809
2810
  /* This part is from bfd_elf_generic_reloc.
2811
     If we're relocating, and this an external symbol, we don't want
2812
     to change anything.  */
2813
3
  if (output_bfd != (bfd *) NULL
2814
0
      && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2815
0
    {
2816
0
      reloc_entry->address += input_section->output_offset;
2817
0
      return bfd_reloc_ok;
2818
0
    }
2819
2820
  /* Sanity check the address (offset in section).  */
2821
3
  bfd_vma octet = (reloc_entry->address
2822
3
       * OCTETS_PER_BYTE (abfd, input_section));
2823
3
  if (!bfd_reloc_offset_in_range (reloc_entry->howto,
2824
3
          abfd, input_section, octet))
2825
1
    return bfd_reloc_outofrange;
2826
2827
2
  ret = bfd_reloc_ok;
2828
2
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2829
0
    ret = bfd_reloc_undefined;
2830
2831
2
  if (bfd_is_com_section (symbol->section))
2832
0
    relocation = 0;
2833
2
  else
2834
2
    relocation = symbol->value;
2835
2836
2
  relocation += symbol->section->output_section->vma;
2837
2
  relocation += symbol->section->output_offset;
2838
2
  relocation += reloc_entry->addend;
2839
2840
  /* Save the information, and let LO12 do the actual relocation.  */
2841
2
  n = bfd_malloc (sizeof (*n));
2842
2
  if (n == NULL)
2843
0
    return bfd_reloc_outofrange;
2844
2
  sdata = nds32_elf_section_data (input_section);
2845
2846
2
  n->addr = (bfd_byte *) data + reloc_entry->address;
2847
2
  n->addend = relocation;
2848
2
  n->next = sdata->nds32_hi20_list;
2849
2
  sdata->nds32_hi20_list = n;
2850
2851
2
  if (output_bfd != (bfd *) NULL)
2852
0
    reloc_entry->address += input_section->output_offset;
2853
2854
2
  return ret;
2855
2
}
2856
2857
/* Handle an NDS32 ELF HI20 reloc.  */
2858
2859
static void
2860
nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2861
       int type ATTRIBUTE_UNUSED,
2862
       Elf_Internal_Rela *relhi,
2863
       Elf_Internal_Rela *rello,
2864
       bfd_byte *contents,
2865
       bfd_vma addend)
2866
0
{
2867
0
  unsigned long insn;
2868
0
  bfd_vma addlo;
2869
2870
0
  insn = bfd_getb32 (contents + relhi->r_offset);
2871
2872
0
  addlo = bfd_getb32 (contents + rello->r_offset);
2873
0
  addlo &= 0xfff;
2874
2875
0
  addend += ((insn & 0xfffff) << 20) + addlo;
2876
2877
0
  insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2878
0
  bfd_putb32 (insn, contents + relhi->r_offset);
2879
0
}
2880
2881
/* Do an R_NDS32_LO12 relocation.  This is a straightforward 12 bit
2882
   inplace relocation; this function exists in order to do the
2883
   R_NDS32_HI20_[SU]LO relocation described above.  */
2884
2885
static bfd_reloc_status_type
2886
nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2887
          void *data, asection *input_section, bfd *output_bfd,
2888
          char **error_message)
2889
6
{
2890
6
  struct _nds32_elf_section_data *sdata;
2891
2892
  /* This part is from bfd_elf_generic_reloc.
2893
     If we're relocating, and this an external symbol, we don't want
2894
     to change anything.  */
2895
6
  if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2896
0
      && reloc_entry->addend == 0)
2897
0
    {
2898
0
      reloc_entry->address += input_section->output_offset;
2899
0
      return bfd_reloc_ok;
2900
0
    }
2901
2902
6
  bfd_vma octet = (reloc_entry->address
2903
6
       * OCTETS_PER_BYTE (input_bfd, input_section));
2904
6
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, input_bfd, input_section,
2905
6
          octet))
2906
2
    return bfd_reloc_outofrange;
2907
2908
4
  sdata = nds32_elf_section_data (input_section);
2909
4
  if (sdata->nds32_hi20_list != NULL)
2910
0
    {
2911
0
      struct nds32_hi20 *l;
2912
2913
0
      l = sdata->nds32_hi20_list;
2914
0
      while (l != NULL)
2915
0
  {
2916
0
    unsigned long insn;
2917
0
    unsigned long val;
2918
0
    unsigned long vallo;
2919
0
    struct nds32_hi20 *next;
2920
2921
    /* Do the HI20 relocation.  Note that we actually don't need
2922
       to know anything about the LO12 itself, except where to
2923
       find the low 12 bits of the addend needed by the LO12.  */
2924
0
    insn = bfd_getb32 (l->addr);
2925
0
    vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2926
0
    vallo &= 0xfff;
2927
0
    switch (reloc_entry->howto->type)
2928
0
      {
2929
0
      case R_NDS32_LO12S3:
2930
0
        vallo <<= 3;
2931
0
        break;
2932
2933
0
      case R_NDS32_LO12S2:
2934
0
        vallo <<= 2;
2935
0
        break;
2936
2937
0
      case R_NDS32_LO12S1:
2938
0
        vallo <<= 1;
2939
0
        break;
2940
2941
0
      case R_NDS32_LO12S0:
2942
0
        vallo <<= 0;
2943
0
        break;
2944
0
      }
2945
2946
0
    val = ((insn & 0xfffff) << 12) + vallo;
2947
0
    val += l->addend;
2948
2949
0
    insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2950
0
    bfd_putb32 ((bfd_vma) insn, l->addr);
2951
2952
0
    next = l->next;
2953
0
    free (l);
2954
0
    l = next;
2955
0
  }
2956
2957
0
      sdata->nds32_hi20_list = NULL;
2958
0
    }
2959
2960
  /* Now do the LO12 reloc in the usual way.
2961
     ??? It would be nice to call bfd_elf_generic_reloc here,
2962
     but we have partial_inplace set.  bfd_elf_generic_reloc will
2963
     pass the handling back to bfd_install_relocation which will install
2964
     a section relative addend which is wrong.  */
2965
4
  return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2966
4
          input_section, output_bfd, error_message);
2967
4
}
2968
2969
/* Do generic partial_inplace relocation.
2970
   This is a local replacement for bfd_elf_generic_reloc.  */
2971
2972
static bfd_reloc_status_type
2973
nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2974
       asymbol *symbol, void *data, asection *input_section,
2975
       bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2976
8
{
2977
8
  bfd_reloc_status_type ret;
2978
8
  bfd_vma relocation;
2979
8
  bfd_byte *inplace_address;
2980
2981
  /* This part is from bfd_elf_generic_reloc.
2982
     If we're relocating, and this an external symbol, we don't want
2983
     to change anything.  */
2984
8
  if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2985
0
      && reloc_entry->addend == 0)
2986
0
    {
2987
0
      reloc_entry->address += input_section->output_offset;
2988
0
      return bfd_reloc_ok;
2989
0
    }
2990
2991
  /* Now do the reloc in the usual way.
2992
     ??? It would be nice to call bfd_elf_generic_reloc here,
2993
     but we have partial_inplace set.  bfd_elf_generic_reloc will
2994
     pass the handling back to bfd_install_relocation which will install
2995
     a section relative addend which is wrong.  */
2996
2997
  /* Sanity check the address (offset in section).  */
2998
8
  bfd_vma octet = (reloc_entry->address
2999
8
       * OCTETS_PER_BYTE (input_bfd, input_section));
3000
8
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, input_bfd, input_section,
3001
8
          octet))
3002
1
    return bfd_reloc_outofrange;
3003
3004
7
  ret = bfd_reloc_ok;
3005
7
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
3006
0
    ret = bfd_reloc_undefined;
3007
3008
7
  if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
3009
0
    relocation = 0;
3010
7
  else
3011
7
    relocation = symbol->value;
3012
3013
  /* Only do this for a final link.  */
3014
7
  if (output_bfd == (bfd *) NULL)
3015
7
    {
3016
7
      relocation += symbol->section->output_section->vma;
3017
7
      relocation += symbol->section->output_offset;
3018
7
    }
3019
3020
7
  relocation += reloc_entry->addend;
3021
7
  switch (reloc_entry->howto->type)
3022
7
    {
3023
2
    case R_NDS32_LO12S3:
3024
2
      relocation >>= 3;
3025
2
      break;
3026
3027
0
    case R_NDS32_LO12S2:
3028
0
      relocation >>= 2;
3029
0
      break;
3030
3031
0
    case R_NDS32_LO12S1:
3032
0
      relocation >>= 1;
3033
0
      break;
3034
3035
2
    case R_NDS32_LO12S0:
3036
5
    default:
3037
5
      relocation >>= 0;
3038
5
      break;
3039
7
    }
3040
3041
7
  inplace_address = (bfd_byte *) data + reloc_entry->address;
3042
3043
7
#define DOIT(x)           \
3044
7
  x = ((x & ~reloc_entry->howto->dst_mask) |    \
3045
7
  (((x & reloc_entry->howto->src_mask) +  relocation) & \
3046
7
  reloc_entry->howto->dst_mask))
3047
3048
7
  switch (bfd_get_reloc_size (reloc_entry->howto))
3049
7
    {
3050
2
    case 2:
3051
2
      {
3052
2
  short x = bfd_getb16 (inplace_address);
3053
3054
2
  DOIT (x);
3055
2
  bfd_putb16 ((bfd_vma) x, inplace_address);
3056
2
      }
3057
2
      break;
3058
5
    case 4:
3059
5
      {
3060
5
  unsigned long x = bfd_getb32 (inplace_address);
3061
3062
5
  DOIT (x);
3063
5
  bfd_putb32 ((bfd_vma) x, inplace_address);
3064
5
      }
3065
5
      break;
3066
0
    default:
3067
0
      BFD_ASSERT (0);
3068
7
    }
3069
3070
7
  if (output_bfd != (bfd *) NULL)
3071
0
    reloc_entry->address += input_section->output_offset;
3072
3073
7
  return ret;
3074
7
}
3075
3076
/* Handle the R_NDS32_SDA15 reloc.
3077
   This reloc is used to compute the address of objects in the small data area
3078
   and to perform loads and stores from that area.
3079
   The lower 15 bits are sign extended and added to the register specified
3080
   in the instruction, which is assumed to point to _SDA_BASE_.
3081
3082
   Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
3083
   the access size, this must be taken care of.  */
3084
3085
static bfd_reloc_status_type
3086
nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3087
           asymbol *symbol, void *data ATTRIBUTE_UNUSED,
3088
           asection *input_section, bfd *output_bfd,
3089
           char **error_message ATTRIBUTE_UNUSED)
3090
0
{
3091
  /* This part is from bfd_elf_generic_reloc.  */
3092
0
  if (output_bfd != (bfd *) NULL
3093
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3094
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3095
0
    {
3096
0
      reloc_entry->address += input_section->output_offset;
3097
0
      return bfd_reloc_ok;
3098
0
    }
3099
3100
0
  if (output_bfd != NULL)
3101
0
    {
3102
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
3103
0
      return bfd_reloc_continue;
3104
0
    }
3105
3106
  /* FIXME: not sure what to do here yet.  But then again, the linker
3107
     may never call us.  */
3108
0
  abort ();
3109
0
}
3110
3111
/* nds32_elf_ignore_reloc is the special function for
3112
   relocation types which don't need to be relocated
3113
   like relaxation relocation types.
3114
   This function simply return bfd_reloc_ok when it is
3115
   invoked.  */
3116
3117
static bfd_reloc_status_type
3118
nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3119
      asymbol *symbol ATTRIBUTE_UNUSED,
3120
      void *data ATTRIBUTE_UNUSED, asection *input_section,
3121
      bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3122
7
{
3123
7
  if (output_bfd != NULL)
3124
0
    reloc_entry->address += input_section->output_offset;
3125
3126
7
  return bfd_reloc_ok;
3127
7
}
3128

3129
3130
/* Map BFD reloc types to NDS32 ELF reloc types.  */
3131
3132
struct nds32_reloc_map_entry
3133
{
3134
  bfd_reloc_code_real_type bfd_reloc_val;
3135
  unsigned char elf_reloc_val;
3136
};
3137
3138
static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3139
{
3140
  {BFD_RELOC_NONE, R_NDS32_NONE},
3141
  {BFD_RELOC_16, R_NDS32_16_RELA},
3142
  {BFD_RELOC_32, R_NDS32_32_RELA},
3143
  {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3144
  {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3145
3146
  {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
3147
  {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3148
  {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3149
  {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3150
  {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3151
  {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
3152
  {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3153
  {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3154
  {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3155
  {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3156
  {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3157
  {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3158
  {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3159
  {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3160
  {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3161
  {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
3162
  {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3163
  {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3164
  {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3165
  {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
3166
  {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3167
  {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3168
  {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3169
  {BFD_RELOC_COPY, R_NDS32_COPY},
3170
  {BFD_RELOC_GLOB_DAT, R_NDS32_GLOB_DAT},
3171
  {BFD_RELOC_JMP_SLOT, R_NDS32_JMP_SLOT},
3172
  {BFD_RELOC_RELATIVE, R_NDS32_RELATIVE},
3173
  {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
3174
  {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3175
  {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
3176
  {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3177
  {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3178
  {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
3179
  {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3180
  {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
3181
  {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3182
  {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3183
  {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3184
  {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3185
  {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3186
  {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3187
  {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3188
  {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3189
  {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3190
  {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3191
  {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3192
  {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3193
  {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
3194
  {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3195
  {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3196
  {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3197
  {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3198
  {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3199
  {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3200
  {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
3201
  {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3202
  {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3203
  {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3204
  {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
3205
  {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3206
  {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3207
  {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3208
  {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
3209
  {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3210
  {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3211
  {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3212
  {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
3213
  {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3214
  {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3215
  {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3216
  {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3217
  {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3218
  {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3219
  {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
3220
  {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3221
  {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
3222
  {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3223
  {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3224
  {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3225
  {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3226
  {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3227
  {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3228
  {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3229
  {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
3230
  {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3231
  {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
3232
  {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
3233
  {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3234
  {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3235
  {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3236
  {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3237
  {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
3238
  {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3239
  {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3240
  {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3241
  {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
3242
  {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
3243
  {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3244
  {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3245
  {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3246
  {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3247
  {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
3248
  /* Not sure.  */
3249
  {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3250
  /* Missing: BFD_RELOC_NDS32_GOTTPOFF.  */
3251
  {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3252
  {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
3253
  {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3254
  {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3255
  {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3256
  {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
3257
  {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3258
  {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3259
  {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3260
  {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3261
  {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3262
  {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3263
  {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3264
  {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3265
  {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3266
  {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3267
  {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3268
  {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3269
  {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3270
  {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3271
  {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3272
  {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3273
  {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3274
  {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3275
  {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3276
  {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3277
  {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
3278
};
3279
3280
/* Patch tag.  */
3281
3282
static reloc_howto_type *
3283
bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3284
         const char *r_name)
3285
0
{
3286
0
  unsigned int i;
3287
3288
0
  for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3289
0
    if (nds32_elf_howto_table[i].name != NULL
3290
0
  && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3291
0
      return &nds32_elf_howto_table[i];
3292
3293
0
  for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3294
0
    if (nds32_elf_relax_howto_table[i].name != NULL
3295
0
  && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3296
0
      return &nds32_elf_relax_howto_table[i];
3297
3298
0
  return NULL;
3299
0
}
3300
3301
static reloc_howto_type *
3302
bfd_elf32_bfd_reloc_type_table_lookup (unsigned int code)
3303
1.63k
{
3304
1.63k
  if (code < R_NDS32_RELAX_ENTRY)
3305
1.53k
    {
3306
1.53k
      if (code < ARRAY_SIZE (nds32_elf_howto_table))
3307
1.52k
  return &nds32_elf_howto_table[code];
3308
1.53k
    }
3309
99
  else
3310
99
    {
3311
99
      if (code - R_NDS32_RELAX_ENTRY < ARRAY_SIZE (nds32_elf_relax_howto_table))
3312
90
  return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3313
99
    }
3314
16
  return NULL;
3315
1.63k
}
3316
3317
static reloc_howto_type *
3318
bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3319
         bfd_reloc_code_real_type code)
3320
0
{
3321
0
  unsigned int i;
3322
3323
0
  for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3324
0
    {
3325
0
      if (nds32_reloc_map[i].bfd_reloc_val == code)
3326
0
  return bfd_elf32_bfd_reloc_type_table_lookup
3327
0
    (nds32_reloc_map[i].elf_reloc_val);
3328
0
    }
3329
3330
0
  return NULL;
3331
0
}
3332
3333
/* Set the howto pointer for an NDS32 ELF reloc.  */
3334
3335
static bool
3336
nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
3337
       Elf_Internal_Rela *dst)
3338
100
{
3339
100
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3340
3341
100
  cache_ptr->howto = NULL;
3342
100
  if (r_type <= R_NDS32_GNU_VTENTRY)
3343
86
    cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3344
100
  if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3345
14
    {
3346
      /* xgettext:c-format */
3347
14
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3348
14
        abfd, r_type);
3349
14
      bfd_set_error (bfd_error_bad_value);
3350
14
      return false;
3351
14
    }
3352
86
  return true;
3353
100
}
3354
3355
static bool
3356
nds32_info_to_howto (bfd *abfd, arelent *cache_ptr,
3357
         Elf_Internal_Rela *dst)
3358
1.56k
{
3359
1.56k
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3360
3361
1.56k
  cache_ptr->howto = NULL;
3362
1.56k
  if (r_type == R_NDS32_NONE
3363
369
      || r_type > R_NDS32_GNU_VTENTRY)
3364
1.54k
    cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3365
1.56k
  if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3366
39
    {
3367
      /* xgettext:c-format */
3368
39
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3369
39
        abfd, r_type);
3370
39
      bfd_set_error (bfd_error_bad_value);
3371
39
      return false;
3372
39
    }
3373
1.52k
  return true;
3374
1.56k
}
3375
3376
/* Support for core dump NOTE sections.
3377
   Reference to include/linux/elfcore.h in Linux.  */
3378
3379
static bool
3380
nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3381
0
{
3382
0
  int offset;
3383
0
  size_t size;
3384
3385
0
  switch (note->descsz)
3386
0
    {
3387
0
    case 0x114:
3388
      /* Linux/NDS32 32-bit, ABI1.  */
3389
3390
      /* pr_cursig */
3391
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3392
3393
      /* pr_pid */
3394
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3395
3396
      /* pr_reg */
3397
0
      offset = 72;
3398
0
      size = 200;
3399
0
      break;
3400
3401
0
    case 0xfc:
3402
      /* Linux/NDS32 32-bit.  */
3403
3404
      /* pr_cursig */
3405
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3406
3407
      /* pr_pid */
3408
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3409
3410
      /* pr_reg */
3411
0
      offset = 72;
3412
0
      size = 176;
3413
0
      break;
3414
3415
0
    default:
3416
0
      return false;
3417
0
    }
3418
3419
  /* Make a ".reg" section.  */
3420
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3421
0
            size, note->descpos + offset);
3422
0
}
3423
3424
static bool
3425
nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3426
0
{
3427
0
  switch (note->descsz)
3428
0
    {
3429
0
    case 124:
3430
      /* Linux/NDS32.  */
3431
3432
      /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform.  */
3433
0
      elf_tdata (abfd)->core->program =
3434
0
  _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3435
0
      elf_tdata (abfd)->core->command =
3436
0
  _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3437
0
      break;
3438
3439
0
    default:
3440
0
      return false;
3441
0
    }
3442
3443
  /* Note that for some reason, a spurious space is tacked
3444
     onto the end of the args in some (at least one anyway)
3445
     implementations, so strip it off if it exists.  */
3446
0
  {
3447
0
    char *command = elf_tdata (abfd)->core->command;
3448
0
    int n = strlen (command);
3449
3450
0
    if (0 < n && command[n - 1] == ' ')
3451
0
      command[n - 1] = '\0';
3452
0
  }
3453
3454
0
  return true;
3455
0
}
3456
3457
/* Hook called by the linker routine which adds symbols from an object
3458
   file.  We must handle the special NDS32 section numbers here.
3459
   We also keep watching for whether we need to create the sdata special
3460
   linker sections.  */
3461
3462
static bool
3463
nds32_elf_add_symbol_hook (bfd *abfd,
3464
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
3465
         Elf_Internal_Sym *sym,
3466
         const char **namep ATTRIBUTE_UNUSED,
3467
         flagword *flagsp ATTRIBUTE_UNUSED,
3468
         asection **secp, bfd_vma *valp)
3469
0
{
3470
0
  switch (sym->st_shndx)
3471
0
    {
3472
0
    case SHN_COMMON:
3473
      /* Common symbols less than the GP size are automatically
3474
   treated as SHN_MIPS_SCOMMON symbols.  */
3475
0
      if (sym->st_size > elf_gp_size (abfd)
3476
0
    || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3477
0
  break;
3478
3479
      /* st_value is the alignment constraint.
3480
   That might be its actual size if it is an array or structure.  */
3481
0
      switch (sym->st_value)
3482
0
  {
3483
0
  case 1:
3484
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3485
0
    break;
3486
0
  case 2:
3487
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3488
0
    break;
3489
0
  case 4:
3490
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3491
0
    break;
3492
0
  case 8:
3493
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3494
0
    break;
3495
0
  default:
3496
0
    return true;
3497
0
  }
3498
3499
0
      (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
3500
0
      *valp = sym->st_size;
3501
0
      break;
3502
0
    }
3503
3504
0
  return true;
3505
0
}
3506
3507
/* This function can figure out the best location for a base register to access
3508
   data relative to this base register
3509
   INPUT:
3510
   sda_d0: size of first DOUBLE WORD data section
3511
   sda_w0: size of first WORD data section
3512
   sda_h0: size of first HALF WORD data section
3513
   sda_b : size of BYTE data section
3514
   sda_hi: size of second HALF WORD data section
3515
   sda_w1: size of second WORD data section
3516
   sda_d1: size of second DOUBLE WORD data section
3517
   OUTPUT:
3518
   offset (always positive) from the beginning of sda_d0 if OK
3519
   a negative error value if fail
3520
   NOTE:
3521
   these 7 sections have to be located back to back if exist
3522
   a pass in 0 value for non-existing section   */
3523
3524
/* Due to the interpretation of simm15 field of load/store depending on
3525
   data accessing size, the organization of base register relative data shall
3526
   like the following figure
3527
   -------------------------------------------
3528
   |  DOUBLE WORD sized data (range +/- 128K)
3529
   -------------------------------------------
3530
   |  WORD sized data (range +/- 64K)
3531
   -------------------------------------------
3532
   |  HALF WORD sized data (range +/- 32K)
3533
   -------------------------------------------
3534
   |  BYTE sized data (range +/- 16K)
3535
   -------------------------------------------
3536
   |  HALF WORD sized data (range +/- 32K)
3537
   -------------------------------------------
3538
   |  WORD sized data (range +/- 64K)
3539
   -------------------------------------------
3540
   |  DOUBLE WORD sized data (range +/- 128K)
3541
   -------------------------------------------
3542
   Its base register shall be set to access these data freely.  */
3543
3544
/* We have to figure out the SDA_BASE value, so that we can adjust the
3545
   symbol value correctly.  We look up the symbol _SDA_BASE_ in the output
3546
   BFD.  If we can't find it, we're stuck.  We cache it in the ELF
3547
   target data.  We don't need to adjust the symbol value for an
3548
   external symbol if we are producing relocatable output.  */
3549
3550
static asection *sda_rela_sec = NULL;
3551
3552
#define SDA_SECTION_NUM 10
3553
3554
static bfd_reloc_status_type
3555
nds32_elf_final_sda_base (bfd *output_bfd,
3556
        struct bfd_link_info *info,
3557
        bfd_vma *psb,
3558
        bool add_symbol)
3559
0
{
3560
0
  int relax_fp_as_gp;
3561
0
  struct elf_nds32_link_hash_table *table;
3562
0
  struct bfd_link_hash_entry *h, *h2;
3563
0
  long unsigned int total = 0;
3564
0
  asection *first = NULL, *final = NULL, *temp;
3565
0
  bfd_vma sda_base = 0;
3566
3567
0
  h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true);
3568
0
  if (!h || (h->type != bfd_link_hash_defined
3569
0
       && h->type != bfd_link_hash_defweak))
3570
0
    {
3571
      /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3572
   4 byte-aligned.  Therefore, it has to set the first section ".data"
3573
   4 byte-aligned.  */
3574
0
      static const char sec_name[SDA_SECTION_NUM][10] =
3575
0
  {
3576
0
    ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3577
0
    ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3578
0
  };
3579
0
      size_t i = 0;
3580
3581
0
      if (output_bfd->sections == NULL)
3582
0
  {
3583
0
    *psb = elf_gp (output_bfd);
3584
0
    return bfd_reloc_ok;
3585
0
  }
3586
3587
      /* Get the first and final section.  */
3588
0
      while (i < ARRAY_SIZE (sec_name))
3589
0
  {
3590
0
    temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3591
0
    if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3592
0
      first = temp;
3593
0
    if (temp && (temp->size != 0 || temp->rawsize != 0))
3594
0
      final = temp;
3595
3596
    /* Summarize the sections in order to check if joining .bss.  */
3597
0
    if (temp && temp->size != 0)
3598
0
      total += temp->size;
3599
0
    else if (temp && temp->rawsize != 0)
3600
0
      total += temp->rawsize;
3601
3602
0
    i++;
3603
0
  }
3604
3605
      /* Check .bss size.  */
3606
0
      temp = bfd_get_section_by_name (output_bfd, ".bss");
3607
0
      if (temp)
3608
0
  {
3609
0
    if (temp->size != 0)
3610
0
      total += temp->size;
3611
0
    else if (temp->rawsize != 0)
3612
0
      total += temp->rawsize;
3613
3614
0
    if (total < 0x80000)
3615
0
      {
3616
0
        if (!first && (temp->size != 0 || temp->rawsize != 0))
3617
0
    first = temp;
3618
0
        if ((temp->size != 0 || temp->rawsize != 0))
3619
0
    final = temp;
3620
0
      }
3621
0
  }
3622
3623
0
      if (first && final)
3624
0
  {
3625
    /* The middle of data region.  */
3626
0
    sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3627
3628
    /* Find the section sda_base located.  */
3629
0
    i = 0;
3630
0
    while (i < ARRAY_SIZE (sec_name))
3631
0
      {
3632
0
        final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3633
0
        if (final && (final->size != 0 || final->rawsize != 0)
3634
0
      && sda_base >= final->vma)
3635
0
    {
3636
0
      first = final;
3637
0
      i++;
3638
0
    }
3639
0
        else
3640
0
    break;
3641
0
      }
3642
0
  }
3643
0
      else
3644
0
  {
3645
    /* If there is not any default data section in output bfd, try to find
3646
       the first data section.  If no data section be found, just simplily
3647
       choose the first output section.  */
3648
0
    temp = output_bfd->sections;
3649
0
    while (temp)
3650
0
      {
3651
0
        if (temp->flags & SEC_ALLOC
3652
0
      && (((temp->flags & SEC_DATA)
3653
0
           && ((temp->flags & SEC_READONLY) == 0))
3654
0
          || (temp->flags & SEC_LOAD) == 0)
3655
0
      && (temp->size != 0 || temp->rawsize != 0))
3656
0
    {
3657
0
      if (!first)
3658
0
        first = temp;
3659
0
      final = temp;
3660
0
    }
3661
0
        temp = temp->next;
3662
0
      }
3663
3664
    /* There is no data or bss section.  */
3665
0
    if (!first || (first->size == 0 && first->rawsize == 0))
3666
0
      {
3667
0
        first = output_bfd->sections;
3668
0
        while (first && first->size == 0 && first->rawsize == 0)
3669
0
    first = first->next;
3670
0
      }
3671
3672
    /* There is no concrete section.  */
3673
0
    if (!first)
3674
0
      {
3675
0
        *psb = elf_gp (output_bfd);
3676
0
        return bfd_reloc_ok;
3677
0
      }
3678
3679
0
    if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3680
0
      sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3681
0
    else
3682
0
      sda_base = first->vma + 0x2000;
3683
0
  }
3684
3685
0
      sda_base -= first->vma;
3686
0
      sda_base = sda_base & (~7);
3687
3688
0
      if (!_bfd_generic_link_add_one_symbol
3689
0
    (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3690
0
     (bfd_vma) sda_base, (const char *) NULL, false,
3691
0
     get_elf_backend_data (output_bfd)->collect, &h))
3692
0
  return false;
3693
3694
0
      sda_rela_sec = first;
3695
0
    }
3696
3697
  /* Set _FP_BASE_ to _SDA_BASE_.  */
3698
0
  table = nds32_elf_hash_table (info);
3699
0
  relax_fp_as_gp = table->relax_fp_as_gp;
3700
0
  h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, false, false, false);
3701
  /* _SDA_BASE_ is difined in linker script.  */
3702
0
  if (!first)
3703
0
    {
3704
0
      first = h->u.def.section;
3705
0
      sda_base = h->u.def.value;
3706
0
    }
3707
3708
0
  if (relax_fp_as_gp && h2
3709
0
      && (h2->type == bfd_link_hash_undefweak
3710
0
    || h2->type == bfd_link_hash_undefined))
3711
0
    {
3712
      /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3713
   And set FP equal to SDA_BASE to do relaxation for
3714
   la $fp, _FP_BASE_.  */
3715
0
      if (!_bfd_generic_link_add_one_symbol
3716
0
    (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3717
0
     first, sda_base, (const char *) NULL,
3718
0
     false, get_elf_backend_data (output_bfd)->collect, &h2))
3719
0
  return false;
3720
0
    }
3721
3722
0
  if (add_symbol)
3723
0
    {
3724
0
      if (h)
3725
0
  {
3726
    /* Now set gp.  */
3727
0
    elf_gp (output_bfd) = (h->u.def.value
3728
0
         + h->u.def.section->output_section->vma
3729
0
         + h->u.def.section->output_offset);
3730
0
  }
3731
0
      else
3732
0
  {
3733
0
    _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
3734
0
    return bfd_reloc_dangerous;
3735
0
  }
3736
0
    }
3737
3738
0
  *psb = h->u.def.value
3739
0
    + h->u.def.section->output_section->vma
3740
0
    + h->u.def.section->output_offset;
3741
0
  return bfd_reloc_ok;
3742
0
}
3743

3744
3745
/* Return size of a PLT entry.  */
3746
#define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3747
3748
/* Create an entry in an nds32 ELF linker hash table.  */
3749
3750
static struct bfd_hash_entry *
3751
nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3752
           struct bfd_hash_table *table,
3753
           const char *string)
3754
0
{
3755
0
  struct elf_nds32_link_hash_entry *ret;
3756
3757
0
  ret = (struct elf_nds32_link_hash_entry *) entry;
3758
3759
  /* Allocate the structure if it has not already been allocated by a
3760
     subclass.  */
3761
0
  if (ret == NULL)
3762
0
    ret = (struct elf_nds32_link_hash_entry *)
3763
0
       bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3764
3765
0
  if (ret == NULL)
3766
0
    return (struct bfd_hash_entry *) ret;
3767
3768
  /* Call the allocation method of the superclass.  */
3769
0
  ret = (struct elf_nds32_link_hash_entry *)
3770
0
    _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3771
3772
0
  if (ret != NULL)
3773
0
    {
3774
0
      struct elf_nds32_link_hash_entry *eh;
3775
3776
0
      eh = (struct elf_nds32_link_hash_entry *) ret;
3777
0
      eh->tls_type = GOT_UNKNOWN;
3778
0
      eh->offset_to_gp = 0;
3779
0
    }
3780
3781
0
  return (struct bfd_hash_entry *) ret;
3782
0
}
3783
3784
/* Create an nds32 ELF linker hash table.  */
3785
3786
static struct bfd_link_hash_table *
3787
nds32_elf_link_hash_table_create (bfd *abfd)
3788
0
{
3789
0
  struct elf_nds32_link_hash_table *ret;
3790
3791
0
  size_t amt = sizeof (struct elf_nds32_link_hash_table);
3792
3793
0
  ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3794
0
  if (ret == NULL)
3795
0
    return NULL;
3796
3797
  /* Patch tag.  */
3798
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3799
0
              nds32_elf_link_hash_newfunc,
3800
0
              sizeof (struct elf_nds32_link_hash_entry)))
3801
0
    {
3802
0
      free (ret);
3803
0
      return NULL;
3804
0
    }
3805
3806
0
  ret->sym_ld_script = NULL;
3807
3808
0
  return &ret->root.root;
3809
0
}
3810
3811
/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3812
   shortcuts to them in our hash table.  */
3813
3814
static bool
3815
create_got_section (bfd *dynobj, struct bfd_link_info *info)
3816
0
{
3817
0
  struct elf_link_hash_table *ehtab;
3818
3819
0
  if (!_bfd_elf_create_got_section (dynobj, info))
3820
0
    return false;
3821
3822
0
  ehtab = elf_hash_table (info);
3823
0
  ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3824
0
  ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3825
0
  if (!ehtab->sgot || !ehtab->sgotplt)
3826
0
    abort ();
3827
3828
  /* _bfd_elf_create_got_section will create it for us.  */
3829
0
  ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3830
0
  if (ehtab->srelgot == NULL
3831
0
      || !bfd_set_section_flags (ehtab->srelgot,
3832
0
         (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3833
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED
3834
0
          | SEC_READONLY))
3835
0
      || !bfd_set_section_alignment (ehtab->srelgot, 2))
3836
0
    return false;
3837
3838
0
  return true;
3839
0
}
3840
3841
/* Create dynamic sections when linking against a dynamic object.  */
3842
3843
static bool
3844
nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3845
0
{
3846
0
  struct elf_link_hash_table *ehtab;
3847
0
  struct elf_nds32_link_hash_table *htab;
3848
0
  flagword flags, pltflags;
3849
0
  register asection *s;
3850
0
  elf_backend_data *bed;
3851
0
  int ptralign = 2;   /* 32-bit  */
3852
0
  const char *secname;
3853
0
  char *relname;
3854
0
  flagword secflags;
3855
0
  asection *sec;
3856
3857
0
  bed = get_elf_backend_data (abfd);
3858
0
  ehtab = elf_hash_table (info);
3859
0
  htab = nds32_elf_hash_table (info);
3860
3861
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3862
     .rel[a].bss sections.  */
3863
3864
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3865
0
     | SEC_LINKER_CREATED);
3866
3867
0
  pltflags = flags;
3868
0
  pltflags |= SEC_CODE;
3869
0
  if (bed->plt_not_loaded)
3870
0
    pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3871
0
  if (bed->plt_readonly)
3872
0
    pltflags |= SEC_READONLY;
3873
3874
0
  s = bfd_make_section (abfd, ".plt");
3875
0
  ehtab->splt = s;
3876
0
  if (s == NULL
3877
0
      || !bfd_set_section_flags (s, pltflags)
3878
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
3879
0
    return false;
3880
3881
0
  if (bed->want_plt_sym)
3882
0
    {
3883
      /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3884
   .plt section.  */
3885
0
      struct bfd_link_hash_entry *bh = NULL;
3886
0
      struct elf_link_hash_entry *h;
3887
3888
0
      if (!(_bfd_generic_link_add_one_symbol
3889
0
      (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3890
0
       (bfd_vma) 0, (const char *) NULL, false,
3891
0
       get_elf_backend_data (abfd)->collect, &bh)))
3892
0
  return false;
3893
3894
0
      h = (struct elf_link_hash_entry *) bh;
3895
0
      h->def_regular = 1;
3896
0
      h->type = STT_OBJECT;
3897
3898
0
      if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3899
0
  return false;
3900
0
    }
3901
3902
0
  s = bfd_make_section (abfd,
3903
0
      bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3904
0
  ehtab->srelplt = s;
3905
0
  if (s == NULL
3906
0
      || !bfd_set_section_flags (s, flags | SEC_READONLY)
3907
0
      || !bfd_set_section_alignment (s, ptralign))
3908
0
    return false;
3909
3910
0
  if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3911
0
    return false;
3912
3913
0
  for (sec = abfd->sections; sec; sec = sec->next)
3914
0
    {
3915
0
      secflags = bfd_section_flags (sec);
3916
0
      if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3917
0
    || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3918
0
  continue;
3919
0
      secname = bfd_section_name (sec);
3920
0
      relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3921
0
      strcpy (relname, ".rela");
3922
0
      strcat (relname, secname);
3923
0
      if (bfd_get_section_by_name (abfd, secname))
3924
0
  continue;
3925
0
      s = bfd_make_section (abfd, relname);
3926
0
      if (s == NULL
3927
0
    || !bfd_set_section_flags (s, flags | SEC_READONLY)
3928
0
    || !bfd_set_section_alignment (s, ptralign))
3929
0
  return false;
3930
0
    }
3931
3932
0
  if (bed->want_dynbss)
3933
0
    {
3934
      /* The .dynbss section is a place to put symbols which are defined
3935
   by dynamic objects, are referenced by regular objects, and are
3936
   not functions.  We must allocate space for them in the process
3937
   image and use a R_*_COPY reloc to tell the dynamic linker to
3938
   initialize them at run time.  The linker script puts the .dynbss
3939
   section into the .bss section of the final image.  */
3940
0
      s = bfd_make_section (abfd, ".dynbss");
3941
0
      htab->root.sdynbss = s;
3942
0
      if (s == NULL
3943
0
    || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
3944
0
  return false;
3945
      /* The .rel[a].bss section holds copy relocs.  This section is not
3946
   normally needed.  We need to create it here, though, so that the
3947
   linker will map it to an output section.  We can't just create it
3948
   only if we need it, because we will not know whether we need it
3949
   until we have seen all the input files, and the first time the
3950
   main linker code calls BFD after examining all the input files
3951
   (size_dynamic_sections) the input sections have already been
3952
   mapped to the output sections.  If the section turns out not to
3953
   be needed, we can discard it later.  We will never need this
3954
   section when generating a shared object, since they do not use
3955
   copy relocs.  */
3956
0
      if (!bfd_link_pic (info))
3957
0
  {
3958
0
    s = bfd_make_section (abfd, (bed->default_use_rela_p
3959
0
               ? ".rela.bss" : ".rel.bss"));
3960
0
    htab->root.srelbss = s;
3961
0
    if (s == NULL
3962
0
        || !bfd_set_section_flags (s, flags | SEC_READONLY)
3963
0
        || !bfd_set_section_alignment (s, ptralign))
3964
0
      return false;
3965
0
  }
3966
0
    }
3967
3968
0
  return true;
3969
0
}
3970
3971
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
3972
static void
3973
nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3974
        struct elf_link_hash_entry *dir,
3975
        struct elf_link_hash_entry *ind)
3976
0
{
3977
0
  struct elf_nds32_link_hash_entry *edir, *eind;
3978
3979
0
  edir = (struct elf_nds32_link_hash_entry *) dir;
3980
0
  eind = (struct elf_nds32_link_hash_entry *) ind;
3981
3982
0
  if (ind->root.type == bfd_link_hash_indirect)
3983
0
    {
3984
0
      if (dir->got.refcount <= 0)
3985
0
  {
3986
0
    edir->tls_type = eind->tls_type;
3987
0
    eind->tls_type = GOT_UNKNOWN;
3988
0
  }
3989
0
    }
3990
3991
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3992
0
}
3993

3994
/* Adjust a symbol defined by a dynamic object and referenced by a
3995
   regular object.  The current definition is in some section of the
3996
   dynamic object, but we're not including those sections.  We have to
3997
   change the definition to something the rest of the link can
3998
   understand.  */
3999
4000
static bool
4001
nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
4002
         struct elf_link_hash_entry *h)
4003
0
{
4004
0
  struct elf_nds32_link_hash_table *htab;
4005
0
  bfd *dynobj;
4006
0
  asection *s;
4007
0
  unsigned int power_of_two;
4008
4009
0
  dynobj = elf_hash_table (info)->dynobj;
4010
4011
  /* Make sure we know what is going on here.  */
4012
0
  BFD_ASSERT (dynobj != NULL
4013
0
        && (h->needs_plt
4014
0
      || h->is_weakalias
4015
0
      || (h->def_dynamic && h->ref_regular && !h->def_regular)));
4016
4017
4018
  /* If this is a function, put it in the procedure linkage table.  We
4019
     will fill in the contents of the procedure linkage table later,
4020
     when we know the address of the .got section.  */
4021
0
  if (h->type == STT_FUNC || h->needs_plt)
4022
0
    {
4023
0
      if (!bfd_link_pic (info)
4024
0
    && !h->def_dynamic
4025
0
    && !h->ref_dynamic
4026
0
    && h->root.type != bfd_link_hash_undefweak
4027
0
    && h->root.type != bfd_link_hash_undefined)
4028
0
  {
4029
    /* This case can occur if we saw a PLT reloc in an input
4030
       file, but the symbol was never referred to by a dynamic
4031
       object.  In such a case, we don't actually need to build
4032
       a procedure linkage table, and we can just do a PCREL
4033
       reloc instead.  */
4034
0
    h->plt.offset = (bfd_vma) - 1;
4035
0
    h->needs_plt = 0;
4036
0
  }
4037
4038
0
      return true;
4039
0
    }
4040
0
  else
4041
0
    h->plt.offset = (bfd_vma) - 1;
4042
4043
  /* If this is a weak symbol, and there is a real definition, the
4044
     processor independent code will have arranged for us to see the
4045
     real definition first, and we can just use the same value.  */
4046
0
  if (h->is_weakalias)
4047
0
    {
4048
0
      struct elf_link_hash_entry *def = weakdef (h);
4049
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4050
0
      h->root.u.def.section = def->root.u.def.section;
4051
0
      h->root.u.def.value = def->root.u.def.value;
4052
0
      return true;
4053
0
    }
4054
4055
  /* This is a reference to a symbol defined by a dynamic object which
4056
     is not a function.  */
4057
4058
  /* If we are creating a shared library, we must presume that the
4059
     only references to the symbol are via the global offset table.
4060
     For such cases we need not do anything here; the relocations will
4061
     be handled correctly by relocate_section.  */
4062
0
  if (bfd_link_pic (info))
4063
0
    return true;
4064
4065
  /* If there are no references to this symbol that do not use the
4066
     GOT, we don't need to generate a copy reloc.  */
4067
0
  if (!h->non_got_ref)
4068
0
    return true;
4069
4070
  /* If -z nocopyreloc was given, we won't generate them either.  */
4071
0
  if (0 && info->nocopyreloc)
4072
0
    {
4073
0
      h->non_got_ref = 0;
4074
0
      return true;
4075
0
    }
4076
4077
  /* If we don't find any dynamic relocs in read-only sections, then
4078
     we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
4079
0
  if (!_bfd_elf_readonly_dynrelocs (h))
4080
0
    {
4081
0
      h->non_got_ref = 0;
4082
0
      return true;
4083
0
    }
4084
4085
  /* We must allocate the symbol in our .dynbss section, which will
4086
     become part of the .bss section of the executable.  There will be
4087
     an entry for this symbol in the .dynsym section.  The dynamic
4088
     object will contain position independent code, so all references
4089
     from the dynamic object to this symbol will go through the global
4090
     offset table.  The dynamic linker will use the .dynsym entry to
4091
     determine the address it must put in the global offset table, so
4092
     both the dynamic object and the regular object will refer to the
4093
     same memory location for the variable.  */
4094
4095
0
  htab = nds32_elf_hash_table (info);
4096
0
  s = htab->root.sdynbss;
4097
0
  BFD_ASSERT (s != NULL);
4098
4099
  /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4100
     to copy the initial value out of the dynamic object and into the
4101
     runtime process image.  We need to remember the offset into the
4102
     .rela.bss section we are going to use.  */
4103
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4104
0
    {
4105
0
      asection *srel;
4106
4107
0
      srel = htab->root.srelbss;
4108
0
      BFD_ASSERT (srel != NULL);
4109
0
      srel->size += sizeof (Elf32_External_Rela);
4110
0
      h->needs_copy = 1;
4111
0
    }
4112
4113
  /* We need to figure out the alignment required for this symbol.  I
4114
     have no idea how ELF linkers handle this.  */
4115
0
  power_of_two = bfd_log2 (h->size);
4116
0
  if (power_of_two > 3)
4117
0
    power_of_two = 3;
4118
4119
  /* Apply the required alignment.  */
4120
0
  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4121
0
  if (!bfd_link_align_section (s, power_of_two))
4122
0
    return false;
4123
4124
  /* Define the symbol as being at this point in the section.  */
4125
0
  h->root.u.def.section = s;
4126
0
  h->root.u.def.value = s->size;
4127
4128
  /* Increment the section size to make room for the symbol.  */
4129
0
  s->size += h->size;
4130
4131
0
  return true;
4132
0
}
4133
4134
/* Allocate space in .plt, .got and associated reloc sections for
4135
   dynamic relocs.  */
4136
4137
static bool
4138
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4139
0
{
4140
0
  struct bfd_link_info *info;
4141
0
  struct elf_link_hash_table *ehtab;
4142
0
  struct elf_nds32_link_hash_table *htab;
4143
0
  struct elf_dyn_relocs *p;
4144
4145
0
  if (h->root.type == bfd_link_hash_indirect)
4146
0
    return true;
4147
4148
  /* When warning symbols are created, they **replace** the "real"
4149
     entry in the hash table, thus we never get to see the real
4150
     symbol in a hash traversal. So look at it now.  */
4151
0
  if (h->root.type == bfd_link_hash_warning)
4152
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4153
4154
0
  info = (struct bfd_link_info *) inf;
4155
0
  ehtab = elf_hash_table (info);
4156
0
  htab = nds32_elf_hash_table (info);
4157
0
  if (htab == NULL)
4158
0
    return false;
4159
4160
0
  if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4161
0
      && h->plt.refcount > 0
4162
0
      && !(bfd_link_pie (info) && h->def_regular))
4163
0
    {
4164
      /* Make sure this symbol is output as a dynamic symbol.
4165
   Undefined weak syms won't yet be marked as dynamic.  */
4166
0
      if (h->dynindx == -1 && !h->forced_local)
4167
0
  {
4168
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
4169
0
      return false;
4170
0
  }
4171
4172
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4173
0
  {
4174
0
    asection *s = ehtab->splt;
4175
4176
    /* If this is the first .plt entry, make room for the special
4177
       first entry.  */
4178
0
    if (s->size == 0)
4179
0
      s->size += PLT_ENTRY_SIZE;
4180
4181
0
    h->plt.offset = s->size;
4182
4183
    /* If this symbol is not defined in a regular file, and we are
4184
       not generating a shared library, then set the symbol to this
4185
       location in the .plt.  This is required to make function
4186
       pointers compare as equal between the normal executable and
4187
       the shared library.  */
4188
0
    if (!bfd_link_pic (info) && !h->def_regular)
4189
0
      {
4190
0
        h->root.u.def.section = s;
4191
0
        h->root.u.def.value = h->plt.offset;
4192
0
      }
4193
4194
    /* Make room for this entry.  */
4195
0
    s->size += PLT_ENTRY_SIZE;
4196
4197
    /* We also need to make an entry in the .got.plt section, which
4198
       will be placed in the .got section by the linker script.  */
4199
0
    ehtab->sgotplt->size += 4;
4200
4201
    /* We also need to make an entry in the .rel.plt section.  */
4202
0
    ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4203
0
    if (htab->tls_desc_trampoline)
4204
0
      htab->next_tls_desc_index++;
4205
0
  }
4206
0
      else
4207
0
  {
4208
0
    h->plt.offset = (bfd_vma) - 1;
4209
0
    h->needs_plt = 0;
4210
0
  }
4211
0
    }
4212
0
  else
4213
0
    {
4214
0
      h->plt.offset = (bfd_vma) - 1;
4215
0
      h->needs_plt = 0;
4216
0
    }
4217
4218
0
  if (h->got.refcount > 0)
4219
0
    {
4220
0
      asection *sgot;
4221
0
      bool dyn;
4222
0
      int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4223
4224
      /* Make sure this symbol is output as a dynamic symbol.
4225
   Undefined weak syms won't yet be marked as dynamic.  */
4226
0
      if (h->dynindx == -1 && !h->forced_local)
4227
0
  {
4228
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
4229
0
      return false;
4230
0
  }
4231
4232
0
      sgot = elf_hash_table (info)->sgot;
4233
0
      h->got.offset = sgot->size;
4234
4235
0
      if (tls_type == GOT_UNKNOWN)
4236
0
  abort ();
4237
4238
      /* Non-TLS symbols, and TLS_IE need one GOT slot.  */
4239
0
      if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4240
0
  sgot->size += 4;
4241
0
      else
4242
0
  {
4243
    /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots.  */
4244
0
    if (tls_type & GOT_TLS_DESC)
4245
0
      sgot->size += 8;
4246
0
  }
4247
4248
0
      dyn = htab->root.dynamic_sections_created;
4249
4250
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4251
0
  {
4252
0
    if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4253
0
      {
4254
        /* TLS_DESC with trampoline needs a relocation slot
4255
     within .rela.plt.  */
4256
0
        htab->num_tls_desc++;
4257
0
        ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4258
0
        htab->tls_trampoline = -1;
4259
0
      }
4260
0
    else
4261
0
      {
4262
        /* other relocations, including TLS_DESC without trampoline, need
4263
     a relocation slot within .rela.got.  */
4264
0
        ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4265
0
      }
4266
0
  }
4267
0
    }
4268
0
  else
4269
0
    h->got.offset = (bfd_vma)-1;
4270
4271
0
  if (h->dyn_relocs == NULL)
4272
0
    return true;
4273
4274
  /* In the shared -Bsymbolic case, discard space allocated for
4275
     dynamic pc-relative relocs against symbols which turn out to be
4276
     defined in regular objects.  For the normal shared case, discard
4277
     space for pc-relative relocs that have become local due to symbol
4278
     visibility changes.  */
4279
4280
0
  if (bfd_link_pic (info))
4281
0
    {
4282
0
      if (h->def_regular && (h->forced_local || info->symbolic))
4283
0
  {
4284
0
    struct elf_dyn_relocs **pp;
4285
4286
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
4287
0
      {
4288
0
        p->count -= p->pc_count;
4289
0
        p->pc_count = 0;
4290
0
        if (p->count == 0)
4291
0
    *pp = p->next;
4292
0
        else
4293
0
    pp = &p->next;
4294
0
      }
4295
0
  }
4296
0
    }
4297
0
  else
4298
0
    {
4299
      /* For the non-shared case, discard space for relocs against
4300
   symbols which turn out to need copy relocs or are not dynamic.  */
4301
4302
0
      if (!h->non_got_ref
4303
0
    && ((h->def_dynamic
4304
0
         && !h->def_regular)
4305
0
        || (htab->root.dynamic_sections_created
4306
0
      && (h->root.type == bfd_link_hash_undefweak
4307
0
          || h->root.type == bfd_link_hash_undefined))))
4308
0
  {
4309
    /* Make sure this symbol is output as a dynamic symbol.
4310
       Undefined weak syms won't yet be marked as dynamic.  */
4311
0
    if (h->dynindx == -1 && !h->forced_local)
4312
0
      {
4313
0
        if (!bfd_elf_link_record_dynamic_symbol (info, h))
4314
0
    return false;
4315
0
      }
4316
4317
    /* If that succeeded, we know we'll be keeping all the
4318
       relocs.  */
4319
0
    if (h->dynindx != -1)
4320
0
      goto keep;
4321
0
  }
4322
4323
0
      h->dyn_relocs = NULL;
4324
4325
0
    keep:;
4326
0
    }
4327
4328
  /* Finally, allocate space.  */
4329
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
4330
0
    {
4331
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
4332
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
4333
0
    }
4334
4335
0
  return true;
4336
0
}
4337
4338
/* Add relocation REL to the end of relocation section SRELOC.  */
4339
4340
static void
4341
elf32_nds32_add_dynreloc (bfd *output_bfd,
4342
        struct bfd_link_info *info ATTRIBUTE_UNUSED,
4343
        asection *sreloc, Elf_Internal_Rela *rel)
4344
0
{
4345
0
  bfd_byte *loc;
4346
0
  if (sreloc == NULL)
4347
0
    abort ();
4348
4349
0
  loc = sreloc->contents;
4350
0
  loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4351
0
  if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4352
0
    abort ();
4353
4354
0
  bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4355
0
}
4356
4357
/* Set the sizes of the dynamic sections.  */
4358
4359
static bool
4360
nds32_elf_late_size_sections (struct bfd_link_info *info)
4361
0
{
4362
0
  struct elf_nds32_link_hash_table *htab;
4363
0
  bfd *dynobj;
4364
0
  asection *s;
4365
0
  bool relocs;
4366
0
  bfd *ibfd;
4367
4368
0
  htab = nds32_elf_hash_table (info);
4369
0
  if (htab == NULL)
4370
0
    return false;
4371
4372
0
  dynobj = elf_hash_table (info)->dynobj;
4373
0
  if (dynobj == NULL)
4374
0
    return true;
4375
4376
0
  if (elf_hash_table (info)->dynamic_sections_created)
4377
0
    {
4378
      /* Set the contents of the .interp section to the interpreter.  */
4379
0
      if (bfd_link_executable (info) && !info->nointerp)
4380
0
  {
4381
0
    s = elf_hash_table (info)->interp;
4382
0
    BFD_ASSERT (s != NULL);
4383
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4384
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4385
0
    s->alloced = 1;
4386
0
  }
4387
0
    }
4388
4389
  /* Set up .got offsets for local syms, and space for local dynamic
4390
     relocs.  */
4391
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4392
0
    {
4393
0
      bfd_signed_vma *local_got;
4394
0
      bfd_signed_vma *end_local_got;
4395
0
      bfd_size_type locsymcount;
4396
0
      Elf_Internal_Shdr *symtab_hdr;
4397
0
      asection *sgot;
4398
0
      char *local_tls_type;
4399
0
      bfd_vma *local_tlsdesc_gotent;
4400
4401
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4402
0
  continue;
4403
4404
0
      for (s = ibfd->sections; s != NULL; s = s->next)
4405
0
  {
4406
0
    struct elf_dyn_relocs *p;
4407
4408
0
    for (p = ((struct elf_dyn_relocs *)
4409
0
        elf_section_data (s)->local_dynrel);
4410
0
         p != NULL; p = p->next)
4411
0
      {
4412
0
        if (!bfd_is_abs_section (p->sec)
4413
0
      && bfd_is_abs_section (p->sec->output_section))
4414
0
    {
4415
      /* Input section has been discarded, either because
4416
         it is a copy of a linkonce section or due to
4417
         linker script /DISCARD/, so we'll be discarding
4418
         the relocs too.  */
4419
0
    }
4420
0
        else if (p->count != 0)
4421
0
    {
4422
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
4423
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
4424
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4425
0
        info->flags |= DF_TEXTREL;
4426
0
    }
4427
0
      }
4428
0
  }
4429
4430
0
      local_got = elf_local_got_refcounts (ibfd);
4431
0
      if (!local_got)
4432
0
  continue;
4433
4434
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
4435
0
      locsymcount = symtab_hdr->sh_info;
4436
0
      end_local_got = local_got + locsymcount;
4437
0
      sgot = elf_hash_table (info)->sgot;
4438
0
      local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4439
0
      local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4440
0
      for (; local_got < end_local_got;
4441
0
     ++local_got, ++local_tls_type, ++local_tlsdesc_gotent)
4442
0
  {
4443
0
    if (*local_got > 0)
4444
0
      {
4445
0
        int num_of_got_entry_needed = 0;
4446
0
        *local_got = sgot->size;
4447
0
        *local_tlsdesc_gotent = sgot->size;
4448
4449
        /* TLS_NORMAL, and TLS_IE need one slot in .got.  */
4450
0
        if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4451
0
    num_of_got_entry_needed = 1;
4452
        /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT.  */
4453
0
        else if (*local_tls_type & GOT_TLS_DESC)
4454
0
    num_of_got_entry_needed = 2;
4455
4456
0
        sgot->size += (num_of_got_entry_needed << 2);
4457
4458
        /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4459
     others need a slot in .rela.got.  */
4460
0
        if (*local_tls_type == GOT_TLS_DESC)
4461
0
    {
4462
0
      if (bfd_link_pic (info))
4463
0
        {
4464
0
          if (htab->tls_desc_trampoline)
4465
0
      {
4466
0
        htab->num_tls_desc++;
4467
0
        htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4468
0
        htab->tls_trampoline = -1;
4469
0
      }
4470
0
          else
4471
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4472
0
        }
4473
0
      else
4474
0
        {
4475
          /* TLS_DESC -> TLS_LE  */
4476
0
        }
4477
0
    }
4478
0
        else
4479
0
    {
4480
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4481
0
    }
4482
0
      }
4483
0
    else
4484
0
      {
4485
0
        *local_got = (bfd_vma) -1;
4486
0
        *local_tlsdesc_gotent = (bfd_vma) -1;
4487
0
      }
4488
0
  }
4489
0
    }
4490
4491
  /* Allocate global sym .plt and .got entries, and space for global
4492
     sym dynamic relocs.  */
4493
0
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4494
4495
  /* For every jump slot reserved in the sgotplt, reloc_count is
4496
     incremented.  However, when we reserve space for TLS descriptors,
4497
     it's not incremented, so in order to compute the space reserved
4498
     for them, it suffices to multiply the reloc count by the jump
4499
     slot size.  */
4500
0
  if (htab->tls_desc_trampoline && htab->root.srelplt)
4501
0
    htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4502
4503
0
  if (htab->tls_trampoline)
4504
0
    {
4505
0
      htab->tls_trampoline = htab->root.splt->size;
4506
4507
      /* If we're not using lazy TLS relocations, don't generate the
4508
   PLT and GOT entries they require.  */
4509
0
      if ((info->flags & DF_BIND_NOW))
4510
0
  htab->root.tlsdesc_plt = 0;
4511
0
      else
4512
0
  {
4513
0
    htab->root.tlsdesc_got = htab->root.sgot->size;
4514
0
    htab->root.sgot->size += 4;
4515
4516
0
    htab->root.tlsdesc_plt = htab->root.splt->size;
4517
0
    htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4518
0
  }
4519
0
    }
4520
4521
  /* We now have determined the sizes of the various dynamic sections.
4522
     Allocate memory for them.  */
4523
  /* The check_relocs and adjust_dynamic_symbol entry points have
4524
     determined the sizes of the various dynamic sections.  Allocate
4525
     memory for them.  */
4526
0
  relocs = false;
4527
0
  for (s = dynobj->sections; s != NULL; s = s->next)
4528
0
    {
4529
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
4530
0
  continue;
4531
4532
0
      if (s == htab->root.splt
4533
0
    || s == elf_hash_table (info)->sgot
4534
0
    || s == elf_hash_table (info)->sgotplt)
4535
0
  {
4536
    /* Strip this section if we don't need it; see the
4537
       comment below.  */
4538
0
    ;
4539
0
  }
4540
0
      else if (startswith (bfd_section_name (s), ".rela"))
4541
0
  {
4542
0
    if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4543
0
      relocs = true;
4544
4545
    /* We use the reloc_count field as a counter if we need
4546
       to copy relocs into the output file.  */
4547
0
    s->reloc_count = 0;
4548
0
  }
4549
0
      else
4550
0
  {
4551
    /* It's not one of our sections, so don't allocate space.  */
4552
0
    continue;
4553
0
  }
4554
4555
0
      if (s->size == 0)
4556
0
  {
4557
    /* If we don't need this section, strip it from the
4558
       output file.  This is mostly to handle .rela.bss and
4559
       .rela.plt.  We must create both sections in
4560
       create_dynamic_sections, because they must be created
4561
       before the linker maps input sections to output
4562
       sections.  The linker does that before
4563
       adjust_dynamic_symbol is called, and it is that
4564
       function which decides whether anything needs to go
4565
       into these sections.  */
4566
0
    s->flags |= SEC_EXCLUDE;
4567
0
    continue;
4568
0
  }
4569
4570
      /* Allocate memory for the section contents.  We use bfd_zalloc
4571
   here in case unused entries are not reclaimed before the
4572
   section's contents are written out.  This should not happen,
4573
   but this way if it does, we get a R_NDS32_NONE reloc instead
4574
   of garbage.  */
4575
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4576
0
      if (s->contents == NULL)
4577
0
  return false;
4578
0
      s->alloced = 1;
4579
0
    }
4580
4581
0
  return _bfd_elf_add_dynamic_tags (info, relocs);
4582
0
}
4583
4584
static bfd_reloc_status_type
4585
nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4586
       bfd_vma relocation, bfd_byte *location)
4587
0
{
4588
0
  int size;
4589
0
  bfd_vma x = 0;
4590
0
  bfd_reloc_status_type flag;
4591
0
  unsigned int rightshift = howto->rightshift;
4592
0
  unsigned int bitpos = howto->bitpos;
4593
4594
0
  if (howto->negate)
4595
0
    relocation = -relocation;
4596
4597
  /* Get the value we are going to relocate.  */
4598
0
  size = bfd_get_reloc_size (howto);
4599
0
  switch (size)
4600
0
    {
4601
0
    default:
4602
0
      abort ();
4603
0
      break;
4604
0
    case 0:
4605
0
      return bfd_reloc_ok;
4606
0
    case 2:
4607
0
      x = bfd_getb16 (location);
4608
0
      break;
4609
0
    case 4:
4610
0
      x = bfd_getb32 (location);
4611
0
      break;
4612
0
    }
4613
4614
  /* Check for overflow.  FIXME: We may drop bits during the addition
4615
     which we don't check for.  We must either check at every single
4616
     operation, which would be tedious, or we must do the computations
4617
     in a type larger than bfd_vma, which would be inefficient.  */
4618
0
  flag = bfd_reloc_ok;
4619
0
  if (howto->complain_on_overflow != complain_overflow_dont)
4620
0
    {
4621
0
      bfd_vma addrmask, fieldmask, signmask, ss;
4622
0
      bfd_vma a, b, sum;
4623
4624
      /* Get the values to be added together.  For signed and unsigned
4625
   relocations, we assume that all values should be truncated to
4626
   the size of an address.  For bitfields, all the bits matter.
4627
   See also bfd_check_overflow.  */
4628
0
      fieldmask = N_ONES (howto->bitsize);
4629
0
      signmask = ~fieldmask;
4630
0
      addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4631
0
      a = (relocation & addrmask) >> rightshift;
4632
0
      b = (x & howto->src_mask & addrmask) >> bitpos;
4633
4634
0
      switch (howto->complain_on_overflow)
4635
0
  {
4636
0
  case complain_overflow_signed:
4637
    /* If any sign bits are set, all sign bits must be set.
4638
       That is, A must be a valid negative address after
4639
       shifting.  */
4640
0
    signmask = ~(fieldmask >> 1);
4641
    /* Fall through.  */
4642
4643
0
  case complain_overflow_bitfield:
4644
    /* Much like the signed check, but for a field one bit
4645
       wider.  We allow a bitfield to represent numbers in the
4646
       range -2**n to 2**n-1, where n is the number of bits in the
4647
       field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
4648
       can't overflow, which is exactly what we want.  */
4649
0
    ss = a & signmask;
4650
0
    if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4651
0
      flag = bfd_reloc_overflow;
4652
4653
    /* We only need this next bit of code if the sign bit of B
4654
       is below the sign bit of A.  This would only happen if
4655
       SRC_MASK had fewer bits than BITSIZE.  Note that if
4656
       SRC_MASK has more bits than BITSIZE, we can get into
4657
       trouble; we would need to verify that B is in range, as
4658
       we do for A above.  */
4659
0
    ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4660
0
    ss >>= bitpos;
4661
4662
    /* Set all the bits above the sign bit.  */
4663
0
    b = (b ^ ss) - ss;
4664
4665
    /* Now we can do the addition.  */
4666
0
    sum = a + b;
4667
4668
    /* See if the result has the correct sign.  Bits above the
4669
       sign bit are junk now; ignore them.  If the sum is
4670
       positive, make sure we did not have all negative inputs;
4671
       if the sum is negative, make sure we did not have all
4672
       positive inputs.  The test below looks only at the sign
4673
       bits, and it really just
4674
       SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4675
4676
       We mask with addrmask here to explicitly allow an address
4677
       wrap-around.  The Linux kernel relies on it, and it is
4678
       the only way to write assembler code which can run when
4679
       loaded at a location 0x80000000 away from the location at
4680
       which it is linked.  */
4681
0
    if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4682
0
      flag = bfd_reloc_overflow;
4683
4684
0
    break;
4685
4686
0
  case complain_overflow_unsigned:
4687
    /* Checking for an unsigned overflow is relatively easy:
4688
       trim the addresses and add, and trim the result as well.
4689
       Overflow is normally indicated when the result does not
4690
       fit in the field.  However, we also need to consider the
4691
       case when, e.g., fieldmask is 0x7fffffff or smaller, an
4692
       input is 0x80000000, and bfd_vma is only 32 bits; then we
4693
       will get sum == 0, but there is an overflow, since the
4694
       inputs did not fit in the field.  Instead of doing a
4695
       separate test, we can check for this by or-ing in the
4696
       operands when testing for the sum overflowing its final
4697
       field.  */
4698
0
    sum = (a + b) & addrmask;
4699
0
    if ((a | b | sum) & signmask)
4700
0
      flag = bfd_reloc_overflow;
4701
0
    break;
4702
4703
0
  default:
4704
0
    abort ();
4705
0
  }
4706
0
    }
4707
4708
  /* Put RELOCATION in the right bits.  */
4709
0
  relocation >>= (bfd_vma) rightshift;
4710
0
  relocation <<= (bfd_vma) bitpos;
4711
4712
  /* Add RELOCATION to the right bits of X.  */
4713
  /* FIXME : 090616
4714
     Because the relaxation may generate duplicate relocation at one address,
4715
     an addition to immediate in the instruction may cause the relocation added
4716
     several times.
4717
     This bug should be fixed in assembler, but a check is also needed here.  */
4718
0
  if (howto->partial_inplace)
4719
0
    x = ((x & ~howto->dst_mask)
4720
0
   | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4721
0
  else
4722
0
    x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4723
4724
4725
  /* Put the relocated value back in the object file.  */
4726
0
  switch (size)
4727
0
    {
4728
0
    default:
4729
0
    case 0:
4730
0
    case 1:
4731
0
    case 8:
4732
0
      abort ();
4733
0
      break;
4734
0
    case 2:
4735
0
      bfd_putb16 (x, location);
4736
0
      break;
4737
0
    case 4:
4738
0
      bfd_putb32 (x, location);
4739
0
      break;
4740
0
    }
4741
4742
0
  return flag;
4743
0
}
4744
4745
static bfd_reloc_status_type
4746
nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4747
             asection *input_section, bfd_byte *contents,
4748
             bfd_vma address, bfd_vma value, bfd_vma addend)
4749
0
{
4750
0
  bfd_vma relocation;
4751
4752
  /* Sanity check the address.  */
4753
0
  bfd_vma octet = address * OCTETS_PER_BYTE (input_bfd, input_section);
4754
0
  if (!bfd_reloc_offset_in_range (howto, input_bfd, input_section, octet))
4755
0
    return bfd_reloc_outofrange;
4756
4757
  /* This function assumes that we are dealing with a basic relocation
4758
     against a symbol.  We want to compute the value of the symbol to
4759
     relocate to.  This is just VALUE, the value of the symbol, plus
4760
     ADDEND, any addend associated with the reloc.  */
4761
0
  relocation = value + addend;
4762
4763
  /* If the relocation is PC relative, we want to set RELOCATION to
4764
     the distance between the symbol (currently in RELOCATION) and the
4765
     location we are relocating.  If pcrel_offset is FALSE we do not
4766
     need to subtract out the offset of the location within the
4767
     section (which is just ADDRESS).  */
4768
0
  if (howto->pc_relative)
4769
0
    {
4770
0
      relocation -= (input_section->output_section->vma
4771
0
         + input_section->output_offset);
4772
0
      if (howto->pcrel_offset)
4773
0
  relocation -= address;
4774
0
    }
4775
4776
0
  return nds32_relocate_contents (howto, input_bfd, relocation,
4777
0
          contents + address);
4778
0
}
4779
4780
static int
4781
nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4782
            const char *name,
4783
            Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4784
            asection *input_sec,
4785
            struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4786
0
{
4787
0
  const char *source;
4788
0
  FILE *sym_ld_script = NULL;
4789
0
  struct elf_nds32_link_hash_table *table;
4790
4791
0
  table = nds32_elf_hash_table (info);
4792
0
  sym_ld_script = table->sym_ld_script;
4793
0
  if (!sym_ld_script)
4794
0
    return true;
4795
4796
0
  if (!h || !name || *name == '\0')
4797
0
    return true;
4798
4799
0
  if (input_sec->flags & SEC_EXCLUDE)
4800
0
    return true;
4801
4802
0
  if (!check_start_export_sym)
4803
0
    {
4804
0
      fprintf (sym_ld_script, "SECTIONS\n{\n");
4805
0
      check_start_export_sym = 1;
4806
0
    }
4807
4808
0
  if (h->root.type == bfd_link_hash_defined
4809
0
      || h->root.type == bfd_link_hash_defweak)
4810
0
    {
4811
0
      if (!h->root.u.def.section->output_section)
4812
0
  return true;
4813
4814
0
      if (bfd_is_const_section (input_sec))
4815
0
  source = input_sec->name;
4816
0
      else
4817
0
  source = bfd_get_filename (input_sec->owner);
4818
4819
0
      fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4820
0
         h->root.root.string,
4821
0
         (long) (h->root.u.def.value
4822
0
    + h->root.u.def.section->output_section->vma
4823
0
    + h->root.u.def.section->output_offset), source);
4824
0
    }
4825
4826
0
  return true;
4827
0
}
4828
4829
/* Relocate an NDS32/D ELF section.
4830
   There is some attempt to make this function usable for many architectures,
4831
   both for RELA and REL type relocs, if only to serve as a learning tool.
4832
4833
   The RELOCATE_SECTION function is called by the new ELF backend linker
4834
   to handle the relocations for a section.
4835
4836
   The relocs are always passed as Rela structures; if the section
4837
   actually uses Rel structures, the r_addend field will always be
4838
   zero.
4839
4840
   This function is responsible for adjust the section contents as
4841
   necessary, and (if using Rela relocs and generating a
4842
   relocatable output file) adjusting the reloc addend as
4843
   necessary.
4844
4845
   This function does not have to worry about setting the reloc
4846
   address or the reloc symbol index.
4847
4848
   LOCAL_SYMS is a pointer to the swapped in local symbols.
4849
4850
   LOCAL_SECTIONS is an array giving the section in the input file
4851
   corresponding to the st_shndx field of each local symbol.
4852
4853
   The global hash table entry for the global symbols can be found
4854
   via elf_sym_hashes (input_bfd).
4855
4856
   When generating relocatable output, this function must handle
4857
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
4858
   going to be the section symbol corresponding to the output
4859
   section, which means that the addend must be adjusted
4860
   accordingly.  */
4861
4862
/* Return the base VMA address which should be subtracted from real addresses
4863
   when resolving @dtpoff relocation.
4864
   This is PT_TLS segment p_vaddr.  */
4865
4866
/* Return the relocation value for @tpoff relocation
4867
   if STT_TLS virtual address is ADDRESS.  */
4868
4869
/* Return the relocation value for @gottpoff relocation
4870
   if STT_TLS virtual address is ADDRESS.  */
4871
4872
static bfd_vma
4873
gottpoff (struct bfd_link_info *info, bfd_vma address)
4874
0
{
4875
0
  bfd_vma tp_base;
4876
0
  bfd_vma tp_offset;
4877
4878
  /* If tls_sec is NULL, we should have signalled an error already.  */
4879
0
  if (elf_hash_table (info)->tls_sec == NULL)
4880
0
    return 0;
4881
4882
0
  tp_base = elf_hash_table (info)->tls_sec->vma;
4883
0
  tp_offset = address - tp_base;
4884
4885
0
  return tp_offset;
4886
0
}
4887
4888
static bool
4889
patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4890
0
{
4891
  /* TLS_GD/TLS_LD model #1
4892
     46 00 00 00 sethi $r0,#0x0
4893
     58 00 00 00 ori $r0,$r0,#0x0
4894
     40 00 74 00 add $r0,$r0,$gp
4895
     04 10 00 00 lwi $r1,[$r0+#0x0]
4896
     4b e0 04 01 jral $lp,$r1  */
4897
4898
  /* TLS_GD/TLS_LD model #2
4899
     46 00 00 00 sethi $r0,#0x0
4900
     58 00 00 00 ori $r0,$r0,#0x0
4901
     38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4902
     40 00 74 00 add $r0,$r0,$gp
4903
     4b e0 04 01 jral $lp,$r1  */
4904
4905
  /* TLS_IE model (non-PIC)
4906
     46 00 00 00 sethi $r0,#0x0
4907
     04 00 00 00 lwi $r0,[$r0+#0x0]
4908
     38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4909
4910
  /* TLS_IE model (PIC)
4911
     46 00 00 00 sethi $r0,#0x0
4912
     58 00 00 00 ori $r0,$r0,#0x0
4913
     38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4914
     38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4915
4916
  /* TLS_GD_TO_IE model
4917
     46 00 00 00 sethi $r0,#0x0
4918
     58 00 00 00 ori $r0,$r0,#0x0
4919
     40 00 74 00 add $r0,$rM,$gp
4920
     04 00 00 01 lwi $r0,[$r0+#0x4]
4921
     40 00 64 00 add $r0,$r0,$r25  */
4922
4923
0
  bool rz = false;
4924
4925
0
  typedef struct
4926
0
    {
4927
0
      uint32_t opcode;
4928
0
      uint32_t mask;
4929
0
    } pat_t;
4930
4931
0
  uint32_t patch[3] =
4932
0
    {
4933
0
      0x40007400, /* add $r0,$rM,$gp     */
4934
0
      0x04000001, /* lwi $r0,[$r0+#0x4]  */
4935
0
      0x40006400, /* add $r0,$r0,$r25    */
4936
0
    };
4937
4938
0
  pat_t mode0[3] =
4939
0
    {
4940
0
  { 0x40000000, 0xfe0003ff },
4941
0
  { 0x04000000, 0xfe000000 },
4942
0
  { 0x4be00001, 0xffff83ff },
4943
0
    };
4944
4945
0
  pat_t mode1[3] =
4946
0
    {
4947
0
  { 0x38007402, 0xfe007fff },
4948
0
  { 0x40007400, 0xfe007fff },
4949
0
  { 0x4be00001, 0xffff83ff },
4950
0
    };
4951
4952
0
  unsigned char *p = contents + rel->r_offset;
4953
4954
0
  uint32_t insn;
4955
0
  uint32_t regidx = 0;
4956
0
  insn = bfd_getb32 (p);
4957
0
  if (INSN_SETHI == (0xfe0fffffu & insn))
4958
0
    {
4959
0
      regidx = 0x1f & (insn >> 20);
4960
0
      p += 4;
4961
0
    }
4962
4963
0
  insn = bfd_getb32 (p);
4964
0
  if (INSN_ORI == (0xfe007fffu & insn))
4965
0
    {
4966
0
      regidx = 0x1f & (insn >> 20);
4967
0
      p += 4;
4968
0
    }
4969
4970
0
  if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction.  */
4971
0
    {
4972
      /* already patched?  */
4973
0
      if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
4974
0
    (patch[1] == bfd_getb32 (p + 4)))
4975
0
  rz = true;
4976
0
    }
4977
0
  else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
4978
0
    {
4979
0
      if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
4980
0
    (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
4981
0
  {
4982
0
    bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4983
0
    bfd_putb32 (patch[1], p + 4);
4984
0
    bfd_putb32 (patch[2], p + 8);
4985
0
    rz = true;
4986
0
  }
4987
0
    }
4988
0
  else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
4989
0
    {
4990
0
      if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
4991
0
    (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
4992
0
  {
4993
0
    bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4994
0
    bfd_putb32 (patch[1], p + 4);
4995
0
    bfd_putb32 (patch[2], p + 8);
4996
0
    rz = true;
4997
0
  }
4998
0
    }
4999
5000
0
  if (!rz)
5001
0
    {
5002
0
      printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
5003
0
        (int) rel->r_offset);
5004
0
      BFD_ASSERT(0); /* Unsupported pattern.  */
5005
0
    }
5006
5007
0
  return rz;
5008
0
}
5009
5010
static enum elf_nds32_tls_type
5011
get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
5012
5013
static unsigned int
5014
ones32 (register unsigned int x)
5015
0
{
5016
  /* 32-bit recursive reduction using SWAR...
5017
     but first step is mapping 2-bit values
5018
     into sum of 2 1-bit values in sneaky way.  */
5019
0
  x -= ((x >> 1) & 0x55555555);
5020
0
  x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
5021
0
  x = (((x >> 4) + x) & 0x0f0f0f0f);
5022
0
  x += (x >> 8);
5023
0
  x += (x >> 16);
5024
0
  return (x & 0x0000003f);
5025
0
}
5026
5027
#if !HAVE_FLS
5028
static unsigned int
5029
fls (register unsigned int x)
5030
0
{
5031
0
  return ffs (x & (-x));
5032
0
}
5033
#endif /* !HAVE_FLS */
5034
5035
#define nds32_elf_local_tlsdesc_gotent(bfd) \
5036
  (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
5037
5038
static int
5039
nds32_elf_relocate_section (struct bfd_link_info * info,
5040
          bfd *      input_bfd,
5041
          asection *       input_section,
5042
          bfd_byte *       contents,
5043
          Elf_Internal_Rela *    relocs,
5044
          Elf_Internal_Sym *     local_syms,
5045
          asection **      local_sections)
5046
0
{
5047
0
  Elf_Internal_Shdr *symtab_hdr;
5048
0
  struct elf_link_hash_entry **sym_hashes;
5049
0
  Elf_Internal_Rela *rel, *relend;
5050
0
  bool ret = true;   /* Assume success.  */
5051
0
  int align = 0;
5052
0
  bfd_reloc_status_type r;
5053
0
  const char *errmsg = NULL;
5054
0
  bfd_vma gp;
5055
0
  struct elf_link_hash_table *ehtab;
5056
0
  struct elf_nds32_link_hash_table *htab;
5057
0
  bfd *dynobj;
5058
0
  bfd_vma *local_got_offsets;
5059
0
  asection *sgot, *splt, *sreloc;
5060
0
  bfd_vma high_address;
5061
0
  struct elf_nds32_link_hash_table *table;
5062
0
  int eliminate_gc_relocs;
5063
0
  bfd_vma fpbase_addr;
5064
5065
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
5066
0
  sym_hashes = elf_sym_hashes (input_bfd);
5067
0
  ehtab = elf_hash_table (info);
5068
0
  htab = nds32_elf_hash_table (info);
5069
0
  high_address = bfd_get_section_limit (input_bfd, input_section);
5070
5071
0
  dynobj = htab->root.dynobj;
5072
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
5073
5074
0
  sgot = ehtab->sgot;
5075
0
  splt = ehtab->splt;
5076
0
  sreloc = NULL;
5077
5078
0
  rel = relocs;
5079
0
  relend = relocs + input_section->reloc_count;
5080
5081
0
  table = nds32_elf_hash_table (info);
5082
0
  eliminate_gc_relocs = table->eliminate_gc_relocs;
5083
5084
  /* By this time, we can adjust the value of _SDA_BASE_.  */
5085
  /* Explain _SDA_BASE_  */
5086
0
  if ((!bfd_link_relocatable (info)))
5087
0
    {
5088
0
      is_SDA_BASE_set = 1;
5089
0
      r = nds32_elf_final_sda_base (info->output_bfd, info, &gp, true);
5090
0
      if (r != bfd_reloc_ok)
5091
0
  return false;
5092
0
    }
5093
5094
  /* Do TLS model conversion once at first.  */
5095
0
  nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5096
5097
  /* Use gp as fp to prevent truncated fit.  Because in relaxation time
5098
     the fp value is set as gp, and it has be reverted for instruction
5099
     setting fp.  */
5100
0
  fpbase_addr = elf_gp (info->output_bfd);
5101
5102
  /* Deal with (dynamic) relocations.  */
5103
0
  for (rel = relocs; rel < relend; rel++)
5104
0
    {
5105
0
      enum elf_nds32_reloc_type r_type;
5106
0
      reloc_howto_type *howto = NULL;
5107
0
      unsigned long r_symndx;
5108
0
      struct elf_link_hash_entry *h = NULL;
5109
0
      const Elf_Internal_Sym *sym = NULL;
5110
0
      asection *sec;
5111
0
      bfd_vma relocation;
5112
0
      bfd_vma relocation_sym = 0xdeadbeef;
5113
0
      Elf_Internal_Rela *lorel;
5114
0
      bfd_vma off;
5115
5116
      /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5117
   ensure it's zero (we use REL relocs, not RELA).  Therefore this
5118
   should be assigning zero to `addend', but for clarity we use
5119
   `r_addend'.  */
5120
5121
0
      bfd_vma addend = rel->r_addend;
5122
0
      bfd_vma offset = rel->r_offset;
5123
5124
0
      r_type = ELF32_R_TYPE (rel->r_info);
5125
0
      if (r_type >= R_NDS32_max)
5126
0
  {
5127
    /* xgettext:c-format */
5128
0
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5129
0
            input_bfd, r_type);
5130
0
    bfd_set_error (bfd_error_bad_value);
5131
0
    ret = false;
5132
0
    continue;
5133
0
  }
5134
5135
0
      if (r_type == R_NDS32_GNU_VTENTRY
5136
0
    || r_type == R_NDS32_GNU_VTINHERIT
5137
0
    || r_type == R_NDS32_NONE
5138
0
    || r_type == R_NDS32_RELA_GNU_VTENTRY
5139
0
    || r_type == R_NDS32_RELA_GNU_VTINHERIT
5140
0
    || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5141
0
    || r_type == R_NDS32_DATA
5142
0
    || r_type == R_NDS32_TRAN)
5143
0
  continue;
5144
5145
      /* If we enter the fp-as-gp region.  Resolve the address
5146
   of best fp-base.  */
5147
0
      if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5148
0
    && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5149
0
  {
5150
0
    int dist;
5151
5152
    /* Distance to relocation of best fp-base is encoded in R_SYM.  */
5153
0
    dist =  rel->r_addend >> 16;
5154
0
    fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5155
0
              local_syms, symtab_hdr);
5156
0
  }
5157
0
      else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5158
0
         && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5159
0
  {
5160
0
    fpbase_addr = elf_gp (info->output_bfd);
5161
0
  }
5162
5163
      /* Skip the relocations used for relaxation.  */
5164
      /* We have to update LONGCALL and LONGJUMP
5165
   relocations when generating the relocatable files.  */
5166
0
      if (!bfd_link_relocatable (info)
5167
0
    && (r_type >= R_NDS32_RELAX_ENTRY
5168
0
        || (r_type >= R_NDS32_LONGCALL4
5169
0
      && r_type <= R_NDS32_LONGJUMP7)))
5170
0
  continue;
5171
5172
0
      howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5173
0
      r_symndx = ELF32_R_SYM (rel->r_info);
5174
5175
      /* This is a final link.  */
5176
0
      sym = NULL;
5177
0
      sec = NULL;
5178
0
      h = NULL;
5179
5180
0
      if (r_symndx < symtab_hdr->sh_info)
5181
0
  {
5182
    /* Local symbol.  */
5183
0
    sym = local_syms + r_symndx;
5184
0
    sec = local_sections[r_symndx];
5185
5186
0
    relocation = _bfd_elf_rela_local_sym (info->output_bfd,
5187
0
            sym, &sec, rel);
5188
0
    addend = rel->r_addend;
5189
5190
    /* keep symbol location for static TLS_IE GOT entry  */
5191
0
    relocation_sym = relocation;
5192
0
    if (bfd_link_relocatable (info))
5193
0
      {
5194
        /* This is a relocatable link.  We don't have to change
5195
     anything, unless the reloc is against a section symbol,
5196
     in which case we have to adjust according to where the
5197
     section symbol winds up in the output section.  */
5198
0
        if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5199
0
    rel->r_addend += sec->output_offset + sym->st_value;
5200
5201
0
        continue;
5202
0
      }
5203
0
  }
5204
0
      else
5205
0
  {
5206
    /* External symbol.  */
5207
0
    if (bfd_link_relocatable (info))
5208
0
      continue;
5209
0
    bool warned, ignored, unresolved_reloc;
5210
0
    int symndx = r_symndx - symtab_hdr->sh_info;
5211
5212
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5213
0
           r_symndx, symtab_hdr, sym_hashes, h, sec,
5214
0
           relocation, unresolved_reloc, warned,
5215
0
           ignored);
5216
5217
    /* keep symbol location for static TLS_IE GOT entry  */
5218
0
    relocation_sym = relocation;
5219
5220
    /* la $fp, _FP_BASE_ is per-function (region).
5221
       Handle it specially.  */
5222
0
    switch ((int) r_type)
5223
0
      {
5224
0
      case R_NDS32_HI20_RELA:
5225
0
      case R_NDS32_LO12S0_RELA:
5226
0
        if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5227
0
        FP_BASE_NAME) == 0)
5228
0
    {
5229
0
      if (!bfd_link_pie (info))
5230
0
        {
5231
0
          _bfd_error_handler
5232
0
      ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5233
0
       input_bfd);
5234
0
        }
5235
0
      relocation = fpbase_addr;
5236
0
    }
5237
0
        break;
5238
0
      case R_NDS32_SDA19S0_RELA:
5239
0
      case R_NDS32_SDA15S0_RELA:
5240
0
      case R_NDS32_20_RELA:
5241
0
        if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5242
0
        FP_BASE_NAME) == 0)
5243
0
    {
5244
0
      relocation = fpbase_addr;
5245
0
      break;
5246
0
    }
5247
0
      }
5248
0
  }
5249
5250
      /* Sanity check the address.  */
5251
0
      if (offset > high_address)
5252
0
  {
5253
0
    r = bfd_reloc_outofrange;
5254
0
    goto check_reloc;
5255
0
  }
5256
5257
0
      if (r_type >= R_NDS32_RELAX_ENTRY)
5258
0
  continue;
5259
5260
0
      switch ((int) r_type)
5261
0
  {
5262
0
  case R_NDS32_GOTOFF:
5263
    /* Relocation is relative to the start of the global offset
5264
       table (for ld24 rx, #uimm24), e.g. access at label+addend
5265
5266
       ld24 rx. #label@GOTOFF + addend
5267
       sub  rx, r12.  */
5268
0
  case R_NDS32_GOTOFF_HI20:
5269
0
  case R_NDS32_GOTOFF_LO12:
5270
0
  case R_NDS32_GOTOFF_LO15:
5271
0
  case R_NDS32_GOTOFF_LO19:
5272
0
    BFD_ASSERT (sgot != NULL);
5273
5274
0
    relocation -= elf_gp (info->output_bfd);
5275
0
    break;
5276
5277
0
  case R_NDS32_9_PLTREL:
5278
0
  case R_NDS32_25_PLTREL:
5279
    /* Relocation is to the entry for this symbol in the
5280
       procedure linkage table.  */
5281
5282
    /* The native assembler will generate a 25_PLTREL reloc
5283
       for a local symbol if you assemble a call from one
5284
       section to another when using -K pic.  */
5285
0
    if (h == NULL)
5286
0
      break;
5287
5288
0
    if (h->forced_local)
5289
0
      break;
5290
5291
    /* We didn't make a PLT entry for this symbol.  This
5292
       happens when statically linking PIC code, or when
5293
       using -Bsymbolic.  */
5294
0
    if (h->plt.offset == (bfd_vma) - 1)
5295
0
      break;
5296
5297
0
    relocation = (splt->output_section->vma
5298
0
      + splt->output_offset + h->plt.offset);
5299
0
    break;
5300
5301
0
  case R_NDS32_PLT_GOTREL_HI20:
5302
0
  case R_NDS32_PLT_GOTREL_LO12:
5303
0
  case R_NDS32_PLT_GOTREL_LO15:
5304
0
  case R_NDS32_PLT_GOTREL_LO19:
5305
0
  case R_NDS32_PLT_GOTREL_LO20:
5306
0
    if (h == NULL
5307
0
        || h->forced_local
5308
0
        || h->plt.offset == (bfd_vma) -1
5309
0
        || (bfd_link_pie (info) && h->def_regular))
5310
0
      {
5311
        /* Maybe we should find better checking to optimize
5312
     PIE PLT relocations.  */
5313
        /* We didn't make a PLT entry for this symbol.  This
5314
     happens when statically linking PIC code, or when
5315
     using -Bsymbolic.  */
5316
0
        if (h)
5317
0
    h->plt.offset = (bfd_vma) -1;   /* Cancel PLT trampoline.  */
5318
0
        relocation -= elf_gp (info->output_bfd);
5319
0
        break;
5320
0
      }
5321
5322
0
    relocation = (splt->output_section->vma
5323
0
      + splt->output_offset + h->plt.offset);
5324
5325
0
    relocation -= elf_gp (info->output_bfd);
5326
0
    break;
5327
5328
0
  case R_NDS32_PLTREL_HI20:
5329
0
  case R_NDS32_PLTREL_LO12:
5330
5331
    /* Relocation is to the entry for this symbol in the
5332
       procedure linkage table.  */
5333
5334
    /* The native assembler will generate a 25_PLTREL reloc
5335
       for a local symbol if you assemble a call from one
5336
       section to another when using -K pic.  */
5337
0
    if (h == NULL)
5338
0
      break;
5339
5340
0
    if (h->forced_local)
5341
0
      break;
5342
5343
0
    if (h->plt.offset == (bfd_vma) - 1)
5344
      /* We didn't make a PLT entry for this symbol.  This
5345
         happens when statically linking PIC code, or when
5346
         using -Bsymbolic.  */
5347
0
      break;
5348
5349
0
    if (splt == NULL)
5350
0
      break;
5351
5352
0
    relocation = (splt->output_section->vma
5353
0
      + splt->output_offset
5354
0
      + h->plt.offset + 4)
5355
0
           - (input_section->output_section->vma
5356
0
        + input_section->output_offset
5357
0
        + rel->r_offset);
5358
5359
0
    break;
5360
5361
0
  case R_NDS32_GOTPC20:
5362
    /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5363
       ld24 rx,#_GLOBAL_OFFSET_TABLE_  */
5364
0
    relocation = elf_gp (info->output_bfd);
5365
0
    break;
5366
5367
0
  case R_NDS32_GOTPC_HI20:
5368
0
  case R_NDS32_GOTPC_LO12:
5369
    /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5370
       bl .+4
5371
       seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5372
       or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5373
       or
5374
       bl .+4
5375
       seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5376
       add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)  */
5377
0
    relocation = elf_gp (info->output_bfd);
5378
0
    relocation -= (input_section->output_section->vma
5379
0
       + input_section->output_offset + rel->r_offset);
5380
0
    break;
5381
5382
0
  case R_NDS32_GOT20:
5383
    /* Fall through.  */
5384
0
  case R_NDS32_GOT_HI20:
5385
0
  case R_NDS32_GOT_LO12:
5386
0
  case R_NDS32_GOT_LO15:
5387
0
  case R_NDS32_GOT_LO19:
5388
    /* Relocation is to the entry for this symbol in the global
5389
       offset table.  */
5390
0
    BFD_ASSERT (sgot != NULL);
5391
5392
0
    if (h != NULL)
5393
0
      {
5394
        /* External symbol  */
5395
0
        bool dyn;
5396
5397
0
        off = h->got.offset;
5398
0
        BFD_ASSERT (off != (bfd_vma) - 1);
5399
0
        dyn = htab->root.dynamic_sections_created;
5400
0
        if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5401
0
                bfd_link_pic (info),
5402
0
                h)
5403
0
      || (bfd_link_pic (info)
5404
0
          && (info->symbolic
5405
0
        || h->dynindx == -1
5406
0
        || h->forced_local) && h->def_regular))
5407
0
    {
5408
      /* This is actually a static link, or it is a
5409
         -Bsymbolic link and the symbol is defined
5410
         locally, or the symbol was forced to be local
5411
         because of a version file.  We must initialize
5412
         this entry in the global offset table.  Since the
5413
         offset must always be a multiple of 4, we use the
5414
         least significant bit to record whether we have
5415
         initialized it already.
5416
5417
         When doing a dynamic link, we create a .rela.got
5418
         relocation entry to initialize the value.  This
5419
         is done in the finish_dynamic_symbol routine.  */
5420
0
      if ((off & 1) != 0) /* clear LSB  */
5421
0
        off &= ~1;
5422
0
      else
5423
0
        {
5424
0
          bfd_put_32 (info->output_bfd, relocation,
5425
0
          sgot->contents + off);
5426
0
          h->got.offset |= 1;
5427
0
        }
5428
0
    }
5429
0
        relocation = sgot->output_section->vma + sgot->output_offset + off
5430
0
         - elf_gp (info->output_bfd);
5431
0
      }
5432
0
    else
5433
0
      {
5434
        /* Local symbol  */
5435
0
        bfd_byte *loc;
5436
5437
0
        BFD_ASSERT (local_got_offsets != NULL
5438
0
        && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5439
5440
0
        off = local_got_offsets[r_symndx];
5441
5442
        /* The offset must always be a multiple of 4.  We use
5443
     the least significant bit to record whether we have
5444
     already processed this entry.  */
5445
0
        if ((off & 1) != 0) /* clear LSB  */
5446
0
    off &= ~1;
5447
0
        else
5448
0
    {
5449
0
      bfd_put_32 (info->output_bfd, relocation,
5450
0
            sgot->contents + off);
5451
5452
0
      if (bfd_link_pic (info))
5453
0
        {
5454
0
          asection *srelgot;
5455
0
          Elf_Internal_Rela outrel;
5456
5457
          /* We need to generate a R_NDS32_RELATIVE reloc
5458
       for the dynamic linker.  */
5459
0
          srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5460
0
          BFD_ASSERT (srelgot != NULL);
5461
5462
0
          outrel.r_offset = (elf_gp (info->output_bfd)
5463
0
           + sgot->output_offset + off);
5464
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5465
0
          outrel.r_addend = relocation;
5466
0
          loc = srelgot->contents;
5467
0
          loc +=
5468
0
      srelgot->reloc_count * sizeof (Elf32_External_Rela);
5469
0
          bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
5470
0
          ++srelgot->reloc_count;
5471
0
        }
5472
0
      local_got_offsets[r_symndx] |= 1;
5473
0
    }
5474
0
        relocation = sgot->output_section->vma + sgot->output_offset + off
5475
0
         - elf_gp (info->output_bfd);
5476
0
      }
5477
5478
0
    break;
5479
5480
0
  case R_NDS32_16_RELA:
5481
0
  case R_NDS32_20_RELA:
5482
0
  case R_NDS32_5_RELA:
5483
0
  case R_NDS32_32_RELA:
5484
0
  case R_NDS32_9_PCREL_RELA:
5485
0
  case R_NDS32_WORD_9_PCREL_RELA:
5486
0
  case R_NDS32_10_UPCREL_RELA:
5487
0
  case R_NDS32_15_PCREL_RELA:
5488
0
  case R_NDS32_17_PCREL_RELA:
5489
0
  case R_NDS32_25_PCREL_RELA:
5490
0
  case R_NDS32_HI20_RELA:
5491
0
  case R_NDS32_LO12S3_RELA:
5492
0
  case R_NDS32_LO12S2_RELA:
5493
0
  case R_NDS32_LO12S2_DP_RELA:
5494
0
  case R_NDS32_LO12S2_SP_RELA:
5495
0
  case R_NDS32_LO12S1_RELA:
5496
0
  case R_NDS32_LO12S0_RELA:
5497
0
  case R_NDS32_LO12S0_ORI_RELA:
5498
0
    if (bfd_link_pic (info) && r_symndx != 0
5499
0
        && (input_section->flags & SEC_ALLOC) != 0
5500
0
        && (eliminate_gc_relocs == 0
5501
0
      || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5502
0
        && ((r_type != R_NDS32_9_PCREL_RELA
5503
0
       && r_type != R_NDS32_WORD_9_PCREL_RELA
5504
0
       && r_type != R_NDS32_10_UPCREL_RELA
5505
0
       && r_type != R_NDS32_15_PCREL_RELA
5506
0
       && r_type != R_NDS32_17_PCREL_RELA
5507
0
       && r_type != R_NDS32_25_PCREL_RELA
5508
0
       && !(r_type == R_NDS32_32_RELA
5509
0
      && strcmp (input_section->name, ".eh_frame") == 0))
5510
0
      || (h != NULL && h->dynindx != -1
5511
0
          && (!info->symbolic || !h->def_regular))))
5512
0
      {
5513
0
        Elf_Internal_Rela outrel;
5514
0
        bool skip, relocate;
5515
0
        bfd_byte *loc;
5516
5517
        /* When generating a shared object, these relocations
5518
     are copied into the output file to be resolved at run
5519
     time.  */
5520
5521
0
        if (sreloc == NULL)
5522
0
    {
5523
0
      const char *name;
5524
5525
0
      name = bfd_elf_string_from_elf_section
5526
0
        (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5527
0
         elf_section_data (input_section)->rela.hdr->sh_name);
5528
0
      if (name == NULL)
5529
0
        return false;
5530
5531
0
      BFD_ASSERT (startswith (name, ".rela")
5532
0
            && strcmp (bfd_section_name (input_section),
5533
0
           name + 5) == 0);
5534
5535
0
      sreloc = bfd_get_section_by_name (dynobj, name);
5536
0
      BFD_ASSERT (sreloc != NULL);
5537
0
    }
5538
5539
0
        skip = false;
5540
0
        relocate = false;
5541
5542
0
        outrel.r_offset = _bfd_elf_section_offset (info->output_bfd,
5543
0
               info,
5544
0
               input_section,
5545
0
               rel->r_offset);
5546
0
        if (outrel.r_offset == (bfd_vma) - 1)
5547
0
    skip = true;
5548
0
        else if (outrel.r_offset == (bfd_vma) - 2)
5549
0
    skip = true, relocate = true;
5550
0
        outrel.r_offset += (input_section->output_section->vma
5551
0
          + input_section->output_offset);
5552
5553
0
        if (skip)
5554
0
    memset (&outrel, 0, sizeof outrel);
5555
0
        else if (r_type == R_NDS32_17_PCREL_RELA
5556
0
           || r_type == R_NDS32_15_PCREL_RELA
5557
0
           || r_type == R_NDS32_25_PCREL_RELA)
5558
0
    {
5559
0
      BFD_ASSERT (h != NULL && h->dynindx != -1);
5560
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5561
0
      outrel.r_addend = rel->r_addend;
5562
0
    }
5563
0
        else
5564
0
    {
5565
      /* h->dynindx may be -1 if this symbol was marked to
5566
         become local.  */
5567
0
      if (h == NULL
5568
0
          || ((info->symbolic || h->dynindx == -1)
5569
0
        && h->def_regular)
5570
0
          || (bfd_link_pie (info) && h->def_regular))
5571
0
        {
5572
0
          relocate = true;
5573
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5574
0
          outrel.r_addend = relocation + rel->r_addend;
5575
5576
0
          if (h)
5577
0
      {
5578
0
        h->plt.offset = (bfd_vma) -1;   /* cancel PLT trampoline.  */
5579
5580
0
        BFD_ASSERT (sgot != NULL);
5581
        /* If we did not allocate got entry for the symbol,
5582
           we can not fill the nonexistent got entry.  */
5583
0
        if (h->got.offset != (bfd_vma) -1
5584
0
            && (h->got.offset & 1) == 0)
5585
0
          {
5586
0
            bfd_put_32 (info->output_bfd, outrel.r_addend,
5587
0
            sgot->contents + h->got.offset);
5588
0
          }
5589
0
      }
5590
0
        }
5591
0
      else
5592
0
        {
5593
0
          if (h->dynindx == -1)
5594
0
      {
5595
0
        _bfd_error_handler
5596
0
          (_("%pB: relocation %s against `%s' can not be used when "
5597
0
             "making a shared object; recompile with -fPIC"),
5598
0
           input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5599
0
        bfd_set_error (bfd_error_bad_value);
5600
0
        return false;
5601
0
      }
5602
5603
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5604
0
          outrel.r_addend = rel->r_addend;
5605
0
        }
5606
0
    }
5607
5608
0
        loc = sreloc->contents;
5609
0
        loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5610
0
        bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
5611
0
        ++sreloc->reloc_count;
5612
5613
        /* If this reloc is against an external symbol, we do
5614
     not want to fiddle with the addend.  Otherwise, we
5615
     need to include the symbol value so that it becomes
5616
     an addend for the dynamic reloc.  */
5617
0
        if (!relocate)
5618
0
    continue;
5619
0
      }
5620
0
    break;
5621
5622
0
  case R_NDS32_25_ABS_RELA:
5623
0
    if (bfd_link_pic (info))
5624
0
      {
5625
0
        _bfd_error_handler
5626
0
    (_("%pB: warning: %s unsupported in shared mode"),
5627
0
     input_bfd, "R_NDS32_25_ABS_RELA");
5628
0
        return false;
5629
0
      }
5630
0
    break;
5631
5632
0
  case R_NDS32_9_PCREL:
5633
0
    r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5634
0
            contents, offset,
5635
0
            sec, relocation, addend);
5636
0
    goto check_reloc;
5637
5638
0
  case R_NDS32_HI20:
5639
    /* We allow an arbitrary number of HI20 relocs before the
5640
       LO12 reloc.  This permits gcc to emit the HI and LO relocs
5641
       itself.  */
5642
0
    for (lorel = rel + 1;
5643
0
         (lorel < relend
5644
0
    && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5645
0
      continue;
5646
0
    if (lorel < relend
5647
0
        && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5648
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5649
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5650
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5651
0
      {
5652
0
        nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5653
0
               contents, relocation + addend);
5654
0
        r = bfd_reloc_ok;
5655
0
      }
5656
0
    else
5657
0
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5658
0
            contents, offset, relocation,
5659
0
            addend);
5660
0
    goto check_reloc;
5661
5662
0
  case R_NDS32_GOT17S2_RELA:
5663
0
  case R_NDS32_GOT15S2_RELA:
5664
0
    BFD_ASSERT (sgot != NULL);
5665
5666
0
    if (h != NULL)
5667
0
      {
5668
0
        bool dyn;
5669
5670
0
        off = h->got.offset;
5671
0
        BFD_ASSERT (off != (bfd_vma) - 1);
5672
5673
0
        dyn = htab->root.dynamic_sections_created;
5674
0
        if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5675
0
      (dyn, bfd_link_pic (info), h)
5676
0
      || (bfd_link_pic (info)
5677
0
          && (info->symbolic
5678
0
        || h->dynindx == -1
5679
0
        || h->forced_local)
5680
0
          && h->def_regular))
5681
0
    {
5682
      /* This is actually a static link, or it is a
5683
         -Bsymbolic link and the symbol is defined
5684
         locally, or the symbol was forced to be local
5685
         because of a version file.  We must initialize
5686
         this entry in the global offset table.  Since the
5687
         offset must always be a multiple of 4, we use the
5688
         least significant bit to record whether we have
5689
         initialized it already.
5690
5691
         When doing a dynamic link, we create a .rela.got
5692
         relocation entry to initialize the value.  This
5693
         is done in the finish_dynamic_symbol routine.  */
5694
0
      if ((off & 1) != 0)
5695
0
        off &= ~1;
5696
0
      else
5697
0
        {
5698
0
          bfd_put_32 (info->output_bfd, relocation,
5699
0
          sgot->contents + off);
5700
0
          h->got.offset |= 1;
5701
0
        }
5702
0
    }
5703
0
      }
5704
0
    else
5705
0
      {
5706
0
        bfd_byte *loc;
5707
5708
0
        BFD_ASSERT (local_got_offsets != NULL
5709
0
        && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5710
5711
0
        off = local_got_offsets[r_symndx];
5712
5713
        /* The offset must always be a multiple of 4.  We use
5714
     the least significant bit to record whether we have
5715
     already processed this entry.  */
5716
0
        if ((off & 1) != 0)
5717
0
    off &= ~1;
5718
0
        else
5719
0
    {
5720
0
      bfd_put_32 (info->output_bfd, relocation,
5721
0
            sgot->contents + off);
5722
5723
0
      if (bfd_link_pic (info))
5724
0
        {
5725
0
          asection *srelgot;
5726
0
          Elf_Internal_Rela outrel;
5727
5728
          /* We need to generate a R_NDS32_RELATIVE reloc
5729
       for the dynamic linker.  */
5730
0
          srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5731
0
          BFD_ASSERT (srelgot != NULL);
5732
5733
0
          outrel.r_offset = (elf_gp (info->output_bfd)
5734
0
           + sgot->output_offset + off);
5735
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5736
0
          outrel.r_addend = relocation;
5737
0
          loc = srelgot->contents;
5738
0
          loc +=
5739
0
      srelgot->reloc_count * sizeof (Elf32_External_Rela);
5740
0
          bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
5741
0
          ++srelgot->reloc_count;
5742
0
        }
5743
0
      local_got_offsets[r_symndx] |= 1;
5744
0
    }
5745
0
      }
5746
0
    relocation = sgot->output_section->vma + sgot->output_offset + off
5747
0
      - elf_gp (info->output_bfd);
5748
5749
0
    if (relocation & align)
5750
0
      {
5751
        /* Incorrect alignment.  */
5752
0
        _bfd_error_handler
5753
0
    (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5754
0
        ret = false;
5755
0
        r = bfd_reloc_dangerous;
5756
0
        goto check_reloc;
5757
0
      }
5758
0
    break;
5759
5760
0
  case R_NDS32_SDA16S3_RELA:
5761
0
  case R_NDS32_SDA15S3_RELA:
5762
0
  case R_NDS32_SDA15S3:
5763
0
    align = 0x7;
5764
0
    goto handle_sda;
5765
5766
0
  case R_NDS32_SDA17S2_RELA:
5767
0
  case R_NDS32_SDA15S2_RELA:
5768
0
  case R_NDS32_SDA12S2_SP_RELA:
5769
0
  case R_NDS32_SDA12S2_DP_RELA:
5770
0
  case R_NDS32_SDA15S2:
5771
0
  case R_NDS32_SDA_FP7U2_RELA:
5772
0
    align = 0x3;
5773
0
    goto handle_sda;
5774
5775
0
  case R_NDS32_SDA18S1_RELA:
5776
0
  case R_NDS32_SDA15S1_RELA:
5777
0
  case R_NDS32_SDA15S1:
5778
0
    align = 0x1;
5779
0
    goto handle_sda;
5780
5781
0
  case R_NDS32_SDA19S0_RELA:
5782
0
  case R_NDS32_SDA15S0_RELA:
5783
0
  case R_NDS32_SDA15S0:
5784
0
    align = 0x0;
5785
0
  handle_sda:
5786
0
    BFD_ASSERT (sec != NULL);
5787
5788
    /* If the symbol is in the abs section, the out_bfd will be null.
5789
       This happens when the relocation has a symbol@GOTOFF.  */
5790
0
    r = nds32_elf_final_sda_base (info->output_bfd, info, &gp, false);
5791
0
    if (r != bfd_reloc_ok)
5792
0
      {
5793
0
        _bfd_error_handler
5794
0
    (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5795
0
        ret = false;
5796
0
        goto check_reloc;
5797
0
      }
5798
5799
    /* At this point `relocation' contains the object's
5800
       address.  */
5801
0
    if (r_type == R_NDS32_SDA_FP7U2_RELA)
5802
0
      {
5803
0
        relocation -= fpbase_addr;
5804
0
      }
5805
0
    else
5806
0
      relocation -= gp;
5807
    /* Now it contains the offset from _SDA_BASE_.  */
5808
5809
    /* Make sure alignment is correct.  */
5810
5811
0
    if (relocation & align)
5812
0
      {
5813
        /* Incorrect alignment.  */
5814
0
        _bfd_error_handler
5815
    /* xgettext:c-format */
5816
0
    (_("%pB(%pA): warning: unaligned small data access"
5817
0
       " of type %d"),
5818
0
     input_bfd, input_section, r_type);
5819
0
        ret = false;
5820
0
        goto check_reloc;
5821
0
      }
5822
0
    break;
5823
5824
0
  case R_NDS32_17IFC_PCREL_RELA:
5825
0
  case R_NDS32_10IFCU_PCREL_RELA:
5826
    /* Do nothing.  */
5827
0
    break;
5828
5829
0
  case R_NDS32_TLS_LE_HI20:
5830
0
  case R_NDS32_TLS_LE_LO12:
5831
0
  case R_NDS32_TLS_LE_20:
5832
0
  case R_NDS32_TLS_LE_15S0:
5833
0
  case R_NDS32_TLS_LE_15S1:
5834
0
  case R_NDS32_TLS_LE_15S2:
5835
    /* We do not have garbage collection for got entries.
5836
       Therefore, IE to LE may have one empty entry, and DESC to
5837
       LE may have two.  */
5838
0
    if (elf_hash_table (info)->tls_sec != NULL)
5839
0
      relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5840
0
    break;
5841
5842
0
  case R_NDS32_TLS_IE_HI20:
5843
0
  case R_NDS32_TLS_IE_LO12S2:
5844
0
  case R_NDS32_TLS_DESC_HI20:
5845
0
  case R_NDS32_TLS_DESC_LO12:
5846
0
  case R_NDS32_TLS_IE_LO12:
5847
0
  case R_NDS32_TLS_IEGP_HI20:
5848
0
  case R_NDS32_TLS_IEGP_LO12:
5849
0
  case R_NDS32_TLS_IEGP_LO12S2:
5850
0
    {
5851
      /* Relocation is to the entry for this symbol in the global
5852
         offset table.  */
5853
0
      enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5854
0
      asection *srelgot;
5855
0
      Elf_Internal_Rela outrel;
5856
0
      bfd_byte *loc;
5857
0
      int indx = 0;
5858
5859
0
      eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5860
5861
0
      BFD_ASSERT (sgot != NULL);
5862
0
      if (h != NULL)
5863
0
        {
5864
0
    bool dyn;
5865
5866
0
    off = h->got.offset;
5867
0
    BFD_ASSERT (off != (bfd_vma) -1);
5868
0
    dyn = htab->root.dynamic_sections_created;
5869
0
    tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5870
0
    if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5871
0
        && (!bfd_link_pic (info)
5872
0
      || !SYMBOL_REFERENCES_LOCAL (info, h)))
5873
0
      indx = h->dynindx;
5874
0
        }
5875
0
      else
5876
0
        {
5877
0
    BFD_ASSERT (local_got_offsets != NULL
5878
0
          && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5879
0
    off = local_got_offsets[r_symndx];
5880
0
    tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5881
0
        }
5882
5883
0
      relocation = sgot->output_section->vma + sgot->output_offset + off;
5884
5885
0
      if (1 < ones32 (tls_type))
5886
0
        {
5887
0
    eff_tls_type = 1 << (fls (tls_type) - 1);
5888
    /* TLS model shall be handled in nds32_elf_unify_tls_model ().  */
5889
5890
    /* TLS model X -> LE is not implement yet!
5891
       workaround here!  */
5892
0
    if (eff_tls_type == GOT_TLS_LE)
5893
0
      {
5894
0
        eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5895
0
      }
5896
0
        }
5897
5898
      /* The offset must always be a multiple of 4.  We use
5899
         the least significant bit to record whether we have
5900
         already processed this entry.  */
5901
0
      bool need_relocs = false;
5902
0
      srelgot = ehtab->srelgot;
5903
0
      if ((bfd_link_pic (info) || indx != 0)
5904
0
    && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5905
0
        || h->root.type != bfd_link_hash_undefweak))
5906
0
        {
5907
0
    need_relocs = true;
5908
0
    BFD_ASSERT (srelgot != NULL);
5909
0
        }
5910
5911
0
      if (off & 1)
5912
0
        {
5913
0
    off &= ~1;
5914
0
    relocation &= ~1;
5915
5916
0
    if (eff_tls_type & GOT_TLS_DESC)
5917
0
      {
5918
0
        relocation -= elf_gp (info->output_bfd);
5919
0
        if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5920
0
          {
5921
      /* TLS model shall be converted.  */
5922
0
      BFD_ASSERT(0);
5923
0
          }
5924
0
      }
5925
0
    else if (eff_tls_type & GOT_TLS_IEGP)
5926
0
      {
5927
0
        relocation -= elf_gp (info->output_bfd);
5928
0
      }
5929
0
        }
5930
0
      else
5931
0
        {
5932
0
    if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5933
0
      {
5934
        /* TLS model workaround shall be applied.  */
5935
0
        BFD_ASSERT(0);
5936
0
      }
5937
0
    else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5938
0
      {
5939
0
        if (eff_tls_type & GOT_TLS_IEGP)
5940
0
          relocation -= elf_gp(info->output_bfd);
5941
5942
0
        if (need_relocs)
5943
0
          {
5944
0
      if (indx == 0)
5945
0
        outrel.r_addend = gottpoff (info, relocation_sym);
5946
0
      else
5947
0
        outrel.r_addend = 0;
5948
0
      outrel.r_offset = (sgot->output_section->vma
5949
0
             + sgot->output_offset + off);
5950
0
      outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5951
5952
0
      elf32_nds32_add_dynreloc (info->output_bfd, info,
5953
0
              srelgot, &outrel);
5954
0
          }
5955
0
        else
5956
0
          {
5957
0
      bfd_put_32 (info->output_bfd,
5958
0
            gottpoff (info, relocation_sym),
5959
0
            sgot->contents + off);
5960
0
          }
5961
0
      }
5962
0
    else if (eff_tls_type & GOT_TLS_DESC)
5963
0
      {
5964
0
        relocation -= elf_gp (info->output_bfd);
5965
0
        if (need_relocs)
5966
0
          {
5967
0
      if (indx == 0)
5968
0
        outrel.r_addend = gottpoff (info, relocation_sym);
5969
0
      else
5970
0
        outrel.r_addend = 0;
5971
0
      outrel.r_offset = (sgot->output_section->vma
5972
0
             + sgot->output_offset + off);
5973
0
      outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
5974
5975
0
      if (htab->tls_desc_trampoline)
5976
0
        {
5977
0
          asection *srelplt;
5978
0
          srelplt = ehtab->srelplt;
5979
0
          loc = srelplt->contents;
5980
0
          loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
5981
0
          BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
5982
0
          <= srelplt->contents + srelplt->size);
5983
5984
0
          bfd_elf32_swap_reloca_out (info->output_bfd,
5985
0
                   &outrel, loc);
5986
0
        }
5987
0
      else
5988
0
        {
5989
0
          loc = srelgot->contents;
5990
0
          loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
5991
0
          bfd_elf32_swap_reloca_out (info->output_bfd,
5992
0
                   &outrel, loc);
5993
0
          ++srelgot->reloc_count;
5994
0
        }
5995
0
          }
5996
0
        else
5997
0
          {
5998
      /* feed me!  */
5999
0
      bfd_put_32 (info->output_bfd, 0xdeadbeef,
6000
0
            sgot->contents + off);
6001
0
      bfd_put_32 (info->output_bfd,
6002
0
            gottpoff (info, relocation_sym),
6003
0
            sgot->contents + off + 4);
6004
0
      patch_tls_desc_to_ie (contents, rel, input_bfd);
6005
0
      BFD_ASSERT(0);
6006
0
          }
6007
0
      }
6008
0
    else
6009
0
      {
6010
        /* TLS model workaround shall be applied.  */
6011
0
        BFD_ASSERT(0);
6012
0
      }
6013
6014
0
    if (h != NULL)
6015
0
      h->got.offset |= 1;
6016
0
    else
6017
0
      local_got_offsets[r_symndx] |= 1;
6018
0
        }
6019
0
    }
6020
0
  break;
6021
    /* DON'T fall through.  */
6022
6023
0
  default:
6024
    /* OLD_NDS32_RELOC.  */
6025
6026
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6027
0
          contents, offset, relocation, addend);
6028
0
    goto check_reloc;
6029
0
  }
6030
6031
0
      switch ((int) r_type)
6032
0
  {
6033
0
  case R_NDS32_20_RELA:
6034
0
  case R_NDS32_5_RELA:
6035
0
  case R_NDS32_9_PCREL_RELA:
6036
0
  case R_NDS32_WORD_9_PCREL_RELA:
6037
0
  case R_NDS32_10_UPCREL_RELA:
6038
0
  case R_NDS32_15_PCREL_RELA:
6039
0
  case R_NDS32_17_PCREL_RELA:
6040
0
  case R_NDS32_25_PCREL_RELA:
6041
0
  case R_NDS32_25_ABS_RELA:
6042
0
  case R_NDS32_HI20_RELA:
6043
0
  case R_NDS32_LO12S3_RELA:
6044
0
  case R_NDS32_LO12S2_RELA:
6045
0
  case R_NDS32_LO12S2_DP_RELA:
6046
0
  case R_NDS32_LO12S2_SP_RELA:
6047
0
  case R_NDS32_LO12S1_RELA:
6048
0
  case R_NDS32_LO12S0_RELA:
6049
0
  case R_NDS32_LO12S0_ORI_RELA:
6050
0
  case R_NDS32_SDA16S3_RELA:
6051
0
  case R_NDS32_SDA17S2_RELA:
6052
0
  case R_NDS32_SDA18S1_RELA:
6053
0
  case R_NDS32_SDA19S0_RELA:
6054
0
  case R_NDS32_SDA15S3_RELA:
6055
0
  case R_NDS32_SDA15S2_RELA:
6056
0
  case R_NDS32_SDA12S2_DP_RELA:
6057
0
  case R_NDS32_SDA12S2_SP_RELA:
6058
0
  case R_NDS32_SDA15S1_RELA:
6059
0
  case R_NDS32_SDA15S0_RELA:
6060
0
  case R_NDS32_SDA_FP7U2_RELA:
6061
0
  case R_NDS32_9_PLTREL:
6062
0
  case R_NDS32_25_PLTREL:
6063
0
  case R_NDS32_GOT20:
6064
0
  case R_NDS32_GOT_HI20:
6065
0
  case R_NDS32_GOT_LO12:
6066
0
  case R_NDS32_GOT_LO15:
6067
0
  case R_NDS32_GOT_LO19:
6068
0
  case R_NDS32_GOT15S2_RELA:
6069
0
  case R_NDS32_GOT17S2_RELA:
6070
0
  case R_NDS32_GOTPC20:
6071
0
  case R_NDS32_GOTPC_HI20:
6072
0
  case R_NDS32_GOTPC_LO12:
6073
0
  case R_NDS32_GOTOFF:
6074
0
  case R_NDS32_GOTOFF_HI20:
6075
0
  case R_NDS32_GOTOFF_LO12:
6076
0
  case R_NDS32_GOTOFF_LO15:
6077
0
  case R_NDS32_GOTOFF_LO19:
6078
0
  case R_NDS32_PLTREL_HI20:
6079
0
  case R_NDS32_PLTREL_LO12:
6080
0
  case R_NDS32_PLT_GOTREL_HI20:
6081
0
  case R_NDS32_PLT_GOTREL_LO12:
6082
0
  case R_NDS32_PLT_GOTREL_LO15:
6083
0
  case R_NDS32_PLT_GOTREL_LO19:
6084
0
  case R_NDS32_PLT_GOTREL_LO20:
6085
0
  case R_NDS32_17IFC_PCREL_RELA:
6086
0
  case R_NDS32_10IFCU_PCREL_RELA:
6087
0
  case R_NDS32_TLS_LE_HI20:
6088
0
  case R_NDS32_TLS_LE_LO12:
6089
0
  case R_NDS32_TLS_IE_HI20:
6090
0
  case R_NDS32_TLS_IE_LO12S2:
6091
0
  case R_NDS32_TLS_LE_20:
6092
0
  case R_NDS32_TLS_LE_15S0:
6093
0
  case R_NDS32_TLS_LE_15S1:
6094
0
  case R_NDS32_TLS_LE_15S2:
6095
0
  case R_NDS32_TLS_DESC_HI20:
6096
0
  case R_NDS32_TLS_DESC_LO12:
6097
0
  case R_NDS32_TLS_IE_LO12:
6098
0
  case R_NDS32_TLS_IEGP_HI20:
6099
0
  case R_NDS32_TLS_IEGP_LO12:
6100
0
  case R_NDS32_TLS_IEGP_LO12S2:
6101
    /* Instruction related relocs must handle endian properly.  */
6102
    /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER.  */
6103
0
    r = nds32_elf_final_link_relocate (howto, input_bfd,
6104
0
               input_section, contents,
6105
0
               rel->r_offset, relocation,
6106
0
               rel->r_addend);
6107
0
    break;
6108
6109
0
  default:
6110
    /* All other relocs can use default handler.  */
6111
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6112
0
          contents, rel->r_offset,
6113
0
          relocation, rel->r_addend);
6114
0
    break;
6115
0
  }
6116
6117
0
    check_reloc:
6118
6119
0
      if (r != bfd_reloc_ok)
6120
0
  {
6121
    /* FIXME: This should be generic enough to go in a utility.  */
6122
0
    const char *name;
6123
6124
0
    if (h != NULL)
6125
0
      name = h->root.root.string;
6126
0
    else
6127
0
      {
6128
0
        name = bfd_elf_string_from_elf_section
6129
0
          (input_bfd, symtab_hdr->sh_link, sym->st_name);
6130
0
        if (name == NULL || *name == '\0')
6131
0
    name = bfd_section_name (sec);
6132
0
      }
6133
6134
0
    if (errmsg != NULL)
6135
0
      goto common_error;
6136
6137
0
    switch (r)
6138
0
      {
6139
0
      case bfd_reloc_overflow:
6140
0
        (*info->callbacks->reloc_overflow)
6141
0
    (info, (h ? &h->root : NULL), name, howto->name,
6142
0
     (bfd_vma) 0, input_bfd, input_section, offset);
6143
0
        break;
6144
6145
0
      case bfd_reloc_undefined:
6146
0
        (*info->callbacks->undefined_symbol)
6147
0
    (info, name, input_bfd, input_section, offset, true);
6148
0
        break;
6149
6150
0
      case bfd_reloc_outofrange:
6151
0
        errmsg = _("internal error: out of range error");
6152
0
        goto common_error;
6153
6154
0
      case bfd_reloc_notsupported:
6155
0
        errmsg = _("internal error: unsupported relocation error");
6156
0
        goto common_error;
6157
6158
0
      case bfd_reloc_dangerous:
6159
0
        errmsg = _("internal error: dangerous error");
6160
0
        goto common_error;
6161
6162
0
      default:
6163
0
        errmsg = _("internal error: unknown error");
6164
        /* Fall through.  */
6165
6166
0
      common_error:
6167
0
        (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6168
0
             input_section, offset);
6169
0
        break;
6170
0
      }
6171
0
  }
6172
0
    }
6173
6174
  /* Resotre header size to avoid overflow load.  */
6175
0
  if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6176
0
    symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6177
6178
0
  return ret;
6179
0
}
6180
6181
/* Finish up dynamic symbol handling.  We set the contents of various
6182
   dynamic sections here.  */
6183
6184
static bool
6185
nds32_elf_finish_dynamic_symbol (struct bfd_link_info *info,
6186
         struct elf_link_hash_entry *h,
6187
         Elf_Internal_Sym *sym)
6188
0
{
6189
0
  struct elf_link_hash_table *ehtab;
6190
0
  struct elf_nds32_link_hash_entry *hent;
6191
0
  bfd_byte *loc;
6192
6193
0
  ehtab = elf_hash_table (info);
6194
0
  hent = (struct elf_nds32_link_hash_entry *) h;
6195
6196
0
  if (h->plt.offset != (bfd_vma) - 1)
6197
0
    {
6198
0
      asection *splt;
6199
0
      asection *sgot;
6200
0
      asection *srela;
6201
6202
0
      bfd_vma plt_index;
6203
0
      bfd_vma got_offset;
6204
0
      bfd_vma local_plt_offset;
6205
0
      Elf_Internal_Rela rela;
6206
6207
      /* This symbol has an entry in the procedure linkage table.  Set
6208
   it up.  */
6209
6210
0
      BFD_ASSERT (h->dynindx != -1);
6211
6212
0
      splt = ehtab->splt;
6213
0
      sgot = ehtab->sgotplt;
6214
0
      srela = ehtab->srelplt;
6215
0
      BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6216
6217
      /* Get the index in the procedure linkage table which
6218
   corresponds to this symbol.  This is the index of this symbol
6219
   in all the symbols for which we are making plt entries.  The
6220
   first entry in the procedure linkage table is reserved.  */
6221
0
      plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6222
6223
      /* Get the offset into the .got table of the entry that
6224
   corresponds to this function.  Each .got entry is 4 bytes.
6225
   The first three are reserved.  */
6226
0
      got_offset = (plt_index + 3) * 4;
6227
6228
      /* Fill in the entry in the procedure linkage table.  */
6229
0
      if (!bfd_link_pic (info))
6230
0
  {
6231
0
    unsigned long insn;
6232
6233
0
    insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6234
0
              + sgot->output_offset + got_offset) >> 12)
6235
0
            & 0xfffff);
6236
0
    bfd_putb32 (insn, splt->contents + h->plt.offset);
6237
6238
0
    insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6239
0
              + sgot->output_offset + got_offset) & 0x0fff)
6240
0
            >> 2);
6241
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6242
6243
0
    insn = PLT_ENTRY_WORD2;
6244
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6245
6246
0
    insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6247
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6248
6249
0
    insn = PLT_ENTRY_WORD4
6250
0
     + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6251
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6252
0
    local_plt_offset = 12;
6253
0
  }
6254
0
      else
6255
0
  {
6256
    /* sda_base must be set at this time.  */
6257
0
    unsigned long insn;
6258
0
    long offset;
6259
6260
0
    offset = sgot->output_section->vma + sgot->output_offset + got_offset
6261
0
       - elf_gp (info->output_bfd);
6262
0
    insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6263
0
    bfd_putb32 (insn, splt->contents + h->plt.offset);
6264
6265
0
    insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6266
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6267
6268
0
    insn = PLT_PIC_ENTRY_WORD2;
6269
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6270
6271
0
    insn = PLT_PIC_ENTRY_WORD3;
6272
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6273
6274
0
    insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6275
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6276
6277
0
    insn = PLT_PIC_ENTRY_WORD5
6278
0
      + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6279
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6280
6281
0
    local_plt_offset = 16;
6282
0
  }
6283
6284
      /* Fill in the entry in the global offset table,
6285
   so it will fall through to the next instruction for the first time.  */
6286
0
      bfd_put_32 (info->output_bfd,
6287
0
      (splt->output_section->vma + splt->output_offset
6288
0
       + h->plt.offset + local_plt_offset),
6289
0
      sgot->contents + got_offset);
6290
6291
      /* Fill in the entry in the .rela.plt section.  */
6292
0
      rela.r_offset = (sgot->output_section->vma
6293
0
           + sgot->output_offset + got_offset);
6294
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6295
0
      rela.r_addend = 0;
6296
0
      loc = srela->contents;
6297
0
      loc += plt_index * sizeof (Elf32_External_Rela);
6298
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
6299
6300
0
      if (!h->def_regular)
6301
0
  {
6302
    /* Mark the symbol as undefined, rather than as defined in
6303
       the .plt section.  Leave the value alone.  */
6304
0
    sym->st_shndx = SHN_UNDEF;
6305
0
    if (!h->ref_regular_nonweak)
6306
0
      sym->st_value = 0;
6307
0
  }
6308
0
    }
6309
6310
0
  if (h->got.offset != (bfd_vma) - 1
6311
0
      && hent->tls_type == GOT_NORMAL)
6312
0
    {
6313
0
      asection *sgot;
6314
0
      asection *srelagot;
6315
0
      Elf_Internal_Rela rela;
6316
6317
      /* This symbol has an entry in the global offset table.
6318
   Set it up.  */
6319
6320
0
      sgot = ehtab->sgot;
6321
0
      srelagot = ehtab->srelgot;
6322
0
      BFD_ASSERT (sgot != NULL && srelagot != NULL);
6323
6324
0
      rela.r_offset = (sgot->output_section->vma
6325
0
           + sgot->output_offset + (h->got.offset & ~1));
6326
6327
      /* If this is a -Bsymbolic link, and the symbol is defined
6328
   locally, we just want to emit a RELATIVE reloc.  Likewise if
6329
   the symbol was forced to be local because of a version file.
6330
   The entry in the global offset table will already have been
6331
   initialized in the relocate_section function.  */
6332
0
      if ((bfd_link_pic (info)
6333
0
     && (info->symbolic || h->dynindx == -1 || h->forced_local)
6334
0
     && h->def_regular)
6335
0
    || (bfd_link_pie (info) && h->def_regular))
6336
0
  {
6337
0
    rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6338
0
    rela.r_addend = (h->root.u.def.value
6339
0
         + h->root.u.def.section->output_section->vma
6340
0
         + h->root.u.def.section->output_offset);
6341
6342
0
    if ((h->got.offset & 1) == 0)
6343
0
      {
6344
0
        bfd_put_32 (info->output_bfd, rela.r_addend,
6345
0
        sgot->contents + h->got.offset);
6346
0
      }
6347
0
  }
6348
0
      else
6349
0
  {
6350
0
    BFD_ASSERT ((h->got.offset & 1) == 0);
6351
0
    bfd_put_32 (info->output_bfd, (bfd_vma) 0,
6352
0
          sgot->contents + h->got.offset);
6353
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6354
0
    rela.r_addend = 0;
6355
0
  }
6356
6357
0
      loc = srelagot->contents;
6358
0
      loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6359
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
6360
0
      ++srelagot->reloc_count;
6361
0
      BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6362
0
    }
6363
6364
0
  if (h->needs_copy)
6365
0
    {
6366
0
      asection *s;
6367
0
      Elf_Internal_Rela rela;
6368
6369
      /* This symbols needs a copy reloc.  Set it up.  */
6370
6371
0
      BFD_ASSERT (h->dynindx != -1
6372
0
      && (h->root.type == bfd_link_hash_defined
6373
0
          || h->root.type == bfd_link_hash_defweak));
6374
6375
0
      s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6376
0
      BFD_ASSERT (s != NULL);
6377
6378
0
      rela.r_offset = (h->root.u.def.value
6379
0
           + h->root.u.def.section->output_section->vma
6380
0
           + h->root.u.def.section->output_offset);
6381
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6382
0
      rela.r_addend = 0;
6383
0
      loc = s->contents;
6384
0
      loc += s->reloc_count * sizeof (Elf32_External_Rela);
6385
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
6386
0
      ++s->reloc_count;
6387
0
    }
6388
6389
  /* Mark some specially defined symbols as absolute.  */
6390
0
  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6391
0
      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6392
0
    sym->st_shndx = SHN_ABS;
6393
6394
0
  return true;
6395
0
}
6396
6397
6398
/* Finish up the dynamic sections.  */
6399
6400
static bool
6401
nds32_elf_finish_dynamic_sections (struct bfd_link_info *info,
6402
           bfd_byte *buf ATTRIBUTE_UNUSED)
6403
0
{
6404
0
  bfd *dynobj;
6405
0
  asection *sdyn;
6406
0
  asection *sgotplt;
6407
0
  struct elf_link_hash_table *ehtab;
6408
0
  struct elf_nds32_link_hash_table *htab;
6409
6410
0
  ehtab = elf_hash_table (info);
6411
0
  htab = nds32_elf_hash_table (info);
6412
0
  if (htab == NULL)
6413
0
    return false;
6414
6415
0
  dynobj = elf_hash_table (info)->dynobj;
6416
6417
0
  sgotplt = ehtab->sgotplt;
6418
  /* A broken linker script might have discarded the dynamic sections.
6419
     Catch this here so that we do not seg-fault later on.  */
6420
0
  if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6421
0
    return false;
6422
0
  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6423
6424
0
  if (elf_hash_table (info)->dynamic_sections_created)
6425
0
    {
6426
0
      asection *splt;
6427
0
      Elf32_External_Dyn *dyncon, *dynconend;
6428
6429
0
      BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6430
6431
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
6432
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6433
6434
0
      for (; dyncon < dynconend; dyncon++)
6435
0
  {
6436
0
    Elf_Internal_Dyn dyn;
6437
0
    asection *s;
6438
6439
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6440
6441
0
    switch (dyn.d_tag)
6442
0
      {
6443
0
      default:
6444
0
        break;
6445
6446
0
      case DT_PLTGOT:
6447
        /* name = ".got";  */
6448
0
        s = ehtab->sgot;
6449
0
        goto get_vma;
6450
0
      case DT_JMPREL:
6451
0
        s = ehtab->srelplt;
6452
0
      get_vma:
6453
0
        BFD_ASSERT (s != NULL);
6454
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6455
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6456
0
        break;
6457
6458
0
      case DT_PLTRELSZ:
6459
0
        s = ehtab->srelplt;
6460
0
        BFD_ASSERT (s != NULL);
6461
0
        dyn.d_un.d_val = s->size;
6462
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6463
0
        break;
6464
6465
0
      case DT_RELASZ:
6466
        /* My reading of the SVR4 ABI indicates that the
6467
     procedure linkage table relocs (DT_JMPREL) should be
6468
     included in the overall relocs (DT_RELA).  This is
6469
     what Solaris does.  However, UnixWare can not handle
6470
     that case.  Therefore, we override the DT_RELASZ entry
6471
     here to make it not include the JMPREL relocs.  Since
6472
     the linker script arranges for .rela.plt to follow all
6473
     other relocation sections, we don't have to worry
6474
     about changing the DT_RELA entry.  */
6475
0
        if (ehtab->srelplt != NULL)
6476
0
    {
6477
0
      s = ehtab->srelplt;
6478
0
      dyn.d_un.d_val -= s->size;
6479
0
    }
6480
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6481
0
        break;
6482
6483
0
      case DT_TLSDESC_PLT:
6484
0
        s = htab->root.splt;
6485
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6486
0
        + htab->root.tlsdesc_plt);
6487
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6488
0
        break;
6489
6490
0
      case DT_TLSDESC_GOT:
6491
0
        s = htab->root.sgot;
6492
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6493
0
        + htab->root.tlsdesc_got);
6494
0
        bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
6495
0
        break;
6496
0
      }
6497
0
  }
6498
6499
      /* Fill in the first entry in the procedure linkage table.  */
6500
0
      splt = ehtab->splt;
6501
0
      if (splt && splt->size > 0)
6502
0
  {
6503
0
    if (bfd_link_pic (info))
6504
0
      {
6505
0
        unsigned long insn;
6506
0
        long offset;
6507
6508
0
        offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6509
0
    - elf_gp (info->output_bfd);
6510
0
        insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6511
0
        bfd_putb32 (insn, splt->contents);
6512
6513
        /* here has a typo?  */
6514
0
        insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6515
0
        bfd_putb32 (insn, splt->contents + 4);
6516
6517
0
        insn = PLT0_PIC_ENTRY_WORD2;
6518
0
        bfd_putb32 (insn, splt->contents + 8);
6519
6520
0
        insn = PLT0_PIC_ENTRY_WORD3;
6521
0
        bfd_putb32 (insn, splt->contents + 12);
6522
6523
0
        insn = PLT0_PIC_ENTRY_WORD4;
6524
0
        bfd_putb32 (insn, splt->contents + 16);
6525
6526
0
        insn = PLT0_PIC_ENTRY_WORD5;
6527
0
        bfd_putb32 (insn, splt->contents + 20);
6528
0
      }
6529
0
    else
6530
0
      {
6531
0
        unsigned long insn;
6532
0
        unsigned long addr;
6533
6534
        /* addr = .got + 4 */
6535
0
        addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6536
0
        insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6537
0
        bfd_putb32 (insn, splt->contents);
6538
6539
0
        insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6540
0
        bfd_putb32 (insn, splt->contents + 4);
6541
6542
0
        insn = PLT0_ENTRY_WORD2;
6543
0
        bfd_putb32 (insn, splt->contents + 8);
6544
6545
0
        insn = PLT0_ENTRY_WORD3;
6546
0
        bfd_putb32 (insn, splt->contents + 12);
6547
6548
0
        insn = PLT0_ENTRY_WORD4;
6549
0
        bfd_putb32 (insn, splt->contents + 16);
6550
0
      }
6551
6552
0
    elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6553
0
      PLT_ENTRY_SIZE;
6554
0
  }
6555
6556
0
      if (htab->root.tlsdesc_plt)
6557
0
  {
6558
    /* Calculate addresses.  */
6559
0
    asection *sgot = sgot = ehtab->sgot;
6560
0
    bfd_vma pltgot = sgotplt->output_section->vma
6561
0
      + sgotplt->output_offset;
6562
0
    bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6563
0
      + htab->root.tlsdesc_got;
6564
6565
    /* Get GP offset.  */
6566
0
    pltgot -= elf_gp (info->output_bfd) - 4; /* PLTGOT[1]  */
6567
0
    tlsdesc_got -= elf_gp (info->output_bfd);
6568
6569
    /* Do relocation.  */
6570
0
    dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6571
0
    dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6572
0
    dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6573
0
    dl_tlsdesc_lazy_trampoline[5] +=  0xfff & pltgot;
6574
6575
    /* Insert .plt.  */
6576
0
    nds32_put_trampoline (splt->contents + htab->root.tlsdesc_plt,
6577
0
        dl_tlsdesc_lazy_trampoline,
6578
0
        ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6579
0
  }
6580
0
    }
6581
6582
  /* Fill in the first three entries in the global offset table.  */
6583
0
  if (sgotplt && sgotplt->size > 0)
6584
0
    {
6585
0
      if (sdyn == NULL)
6586
0
  bfd_put_32 (info->output_bfd, 0, sgotplt->contents);
6587
0
      else
6588
0
  bfd_put_32 (info->output_bfd,
6589
0
        sdyn->output_section->vma + sdyn->output_offset,
6590
0
        sgotplt->contents);
6591
0
      bfd_put_32 (info->output_bfd, 0, sgotplt->contents + 4);
6592
0
      bfd_put_32 (info->output_bfd, 0, sgotplt->contents + 8);
6593
6594
0
      elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6595
0
    }
6596
6597
0
  return true;
6598
0
}
6599

6600
6601
/* Set the right machine number.  */
6602
6603
static bool
6604
nds32_elf_object_p (bfd *abfd)
6605
1.65k
{
6606
1.65k
  static unsigned int cur_arch = 0;
6607
6608
1.65k
  if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6609
1.61k
    {
6610
      /* E_N1_ARCH is a wild card, so it is set only when no others exist.  */
6611
1.61k
      cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6612
1.61k
    }
6613
6614
1.65k
  switch (cur_arch)
6615
1.65k
    {
6616
1.54k
    default:
6617
1.54k
    case E_N1_ARCH:
6618
1.54k
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6619
1.54k
      break;
6620
15
    case E_N1H_ARCH:
6621
15
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6622
15
      break;
6623
66
    case E_NDS_ARCH_STAR_V2_0:
6624
66
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6625
66
      break;
6626
11
    case E_NDS_ARCH_STAR_V3_0:
6627
11
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6628
11
      break;
6629
20
    case E_NDS_ARCH_STAR_V3_M:
6630
20
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6631
20
      break;
6632
1.65k
    }
6633
6634
1.65k
  return true;
6635
1.65k
}
6636
6637
/* Store the machine number in the flags field.  */
6638
6639
static bool
6640
nds32_elf_final_write_processing (bfd *abfd)
6641
13
{
6642
13
  unsigned long val;
6643
13
  static unsigned int cur_mach = 0;
6644
6645
13
  if (bfd_mach_n1 != bfd_get_mach (abfd))
6646
1
    {
6647
1
      cur_mach = bfd_get_mach (abfd);
6648
1
    }
6649
6650
13
  switch (cur_mach)
6651
13
    {
6652
0
    case bfd_mach_n1:
6653
      /* Only happen when object is empty, since the case is abandon.  */
6654
0
      val = E_N1_ARCH;
6655
0
      val |= E_NDS_ABI_AABI;
6656
0
      val |= E_NDS32_ELF_VER_1_4;
6657
0
      break;
6658
0
    case bfd_mach_n1h:
6659
0
      val = E_N1H_ARCH;
6660
0
      break;
6661
0
    case bfd_mach_n1h_v2:
6662
0
      val = E_NDS_ARCH_STAR_V2_0;
6663
0
      break;
6664
0
    case bfd_mach_n1h_v3:
6665
0
      val = E_NDS_ARCH_STAR_V3_0;
6666
0
      break;
6667
13
    case bfd_mach_n1h_v3m:
6668
13
      val = E_NDS_ARCH_STAR_V3_M;
6669
13
      break;
6670
0
    default:
6671
0
      val = 0;
6672
0
      break;
6673
13
    }
6674
6675
13
  elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6676
13
  elf_elfheader (abfd)->e_flags |= val;
6677
13
  return _bfd_elf_final_write_processing (abfd);
6678
13
}
6679
6680
/* Function to keep NDS32 specific file flags.  */
6681
6682
static bool
6683
nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6684
0
{
6685
0
  BFD_ASSERT (!elf_flags_init (abfd)
6686
0
        || elf_elfheader (abfd)->e_flags == flags);
6687
6688
0
  elf_elfheader (abfd)->e_flags = flags;
6689
0
  elf_flags_init (abfd) = true;
6690
0
  return true;
6691
0
}
6692
6693
static unsigned int
6694
convert_e_flags (unsigned int e_flags, unsigned int arch)
6695
0
{
6696
0
  if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6697
0
    {
6698
      /* From 0.9 to 1.0.  */
6699
0
      e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6700
6701
      /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6702
0
      e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6703
0
      if (arch == E_NDS_ARCH_STAR_V1_0)
6704
0
  {
6705
    /* Done.  */
6706
0
    return e_flags;
6707
0
  }
6708
0
    }
6709
6710
  /* From 1.0 to 2.0.  */
6711
0
  e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6712
6713
  /* Clear E_NDS32_HAS_MFUSR_PC_INST.  */
6714
0
  e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6715
6716
  /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6717
0
  e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6718
0
  return e_flags;
6719
0
}
6720
6721
static bool
6722
nds32_check_vec_size (bfd *ibfd)
6723
0
{
6724
0
  static unsigned int nds32_vec_size = 0;
6725
6726
0
  asection *sec_t = NULL;
6727
0
  bfd_byte *contents = NULL;
6728
6729
0
  sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6730
6731
0
  if (sec_t && sec_t->size >= 4)
6732
0
    {
6733
      /* Get vec_size in file.  */
6734
0
      unsigned int flag_t;
6735
6736
0
      nds32_get_section_contents (ibfd, sec_t, &contents, true);
6737
0
      flag_t = bfd_get_32 (ibfd, contents);
6738
6739
      /* The value could only be 4 or 16.  */
6740
6741
0
      if (!nds32_vec_size)
6742
  /* Set if not set yet.  */
6743
0
  nds32_vec_size = (flag_t & 0x3);
6744
0
      else if (nds32_vec_size != (flag_t & 0x3))
6745
0
  {
6746
0
    _bfd_error_handler
6747
      /* xgettext:c-format */
6748
0
      (_("%pB: ISR vector size mismatch"
6749
0
         " with previous modules, previous %u-byte, current %u-byte"),
6750
0
       ibfd,
6751
0
       nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6752
0
       (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6753
0
    return false;
6754
0
  }
6755
0
      else
6756
  /* Only keep the first vec_size section.  */
6757
0
  sec_t->flags |= SEC_EXCLUDE;
6758
0
    }
6759
6760
0
  return true;
6761
0
}
6762
6763
/* Merge backend specific data from an object file to the output
6764
   object file when linking.  */
6765
6766
static bool
6767
nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6768
0
{
6769
0
  bfd *obfd = info->output_bfd;
6770
0
  flagword out_flags;
6771
0
  flagword in_flags;
6772
0
  flagword out_16regs;
6773
0
  flagword in_no_mac;
6774
0
  flagword out_no_mac;
6775
0
  flagword in_16regs;
6776
0
  flagword out_version;
6777
0
  flagword in_version;
6778
0
  flagword out_fpu_config;
6779
0
  flagword in_fpu_config;
6780
6781
  /* FIXME: What should be checked when linking shared libraries?  */
6782
0
  if ((ibfd->flags & DYNAMIC) != 0)
6783
0
    return true;
6784
6785
  /* TODO: Revise to use object-attributes instead.  */
6786
0
  if (!nds32_check_vec_size (ibfd))
6787
0
    return false;
6788
6789
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
6790
0
    return true;
6791
6792
0
  if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6793
0
    {
6794
0
      _bfd_error_handler
6795
0
  (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6796
6797
0
      bfd_set_error (bfd_error_bad_value);
6798
0
      return false;
6799
0
    }
6800
6801
  /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6802
     treat as generic one without checking and merging.  */
6803
0
  if (elf_elfheader (ibfd)->e_flags)
6804
0
    {
6805
0
      in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6806
0
      if (in_version == E_NDS32_ELF_VER_1_2)
6807
0
  {
6808
0
    _bfd_error_handler
6809
0
      (_("%pB: warning: older version of object file encountered, "
6810
0
         "please recompile with current tool chain"), ibfd);
6811
0
  }
6812
6813
      /* We may need to merge V1 and V2 arch object files to V2.  */
6814
0
      if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6815
0
    != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6816
0
  {
6817
    /* Need to convert version.  */
6818
0
    if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6819
0
        == E_NDS_ARCH_STAR_RESERVED)
6820
0
      {
6821
0
        elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6822
0
      }
6823
0
    else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6824
0
       == E_NDS_ARCH_STAR_V3_M
6825
0
       && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6826
0
       == E_NDS_ARCH_STAR_V3_0)
6827
0
      {
6828
0
        elf_elfheader (ibfd)->e_flags =
6829
0
    (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6830
0
    | E_NDS_ARCH_STAR_V3_0;
6831
0
      }
6832
0
    else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6833
0
       == E_NDS_ARCH_STAR_V0_9
6834
0
       || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6835
0
       > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6836
0
      {
6837
0
        elf_elfheader (obfd)->e_flags =
6838
0
    convert_e_flags (elf_elfheader (obfd)->e_flags,
6839
0
         (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6840
0
      }
6841
0
    else
6842
0
      {
6843
0
        elf_elfheader (ibfd)->e_flags =
6844
0
    convert_e_flags (elf_elfheader (ibfd)->e_flags,
6845
0
         (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6846
0
      }
6847
0
  }
6848
6849
      /* Extract some flags.  */
6850
0
      in_flags = elf_elfheader (ibfd)->e_flags
6851
0
  & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6852
0
       | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6853
6854
      /* The following flags need special treatment.  */
6855
0
      in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6856
0
      in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6857
0
      in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6858
6859
      /* Extract some flags.  */
6860
0
      out_flags = elf_elfheader (obfd)->e_flags
6861
0
  & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6862
0
       | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6863
6864
      /* The following flags need special treatment.  */
6865
0
      out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6866
0
      out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6867
0
      out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6868
0
      out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6869
0
      if (!elf_flags_init (obfd))
6870
0
  {
6871
    /* If the input is the default architecture then do not
6872
       bother setting the flags for the output architecture,
6873
       instead allow future merges to do this.  If no future
6874
       merges ever set these flags then they will retain their
6875
       unitialised values, which surprise surprise, correspond
6876
       to the default values.  */
6877
0
    if (bfd_get_arch_info (ibfd)->the_default)
6878
0
      return true;
6879
6880
0
    elf_flags_init (obfd) = true;
6881
0
    elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6882
6883
0
    if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6884
0
        && bfd_get_arch_info (obfd)->the_default)
6885
0
      {
6886
0
        return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6887
0
          bfd_get_mach (ibfd));
6888
0
      }
6889
6890
0
    return true;
6891
0
  }
6892
6893
      /* Check flag compatibility.  */
6894
0
      if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6895
0
  {
6896
0
    _bfd_error_handler
6897
0
      (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6898
0
    bfd_set_error (bfd_error_bad_value);
6899
0
    return false;
6900
0
  }
6901
6902
0
      if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6903
0
  {
6904
0
    if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6905
0
      {
6906
0
        _bfd_error_handler
6907
0
    (_("%pB: error: instruction set mismatch with previous modules"),
6908
0
     ibfd);
6909
6910
0
        bfd_set_error (bfd_error_bad_value);
6911
0
        return false;
6912
0
      }
6913
0
  }
6914
6915
      /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6916
   and perf ext1 and DIV are mergerd to perf ext1.  */
6917
0
      if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6918
0
  {
6919
0
    elf_elfheader (obfd)->e_flags =
6920
0
      (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6921
0
      | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6922
0
      | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6923
0
         ?  E_NDS32_HAS_EXT_INST : 0)
6924
0
      | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6925
0
         ?  E_NDS32_HAS_EXT_INST : 0)
6926
0
      | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6927
0
      | ((in_version > out_version) ? out_version : in_version);
6928
0
  }
6929
0
      else
6930
0
  {
6931
0
    if (in_version != out_version)
6932
0
      _bfd_error_handler
6933
        /* xgettext:c-format */
6934
0
        (_("%pB: warning: incompatible elf-versions %s and %s"),
6935
0
         ibfd, nds32_elfver_strtab[out_version],
6936
0
         nds32_elfver_strtab[in_version]);
6937
6938
0
    elf_elfheader (obfd)->e_flags = in_flags | out_flags
6939
0
      | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6940
0
      | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6941
0
      | (in_version > out_version ?  out_version : in_version);
6942
0
  }
6943
0
    }
6944
6945
0
  return true;
6946
0
}
6947
6948
/* Display the flags field.  */
6949
6950
static bool
6951
nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6952
345
{
6953
345
  FILE *file = (FILE *) ptr;
6954
6955
345
  BFD_ASSERT (abfd != NULL && ptr != NULL);
6956
6957
345
  _bfd_elf_print_private_bfd_data (abfd, ptr);
6958
6959
345
  fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6960
6961
345
  switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6962
345
    {
6963
345
    default:
6964
345
    case E_N1_ARCH:
6965
345
      fprintf (file, _(": n1 instructions"));
6966
345
      break;
6967
0
    case E_N1H_ARCH:
6968
0
      fprintf (file, _(": n1h instructions"));
6969
0
      break;
6970
345
    }
6971
6972
345
  fputc ('\n', file);
6973
6974
345
  return true;
6975
345
}
6976
6977
static unsigned int
6978
nds32_elf_action_discarded (asection *sec)
6979
0
{
6980
6981
0
  if (startswith (sec->name, ".gcc_except_table"))
6982
0
    return 0;
6983
6984
0
  return _bfd_elf_default_action_discarded (sec);
6985
0
}
6986
6987
static asection *
6988
nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6989
      struct elf_reloc_cookie *cookie,
6990
      struct elf_link_hash_entry *h,
6991
      unsigned int symndx)
6992
0
{
6993
0
  if (h != NULL)
6994
0
    switch (ELF32_R_TYPE (cookie->rel->r_info))
6995
0
      {
6996
0
      case R_NDS32_GNU_VTINHERIT:
6997
0
      case R_NDS32_GNU_VTENTRY:
6998
0
      case R_NDS32_RELA_GNU_VTINHERIT:
6999
0
      case R_NDS32_RELA_GNU_VTENTRY:
7000
0
  return NULL;
7001
0
      }
7002
7003
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
7004
0
}
7005
7006
static enum elf_nds32_tls_type
7007
get_tls_type (enum elf_nds32_reloc_type r_type,
7008
        struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
7009
0
{
7010
0
  enum elf_nds32_tls_type tls_type;
7011
7012
0
  switch (r_type)
7013
0
    {
7014
0
    case R_NDS32_TLS_LE_HI20:
7015
0
    case R_NDS32_TLS_LE_LO12:
7016
0
      tls_type = GOT_TLS_LE;
7017
0
      break;
7018
0
    case R_NDS32_TLS_IE_HI20:
7019
0
    case R_NDS32_TLS_IE_LO12S2:
7020
0
    case R_NDS32_TLS_IE_LO12:
7021
0
      tls_type = GOT_TLS_IE;
7022
0
      break;
7023
0
    case R_NDS32_TLS_IEGP_HI20:
7024
0
    case R_NDS32_TLS_IEGP_LO12:
7025
0
    case R_NDS32_TLS_IEGP_LO12S2:
7026
0
      tls_type = GOT_TLS_IEGP;
7027
0
      break;
7028
0
    case R_NDS32_TLS_DESC_HI20:
7029
0
    case R_NDS32_TLS_DESC_LO12:
7030
0
    case R_NDS32_TLS_DESC_ADD:
7031
0
    case R_NDS32_TLS_DESC_FUNC:
7032
0
    case R_NDS32_TLS_DESC_CALL:
7033
0
      tls_type = GOT_TLS_DESC;
7034
0
      break;
7035
0
    default:
7036
0
      tls_type = GOT_NORMAL;
7037
0
      break;
7038
0
    }
7039
7040
0
  return tls_type;
7041
0
}
7042
7043
/* Ensure that we have allocated bookkeeping structures for ABFD's local
7044
   symbols.  */
7045
7046
static bool
7047
elf32_nds32_allocate_local_sym_info (bfd *abfd)
7048
0
{
7049
0
  if (elf_local_got_refcounts (abfd) == NULL)
7050
0
    {
7051
0
      bfd_size_type num_syms;
7052
0
      bfd_size_type size;
7053
0
      char *data;
7054
7055
0
      num_syms = elf_symtab_hdr (abfd).sh_info;
7056
      /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
7057
   gp_offset.  The details can refer to struct elf_nds32_obj_tdata.  */
7058
0
      size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
7059
0
       + sizeof (bfd_vma) + sizeof (int)
7060
0
       + sizeof (bool) + sizeof (bfd_vma));
7061
0
      data = bfd_zalloc (abfd, size);
7062
0
      if (data == NULL)
7063
0
  return false;
7064
7065
0
      elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7066
0
      data += num_syms * sizeof (bfd_signed_vma);
7067
7068
0
      elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7069
0
      data += num_syms * sizeof (char);
7070
7071
0
      elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7072
0
      data += num_syms * sizeof (bfd_vma);
7073
7074
0
      elf32_nds32_local_gp_offset (abfd) = (int *) data;
7075
0
      data += num_syms * sizeof (int);
7076
0
    }
7077
7078
0
  return true;
7079
0
}
7080
7081
/* Look through the relocs for a section during the first phase.
7082
   Since we don't do .gots or .plts, we just need to consider the
7083
   virtual table relocs for gc.  */
7084
7085
static bool
7086
nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7087
      asection *sec, const Elf_Internal_Rela *relocs)
7088
0
{
7089
0
  Elf_Internal_Shdr *symtab_hdr;
7090
0
  struct elf_link_hash_entry **sym_hashes;
7091
0
  const Elf_Internal_Rela *rel;
7092
0
  const Elf_Internal_Rela *rel_end;
7093
0
  struct elf_link_hash_table *ehtab;
7094
0
  struct elf_nds32_link_hash_table *htab;
7095
0
  bfd *dynobj;
7096
0
  asection *sreloc = NULL;
7097
7098
  /* No need for relocation if relocatable already.  */
7099
0
  if (bfd_link_relocatable (info))
7100
0
    {
7101
0
      elf32_nds32_check_relax_group (abfd, sec);
7102
0
      return true;
7103
0
    }
7104
7105
0
  symtab_hdr = &elf_symtab_hdr (abfd);
7106
0
  sym_hashes = elf_sym_hashes (abfd);
7107
7108
0
  ehtab = elf_hash_table (info);
7109
0
  htab = nds32_elf_hash_table (info);
7110
0
  dynobj = htab->root.dynobj;
7111
7112
0
  rel_end = relocs + sec->reloc_count;
7113
0
  for (rel = relocs; rel < rel_end; rel++)
7114
0
    {
7115
0
      enum elf_nds32_reloc_type r_type;
7116
0
      struct elf_link_hash_entry *h;
7117
0
      unsigned long r_symndx;
7118
0
      enum elf_nds32_tls_type tls_type, old_tls_type;
7119
7120
0
      r_symndx = ELF32_R_SYM (rel->r_info);
7121
0
      r_type = ELF32_R_TYPE (rel->r_info);
7122
0
      if (r_symndx < symtab_hdr->sh_info)
7123
0
  h = NULL;
7124
0
      else
7125
0
  {
7126
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7127
0
    while (h->root.type == bfd_link_hash_indirect
7128
0
     || h->root.type == bfd_link_hash_warning)
7129
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
7130
0
  }
7131
7132
      /* Create .got section if necessary.
7133
   Some relocs require a global offset table.  We create
7134
   got section here, since these relocation need a got section
7135
   and if it is not created yet.  */
7136
0
      if (ehtab->sgot == NULL)
7137
0
  {
7138
0
    switch (r_type)
7139
0
      {
7140
0
      case R_NDS32_GOT_HI20:
7141
0
      case R_NDS32_GOT_LO12:
7142
0
      case R_NDS32_GOT_LO15:
7143
0
      case R_NDS32_GOT_LO19:
7144
0
      case R_NDS32_GOT17S2_RELA:
7145
0
      case R_NDS32_GOT15S2_RELA:
7146
0
      case R_NDS32_GOTOFF:
7147
0
      case R_NDS32_GOTOFF_HI20:
7148
0
      case R_NDS32_GOTOFF_LO12:
7149
0
      case R_NDS32_GOTOFF_LO15:
7150
0
      case R_NDS32_GOTOFF_LO19:
7151
0
      case R_NDS32_GOTPC20:
7152
0
      case R_NDS32_GOTPC_HI20:
7153
0
      case R_NDS32_GOTPC_LO12:
7154
0
      case R_NDS32_GOT20:
7155
0
      case R_NDS32_TLS_IE_HI20:
7156
0
      case R_NDS32_TLS_IE_LO12:
7157
0
      case R_NDS32_TLS_IE_LO12S2:
7158
0
      case R_NDS32_TLS_IEGP_HI20:
7159
0
      case R_NDS32_TLS_IEGP_LO12:
7160
0
      case R_NDS32_TLS_IEGP_LO12S2:
7161
0
      case R_NDS32_TLS_DESC_HI20:
7162
0
      case R_NDS32_TLS_DESC_LO12:
7163
0
        if (dynobj == NULL)
7164
0
    {
7165
0
      dynobj = _bfd_elf_link_dynobj (info);
7166
0
      if (dynobj == NULL)
7167
0
        return false;
7168
0
    }
7169
0
        if (!create_got_section (dynobj, info))
7170
0
    return false;
7171
0
        break;
7172
7173
0
      default:
7174
0
        break;
7175
0
      }
7176
0
  }
7177
7178
      /* Check relocation type.  */
7179
0
      switch ((int) r_type)
7180
0
  {
7181
0
  case R_NDS32_GOT_HI20:
7182
0
  case R_NDS32_GOT_LO12:
7183
0
  case R_NDS32_GOT_LO15:
7184
0
  case R_NDS32_GOT_LO19:
7185
0
  case R_NDS32_GOT20:
7186
0
  case R_NDS32_TLS_LE_HI20:
7187
0
  case R_NDS32_TLS_LE_LO12:
7188
0
  case R_NDS32_TLS_IE_HI20:
7189
0
  case R_NDS32_TLS_IE_LO12:
7190
0
  case R_NDS32_TLS_IE_LO12S2:
7191
0
  case R_NDS32_TLS_IEGP_HI20:
7192
0
  case R_NDS32_TLS_IEGP_LO12:
7193
0
  case R_NDS32_TLS_IEGP_LO12S2:
7194
0
  case R_NDS32_TLS_DESC_HI20:
7195
0
  case R_NDS32_TLS_DESC_LO12:
7196
0
    tls_type = get_tls_type (r_type, h);
7197
0
    if (h)
7198
0
      {
7199
0
        if (tls_type != GOT_TLS_LE)
7200
0
    h->got.refcount += 1;
7201
0
        old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7202
0
      }
7203
0
    else
7204
0
      {
7205
        /* This is a global offset table entry for a local symbol.  */
7206
0
        if (!elf32_nds32_allocate_local_sym_info (abfd))
7207
0
    return false;
7208
7209
0
        BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7210
0
        if (tls_type != GOT_TLS_LE)
7211
0
    elf_local_got_refcounts (abfd)[r_symndx] += 1;
7212
0
        old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7213
0
      }
7214
7215
    /* We would already have issued an error message if there
7216
       is a TLS/non-TLS mismatch, based on the symbol
7217
       type.  So just combine any TLS types needed.  */
7218
0
    if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7219
0
        && tls_type != GOT_NORMAL)
7220
0
      tls_type |= old_tls_type;
7221
7222
    /* DESC to IE/IEGP if link to executable.  */
7223
0
    if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7224
0
        && (bfd_link_executable (info)))
7225
0
      tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7226
7227
0
    if (old_tls_type != tls_type)
7228
0
      {
7229
0
        if (h != NULL)
7230
0
    elf32_nds32_hash_entry (h)->tls_type = tls_type;
7231
0
        else
7232
0
    elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7233
0
      }
7234
0
    break;
7235
0
  case R_NDS32_9_PLTREL:
7236
0
  case R_NDS32_25_PLTREL:
7237
0
  case R_NDS32_PLTREL_HI20:
7238
0
  case R_NDS32_PLTREL_LO12:
7239
0
  case R_NDS32_PLT_GOTREL_HI20:
7240
0
  case R_NDS32_PLT_GOTREL_LO12:
7241
0
  case R_NDS32_PLT_GOTREL_LO15:
7242
0
  case R_NDS32_PLT_GOTREL_LO19:
7243
0
  case R_NDS32_PLT_GOTREL_LO20:
7244
7245
    /* This symbol requires a procedure linkage table entry.  We
7246
       actually build the entry in adjust_dynamic_symbol,
7247
       because this might be a case of linking PIC code without
7248
       linking in any dynamic objects, in which case we don't
7249
       need to generate a procedure linkage table after all.  */
7250
7251
    /* If this is a local symbol, we resolve it directly without
7252
       creating a procedure linkage table entry.  */
7253
0
    if (h == NULL)
7254
0
      continue;
7255
7256
0
    if (h->forced_local
7257
0
        || (bfd_link_pie (info) && h->def_regular))
7258
0
      break;
7259
7260
0
    elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7261
0
    h->needs_plt = 1;
7262
0
    h->plt.refcount += 1;
7263
0
    break;
7264
7265
0
  case R_NDS32_16_RELA:
7266
0
  case R_NDS32_20_RELA:
7267
0
  case R_NDS32_5_RELA:
7268
0
  case R_NDS32_32_RELA:
7269
0
  case R_NDS32_HI20_RELA:
7270
0
  case R_NDS32_LO12S3_RELA:
7271
0
  case R_NDS32_LO12S2_RELA:
7272
0
  case R_NDS32_LO12S2_DP_RELA:
7273
0
  case R_NDS32_LO12S2_SP_RELA:
7274
0
  case R_NDS32_LO12S1_RELA:
7275
0
  case R_NDS32_LO12S0_RELA:
7276
0
  case R_NDS32_LO12S0_ORI_RELA:
7277
0
  case R_NDS32_SDA16S3_RELA:
7278
0
  case R_NDS32_SDA17S2_RELA:
7279
0
  case R_NDS32_SDA18S1_RELA:
7280
0
  case R_NDS32_SDA19S0_RELA:
7281
0
  case R_NDS32_SDA15S3_RELA:
7282
0
  case R_NDS32_SDA15S2_RELA:
7283
0
  case R_NDS32_SDA12S2_DP_RELA:
7284
0
  case R_NDS32_SDA12S2_SP_RELA:
7285
0
  case R_NDS32_SDA15S1_RELA:
7286
0
  case R_NDS32_SDA15S0_RELA:
7287
0
  case R_NDS32_SDA_FP7U2_RELA:
7288
0
  case R_NDS32_15_PCREL_RELA:
7289
0
  case R_NDS32_17_PCREL_RELA:
7290
0
  case R_NDS32_25_PCREL_RELA:
7291
7292
0
    if (h != NULL && !bfd_link_pic (info))
7293
0
      {
7294
0
        h->non_got_ref = 1;
7295
0
        h->plt.refcount += 1;
7296
0
      }
7297
7298
    /* If we are creating a shared library, and this is a reloc against
7299
       a global symbol, or a non PC relative reloc against a local
7300
       symbol, then we need to copy the reloc into the shared library.
7301
       However, if we are linking with -Bsymbolic, we do not need to
7302
       copy a reloc against a global symbol which is defined in an
7303
       object we are including in the link (i.e., DEF_REGULAR is set).
7304
       At this point we have not seen all the input files, so it is
7305
       possible that DEF_REGULAR is not set now but will be set later
7306
       (it is never cleared).  We account for that possibility below by
7307
       storing information in the dyn_relocs field of the hash table
7308
       entry.  A similar situation occurs when creating shared libraries
7309
       and symbol visibility changes render the symbol local.
7310
7311
       If on the other hand, we are creating an executable, we may need
7312
       to keep relocations for symbols satisfied by a dynamic library
7313
       if we manage to avoid copy relocs for the symbol.  */
7314
0
    if ((bfd_link_pic (info)
7315
0
         && (sec->flags & SEC_ALLOC) != 0
7316
0
         && ((r_type != R_NDS32_25_PCREL_RELA
7317
0
        && r_type != R_NDS32_15_PCREL_RELA
7318
0
        && r_type != R_NDS32_17_PCREL_RELA
7319
0
        && !(r_type == R_NDS32_32_RELA
7320
0
       && strcmp (sec->name, ".eh_frame") == 0))
7321
0
       || (h != NULL
7322
0
           && (!info->symbolic
7323
0
         || h->root.type == bfd_link_hash_defweak
7324
0
         || !h->def_regular))))
7325
0
        || (!bfd_link_pic (info)
7326
0
      && (sec->flags & SEC_ALLOC) != 0
7327
0
      && h != NULL
7328
0
      && (h->root.type == bfd_link_hash_defweak
7329
0
          || !h->def_regular)))
7330
0
      {
7331
0
        struct elf_dyn_relocs *p;
7332
0
        struct elf_dyn_relocs **head;
7333
7334
0
        if (dynobj == NULL)
7335
0
    {
7336
0
      dynobj = _bfd_elf_link_dynobj (info);
7337
0
      if (dynobj == NULL)
7338
0
        return false;
7339
0
    }
7340
7341
        /* When creating a shared object, we must copy these
7342
     relocs into the output file.  We create a reloc
7343
     section in dynobj and make room for the reloc.  */
7344
0
        if (sreloc == NULL)
7345
0
    {
7346
0
      const char *name;
7347
7348
0
      name = bfd_elf_string_from_elf_section
7349
0
        (abfd, elf_elfheader (abfd)->e_shstrndx,
7350
0
         elf_section_data (sec)->rela.hdr->sh_name);
7351
0
      if (name == NULL)
7352
0
        return false;
7353
7354
0
      BFD_ASSERT (startswith (name, ".rela")
7355
0
            && strcmp (bfd_section_name (sec),
7356
0
           name + 5) == 0);
7357
7358
0
      sreloc = bfd_get_section_by_name (dynobj, name);
7359
0
      if (sreloc == NULL)
7360
0
        {
7361
0
          flagword flags;
7362
7363
0
          sreloc = bfd_make_section (dynobj, name);
7364
0
          flags = (SEC_HAS_CONTENTS | SEC_READONLY
7365
0
             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7366
0
          if ((sec->flags & SEC_ALLOC) != 0)
7367
0
      flags |= SEC_ALLOC | SEC_LOAD;
7368
0
          if (sreloc == NULL
7369
0
        || !bfd_set_section_flags (sreloc, flags)
7370
0
        || !bfd_set_section_alignment (sreloc, 2))
7371
0
      return false;
7372
7373
0
          elf_section_type (sreloc) = SHT_RELA;
7374
0
        }
7375
0
      elf_section_data (sec)->sreloc = sreloc;
7376
0
    }
7377
7378
        /* If this is a global symbol, we count the number of
7379
     relocations we need for this symbol.  */
7380
0
        if (h != NULL)
7381
0
    head = &h->dyn_relocs;
7382
0
        else
7383
0
    {
7384
0
      asection *s;
7385
0
      void *vpp;
7386
7387
0
      Elf_Internal_Sym *isym;
7388
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
7389
0
            abfd, r_symndx);
7390
0
      if (isym == NULL)
7391
0
        return false;
7392
7393
      /* Track dynamic relocs needed for local syms too.  */
7394
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7395
0
      if (s == NULL)
7396
0
        return false;
7397
7398
0
      vpp = &elf_section_data (s)->local_dynrel;
7399
0
      head = (struct elf_dyn_relocs **) vpp;
7400
0
    }
7401
7402
0
        p = *head;
7403
0
        if (p == NULL || p->sec != sec)
7404
0
    {
7405
0
      size_t amt = sizeof (*p);
7406
0
      p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7407
0
      if (p == NULL)
7408
0
        return false;
7409
0
      p->next = *head;
7410
0
      *head = p;
7411
0
      p->sec = sec;
7412
0
      p->count = 0;
7413
0
      p->pc_count = 0;
7414
0
    }
7415
7416
0
        p->count += 1;
7417
7418
        /* Since eh_frame is readonly, R_NDS32_32_RELA
7419
     reloc for eh_frame will cause shared library has
7420
     TEXTREL entry in the dynamic section. This lead glibc
7421
     testsuites to failure (bug-13092) and cause kernel fail
7422
     (bug-11819).  I think the best solution is to replace
7423
     absolute reloc with pc relative reloc in the eh_frame.
7424
     To do that, we need to support the following issues:
7425
7426
     === For GCC ===
7427
     * gcc/config/nds32/nds32.h: Define
7428
     ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7429
     and DW_EH_PE_sdata4 into DWARF exception header when
7430
     option have '-fpic'.
7431
7432
     === For binutils ===
7433
     * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7434
     * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7435
     may break our nds DIFF mechanism, therefore, we
7436
     must disable all linker relaxations to ensure
7437
     correctness.
7438
     * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7439
     R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7440
     do the necessary modification.
7441
7442
     Unfortunately, it still have some problems for nds32
7443
     to support pc relative reloc in the eh_frame. So I use
7444
     another solution to fix this issue.
7445
7446
     However, I find that ld always emit TEXTREL marker for
7447
     R_NDS32_NONE relocs in rel.dyn. These none relocs are
7448
     correspond to R_NDS32_32_RELA for .eh_frame section.
7449
     It means that we always reserve redundant entries of rel.dyn
7450
     for these relocs which actually do nothing in dynamic linker.
7451
7452
     Therefore, we regard these relocs as pc relative relocs
7453
     here and increase the pc_count.  */
7454
0
        if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7455
0
      || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7456
0
      || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7457
0
      || (r_type == R_NDS32_32_RELA
7458
0
          && strcmp (sec->name, ".eh_frame") == 0))
7459
0
    p->pc_count += 1;
7460
0
      }
7461
0
    break;
7462
7463
    /* This relocation describes the C++ object vtable hierarchy.
7464
       Reconstruct it for later use during GC.  */
7465
0
  case R_NDS32_RELA_GNU_VTINHERIT:
7466
0
  case R_NDS32_GNU_VTINHERIT:
7467
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7468
0
      return false;
7469
0
    break;
7470
7471
    /* This relocation describes which C++ vtable entries are actually
7472
       used.  Record for later use during GC.  */
7473
0
  case R_NDS32_GNU_VTENTRY:
7474
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7475
0
      return false;
7476
0
    break;
7477
0
  case R_NDS32_RELA_GNU_VTENTRY:
7478
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7479
0
      return false;
7480
0
    break;
7481
0
  }
7482
0
    }
7483
7484
0
  return true;
7485
0
}
7486
7487
/* Write VAL in uleb128 format to P, returning a pointer to the
7488
   following byte.
7489
   This code is copied from elf-attr.c.  */
7490
7491
static bfd_byte *
7492
write_uleb128 (bfd_byte *p, unsigned int val)
7493
0
{
7494
0
  bfd_byte c;
7495
0
  do
7496
0
    {
7497
0
      c = val & 0x7f;
7498
0
      val >>= 7;
7499
0
      if (val)
7500
0
  c |= 0x80;
7501
0
      *(p++) = c;
7502
0
    }
7503
0
  while (val);
7504
0
  return p;
7505
0
}
7506
7507
static bfd_signed_vma
7508
calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7509
      Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7510
0
{
7511
0
  bfd_signed_vma foff;
7512
0
  bfd_vma symval, addend;
7513
0
  asection *sym_sec;
7514
7515
  /* Get the value of the symbol referred to by the reloc.  */
7516
0
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7517
0
    {
7518
0
      Elf_Internal_Sym *isym;
7519
7520
      /* A local symbol.  */
7521
0
      isym = isymbuf + ELF32_R_SYM (irel->r_info);
7522
7523
0
      if (isym->st_shndx == SHN_UNDEF)
7524
0
  sym_sec = bfd_und_section_ptr;
7525
0
      else if (isym->st_shndx == SHN_ABS)
7526
0
  sym_sec = bfd_abs_section_ptr;
7527
0
      else if (isym->st_shndx == SHN_COMMON)
7528
0
  sym_sec = bfd_com_section_ptr;
7529
0
      else
7530
0
  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7531
0
      symval = isym->st_value + sym_sec->output_section->vma
7532
0
         + sym_sec->output_offset;
7533
0
    }
7534
0
  else
7535
0
    {
7536
0
      unsigned long indx;
7537
0
      struct elf_link_hash_entry *h;
7538
7539
      /* An external symbol.  */
7540
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7541
0
      h = elf_sym_hashes (abfd)[indx];
7542
0
      BFD_ASSERT (h != NULL);
7543
7544
0
      if (h->root.type != bfd_link_hash_defined
7545
0
    && h->root.type != bfd_link_hash_defweak)
7546
  /* This appears to be a reference to an undefined
7547
     symbol.  Just ignore it--it will be caught by the
7548
     regular reloc processing.  */
7549
0
  return 0;
7550
7551
0
      if (h->root.u.def.section->flags & SEC_MERGE)
7552
0
  {
7553
0
    sym_sec = h->root.u.def.section;
7554
0
    symval = _bfd_merged_section_offset (abfd, &sym_sec,
7555
0
                 h->root.u.def.value);
7556
0
    symval = symval + sym_sec->output_section->vma
7557
0
       + sym_sec->output_offset;
7558
0
  }
7559
0
      else
7560
0
  symval = (h->root.u.def.value
7561
0
      + h->root.u.def.section->output_section->vma
7562
0
      + h->root.u.def.section->output_offset);
7563
0
    }
7564
7565
0
  addend = irel->r_addend;
7566
7567
0
  foff = (symval + addend
7568
0
    - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7569
0
  return foff;
7570
0
}
7571

7572
7573
/* Convert a 32-bit instruction to 16-bit one.
7574
   INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7575
   instruction.  If INSN_TYPE is not NULL, it the CGEN instruction
7576
   type of INSN16.  Return 1 if successful.  */
7577
7578
static int
7579
nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7580
           int *pinsn_type)
7581
0
{
7582
0
  uint16_t insn16 = 0;
7583
0
  int insn_type = 0;
7584
0
  unsigned long mach = bfd_get_mach (abfd);
7585
7586
0
  if (N32_SH5 (insn) != 0)
7587
0
    return 0;
7588
7589
0
  switch (N32_SUB5 (insn))
7590
0
    {
7591
0
    case N32_ALU1_ADD_SLLI:
7592
0
    case N32_ALU1_ADD_SRLI:
7593
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7594
0
  {
7595
0
    insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7596
0
        N32_RB5 (insn));
7597
0
    insn_type = NDS32_INSN_ADD333;
7598
0
  }
7599
0
      else if (N32_IS_RT4 (insn))
7600
0
  {
7601
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7602
0
      insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7603
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7604
0
      insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7605
0
    insn_type = NDS32_INSN_ADD45;
7606
0
  }
7607
0
      break;
7608
7609
0
    case N32_ALU1_SUB_SLLI:
7610
0
    case N32_ALU1_SUB_SRLI:
7611
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7612
0
  {
7613
0
    insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7614
0
        N32_RB5 (insn));
7615
0
    insn_type = NDS32_INSN_SUB333;
7616
0
  }
7617
0
      else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7618
0
  {
7619
0
    insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7620
0
    insn_type = NDS32_INSN_SUB45;
7621
0
  }
7622
0
      break;
7623
7624
0
    case N32_ALU1_AND_SLLI:
7625
0
    case N32_ALU1_AND_SRLI:
7626
      /* and $rt, $rt, $rb -> and33 for v3, v3m.  */
7627
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7628
0
    && N32_IS_RB3 (insn))
7629
0
  {
7630
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7631
0
      insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7632
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7633
0
      insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7634
0
    if (insn16)
7635
0
      insn_type = NDS32_INSN_AND33;
7636
0
  }
7637
0
      break;
7638
7639
0
    case N32_ALU1_XOR_SLLI:
7640
0
    case N32_ALU1_XOR_SRLI:
7641
      /* xor $rt, $rt, $rb -> xor33 for v3, v3m.  */
7642
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7643
0
    && N32_IS_RB3 (insn))
7644
0
  {
7645
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7646
0
      insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7647
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7648
0
      insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7649
0
    if (insn16)
7650
0
      insn_type = NDS32_INSN_XOR33;
7651
0
  }
7652
0
      break;
7653
7654
0
    case N32_ALU1_OR_SLLI:
7655
0
    case N32_ALU1_OR_SRLI:
7656
      /* or $rt, $rt, $rb -> or33 for v3, v3m.  */
7657
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7658
0
    && N32_IS_RB3 (insn))
7659
0
  {
7660
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7661
0
      insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7662
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7663
0
      insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7664
0
    if (insn16)
7665
0
      insn_type = NDS32_INSN_OR33;
7666
0
  }
7667
0
      break;
7668
0
    case N32_ALU1_NOR:
7669
      /* nor $rt, $ra, $ra -> not33 for v3, v3m.  */
7670
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7671
0
    && N32_RA5 (insn) == N32_RB5 (insn))
7672
0
  {
7673
0
    insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7674
0
    insn_type = NDS32_INSN_NOT33;
7675
0
  }
7676
0
      break;
7677
0
    case N32_ALU1_SRAI:
7678
0
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7679
0
  {
7680
0
    insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7681
0
    insn_type = NDS32_INSN_SRAI45;
7682
0
  }
7683
0
      break;
7684
7685
0
    case N32_ALU1_SRLI:
7686
0
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7687
0
  {
7688
0
    insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7689
0
    insn_type = NDS32_INSN_SRLI45;
7690
0
  }
7691
0
      break;
7692
7693
0
    case N32_ALU1_SLLI:
7694
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7695
0
  {
7696
0
    insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7697
0
        N32_UB5 (insn));
7698
0
    insn_type = NDS32_INSN_SLLI333;
7699
0
  }
7700
0
      break;
7701
7702
0
    case N32_ALU1_ZEH:
7703
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7704
0
  {
7705
0
    insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7706
0
    insn_type = NDS32_INSN_ZEH33;
7707
0
  }
7708
0
      break;
7709
7710
0
    case N32_ALU1_SEB:
7711
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7712
0
  {
7713
0
    insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7714
0
    insn_type = NDS32_INSN_SEB33;
7715
0
  }
7716
0
      break;
7717
7718
0
    case N32_ALU1_SEH:
7719
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7720
0
  {
7721
0
    insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7722
0
    insn_type = NDS32_INSN_SEH33;
7723
0
  }
7724
0
      break;
7725
7726
0
    case N32_ALU1_SLT:
7727
0
      if (N32_RT5 (insn) == N32_REG_R15 && N32_IS_RA4 (insn))
7728
0
  {
7729
    /* Implicit r15.  */
7730
0
    insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7731
0
    insn_type = NDS32_INSN_SLT45;
7732
0
  }
7733
0
      break;
7734
7735
0
    case N32_ALU1_SLTS:
7736
0
      if (N32_RT5 (insn) == N32_REG_R15 && N32_IS_RA4 (insn))
7737
0
  {
7738
    /* Implicit r15.  */
7739
0
    insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7740
0
    insn_type = NDS32_INSN_SLTS45;
7741
0
  }
7742
0
      break;
7743
0
    }
7744
7745
0
  if ((insn16 & 0x8000) == 0)
7746
0
    return 0;
7747
7748
0
  if (pinsn16)
7749
0
    *pinsn16 = insn16;
7750
0
  if (pinsn_type)
7751
0
    *pinsn_type = insn_type;
7752
0
  return 1;
7753
0
}
7754
7755
static int
7756
nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7757
           int *pinsn_type)
7758
0
{
7759
0
  uint16_t insn16 = 0;
7760
0
  int insn_type;
7761
0
  unsigned long mach = bfd_get_mach (abfd);
7762
7763
  /* TODO: bset, bclr, btgl, btst.  */
7764
0
  if (__GF (insn, 6, 4) != 0)
7765
0
    return 0;
7766
7767
0
  switch (N32_IMMU (insn, 6))
7768
0
    {
7769
0
    case N32_ALU2_MUL:
7770
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7771
0
    && N32_IS_RB3 (insn))
7772
0
  {
7773
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7774
0
      insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7775
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7776
0
      insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7777
0
    if (insn16)
7778
0
      insn_type = NDS32_INSN_MUL33;
7779
0
  }
7780
0
    }
7781
7782
0
  if ((insn16 & 0x8000) == 0)
7783
0
    return 0;
7784
7785
0
  if (pinsn16)
7786
0
    *pinsn16 = insn16;
7787
0
  if (pinsn_type)
7788
0
    *pinsn_type = insn_type;
7789
0
  return 1;
7790
0
}
7791
7792
int
7793
bfd_elf_nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7794
        int *pinsn_type)
7795
0
{
7796
0
  int op6;
7797
0
  uint16_t insn16 = 0;
7798
0
  int insn_type = 0;
7799
0
  unsigned long mach = bfd_get_mach (abfd);
7800
7801
  /* Decode 32-bit instruction.  */
7802
0
  if (insn & 0x80000000)
7803
0
    {
7804
      /* Not 32-bit insn.  */
7805
0
      return 0;
7806
0
    }
7807
7808
0
  op6 = N32_OP6 (insn);
7809
7810
  /* Convert it to 16-bit instruction.  */
7811
0
  switch (op6)
7812
0
    {
7813
0
    case N32_OP6_MOVI:
7814
0
      if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7815
0
  {
7816
0
    insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7817
0
    insn_type = NDS32_INSN_MOVI55;
7818
0
  }
7819
0
      else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7820
0
         && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7821
0
  {
7822
0
    insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7823
0
             N32_IMM20S (insn) - 16);
7824
0
    insn_type = NDS32_INSN_MOVPI45;
7825
0
  }
7826
0
      break;
7827
7828
0
    case N32_OP6_ADDI:
7829
0
      if (N32_IMM15S (insn) == 0)
7830
0
  {
7831
    /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7832
       because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7833
0
    if (mach <= MACH_V2
7834
0
        || N32_RT5 (insn) != N32_REG_SP || N32_RA5 (insn) != N32_REG_SP)
7835
0
      {
7836
0
        insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7837
0
        insn_type = NDS32_INSN_MOV55;
7838
0
      }
7839
0
  }
7840
0
      else if (N32_IMM15S (insn) > 0)
7841
0
  {
7842
0
    if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7843
0
      {
7844
0
        insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7845
0
            N32_IMM15S (insn));
7846
0
        insn_type = NDS32_INSN_ADDI333;
7847
0
      }
7848
0
    else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7849
0
       && N32_IMM15S (insn) < 32)
7850
0
      {
7851
0
        insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7852
0
        insn_type = NDS32_INSN_ADDI45;
7853
0
      }
7854
0
    else if (mach >= MACH_V2 && N32_RT5 (insn) == N32_REG_SP
7855
0
       && N32_RT5 (insn) == N32_RA5 (insn)
7856
0
       && N32_IMM15S (insn) < 512)
7857
0
      {
7858
0
        insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7859
0
        insn_type = NDS32_INSN_ADDI10_SP;
7860
0
      }
7861
0
    else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7862
0
       && N32_RA5 (insn) == N32_REG_SP && N32_IMM15S (insn) < 256
7863
0
       && (N32_IMM15S (insn) % 4 == 0))
7864
0
      {
7865
0
        insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7866
0
           N32_IMM15S (insn) >> 2);
7867
0
        insn_type = NDS32_INSN_ADDRI36_SP;
7868
0
      }
7869
0
  }
7870
0
      else
7871
0
  {
7872
    /* Less than 0.  */
7873
0
    if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7874
0
      {
7875
0
        insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7876
0
            0 - N32_IMM15S (insn));
7877
0
        insn_type = NDS32_INSN_SUBI333;
7878
0
      }
7879
0
    else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7880
0
       && N32_IMM15S (insn) > -32)
7881
0
      {
7882
0
        insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7883
0
           0 - N32_IMM15S (insn));
7884
0
        insn_type = NDS32_INSN_SUBI45;
7885
0
      }
7886
0
    else if (mach >= MACH_V2 && N32_RT5 (insn) == N32_REG_SP
7887
0
       && N32_RT5 (insn) == N32_RA5 (insn)
7888
0
       && N32_IMM15S (insn) >= -512)
7889
0
      {
7890
0
        insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7891
0
        insn_type = NDS32_INSN_ADDI10_SP;
7892
0
      }
7893
0
  }
7894
0
      break;
7895
7896
0
    case N32_OP6_ORI:
7897
0
      if (N32_IMM15S (insn) == 0)
7898
0
  {
7899
    /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7900
       because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7901
0
    if (mach <= MACH_V2
7902
0
        || N32_RT5 (insn) != N32_REG_SP || N32_RA5 (insn) != N32_REG_SP)
7903
0
      {
7904
0
        insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7905
0
        insn_type = NDS32_INSN_MOV55;
7906
0
      }
7907
0
  }
7908
0
      break;
7909
7910
0
    case N32_OP6_SUBRI:
7911
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7912
0
    && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7913
0
  {
7914
0
    insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7915
0
    insn_type = NDS32_INSN_NEG33;
7916
0
  }
7917
0
      break;
7918
7919
0
    case N32_OP6_ANDI:
7920
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7921
0
  {
7922
0
    if (N32_IMM15U (insn) == 1)
7923
0
      {
7924
0
        insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7925
0
        insn_type = NDS32_INSN_XLSB33;
7926
0
      }
7927
0
    else if (N32_IMM15U (insn) == 0x7ff)
7928
0
      {
7929
0
        insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7930
0
        insn_type = NDS32_INSN_X11B33;
7931
0
      }
7932
0
    else if (N32_IMM15U (insn) == 0xff)
7933
0
      {
7934
0
        insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7935
0
        insn_type = NDS32_INSN_ZEB33;
7936
0
      }
7937
0
    else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7938
0
       && N32_IMM15U (insn) < 256)
7939
0
      {
7940
0
        int imm15u = N32_IMM15U (insn);
7941
7942
0
        if (__builtin_popcount (imm15u) == 1)
7943
0
    {
7944
      /* BMSKI33 */
7945
0
      int imm3u = __builtin_ctz (imm15u);
7946
7947
0
      insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7948
0
      insn_type = NDS32_INSN_BMSKI33;
7949
0
    }
7950
0
        else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7951
0
    {
7952
      /* FEXTI33 */
7953
0
      int imm3u = __builtin_ctz (imm15u + 1) - 1;
7954
7955
0
      insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7956
0
      insn_type = NDS32_INSN_FEXTI33;
7957
0
    }
7958
0
      }
7959
0
  }
7960
0
      break;
7961
7962
0
    case N32_OP6_SLTI:
7963
0
      if (N32_RT5 (insn) == N32_REG_R15 && N32_IS_RA4 (insn)
7964
0
    && IS_WITHIN_U (N32_IMM15S (insn), 5))
7965
0
  {
7966
0
    insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7967
0
    insn_type = NDS32_INSN_SLTI45;
7968
0
  }
7969
0
      break;
7970
7971
0
    case N32_OP6_SLTSI:
7972
0
      if (N32_RT5 (insn) == N32_REG_R15 && N32_IS_RA4 (insn)
7973
0
    && IS_WITHIN_U (N32_IMM15S (insn), 5))
7974
0
  {
7975
0
    insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7976
0
    insn_type = NDS32_INSN_SLTSI45;
7977
0
  }
7978
0
      break;
7979
7980
0
    case N32_OP6_LWI:
7981
0
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7982
0
  {
7983
0
    insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7984
0
    insn_type = NDS32_INSN_LWI450;
7985
0
  }
7986
0
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7987
0
         && IS_WITHIN_U (N32_IMM15S (insn), 3))
7988
0
  {
7989
0
    insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7990
0
        N32_IMM15S (insn));
7991
0
    insn_type = NDS32_INSN_LWI333;
7992
0
  }
7993
0
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == N32_REG_FP
7994
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
7995
0
  {
7996
0
    insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7997
0
    insn_type = NDS32_INSN_LWI37;
7998
0
  }
7999
0
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn)
8000
0
         && N32_RA5 (insn) == N32_REG_SP
8001
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
8002
0
  {
8003
0
    insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
8004
0
    insn_type = NDS32_INSN_LWI37_SP;
8005
0
  }
8006
0
      else if (mach >= MACH_V2 && N32_IS_RT4 (insn)
8007
0
         && N32_RA5 (insn) == N32_REG_R8
8008
0
         && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
8009
0
  {
8010
0
    insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
8011
0
             N32_IMM15S (insn) + 32);
8012
0
    insn_type = NDS32_INSN_LWI45_FE;
8013
0
  }
8014
0
      break;
8015
8016
0
    case N32_OP6_SWI:
8017
0
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
8018
0
  {
8019
0
    insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
8020
0
    insn_type = NDS32_INSN_SWI450;
8021
0
  }
8022
0
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8023
0
         && IS_WITHIN_U (N32_IMM15S (insn), 3))
8024
0
  {
8025
0
    insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
8026
0
        N32_IMM15S (insn));
8027
0
    insn_type = NDS32_INSN_SWI333;
8028
0
  }
8029
0
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == N32_REG_FP
8030
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
8031
0
  {
8032
0
    insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8033
0
    insn_type = NDS32_INSN_SWI37;
8034
0
  }
8035
0
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn)
8036
0
         && N32_RA5 (insn) == N32_REG_SP
8037
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
8038
0
  {
8039
0
    insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
8040
0
    insn_type = NDS32_INSN_SWI37_SP;
8041
0
  }
8042
0
      break;
8043
8044
0
    case N32_OP6_LWI_BI:
8045
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8046
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8047
0
  {
8048
0
    insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8049
0
        N32_IMM15S (insn));
8050
0
    insn_type = NDS32_INSN_LWI333_BI;
8051
0
  }
8052
0
      break;
8053
8054
0
    case N32_OP6_SWI_BI:
8055
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8056
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8057
0
  {
8058
0
    insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
8059
0
        N32_IMM15S (insn));
8060
0
    insn_type = NDS32_INSN_SWI333_BI;
8061
0
  }
8062
0
      break;
8063
8064
0
    case N32_OP6_LHI:
8065
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8066
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8067
0
  {
8068
0
    insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
8069
0
        N32_IMM15S (insn));
8070
0
    insn_type = NDS32_INSN_LHI333;
8071
0
  }
8072
0
      break;
8073
8074
0
    case N32_OP6_SHI:
8075
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8076
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8077
0
  {
8078
0
    insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8079
0
        N32_IMM15S (insn));
8080
0
    insn_type = NDS32_INSN_SHI333;
8081
0
  }
8082
0
      break;
8083
8084
0
    case N32_OP6_LBI:
8085
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8086
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8087
0
  {
8088
0
    insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8089
0
        N32_IMM15S (insn));
8090
0
    insn_type = NDS32_INSN_LBI333;
8091
0
  }
8092
0
      break;
8093
8094
0
    case N32_OP6_SBI:
8095
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8096
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8097
0
  {
8098
0
    insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8099
0
        N32_IMM15S (insn));
8100
0
    insn_type = NDS32_INSN_SBI333;
8101
0
  }
8102
0
      break;
8103
8104
0
    case N32_OP6_ALU1:
8105
0
      return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8106
8107
0
    case N32_OP6_ALU2:
8108
0
      return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8109
8110
0
    case N32_OP6_BR1:
8111
0
      if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8112
0
  goto done;
8113
8114
0
      if ((insn & N32_BIT (14)) == 0)
8115
0
  {
8116
    /* N32_BR1_BEQ */
8117
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == N32_REG_R5
8118
0
        && N32_RT5 (insn) != N32_REG_R5)
8119
0
      insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8120
0
    else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == N32_REG_R5
8121
0
       && N32_RA5 (insn) != N32_REG_R5)
8122
0
      insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8123
0
    insn_type = NDS32_INSN_BEQS38;
8124
0
    break;
8125
0
  }
8126
0
      else
8127
0
  {
8128
    /* N32_BR1_BNE */
8129
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == N32_REG_R5
8130
0
        && N32_RT5 (insn) != N32_REG_R5)
8131
0
      insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8132
0
    else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == N32_REG_R5
8133
0
       && N32_RA5 (insn) != N32_REG_R5)
8134
0
      insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8135
0
    insn_type = NDS32_INSN_BNES38;
8136
0
    break;
8137
0
  }
8138
0
      break;
8139
8140
0
    case N32_OP6_BR2:
8141
0
      switch (N32_BR2_SUB (insn))
8142
0
  {
8143
0
  case N32_BR2_BEQZ:
8144
0
    if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8145
0
      {
8146
0
        insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8147
0
        insn_type = NDS32_INSN_BEQZ38;
8148
0
      }
8149
0
    else if (N32_RT5 (insn) == N32_REG_R15
8150
0
       && IS_WITHIN_S (N32_IMM16S (insn), 8))
8151
0
      {
8152
0
        insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8153
0
        insn_type = NDS32_INSN_BEQZS8;
8154
0
      }
8155
0
    break;
8156
8157
0
  case N32_BR2_BNEZ:
8158
0
    if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8159
0
      {
8160
0
        insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8161
0
        insn_type = NDS32_INSN_BNEZ38;
8162
0
      }
8163
0
    else if (N32_RT5 (insn) == N32_REG_R15
8164
0
       && IS_WITHIN_S (N32_IMM16S (insn), 8))
8165
0
      {
8166
0
        insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8167
0
        insn_type = NDS32_INSN_BNEZS8;
8168
0
      }
8169
0
    break;
8170
8171
0
  case N32_BR2_SOP0:
8172
0
    if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8173
0
      {
8174
0
        insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8175
0
        insn_type = NDS32_INSN_IFCALL9;
8176
0
      }
8177
0
    break;
8178
0
  }
8179
0
      break;
8180
8181
0
    case N32_OP6_JI:
8182
0
      if ((insn & N32_BIT (24)) == 0)
8183
0
  {
8184
    /* N32_JI_J */
8185
0
    if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8186
0
      {
8187
0
        insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8188
0
        insn_type = NDS32_INSN_J8;
8189
0
      }
8190
0
  }
8191
0
      break;
8192
8193
0
    case N32_OP6_JREG:
8194
0
      if (__GF (insn, 8, 2) != 0)
8195
0
  goto done;
8196
8197
0
      switch (N32_IMMU (insn, 5))
8198
0
  {
8199
0
  case N32_JREG_JR:
8200
0
    if (N32_JREG_HINT (insn) == 0)
8201
0
      {
8202
        /* jr */
8203
0
        insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8204
0
        insn_type = NDS32_INSN_JR5;
8205
0
      }
8206
0
    else if (N32_JREG_HINT (insn) == 1)
8207
0
      {
8208
        /* ret */
8209
0
        insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8210
0
        insn_type = NDS32_INSN_RET5;
8211
0
      }
8212
0
    else if (N32_JREG_HINT (insn) == 3)
8213
0
      {
8214
        /* ifret = mov55 $sp, $sp */
8215
0
        insn16 = N16_TYPE55 (MOV55, N32_REG_SP, N32_REG_SP);
8216
0
        insn_type = NDS32_INSN_IFRET;
8217
0
      }
8218
0
    break;
8219
8220
0
  case N32_JREG_JRAL:
8221
    /* It's convertible when return rt5 is $lp and address
8222
       translation is kept.  */
8223
0
    if (N32_RT5 (insn) == N32_REG_LP && N32_JREG_HINT (insn) == 0)
8224
0
      {
8225
0
        insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8226
0
        insn_type = NDS32_INSN_JRAL5;
8227
0
      }
8228
0
    break;
8229
0
  }
8230
0
      break;
8231
8232
0
    case N32_OP6_MISC:
8233
0
      if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8234
0
  {
8235
    /* For v3, swid above 31 are used for ex9.it.  */
8236
0
    insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8237
0
    insn_type = NDS32_INSN_BREAK16;
8238
0
  }
8239
0
      break;
8240
8241
0
    default:
8242
      /* This instruction has no 16-bit variant.  */
8243
0
      goto done;
8244
0
    }
8245
8246
0
 done:
8247
  /* Bit-15 of insn16 should be set for a valid instruction.  */
8248
0
  if ((insn16 & 0x8000) == 0)
8249
0
    return 0;
8250
8251
0
  if (pinsn16)
8252
0
    *pinsn16 = insn16;
8253
0
  if (pinsn_type)
8254
0
    *pinsn_type = insn_type;
8255
0
  return 1;
8256
0
}
8257
8258
static int
8259
special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8260
        Elf_Internal_Rela *reloc)
8261
0
{
8262
0
  uint16_t insn16 = 0;
8263
8264
0
  if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8265
0
      || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8266
0
    return 0;
8267
8268
0
  if (!N32_IS_RT3 (insn))
8269
0
    return 0;
8270
8271
0
  switch (N32_OP6 (insn))
8272
0
    {
8273
0
    case N32_OP6_LWI:
8274
0
      if (N32_RA5 (insn) == N32_REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8275
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8276
0
      break;
8277
0
    case N32_OP6_SWI:
8278
0
      if (N32_RA5 (insn) == N32_REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8279
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8280
0
      break;
8281
0
    case N32_OP6_HWGP:
8282
0
      if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8283
0
  break;
8284
8285
0
      if (__GF (insn, 17, 3) == 6)
8286
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8287
0
      else if (__GF (insn, 17, 3) == 7)
8288
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8289
0
      break;
8290
0
    }
8291
8292
0
  if ((insn16 & 0x8000) == 0)
8293
0
    return 0;
8294
8295
0
  *pinsn16 = insn16;
8296
0
  return 1;
8297
0
}
8298
8299
/* Convert a 16-bit instruction to 32-bit one.
8300
   INSN16 it the input and PINSN it the point to output.
8301
   Return non-zero on successful.  Otherwise 0 is returned.  */
8302
8303
int
8304
bfd_elf_nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8305
0
{
8306
0
  uint32_t insn = 0xffffffff;
8307
0
  unsigned long mach = bfd_get_mach (abfd);
8308
8309
  /* NOTE: push25, pop25 and movd44 do not have 32-bit variants.  */
8310
8311
0
  switch (__GF (insn16, 9, 6))
8312
0
    {
8313
0
    case 0x4:     /* add45 */
8314
0
      insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8315
0
           N16_RA5 (insn16));
8316
0
      goto done;
8317
0
    case 0x5:     /* sub45 */
8318
0
      insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8319
0
           N16_RA5 (insn16));
8320
0
      goto done;
8321
0
    case 0x6:     /* addi45 */
8322
0
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8323
0
      N16_IMM5U (insn16));
8324
0
      goto done;
8325
0
    case 0x7:     /* subi45 */
8326
0
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8327
0
      -N16_IMM5U (insn16));
8328
0
      goto done;
8329
0
    case 0x8:     /* srai45 */
8330
0
      insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8331
0
           N16_IMM5U (insn16));
8332
0
      goto done;
8333
0
    case 0x9:     /* srli45 */
8334
0
      insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8335
0
           N16_IMM5U (insn16));
8336
0
      goto done;
8337
0
    case 0xa:     /* slli333 */
8338
0
      insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8339
0
           N16_IMM3U (insn16));
8340
0
      goto done;
8341
0
    case 0xc:     /* add333 */
8342
0
      insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8343
0
           N16_RB3 (insn16));
8344
0
      goto done;
8345
0
    case 0xd:     /* sub333 */
8346
0
      insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8347
0
           N16_RB3 (insn16));
8348
0
      goto done;
8349
0
    case 0xe:     /* addi333 */
8350
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8351
0
      N16_IMM3U (insn16));
8352
0
      goto done;
8353
0
    case 0xf:     /* subi333 */
8354
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8355
0
      -N16_IMM3U (insn16));
8356
0
      goto done;
8357
0
    case 0x10:      /* lwi333 */
8358
0
      insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8359
0
      N16_IMM3U (insn16));
8360
0
      goto done;
8361
0
    case 0x12:      /* lhi333 */
8362
0
      insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8363
0
      N16_IMM3U (insn16));
8364
0
      goto done;
8365
0
    case 0x13:      /* lbi333 */
8366
0
      insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8367
0
      N16_IMM3U (insn16));
8368
0
      goto done;
8369
0
    case 0x11:      /* lwi333.bi */
8370
0
      insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8371
0
      N16_IMM3U (insn16));
8372
0
      goto done;
8373
0
    case 0x14:      /* swi333 */
8374
0
      insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8375
0
      N16_IMM3U (insn16));
8376
0
      goto done;
8377
0
    case 0x16:      /* shi333 */
8378
0
      insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8379
0
      N16_IMM3U (insn16));
8380
0
      goto done;
8381
0
    case 0x17:      /* sbi333 */
8382
0
      insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8383
0
      N16_IMM3U (insn16));
8384
0
      goto done;
8385
0
    case 0x15:      /* swi333.bi */
8386
0
      insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8387
0
      N16_IMM3U (insn16));
8388
0
      goto done;
8389
0
    case 0x18:      /* addri36.sp */
8390
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N32_REG_SP,
8391
0
      N16_IMM6U (insn16) << 2);
8392
0
      goto done;
8393
0
    case 0x19:      /* lwi45.fe */
8394
0
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N32_REG_R8,
8395
0
      (N16_IMM5U (insn16) - 32));
8396
0
      goto done;
8397
0
    case 0x1a:      /* lwi450 */
8398
0
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8399
0
      goto done;
8400
0
    case 0x1b:      /* swi450 */
8401
0
      insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8402
0
      goto done;
8403
8404
      /* These are r15 implied instructions.  */
8405
0
    case 0x30:      /* slts45 */
8406
0
      insn = N32_ALU1 (SLTS, N32_REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8407
0
      goto done;
8408
0
    case 0x31:      /* slt45 */
8409
0
      insn = N32_ALU1 (SLT, N32_REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8410
0
      goto done;
8411
0
    case 0x32:      /* sltsi45 */
8412
0
      insn = N32_TYPE2 (SLTSI, N32_REG_TA, N16_RT4 (insn16),
8413
0
      N16_IMM5U (insn16));
8414
0
      goto done;
8415
0
    case 0x33:      /* slti45 */
8416
0
      insn = N32_TYPE2 (SLTI, N32_REG_TA, N16_RT4 (insn16),
8417
0
      N16_IMM5U (insn16));
8418
0
      goto done;
8419
0
    case 0x34:      /* beqzs8, bnezs8 */
8420
0
      if (insn16 & N32_BIT (8))
8421
0
  insn = N32_BR2 (BNEZ, N32_REG_TA, N16_IMM8S (insn16));
8422
0
      else
8423
0
  insn = N32_BR2 (BEQZ, N32_REG_TA, N16_IMM8S (insn16));
8424
0
      goto done;
8425
8426
0
    case 0x35:      /* break16, ex9.it */
8427
      /* Only consider range of v3 break16.  */
8428
0
      insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8429
0
      goto done;
8430
8431
0
    case 0x3c:      /* ifcall9 */
8432
0
      insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8433
0
      goto done;
8434
0
    case 0x3d:      /* movpi45 */
8435
0
      insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8436
0
      goto done;
8437
8438
0
    case 0x3f:      /* MISC33 */
8439
0
      switch (insn16 & 0x7)
8440
0
  {
8441
0
  case 2:     /* neg33 */
8442
0
    insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8443
0
    break;
8444
0
  case 3:     /* not33 */
8445
0
    insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8446
0
         N16_RA3 (insn16));
8447
0
    break;
8448
0
  case 4:     /* mul33 */
8449
0
    insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8450
0
         N16_RA3 (insn16));
8451
0
    break;
8452
0
  case 5:     /* xor33 */
8453
0
    insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8454
0
         N16_RA3 (insn16));
8455
0
    break;
8456
0
  case 6:     /* and33 */
8457
0
    insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8458
0
         N16_RA3 (insn16));
8459
0
    break;
8460
0
  case 7:     /* or33 */
8461
0
    insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8462
0
         N16_RA3 (insn16));
8463
0
    break;
8464
0
  }
8465
0
      goto done;
8466
8467
0
    case 0xb:
8468
0
      switch (insn16 & 0x7)
8469
0
  {
8470
0
  case 0:     /* zeb33 */
8471
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8472
0
    break;
8473
0
  case 1:     /* zeh33 */
8474
0
    insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8475
0
    break;
8476
0
  case 2:     /* seb33 */
8477
0
    insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8478
0
    break;
8479
0
  case 3:     /* seh33 */
8480
0
    insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8481
0
    break;
8482
0
  case 4:     /* xlsb33 */
8483
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8484
0
    break;
8485
0
  case 5:     /* x11b33 */
8486
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8487
0
    break;
8488
0
  case 6:     /* bmski33 */
8489
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8490
0
          1 << __GF (insn16, 3, 3));
8491
0
    break;
8492
0
  case 7:     /* fexti33 */
8493
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8494
0
          (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8495
0
    break;
8496
0
  }
8497
0
      goto done;
8498
0
    }
8499
8500
0
  switch (__GF (insn16, 10, 5))
8501
0
    {
8502
0
    case 0x0:     /* mov55 or ifret16 */
8503
0
      if (mach >= MACH_V3 && N16_RT5 (insn16) == N32_REG_SP
8504
0
    && N16_RT5 (insn16) == N16_RA5 (insn16))
8505
0
  insn = N32_JREG (JR, 0, 0, 0, 3);
8506
0
      else
8507
0
  insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8508
0
      goto done;
8509
0
    case 0x1:     /* movi55 */
8510
0
      insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8511
0
      goto done;
8512
0
    case 0x1b:      /* addi10s (V2) */
8513
0
      insn = N32_TYPE2 (ADDI, N32_REG_SP, N32_REG_SP, N16_IMM10S (insn16));
8514
0
      goto done;
8515
0
    }
8516
8517
0
  switch (__GF (insn16, 11, 4))
8518
0
    {
8519
0
    case 0x7:     /* lwi37.fp/swi37.fp */
8520
0
      if (insn16 & N32_BIT (7)) /* swi37.fp */
8521
0
  insn = N32_TYPE2 (SWI, N16_RT38 (insn16), N32_REG_FP,
8522
0
        N16_IMM7U (insn16));
8523
0
      else      /* lwi37.fp */
8524
0
  insn = N32_TYPE2 (LWI, N16_RT38 (insn16), N32_REG_FP,
8525
0
        N16_IMM7U (insn16));
8526
0
      goto done;
8527
0
    case 0x8:     /* beqz38 */
8528
0
      insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8529
0
      goto done;
8530
0
    case 0x9:     /* bnez38 */
8531
0
      insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8532
0
      goto done;
8533
0
    case 0xa:     /* beqs38/j8, implied r5 */
8534
0
      if (N16_RT38 (insn16) == 5)
8535
0
  insn = N32_JI (J, N16_IMM8S (insn16));
8536
0
      else
8537
0
  insn = N32_BR1 (BEQ, N16_RT38 (insn16), N32_REG_R5,
8538
0
      N16_IMM8S (insn16));
8539
0
      goto done;
8540
0
    case 0xb:     /* bnes38 and others.  */
8541
0
      if (N16_RT38 (insn16) == 5)
8542
0
  {
8543
0
    switch (__GF (insn16, 5, 3))
8544
0
      {
8545
0
      case 0:   /* jr5 */
8546
0
        insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8547
0
        break;
8548
0
      case 4:   /* ret5 */
8549
0
        insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8550
0
        break;
8551
0
      case 1:   /* jral5 */
8552
0
        insn = N32_JREG (JRAL, N32_REG_LP, N16_RA5 (insn16), 0, 0);
8553
0
        break;
8554
0
      case 2:   /* ex9.it imm5 */
8555
        /* ex9.it had no 32-bit variantl.  */
8556
0
        break;
8557
0
      case 5:   /* add5.pc */
8558
        /* add5.pc had no 32-bit variantl.  */
8559
0
        break;
8560
0
      }
8561
0
  }
8562
0
      else      /* bnes38 */
8563
0
  insn = N32_BR1 (BNE, N16_RT38 (insn16), N32_REG_R5,
8564
0
      N16_IMM8S (insn16));
8565
0
      goto done;
8566
0
    case 0xe:     /* lwi37/swi37 */
8567
0
      if (insn16 & (1 << 7)) /* swi37.sp */
8568
0
  insn = N32_TYPE2 (SWI, N16_RT38 (insn16), N32_REG_SP,
8569
0
        N16_IMM7U (insn16));
8570
0
      else      /* lwi37.sp */
8571
0
  insn = N32_TYPE2 (LWI, N16_RT38 (insn16), N32_REG_SP,
8572
0
        N16_IMM7U (insn16));
8573
0
      goto done;
8574
0
    }
8575
8576
0
 done:
8577
0
  if (insn & 0x80000000)
8578
0
    return 0;
8579
8580
0
  if (pinsn)
8581
0
    *pinsn = insn;
8582
0
  return 1;
8583
0
}
8584

8585
8586
static bool
8587
is_sda_access_insn (unsigned long insn)
8588
0
{
8589
0
  switch (N32_OP6 (insn))
8590
0
    {
8591
0
    case N32_OP6_LWI:
8592
0
    case N32_OP6_LHI:
8593
0
    case N32_OP6_LHSI:
8594
0
    case N32_OP6_LBI:
8595
0
    case N32_OP6_LBSI:
8596
0
    case N32_OP6_SWI:
8597
0
    case N32_OP6_SHI:
8598
0
    case N32_OP6_SBI:
8599
0
    case N32_OP6_LWC:
8600
0
    case N32_OP6_LDC:
8601
0
    case N32_OP6_SWC:
8602
0
    case N32_OP6_SDC:
8603
0
      return true;
8604
0
    default:
8605
0
      ;
8606
0
    }
8607
0
  return false;
8608
0
}
8609
8610
static unsigned long
8611
turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8612
0
{
8613
0
  uint32_t oinsn = 0;
8614
8615
0
  switch (type)
8616
0
    {
8617
0
    case R_NDS32_GOT_LO12:
8618
0
    case R_NDS32_GOTOFF_LO12:
8619
0
    case R_NDS32_PLTREL_LO12:
8620
0
    case R_NDS32_PLT_GOTREL_LO12:
8621
0
    case R_NDS32_LO12S0_RELA:
8622
0
      switch (N32_OP6 (insn))
8623
0
  {
8624
0
  case N32_OP6_LBI:
8625
    /* lbi.gp */
8626
0
    oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8627
0
    break;
8628
0
  case N32_OP6_LBSI:
8629
    /* lbsi.gp */
8630
0
    oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8631
0
    break;
8632
0
  case N32_OP6_SBI:
8633
    /* sbi.gp */
8634
0
    oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8635
0
    break;
8636
0
  case N32_OP6_ORI:
8637
    /* addi.gp */
8638
0
    oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8639
0
    break;
8640
0
  }
8641
0
      break;
8642
8643
0
    case R_NDS32_LO12S1_RELA:
8644
0
      switch (N32_OP6 (insn))
8645
0
  {
8646
0
  case N32_OP6_LHI:
8647
    /* lhi.gp */
8648
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8649
0
    break;
8650
0
  case N32_OP6_LHSI:
8651
    /* lhsi.gp */
8652
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8653
0
    break;
8654
0
  case N32_OP6_SHI:
8655
    /* shi.gp */
8656
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8657
0
    break;
8658
0
  }
8659
0
      break;
8660
8661
0
    case R_NDS32_LO12S2_RELA:
8662
0
      switch (N32_OP6 (insn))
8663
0
  {
8664
0
  case N32_OP6_LWI:
8665
    /* lwi.gp */
8666
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8667
0
    break;
8668
0
  case N32_OP6_SWI:
8669
    /* swi.gp */
8670
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8671
0
    break;
8672
0
  }
8673
0
      break;
8674
8675
0
    case R_NDS32_LO12S2_DP_RELA:
8676
0
    case R_NDS32_LO12S2_SP_RELA:
8677
0
      oinsn = (insn & 0x7ff07000) | (N32_REG_GP << 15);
8678
0
      break;
8679
0
    }
8680
8681
0
  if (oinsn)
8682
0
    *pinsn = oinsn;
8683
8684
0
  return oinsn != 0;
8685
0
}
8686
8687
/* Linker hasn't found the correct merge section for non-section symbol
8688
   in relax time, this work is left to the function elf_link_input_bfd().
8689
   So for non-section symbol, _bfd_merged_section_offset is also needed
8690
   to find the correct symbol address.  */
8691
8692
static bfd_vma
8693
nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8694
        asection **psec, Elf_Internal_Rela *rel)
8695
0
{
8696
0
  asection *sec = *psec;
8697
0
  bfd_vma relocation;
8698
8699
0
  relocation = (sec->output_section->vma
8700
0
    + sec->output_offset + sym->st_value);
8701
0
  if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8702
0
    {
8703
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8704
0
  rel->r_addend =
8705
0
    _bfd_merged_section_offset (abfd, psec,
8706
0
              sym->st_value + rel->r_addend);
8707
0
      else
8708
0
  rel->r_addend =
8709
0
    _bfd_merged_section_offset (abfd, psec,
8710
0
              sym->st_value) + rel->r_addend;
8711
8712
0
      if (sec != *psec)
8713
0
  {
8714
    /* If we have changed the section, and our original section is
8715
       marked with SEC_EXCLUDE, it means that the original
8716
       SEC_MERGE section has been completely subsumed in some
8717
       other SEC_MERGE section.  In this case, we need to leave
8718
       some info around for --emit-relocs.  */
8719
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
8720
0
      sec->kept_section = *psec;
8721
0
    sec = *psec;
8722
0
  }
8723
0
      rel->r_addend -= relocation;
8724
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
8725
0
    }
8726
0
  return relocation;
8727
0
}
8728
8729
static bfd_vma
8730
calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8731
        Elf_Internal_Sym *isymbuf,
8732
        Elf_Internal_Shdr *symtab_hdr)
8733
0
{
8734
0
  bfd_signed_vma foff;
8735
0
  bfd_vma symval, addend;
8736
0
  Elf_Internal_Rela irel_fn;
8737
0
  Elf_Internal_Sym *isym;
8738
0
  asection *sym_sec;
8739
8740
  /* Get the value of the symbol referred to by the reloc.  */
8741
0
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8742
0
    {
8743
      /* A local symbol.  */
8744
0
      isym = isymbuf + ELF32_R_SYM (irel->r_info);
8745
8746
0
      if (isym->st_shndx == SHN_UNDEF)
8747
0
  sym_sec = bfd_und_section_ptr;
8748
0
      else if (isym->st_shndx == SHN_ABS)
8749
0
  sym_sec = bfd_abs_section_ptr;
8750
0
      else if (isym->st_shndx == SHN_COMMON)
8751
0
  sym_sec = bfd_com_section_ptr;
8752
0
      else
8753
0
  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8754
0
      memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8755
0
      symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8756
0
      addend = irel_fn.r_addend;
8757
0
    }
8758
0
  else
8759
0
    {
8760
0
      unsigned long indx;
8761
0
      struct elf_link_hash_entry *h;
8762
8763
      /* An external symbol.  */
8764
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8765
0
      h = elf_sym_hashes (abfd)[indx];
8766
0
      BFD_ASSERT (h != NULL);
8767
8768
0
      while (h->root.type == bfd_link_hash_indirect
8769
0
       || h->root.type == bfd_link_hash_warning)
8770
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
8771
8772
0
      if (h->root.type != bfd_link_hash_defined
8773
0
    && h->root.type != bfd_link_hash_defweak)
8774
  /* This appears to be a reference to an undefined
8775
     symbol.  Just ignore it--it will be caught by the
8776
     regular reloc processing.  */
8777
0
  return 0;
8778
8779
0
      if (h->root.u.def.section->flags & SEC_MERGE)
8780
0
  {
8781
0
    sym_sec = h->root.u.def.section;
8782
0
    symval = _bfd_merged_section_offset (abfd, &sym_sec,
8783
0
                 h->root.u.def.value);
8784
0
    symval = symval + sym_sec->output_section->vma
8785
0
       + sym_sec->output_offset;
8786
0
  }
8787
0
      else
8788
0
  symval = (h->root.u.def.value
8789
0
      + h->root.u.def.section->output_section->vma
8790
0
      + h->root.u.def.section->output_offset);
8791
0
      addend = irel->r_addend;
8792
0
    }
8793
8794
0
  foff = symval + addend;
8795
8796
0
  return foff;
8797
0
}
8798
8799
static int
8800
is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8801
        asection *sec, Elf_Internal_Rela *rel)
8802
0
{
8803
0
  bfd_byte *contents;
8804
0
  unsigned short insn16;
8805
8806
0
  if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8807
0
    return false;
8808
0
  contents = elf_section_data (sec)->this_hdr.contents;
8809
0
  insn16 = bfd_getb16 (contents + rel->r_offset);
8810
0
  if (insn16 == NDS32_NOP16)
8811
0
    return true;
8812
0
  return false;
8813
0
}
8814
8815
/* It checks whether the instruction could be converted to
8816
   16-bit form and returns the converted one.
8817
8818
   `internal_relocs' is supposed to be sorted.  */
8819
8820
static int
8821
is_convert_32_to_16 (bfd *abfd, asection *sec,
8822
         Elf_Internal_Rela *reloc,
8823
         Elf_Internal_Rela *internal_relocs,
8824
         Elf_Internal_Rela *irelend,
8825
         uint16_t *insn16)
8826
0
{
8827
0
#define NORMAL_32_TO_16 (1 << 0)
8828
0
#define SPECIAL_32_TO_16 (1 << 1)
8829
0
  bfd_byte *contents = NULL;
8830
0
  bfd_signed_vma off;
8831
0
  bfd_vma mem_addr;
8832
0
  uint32_t insn = 0;
8833
0
  Elf_Internal_Rela *pc_rel;
8834
0
  Elf_Internal_Shdr *symtab_hdr;
8835
0
  Elf_Internal_Sym *isymbuf = NULL;
8836
0
  int convert_type;
8837
0
  bfd_vma offset;
8838
8839
0
  if (reloc->r_offset + 4 > sec->size)
8840
0
    return false;
8841
8842
0
  offset = reloc->r_offset;
8843
8844
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true))
8845
0
    return false;
8846
0
  insn = bfd_getb32 (contents + offset);
8847
8848
0
  if (bfd_elf_nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8849
0
    convert_type = NORMAL_32_TO_16;
8850
0
  else if (special_convert_32_to_16 (insn, insn16, reloc))
8851
0
    convert_type = SPECIAL_32_TO_16;
8852
0
  else
8853
0
    return false;
8854
8855
0
  symtab_hdr = &elf_symtab_hdr (abfd);
8856
0
  if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8857
0
    return false;
8858
8859
  /* Find the first relocation of the same relocation-type,
8860
     so we iteratie them forward.  */
8861
0
  pc_rel = reloc;
8862
0
  while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8863
0
    pc_rel--;
8864
8865
0
  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8866
0
    {
8867
0
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8868
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8869
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8870
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8871
0
  {
8872
0
    off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8873
0
    if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8874
0
        || off == 0)
8875
0
      return false;
8876
0
    break;
8877
0
  }
8878
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8879
0
  {
8880
    /* movi => movi55  */
8881
0
    mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8882
0
                 symtab_hdr);
8883
    /* mem_addr is unsigned, but the value should
8884
       be between [-16, 15].  */
8885
0
    if ((mem_addr + 0x10) >> 5)
8886
0
      return false;
8887
0
    break;
8888
0
  }
8889
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8890
0
         || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8891
0
  {
8892
    /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8893
       because it can be relaxed to addi for TLS_LE_ADD.  */
8894
0
    return false;
8895
0
  }
8896
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8897
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8898
0
         && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8899
0
         && convert_type == SPECIAL_32_TO_16)
8900
0
  {
8901
    /* fp-as-gp
8902
       We've selected a best fp-base for this access, so we can
8903
       always resolve it anyway.  Do nothing.  */
8904
0
    break;
8905
0
  }
8906
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8907
0
    && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8908
0
         || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8909
0
       && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8910
0
         || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8911
0
       && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8912
0
  {
8913
    /* Prevent unresolved addi instruction translate
8914
       to addi45 or addi333.  */
8915
0
    return false;
8916
0
  }
8917
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8918
0
  {
8919
0
    off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8920
0
    if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8921
0
      return false;
8922
0
    break;
8923
0
  }
8924
0
    }
8925
8926
0
  return true;
8927
0
}
8928
8929
static void
8930
nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8931
        Elf_Internal_Rela *reloc,
8932
        Elf_Internal_Rela *internal_relocs,
8933
        Elf_Internal_Rela *irelend,
8934
        unsigned short insn16)
8935
0
{
8936
0
  Elf_Internal_Rela *pc_rel;
8937
0
  bfd_vma offset;
8938
8939
0
  offset = reloc->r_offset;
8940
0
  bfd_putb16 (insn16, contents + offset);
8941
  /* Find the first relocation of the same relocation-type,
8942
     so we iteratie them forward.  */
8943
0
  pc_rel = reloc;
8944
0
  while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8945
0
    pc_rel--;
8946
8947
0
  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8948
0
    {
8949
0
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8950
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8951
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8952
0
  {
8953
0
    pc_rel->r_info =
8954
0
      ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8955
0
  }
8956
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8957
0
  pc_rel->r_info =
8958
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8959
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8960
0
  pc_rel->r_info =
8961
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8962
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8963
0
         || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8964
0
  pc_rel->r_info =
8965
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8966
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8967
0
  pc_rel->r_info =
8968
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8969
0
    }
8970
0
}
8971
8972
/* Find a relocation of type specified by `reloc_type'
8973
   of the same r_offset with reloc.
8974
   If not found, return irelend.
8975
8976
   Assuming relocations are sorted by r_offset,
8977
   we find the relocation from `reloc' backward untill relocs,
8978
   or find it from `reloc' forward untill irelend.  */
8979
8980
static Elf_Internal_Rela *
8981
find_relocs_at_address (Elf_Internal_Rela *reloc,
8982
      Elf_Internal_Rela *relocs,
8983
      Elf_Internal_Rela *irelend,
8984
      enum elf_nds32_reloc_type reloc_type)
8985
0
{
8986
0
  Elf_Internal_Rela *rel_t;
8987
8988
  /* Find backward.  */
8989
0
  for (rel_t = reloc;
8990
0
       rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8991
0
       rel_t--)
8992
0
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8993
0
      return rel_t;
8994
8995
  /* We didn't find it backward.  Try find it forward.  */
8996
0
  for (rel_t = reloc;
8997
0
       rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8998
0
       rel_t++)
8999
0
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
9000
0
      return rel_t;
9001
9002
0
  return irelend;
9003
0
}
9004
9005
/* Find a relocation of specified type and offset.
9006
   `reloc' is just a refence point to find a relocation at specified offset.
9007
   If not found, return irelend.
9008
9009
   Assuming relocations are sorted by r_offset,
9010
   we find the relocation from `reloc' backward untill relocs,
9011
   or find it from `reloc' forward untill irelend.  */
9012
9013
static Elf_Internal_Rela *
9014
find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
9015
           Elf_Internal_Rela *relocs,
9016
           Elf_Internal_Rela *irelend,
9017
           enum elf_nds32_reloc_type reloc_type,
9018
           bfd_vma offset_p)
9019
0
{
9020
0
  Elf_Internal_Rela *rel_t = NULL;
9021
9022
  /* First, we try to find a relocation of offset `offset_p',
9023
     and then we use find_relocs_at_address to find specific type.  */
9024
9025
0
  if (reloc->r_offset > offset_p)
9026
0
    {
9027
      /* Find backward.  */
9028
0
      for (rel_t = reloc;
9029
0
     rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
9030
0
  /* Do nothing.  */;
9031
0
    }
9032
0
  else if (reloc->r_offset < offset_p)
9033
0
    {
9034
      /* Find forward.  */
9035
0
      for (rel_t = reloc;
9036
0
     rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
9037
0
  /* Do nothing.  */;
9038
0
    }
9039
0
  else
9040
0
    rel_t = reloc;
9041
9042
  /* Not found?  */
9043
0
  if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
9044
0
    return irelend;
9045
9046
0
  return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
9047
0
}
9048
9049
typedef struct nds32_elf_blank nds32_elf_blank_t;
9050
struct nds32_elf_blank
9051
{
9052
  /* Where the blank begins.  */
9053
  bfd_vma offset;
9054
  /* The size of the blank.  */
9055
  bfd_vma size;
9056
  /* The accumulative size before this blank.  */
9057
  bfd_vma total_size;
9058
  nds32_elf_blank_t *next;
9059
  nds32_elf_blank_t *prev;
9060
};
9061
9062
static nds32_elf_blank_t *blank_free_list = NULL;
9063
9064
static nds32_elf_blank_t *
9065
create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
9066
0
{
9067
0
  nds32_elf_blank_t *blank_t;
9068
9069
0
  if (blank_free_list)
9070
0
    {
9071
0
      blank_t = blank_free_list;
9072
0
      blank_free_list = blank_free_list->next;
9073
0
    }
9074
0
  else
9075
0
    blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9076
9077
0
  if (blank_t == NULL)
9078
0
    return NULL;
9079
9080
0
  blank_t->offset = offset_p;
9081
0
  blank_t->size = size_p;
9082
0
  blank_t->total_size = 0;
9083
0
  blank_t->next = NULL;
9084
0
  blank_t->prev = NULL;
9085
9086
0
  return blank_t;
9087
0
}
9088
9089
static void
9090
remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9091
0
{
9092
0
  if (blank_free_list)
9093
0
    {
9094
0
      blank_free_list->prev = blank_p;
9095
0
      blank_p->next = blank_free_list;
9096
0
    }
9097
0
  else
9098
0
    blank_p->next = NULL;
9099
9100
0
  blank_p->prev = NULL;
9101
0
  blank_free_list = blank_p;
9102
0
}
9103
9104
static void
9105
clean_nds32_elf_blank (void)
9106
0
{
9107
0
  nds32_elf_blank_t *blank_t;
9108
9109
0
  while (blank_free_list)
9110
0
    {
9111
0
      blank_t = blank_free_list;
9112
0
      blank_free_list = blank_free_list->next;
9113
0
      free (blank_t);
9114
0
    }
9115
0
}
9116
9117
static nds32_elf_blank_t *
9118
search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9119
0
{
9120
0
  nds32_elf_blank_t *blank_t;
9121
9122
0
  if (!blank_p)
9123
0
    return NULL;
9124
0
  blank_t = blank_p;
9125
9126
0
  while (blank_t && addr < blank_t->offset)
9127
0
    blank_t = blank_t->prev;
9128
0
  while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9129
0
    blank_t = blank_t->next;
9130
9131
0
  return blank_t;
9132
0
}
9133
9134
static bfd_vma
9135
get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9136
         int overwrite)
9137
0
{
9138
0
  nds32_elf_blank_t *blank_t;
9139
9140
0
  blank_t = search_nds32_elf_blank (*blank_p, addr);
9141
0
  if (!blank_t)
9142
0
    return 0;
9143
9144
0
  if (overwrite)
9145
0
    *blank_p = blank_t;
9146
9147
0
  if (addr < blank_t->offset + blank_t->size)
9148
0
    return blank_t->total_size + (addr - blank_t->offset);
9149
0
  else
9150
0
    return blank_t->total_size + blank_t->size;
9151
0
}
9152
9153
static bool
9154
insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9155
0
{
9156
0
  nds32_elf_blank_t *blank_t, *blank_t2;
9157
9158
0
  if (!*blank_p)
9159
0
    {
9160
0
      *blank_p = create_nds32_elf_blank (addr, len);
9161
0
      return *blank_p != NULL;
9162
0
    }
9163
9164
0
  blank_t = search_nds32_elf_blank (*blank_p, addr);
9165
9166
0
  if (blank_t == NULL)
9167
0
    {
9168
0
      blank_t = create_nds32_elf_blank (addr, len);
9169
0
      if (!blank_t)
9170
0
  return false;
9171
0
      while ((*blank_p)->prev != NULL)
9172
0
  *blank_p = (*blank_p)->prev;
9173
0
      blank_t->next = *blank_p;
9174
0
      (*blank_p)->prev = blank_t;
9175
0
      (*blank_p) = blank_t;
9176
0
      return true;
9177
0
    }
9178
9179
0
  if (addr < blank_t->offset + blank_t->size)
9180
0
    {
9181
      /* Extend the origin blank.  */
9182
0
      if (addr + len > blank_t->offset + blank_t->size)
9183
0
  blank_t->size = addr + len - blank_t->offset;
9184
0
    }
9185
0
  else
9186
0
    {
9187
0
      blank_t2 = create_nds32_elf_blank (addr, len);
9188
0
      if (!blank_t2)
9189
0
  return false;
9190
0
      if (blank_t->next)
9191
0
  {
9192
0
    blank_t->next->prev = blank_t2;
9193
0
    blank_t2->next = blank_t->next;
9194
0
  }
9195
0
      blank_t2->prev = blank_t;
9196
0
      blank_t->next = blank_t2;
9197
0
      *blank_p = blank_t2;
9198
0
    }
9199
9200
0
  return true;
9201
0
}
9202
9203
static bool
9204
insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9205
             bfd_vma len)
9206
0
{
9207
0
  nds32_elf_blank_t *blank_t;
9208
9209
0
  if (!insert_nds32_elf_blank (blank_p, addr, len))
9210
0
    return false;
9211
9212
0
  blank_t = *blank_p;
9213
9214
0
  if (!blank_t->prev)
9215
0
    {
9216
0
      blank_t->total_size = 0;
9217
0
      blank_t = blank_t->next;
9218
0
    }
9219
9220
0
  while (blank_t)
9221
0
    {
9222
0
      blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9223
0
      blank_t = blank_t->next;
9224
0
    }
9225
9226
0
  return true;
9227
0
}
9228
9229
static void
9230
calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9231
0
{
9232
0
  nds32_elf_blank_t *blank_t;
9233
0
  bfd_vma total_size = 0;
9234
9235
0
  if (!blank_p)
9236
0
    return;
9237
9238
0
  blank_t = blank_p;
9239
0
  while (blank_t->prev)
9240
0
    blank_t = blank_t->prev;
9241
0
  while (blank_t)
9242
0
    {
9243
0
      blank_t->total_size = total_size;
9244
0
      total_size += blank_t->size;
9245
0
      blank_t = blank_t->next;
9246
0
    }
9247
0
}
9248
9249
static bool
9250
nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9251
             nds32_elf_blank_t *blank_p)
9252
0
{
9253
0
  Elf_Internal_Shdr *symtab_hdr;  /* Symbol table header of this bfd.  */
9254
0
  Elf_Internal_Sym *isym = NULL; /* Symbol table of this bfd.  */
9255
0
  Elf_Internal_Sym *isymend;    /* Symbol entry iterator.  */
9256
0
  unsigned int sec_shndx;   /* The section the be relaxed.  */
9257
0
  bfd_byte *contents;     /* Contents data of iterating section.  */
9258
0
  Elf_Internal_Rela *internal_relocs;
9259
0
  Elf_Internal_Rela *irel;
9260
0
  Elf_Internal_Rela *irelend;
9261
0
  struct elf_link_hash_entry **sym_hashes;
9262
0
  struct elf_link_hash_entry **end_hashes;
9263
0
  unsigned int symcount;
9264
0
  asection *sect;
9265
0
  nds32_elf_blank_t *blank_t;
9266
0
  nds32_elf_blank_t *blank_t2;
9267
0
  nds32_elf_blank_t *blank_head;
9268
9269
0
  blank_head = blank_t = blank_p;
9270
0
  while (blank_head->prev != NULL)
9271
0
    blank_head = blank_head->prev;
9272
0
  while (blank_t->next != NULL)
9273
0
    blank_t = blank_t->next;
9274
9275
0
  if (blank_t->offset + blank_t->size <= sec->size)
9276
0
    {
9277
0
      blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9278
0
      blank_t->next->prev = blank_t;
9279
0
    }
9280
0
  if (blank_head->offset > 0)
9281
0
    {
9282
0
      blank_head->prev = create_nds32_elf_blank (0, 0);
9283
0
      blank_head->prev->next = blank_head;
9284
0
      blank_head = blank_head->prev;
9285
0
    }
9286
9287
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9288
9289
  /* The deletion must stop at the next ALIGN reloc for an alignment
9290
     power larger than the number of bytes we are deleting.  */
9291
9292
0
  symtab_hdr = &elf_symtab_hdr (abfd);
9293
0
  if (!nds32_get_local_syms (abfd, sec, &isym))
9294
0
    return false;
9295
9296
0
  if (isym == NULL)
9297
0
    {
9298
0
      isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9299
0
           symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9300
0
      symtab_hdr->contents = (bfd_byte *) isym;
9301
0
    }
9302
9303
0
  if (isym == NULL || symtab_hdr->sh_info == 0)
9304
0
    return false;
9305
9306
0
  blank_t = blank_head;
9307
0
  calc_nds32_blank_total (blank_head);
9308
9309
0
  for (sect = abfd->sections; sect != NULL; sect = sect->next)
9310
0
    {
9311
      /* Adjust all the relocs.  */
9312
9313
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
9314
0
      internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9315
0
               true);
9316
0
      irelend = internal_relocs + sect->reloc_count;
9317
9318
0
      blank_t = blank_head;
9319
0
      blank_t2 = blank_head;
9320
9321
0
      if (!(sect->flags & SEC_RELOC))
9322
0
  continue;
9323
9324
0
      contents = NULL;
9325
0
      nds32_get_section_contents (abfd, sect, &contents, true);
9326
9327
0
      for (irel = internal_relocs; irel < irelend; irel++)
9328
0
  {
9329
0
    bfd_vma raddr;
9330
9331
0
    if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9332
0
        && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9333
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9334
0
      {
9335
0
        unsigned long val = 0;
9336
0
        unsigned long mask;
9337
0
        long before, between;
9338
0
        long offset = 0;
9339
9340
0
        switch (ELF32_R_TYPE (irel->r_info))
9341
0
    {
9342
0
    case R_NDS32_DIFF8:
9343
0
      offset = bfd_get_8 (abfd, contents + irel->r_offset);
9344
0
      break;
9345
0
    case R_NDS32_DIFF16:
9346
0
      offset = bfd_get_16 (abfd, contents + irel->r_offset);
9347
0
      break;
9348
0
    case R_NDS32_DIFF32:
9349
0
      val = bfd_get_32 (abfd, contents + irel->r_offset);
9350
      /* Get the signed bit and mask for the high part.  The
9351
         gcc will alarm when right shift 32-bit since the
9352
         type size of long may be 32-bit.  */
9353
0
      mask = 0 - (val >> 31);
9354
0
      if (mask)
9355
0
        offset = (val | (mask - 0xffffffff));
9356
0
      else
9357
0
        offset = val;
9358
0
      break;
9359
0
    default:
9360
0
      BFD_ASSERT (0);
9361
0
    }
9362
9363
        /*      DIFF value
9364
    0      |encoded in location|
9365
    |------------|-------------------|---------
9366
          sym+off(addend)
9367
    -- before ---| *****************
9368
    --------------------- between ---|
9369
9370
    We only care how much data are relax between DIFF,
9371
    marked as ***.  */
9372
9373
0
        before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9374
0
        between = get_nds32_elf_blank_total (&blank_t,
9375
0
               irel->r_addend + offset, 0);
9376
0
        if (between == before)
9377
0
    goto done_adjust_diff;
9378
9379
0
        switch (ELF32_R_TYPE (irel->r_info))
9380
0
    {
9381
0
    case R_NDS32_DIFF8:
9382
0
      bfd_put_8 (abfd, offset - (between - before),
9383
0
           contents + irel->r_offset);
9384
0
      break;
9385
0
    case R_NDS32_DIFF16:
9386
0
      bfd_put_16 (abfd, offset - (between - before),
9387
0
            contents + irel->r_offset);
9388
0
      break;
9389
0
    case R_NDS32_DIFF32:
9390
0
      bfd_put_32 (abfd, offset - (between - before),
9391
0
            contents + irel->r_offset);
9392
0
      break;
9393
0
    }
9394
0
      }
9395
0
    else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9396
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9397
0
      {
9398
0
        bfd_vma val;
9399
0
        unsigned long before, between;
9400
0
        bfd_byte *endp, *p;
9401
9402
0
        p = contents + irel->r_offset;
9403
0
        endp = contents + sec->size;
9404
0
        val = _bfd_safe_read_leb128 (abfd, &p, false, endp);
9405
9406
0
        before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9407
0
        between = get_nds32_elf_blank_total (&blank_t,
9408
0
               irel->r_addend + val, 0);
9409
0
        if (between == before)
9410
0
    goto done_adjust_diff;
9411
9412
0
        endp = p - 1;
9413
0
        p = contents + irel->r_offset;
9414
0
        memset (p, 0x80, endp - p);
9415
0
        *endp = 0;
9416
0
        p = write_uleb128 (p, val - (between - before)) - 1;
9417
0
        if (p < endp)
9418
0
    *p |= 0x80;
9419
0
      }
9420
0
  done_adjust_diff:
9421
9422
0
    if (sec == sect)
9423
0
      {
9424
0
        raddr = irel->r_offset;
9425
0
        irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9426
0
                 irel->r_offset, 1);
9427
9428
0
        if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9429
0
    continue;
9430
0
        if (blank_t2 && blank_t2->next
9431
0
      && (blank_t2->offset > raddr
9432
0
          || blank_t2->next->offset <= raddr))
9433
0
    _bfd_error_handler
9434
0
      (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9435
0
       abfd);
9436
9437
        /* Mark reloc in deleted portion as NONE.
9438
     For some relocs like R_NDS32_LABEL that doesn't modify the
9439
     content in the section.  R_NDS32_LABEL doesn't belong to the
9440
     instruction in the section, so we should preserve it.  */
9441
0
        if (raddr >= blank_t2->offset
9442
0
      && raddr < blank_t2->offset + blank_t2->size
9443
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9444
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9445
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9446
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9447
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9448
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9449
0
    {
9450
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9451
0
                 R_NDS32_NONE);
9452
0
      continue;
9453
0
    }
9454
0
      }
9455
9456
0
    if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9457
0
        || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9458
0
        || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9459
0
      continue;
9460
9461
0
    if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9462
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9463
0
        && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9464
0
      {
9465
0
        if (irel->r_addend <= sec->size)
9466
0
    irel->r_addend -=
9467
0
      get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9468
0
      }
9469
0
  }
9470
0
    }
9471
9472
  /* Adjust the local symbols defined in this section.  */
9473
0
  blank_t = blank_head;
9474
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9475
0
    {
9476
0
      if (isym->st_shndx == sec_shndx)
9477
0
  {
9478
0
    if (isym->st_value <= sec->size)
9479
0
      {
9480
0
        bfd_vma ahead;
9481
0
        bfd_vma orig_addr = isym->st_value;
9482
9483
0
        ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9484
0
        isym->st_value -= ahead;
9485
9486
        /* Adjust function size.  */
9487
0
        if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9488
0
      && isym->st_size > 0)
9489
0
    isym->st_size -=
9490
0
      get_nds32_elf_blank_total
9491
0
      (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9492
0
      }
9493
0
  }
9494
0
    }
9495
9496
  /* Now adjust the global symbols defined in this section.  */
9497
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9498
0
        - symtab_hdr->sh_info);
9499
0
  sym_hashes = elf_sym_hashes (abfd);
9500
0
  end_hashes = sym_hashes + symcount;
9501
0
  blank_t = blank_head;
9502
0
  for (; sym_hashes < end_hashes; sym_hashes++)
9503
0
    {
9504
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
9505
9506
0
      if ((sym_hash->root.type == bfd_link_hash_defined
9507
0
     || sym_hash->root.type == bfd_link_hash_defweak)
9508
0
    && sym_hash->root.u.def.section == sec)
9509
0
  {
9510
0
    if (sym_hash->root.u.def.value <= sec->size)
9511
0
      {
9512
0
        bfd_vma ahead;
9513
0
        bfd_vma orig_addr = sym_hash->root.u.def.value;
9514
9515
0
        ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9516
0
        sym_hash->root.u.def.value -= ahead;
9517
9518
        /* Adjust function size.  */
9519
0
        if (sym_hash->type == STT_FUNC)
9520
0
    sym_hash->size -=
9521
0
      get_nds32_elf_blank_total
9522
0
      (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9523
9524
0
      }
9525
0
  }
9526
0
    }
9527
9528
0
  contents = elf_section_data (sec)->this_hdr.contents;
9529
0
  blank_t = blank_head;
9530
0
  while (blank_t->next)
9531
0
    {
9532
      /* Actually delete the bytes.  */
9533
9534
      /* If current blank is the last blank overlap with current section,
9535
   go to finish process.  */
9536
0
      if (sec->size <= (blank_t->next->offset))
9537
0
  break;
9538
9539
0
      memmove (contents + blank_t->offset - blank_t->total_size,
9540
0
         contents + blank_t->offset + blank_t->size,
9541
0
         blank_t->next->offset - (blank_t->offset + blank_t->size));
9542
9543
0
      blank_t = blank_t->next;
9544
0
    }
9545
9546
0
  if (sec->size > (blank_t->offset + blank_t->size))
9547
0
    {
9548
      /* There are remaining code between blank and section boundary.
9549
   Move the remaining code to appropriate location.  */
9550
0
      memmove (contents + blank_t->offset - blank_t->total_size,
9551
0
         contents + blank_t->offset + blank_t->size,
9552
0
         sec->size - (blank_t->offset + blank_t->size));
9553
0
      sec->size -= blank_t->total_size + blank_t->size;
9554
0
    }
9555
0
  else
9556
    /* This blank is not entirely included in the section,
9557
       reduce the section size by only part of the blank size.  */
9558
0
    sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9559
9560
0
  while (blank_head)
9561
0
    {
9562
0
      blank_t = blank_head;
9563
0
      blank_head = blank_head->next;
9564
0
      remove_nds32_elf_blank (blank_t);
9565
0
    }
9566
9567
0
  return true;
9568
0
}
9569
9570
/* Get the contents of a section.  */
9571
9572
static int
9573
nds32_get_section_contents (bfd *abfd, asection *sec,
9574
          bfd_byte **contents_p, bool cache)
9575
55
{
9576
  /* Get the section contents.  */
9577
55
  if (elf_section_data (sec)->this_hdr.contents != NULL)
9578
0
    *contents_p = elf_section_data (sec)->this_hdr.contents;
9579
55
  else
9580
55
    {
9581
55
      if (!bfd_get_full_section_contents (abfd, sec, contents_p))
9582
3
  return false;
9583
52
      if (cache)
9584
0
  elf_section_data (sec)->this_hdr.contents = *contents_p;
9585
52
    }
9586
9587
52
  return true;
9588
55
}
9589
9590
/* Get the contents of the internal symbol of abfd.  */
9591
9592
static int
9593
nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9594
          Elf_Internal_Sym **isymbuf_p)
9595
0
{
9596
0
  Elf_Internal_Shdr *symtab_hdr;
9597
0
  symtab_hdr = &elf_symtab_hdr (abfd);
9598
9599
  /* Read this BFD's local symbols if we haven't done so already.  */
9600
0
  if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9601
0
    {
9602
0
      *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9603
0
      if (*isymbuf_p == NULL)
9604
0
  {
9605
0
    *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9606
0
               symtab_hdr->sh_info, 0,
9607
0
               NULL, NULL, NULL);
9608
0
    if (*isymbuf_p == NULL)
9609
0
      return false;
9610
0
  }
9611
0
    }
9612
0
  symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9613
9614
0
  return true;
9615
0
}
9616
9617
/* Range of small data.  */
9618
static bfd_vma sdata_range[2][2];
9619
static bfd_vma const sdata_init_range[2] =
9620
{ ACCURATE_12BIT_S1, ACCURATE_19BIT };
9621
9622
static int
9623
nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9624
         bfd_byte *contents, bfd_vma addr)
9625
0
{
9626
0
  unsigned long insn = bfd_getb32 (contents + addr);
9627
9628
0
  if (insn & 0x80000000)
9629
0
    return 2;
9630
9631
0
  return 4;
9632
0
}
9633
9634
/* Set the gp relax range.  We have to measure the safe range
9635
   to do gp relaxation.  */
9636
9637
static void
9638
relax_range_measurement (bfd *abfd, struct bfd_link_info *link_info)
9639
0
{
9640
0
  asection *sec_f, *sec_b;
9641
  /* For upper bound.   */
9642
0
  bfd_vma maxpgsz;
9643
0
  bfd_vma align;
9644
0
  static int decide_relax_range = 0;
9645
0
  int i;
9646
0
  int range_number = ARRAY_SIZE (sdata_init_range);
9647
9648
0
  if (decide_relax_range)
9649
0
    return;
9650
0
  decide_relax_range = 1;
9651
9652
0
  if (sda_rela_sec == NULL)
9653
0
    {
9654
      /* Since there is no data sections, we assume the range is page size.  */
9655
0
      for (i = 0; i < range_number; i++)
9656
0
  {
9657
0
    sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9658
0
    sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9659
0
  }
9660
0
      return;
9661
0
    }
9662
9663
  /* Get the biggest alignment power after the gp located section.  */
9664
0
  sec_f = sda_rela_sec->output_section;
9665
0
  sec_b = sec_f->next;
9666
0
  align = 0;
9667
0
  while (sec_b != NULL)
9668
0
    {
9669
0
      if ((unsigned)(1 << sec_b->alignment_power) > align)
9670
0
  align = (1 << sec_b->alignment_power);
9671
0
      sec_b = sec_b->next;
9672
0
    }
9673
9674
0
  if (link_info != NULL)
9675
0
    maxpgsz = link_info->maxpagesize;
9676
0
  else
9677
0
    maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9678
  /* I guess we can not determine the section before
9679
     gp located section, so we assume the align is max page size.  */
9680
0
  for (i = 0; i < range_number; i++)
9681
0
    {
9682
0
      sdata_range[i][1] = sdata_init_range[i] - align;
9683
0
      BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9684
0
      sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9685
0
      BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9686
0
    }
9687
0
}
9688
9689
/* These are macros used to check flags encoded in r_addend.
9690
   They are only used by nds32_elf_relax_section ().  */
9691
0
#define GET_SEQ_LEN(addend)     ((addend) & 0x000000ff)
9692
0
#define IS_1ST_CONVERT(addend)  ((addend) & 0x80000000)
9693
#define IS_OPTIMIZE(addend)     ((addend) & 0x40000000)
9694
0
#define IS_16BIT_ON(addend)     ((addend) & 0x20000000)
9695
9696
static const char * unrecognized_reloc_msg =
9697
  /* xgettext:c-format */
9698
  N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9699
9700
/* Relax LONGCALL1 relocation for nds32_elf_relax_section.  */
9701
9702
static bool
9703
nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9704
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9705
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9706
         Elf_Internal_Shdr *symtab_hdr)
9707
0
{
9708
  /* There are 3 variations for LONGCALL1
9709
     case 4-4-2; 16-bit on, optimize off or optimize for space
9710
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9711
     ori   ta, ta, lo12(symbol) ; LO12S0
9712
     jral5 ta     ;
9713
9714
     case 4-4-4; 16-bit off, optimize don't care
9715
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9716
     ori   ta, ta, lo12(symbol) ; LO12S0
9717
     jral  ta     ;
9718
9719
     case 4-4-4; 16-bit on, optimize for speed
9720
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9721
     ori   ta, ta, lo12(symbol) ; LO12S0
9722
     jral  ta     ;
9723
     Check code for -mlong-calls output.  */
9724
9725
  /* Get the reloc for the address from which the register is
9726
     being loaded.  This reloc will tell us which function is
9727
     actually being called.  */
9728
9729
0
  bfd_vma laddr;
9730
0
  int seq_len;  /* Original length of instruction sequence.  */
9731
0
  uint32_t insn;
9732
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9733
0
  bfd_signed_vma foff;
9734
0
  uint16_t insn16;
9735
9736
0
  irelend = internal_relocs + sec->reloc_count;
9737
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9738
0
  laddr = irel->r_offset;
9739
0
  *insn_len = seq_len;
9740
9741
0
  hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9742
0
             R_NDS32_HI20_RELA, laddr);
9743
0
  lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9744
0
             R_NDS32_LO12S0_ORI_RELA,
9745
0
             laddr + 4);
9746
9747
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9748
0
    {
9749
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9750
0
        (uint64_t) irel->r_offset);
9751
0
      return false;
9752
0
    }
9753
9754
  /* Get the value of the symbol referred to by the reloc.  */
9755
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9756
9757
  /* This condition only happened when symbol is undefined.  */
9758
0
  if (foff == 0
9759
0
      || foff < -CONSERVATIVE_24BIT_S1
9760
0
      || foff >= CONSERVATIVE_24BIT_S1)
9761
0
    return false;
9762
9763
  /* Relax to: jal symbol; 25_PCREL.  */
9764
  /* For simplicity of coding, we are going to modify the section
9765
     contents, the section relocs, and the BFD symbol table.  We
9766
     must tell the rest of the code not to free up this
9767
     information.  It would be possible to instead create a table
9768
     of changes which have to be made, as is done in coff-mips.c;
9769
     that would be more work, but would require less memory when
9770
     the linker is run.  */
9771
9772
  /* Replace the long call with a jal.  */
9773
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9774
0
             R_NDS32_25_PCREL_RELA);
9775
0
  irel->r_addend = hi_irelfn->r_addend;
9776
9777
  /* We don't resolve this here but resolve it in relocate_section.  */
9778
0
  insn = INSN_JAL;
9779
0
  bfd_putb32 (insn, contents + irel->r_offset);
9780
9781
0
  hi_irelfn->r_info =
9782
0
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9783
0
  lo_irelfn->r_info =
9784
0
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9785
0
  *insn_len = 4;
9786
9787
0
  if (seq_len & 0x2)
9788
0
    {
9789
0
      insn16 = NDS32_NOP16;
9790
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9791
0
      lo_irelfn->r_info =
9792
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9793
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9794
0
      *insn_len += 2;
9795
0
    }
9796
0
  return true;
9797
0
}
9798
9799
0
#define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9800
/* Relax LONGCALL2 relocation for nds32_elf_relax_section.  */
9801
9802
static bool
9803
nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9804
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9805
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9806
         Elf_Internal_Shdr *symtab_hdr)
9807
0
{
9808
  /* bltz  rt, .L1   ; LONGCALL2
9809
     jal   symbol   ; 25_PCREL
9810
     .L1: */
9811
9812
  /* Get the reloc for the address from which the register is
9813
     being loaded.  This reloc will tell us which function is
9814
     actually being called.  */
9815
9816
0
  bfd_vma laddr;
9817
0
  uint32_t insn;
9818
0
  Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9819
0
  bfd_signed_vma foff;
9820
9821
0
  irelend = internal_relocs + sec->reloc_count;
9822
0
  laddr = irel->r_offset;
9823
0
  i1_irelfn =
9824
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9825
0
         R_NDS32_25_PCREL_RELA, laddr + 4);
9826
9827
0
  if (i1_irelfn == irelend)
9828
0
    {
9829
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9830
0
        (uint64_t) irel->r_offset);
9831
0
      return false;
9832
0
    }
9833
9834
0
  insn = bfd_getb32 (contents + laddr);
9835
9836
  /* Get the value of the symbol referred to by the reloc.  */
9837
0
  foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9838
9839
0
  if (foff == 0
9840
0
      || foff < -CONSERVATIVE_16BIT_S1
9841
0
      || foff >= CONSERVATIVE_16BIT_S1)
9842
0
    return false;
9843
9844
  /* Relax to bgezal   rt, label ; 17_PCREL
9845
     or   bltzal   rt, label ; 17_PCREL */
9846
9847
  /* Convert to complimentary conditional call.  */
9848
0
  insn = CONVERT_CONDITION_CALL (insn);
9849
9850
  /* For simplicity of coding, we are going to modify the section
9851
     contents, the section relocs, and the BFD symbol table.  We
9852
     must tell the rest of the code not to free up this
9853
     information.  It would be possible to instead create a table
9854
     of changes which have to be made, as is done in coff-mips.c;
9855
     that would be more work, but would require less memory when
9856
     the linker is run.  */
9857
9858
  /* Clean unnessary relocations.  */
9859
0
  i1_irelfn->r_info =
9860
0
    ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9861
0
  cond_irelfn =
9862
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9863
0
         R_NDS32_17_PCREL_RELA, laddr);
9864
0
  if (cond_irelfn != irelend)
9865
0
    cond_irelfn->r_info =
9866
0
      ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9867
9868
  /* Replace the long call with a bgezal.  */
9869
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9870
0
             R_NDS32_17_PCREL_RELA);
9871
0
  irel->r_addend = i1_irelfn->r_addend;
9872
9873
0
  bfd_putb32 (insn, contents + irel->r_offset);
9874
9875
0
  *insn_len = 4;
9876
0
  return true;
9877
0
}
9878
9879
/* Relax LONGCALL3 relocation for nds32_elf_relax_section.  */
9880
9881
static bool
9882
nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9883
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9884
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9885
         Elf_Internal_Shdr *symtab_hdr)
9886
0
{
9887
  /* There are 3 variations for LONGCALL3
9888
     case 4-4-4-2; 16-bit on, optimize off or optimize for space
9889
     bltz  rt,   $1      ; LONGCALL3
9890
     sethi ta,   hi20(symbol)    ; HI20
9891
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9892
     jral5 ta        ;
9893
     $1
9894
9895
     case 4-4-4-4; 16-bit off, optimize don't care
9896
     bltz  rt,   $1      ; LONGCALL3
9897
     sethi ta,   hi20(symbol)    ; HI20
9898
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9899
     jral  ta        ;
9900
     $1
9901
9902
     case 4-4-4-4; 16-bit on, optimize for speed
9903
     bltz  rt,   $1      ; LONGCALL3
9904
     sethi ta,   hi20(symbol)    ; HI20
9905
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9906
     jral  ta        ;
9907
     $1 */
9908
9909
  /* Get the reloc for the address from which the register is
9910
     being loaded.  This reloc will tell us which function is
9911
     actually being called.  */
9912
9913
0
  bfd_vma laddr;
9914
0
  int seq_len;  /* Original length of instruction sequence.  */
9915
0
  uint32_t insn;
9916
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9917
0
  bfd_signed_vma foff;
9918
0
  uint16_t insn16;
9919
9920
0
  irelend = internal_relocs + sec->reloc_count;
9921
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9922
0
  laddr = irel->r_offset;
9923
0
  *insn_len = seq_len;
9924
9925
0
  hi_irelfn =
9926
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9927
0
         R_NDS32_HI20_RELA, laddr + 4);
9928
0
  lo_irelfn =
9929
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9930
0
         R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9931
9932
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9933
0
    {
9934
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9935
0
        (uint64_t) irel->r_offset);
9936
0
      return false;
9937
0
    }
9938
9939
  /* Get the value of the symbol referred to by the reloc.  */
9940
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9941
9942
0
  if (foff == 0
9943
0
      || foff < -CONSERVATIVE_24BIT_S1
9944
0
      || foff >= CONSERVATIVE_24BIT_S1)
9945
0
    return false;
9946
9947
0
  insn = bfd_getb32 (contents + laddr);
9948
0
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9949
0
    {
9950
      /* Relax to  bgezal   rt, label ; 17_PCREL
9951
   or    bltzal   rt, label ; 17_PCREL */
9952
9953
      /* Convert to complimentary conditional call.  */
9954
0
      insn = CONVERT_CONDITION_CALL (insn);
9955
0
      bfd_putb32 (insn, contents + irel->r_offset);
9956
9957
0
      *insn_len = 4;
9958
0
      irel->r_info =
9959
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9960
0
      hi_irelfn->r_info =
9961
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9962
0
      lo_irelfn->r_info =
9963
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9964
9965
0
      cond_irelfn =
9966
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
9967
0
             R_NDS32_17_PCREL_RELA, laddr);
9968
0
      if (cond_irelfn != irelend)
9969
0
  {
9970
0
    cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9971
0
                R_NDS32_17_PCREL_RELA);
9972
0
    cond_irelfn->r_addend = hi_irelfn->r_addend;
9973
0
  }
9974
9975
0
      if (seq_len & 0x2)
9976
0
  {
9977
0
    insn16 = NDS32_NOP16;
9978
0
    bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9979
0
    hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9980
0
              R_NDS32_INSN16);
9981
0
    hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9982
0
    insn_len += 2;
9983
0
  }
9984
0
    }
9985
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9986
0
    {
9987
      /* Relax to the following instruction sequence
9988
   bltz  rt,   $1 ; LONGCALL2
9989
   jal   symbol   ; 25_PCREL
9990
   $1 */
9991
0
      *insn_len = 8;
9992
0
      insn = INSN_JAL;
9993
0
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9994
9995
0
      hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9996
0
          R_NDS32_25_PCREL_RELA);
9997
0
      irel->r_info =
9998
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9999
10000
0
      lo_irelfn->r_info =
10001
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10002
10003
0
      if (seq_len & 0x2)
10004
0
  {
10005
0
    insn16 = NDS32_NOP16;
10006
0
    bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10007
0
    lo_irelfn->r_info =
10008
0
      ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
10009
0
    lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10010
0
    insn_len += 2;
10011
0
  }
10012
0
    }
10013
0
  return true;
10014
0
}
10015
10016
/* Relax LONGJUMP1 relocation for nds32_elf_relax_section.  */
10017
10018
static bool
10019
nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10020
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10021
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10022
         Elf_Internal_Shdr *symtab_hdr)
10023
0
{
10024
  /* There are 3 variations for LONGJUMP1
10025
     case 4-4-2; 16-bit bit on, optimize off or optimize for space
10026
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
10027
     ori   ta, ta, lo12(symbol)  ; LO12S0
10028
     jr5   ta      ;
10029
10030
     case 4-4-4; 16-bit off, optimize don't care
10031
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
10032
     ori   ta, ta, lo12(symbol)  ; LO12S0
10033
     jr    ta      ;
10034
10035
     case 4-4-4; 16-bit on, optimize for speed
10036
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
10037
     ori   ta, ta, lo12(symbol)  ; LO12S0
10038
     jr    ta      ;  */
10039
10040
  /* Get the reloc for the address from which the register is
10041
     being loaded.  This reloc will tell us which function is
10042
     actually being called.  */
10043
10044
0
  bfd_vma laddr;
10045
0
  int seq_len;  /* Original length of instruction sequence.  */
10046
0
  int insn16_on;  /* 16-bit on/off.  */
10047
0
  uint32_t insn;
10048
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
10049
0
  bfd_signed_vma foff;
10050
0
  uint16_t insn16;
10051
0
  unsigned long reloc;
10052
10053
0
  irelend = internal_relocs + sec->reloc_count;
10054
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10055
0
  laddr = irel->r_offset;
10056
0
  *insn_len = seq_len;
10057
0
  insn16_on = IS_16BIT_ON (irel->r_addend);
10058
10059
0
  hi_irelfn =
10060
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10061
0
         R_NDS32_HI20_RELA, laddr);
10062
0
  lo_irelfn =
10063
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10064
0
         R_NDS32_LO12S0_ORI_RELA, laddr + 4);
10065
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
10066
0
    {
10067
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
10068
0
        (uint64_t) irel->r_offset);
10069
0
      return false;
10070
0
    }
10071
10072
  /* Get the value of the symbol referred to by the reloc.  */
10073
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10074
10075
0
  if (foff == 0
10076
0
      || foff >= CONSERVATIVE_24BIT_S1
10077
0
      || foff < -CONSERVATIVE_24BIT_S1)
10078
0
    return false;
10079
10080
0
  if (insn16_on
10081
0
      && foff >= -ACCURATE_8BIT_S1
10082
0
      && foff < ACCURATE_8BIT_S1
10083
0
      && (seq_len & 0x2))
10084
0
    {
10085
      /* j8 label */
10086
      /* 16-bit on, but not optimized for speed.  */
10087
0
      reloc = R_NDS32_9_PCREL_RELA;
10088
0
      insn16 = INSN_J8;
10089
0
      bfd_putb16 (insn16, contents + irel->r_offset);
10090
0
      *insn_len = 2;
10091
0
      irel->r_info =
10092
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10093
0
    }
10094
0
  else
10095
0
    {
10096
      /* j     label */
10097
0
      reloc = R_NDS32_25_PCREL_RELA;
10098
0
      insn = INSN_J;
10099
0
      bfd_putb32 (insn, contents + irel->r_offset);
10100
0
      *insn_len = 4;
10101
0
      irel->r_info =
10102
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10103
0
      irel->r_addend = 0;
10104
0
    }
10105
10106
0
  hi_irelfn->r_info =
10107
0
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10108
0
  lo_irelfn->r_info =
10109
0
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10110
10111
0
  if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10112
0
    {
10113
0
      insn16 = NDS32_NOP16;
10114
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10115
0
      lo_irelfn->r_info =
10116
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10117
0
          R_NDS32_INSN16);
10118
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10119
0
      *insn_len += 2;
10120
0
    }
10121
0
  return true;
10122
0
}
10123
10124
/* Revert condition branch.  This function does not check if the input
10125
   instruction is condition branch or not.  */
10126
10127
static void
10128
nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10129
         uint16_t *re_insn16, uint32_t *re_insn)
10130
0
{
10131
0
  uint32_t comp_insn = 0;
10132
0
  uint16_t comp_insn16 = 0;
10133
10134
0
  if (insn)
10135
0
    {
10136
0
      if (N32_OP6 (insn) == N32_OP6_BR1)
10137
0
  {
10138
    /* beqs label.  */
10139
0
    comp_insn = (insn ^ 0x4000) & 0xffffc000;
10140
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == N32_REG_R5)
10141
0
      {
10142
        /* Insn can be contracted to 16-bit implied r5.  */
10143
0
        comp_insn16 =
10144
0
    (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10145
0
        comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10146
0
      }
10147
0
  }
10148
0
      else if (N32_OP6 (insn) == N32_OP6_BR3)
10149
0
  {
10150
    /* bnec $ta, imm11, label.  */
10151
0
    comp_insn = (insn ^ 0x80000) & 0xffffff00;
10152
0
  }
10153
0
      else
10154
0
  {
10155
0
    comp_insn = (insn ^ 0x10000) & 0xffffc000;
10156
0
    if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10157
0
        || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10158
0
      {
10159
0
        if (N32_IS_RT3 (insn))
10160
0
    {
10161
      /* Insn can be contracted to 16-bit.  */
10162
0
      comp_insn16 =
10163
0
        (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10164
0
      comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10165
0
    }
10166
0
        else if (N32_RT5 (insn) == N32_REG_R15)
10167
0
    {
10168
      /* Insn can be contracted to 16-bit.  */
10169
0
      comp_insn16 =
10170
0
        (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10171
0
    }
10172
0
      }
10173
0
  }
10174
0
    }
10175
0
  else
10176
0
    {
10177
0
      switch ((insn16 & 0xf000) >> 12)
10178
0
  {
10179
0
  case 0xc:
10180
    /* beqz38 or bnez38 */
10181
0
    comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10182
0
    comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10183
0
    comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10184
0
    break;
10185
10186
0
  case 0xd:
10187
    /* beqs38 or bnes38 */
10188
0
    comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10189
0
    comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10190
0
    comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10191
0
      | (N32_REG_R5 << 15);
10192
0
    break;
10193
10194
0
  case 0xe:
10195
    /* beqzS8 or bnezS8 */
10196
0
    comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10197
0
    comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10198
0
    comp_insn |= N32_REG_R15 << 20;
10199
0
    break;
10200
10201
0
  default:
10202
0
    break;
10203
0
  }
10204
0
    }
10205
0
  if (comp_insn && re_insn)
10206
0
    *re_insn = comp_insn;
10207
0
  if (comp_insn16 && re_insn16)
10208
0
    *re_insn16 = comp_insn16;
10209
0
}
10210
10211
/* Relax LONGJUMP2 relocation for nds32_elf_relax_section.  */
10212
10213
static bool
10214
nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10215
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10216
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10217
         Elf_Internal_Shdr *symtab_hdr)
10218
0
{
10219
  /* There are 3 variations for LONGJUMP2
10220
     case 2-4;  1st insn convertible, 16-bit on,
10221
     optimize off or optimize for space
10222
     bnes38  rt, ra, $1 ; LONGJUMP2
10223
     j       label      ; 25_PCREL
10224
     $1:
10225
10226
     case 4-4; 1st insn not convertible
10227
     bne  rt, ra, $1 ; LONGJUMP2
10228
     j    label      ; 25_PCREL
10229
     $1:
10230
10231
     case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10232
     bne  rt, ra, $1 ; LONGJUMP2
10233
     j    label      ; 25_PCREL
10234
     $1: */
10235
10236
  /* Get the reloc for the address from which the register is
10237
     being loaded.  This reloc will tell us which function is
10238
     actually being called.  */
10239
10240
0
  bfd_vma laddr;
10241
0
  int seq_len;  /* Original length of instruction sequence.  */
10242
0
  Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10243
0
  int first_size;
10244
0
  unsigned int i;
10245
0
  bfd_signed_vma foff;
10246
0
  uint32_t insn, re_insn = 0;
10247
0
  uint16_t insn16, re_insn16 = 0;
10248
0
  unsigned long reloc, cond_reloc;
10249
10250
0
  enum elf_nds32_reloc_type checked_types[] =
10251
0
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10252
10253
0
  irelend = internal_relocs + sec->reloc_count;
10254
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10255
0
  laddr = irel->r_offset;
10256
0
  *insn_len = seq_len;
10257
0
  first_size = (seq_len == 6) ? 2 : 4;
10258
10259
0
  i2_irelfn =
10260
0
    find_relocs_at_address_addr (irel, internal_relocs,
10261
0
         irelend, R_NDS32_25_PCREL_RELA,
10262
0
         laddr + first_size);
10263
10264
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10265
0
    {
10266
0
      cond_irelfn =
10267
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10268
0
             checked_types[i], laddr);
10269
0
      if (cond_irelfn != irelend)
10270
0
  break;
10271
0
    }
10272
10273
0
  if (i2_irelfn == irelend || cond_irelfn == irelend)
10274
0
    {
10275
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10276
0
        (uint64_t) irel->r_offset);
10277
0
      return false;
10278
0
    }
10279
10280
  /* Get the value of the symbol referred to by the reloc.  */
10281
0
  foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10282
0
  if (foff == 0
10283
0
      || foff < -CONSERVATIVE_16BIT_S1
10284
0
      || foff >= CONSERVATIVE_16BIT_S1)
10285
0
    return false;
10286
10287
  /* Get the all corresponding instructions.  */
10288
0
  if (first_size == 4)
10289
0
    {
10290
0
      insn = bfd_getb32 (contents + laddr);
10291
0
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10292
0
    }
10293
0
  else
10294
0
    {
10295
0
      insn16 = bfd_getb16 (contents + laddr);
10296
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10297
0
    }
10298
10299
0
  if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10300
0
      && foff < ACCURATE_8BIT_S1 - first_size)
10301
0
    {
10302
0
      if (first_size == 4)
10303
0
  {
10304
    /* Don't convert it to 16-bit now, keep this as relaxable for
10305
       ``label reloc; INSN16''.  */
10306
10307
    /* Save comp_insn32 to buffer.  */
10308
0
    bfd_putb32 (re_insn, contents + irel->r_offset);
10309
0
    *insn_len = 4;
10310
0
    reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10311
0
      R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10312
0
    cond_reloc = R_NDS32_INSN16;
10313
0
  }
10314
0
      else
10315
0
  {
10316
0
    bfd_putb16 (re_insn16, contents + irel->r_offset);
10317
0
    *insn_len = 2;
10318
0
    reloc = R_NDS32_9_PCREL_RELA;
10319
0
    cond_reloc = R_NDS32_NONE;
10320
0
  }
10321
0
    }
10322
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
10323
0
     && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10324
0
         && foff < ACCURATE_14BIT_S1 - first_size))
10325
0
    {
10326
      /* beqs     label    ; 15_PCREL */
10327
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10328
0
      *insn_len = 4;
10329
0
      reloc = R_NDS32_15_PCREL_RELA;
10330
0
      cond_reloc = R_NDS32_NONE;
10331
0
    }
10332
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
10333
0
     && foff >= -CONSERVATIVE_16BIT_S1
10334
0
     && foff < CONSERVATIVE_16BIT_S1)
10335
0
    {
10336
      /* beqz     label ; 17_PCREL */
10337
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10338
0
      *insn_len = 4;
10339
0
      reloc = R_NDS32_17_PCREL_RELA;
10340
0
      cond_reloc = R_NDS32_NONE;
10341
0
    }
10342
0
  else
10343
0
    return false;
10344
10345
  /* Set all relocations.  */
10346
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10347
0
  irel->r_addend = i2_irelfn->r_addend;
10348
10349
0
  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10350
0
              cond_reloc);
10351
0
  cond_irelfn->r_addend = 0;
10352
10353
0
  if ((seq_len ^ *insn_len ) & 0x2)
10354
0
    {
10355
0
      insn16 = NDS32_NOP16;
10356
0
      bfd_putb16 (insn16, contents + irel->r_offset + 4);
10357
0
      i2_irelfn->r_offset = 4;
10358
0
      i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10359
0
          R_NDS32_INSN16);
10360
0
      i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10361
0
      *insn_len += 2;
10362
0
    }
10363
0
  else
10364
0
    i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10365
0
              R_NDS32_NONE);
10366
0
  return true;
10367
0
}
10368
10369
/* Relax LONGJUMP3 relocation for nds32_elf_relax_section.  */
10370
10371
static bool
10372
nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10373
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10374
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10375
         Elf_Internal_Shdr *symtab_hdr)
10376
0
{
10377
  /* There are 5 variations for LONGJUMP3
10378
     case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10379
     optimize off or optimize for space
10380
     bnes38   rt, ra, $1      ; LONGJUMP3
10381
     sethi    ta, hi20(symbol)      ; HI20
10382
     ori      ta, ta, lo12(symbol)  ; LO12S0
10383
     jr5      ta        ;
10384
     $1:          ;
10385
10386
     case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10387
     bnes38   rt, ra, $1     ; LONGJUMP3
10388
     sethi    ta, hi20(symbol)     ; HI20
10389
     ori      ta, ta, lo12(symbol) ; LO12S0
10390
     jr5      ta       ;
10391
     $1:         ; LABEL
10392
10393
     case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10394
     optimize off or optimize for space
10395
     bne   rt, ra, $1   ; LONGJUMP3
10396
     sethi ta, hi20(symbol) ; HI20
10397
     ori   ta, ta, lo12(symbol) ; LO12S0
10398
     jr5   ta     ;
10399
     $1:      ;
10400
10401
     case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10402
     16-bit off if no INSN16
10403
     bne   rt, ra, $1   ; LONGJUMP3
10404
     sethi ta, hi20(symbol) ; HI20
10405
     ori   ta, ta, lo12(symbol) ; LO12S0
10406
     jr    ta     ;
10407
     $1:      ;
10408
10409
     case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10410
     16-bit off if no INSN16
10411
     bne   rt, ra, $1   ; LONGJUMP3
10412
     sethi ta, hi20(symbol) ; HI20
10413
     ori   ta, ta, lo12(symbol) ; LO12S0
10414
     jr    ta     ;
10415
     $1:      ; LABEL */
10416
10417
  /* Get the reloc for the address from which the register is
10418
     being loaded.  This reloc will tell us which function is
10419
     actually being called.  */
10420
0
  enum elf_nds32_reloc_type checked_types[] =
10421
0
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10422
10423
0
  int reloc_off = 0, cond_removed = 0, convertible;
10424
0
  bfd_vma laddr;
10425
0
  int seq_len;  /* Original length of instruction sequence.  */
10426
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10427
0
  int first_size;
10428
0
  unsigned int i;
10429
0
  bfd_signed_vma foff;
10430
0
  uint32_t insn, re_insn = 0;
10431
0
  uint16_t insn16, re_insn16 = 0;
10432
0
  unsigned long reloc, cond_reloc;
10433
10434
0
  irelend = internal_relocs + sec->reloc_count;
10435
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10436
0
  laddr = irel->r_offset;
10437
0
  *insn_len = seq_len;
10438
10439
0
  convertible = IS_1ST_CONVERT (irel->r_addend);
10440
10441
0
  if (convertible)
10442
0
    first_size = 2;
10443
0
  else
10444
0
    first_size = 4;
10445
10446
  /* Get all needed relocations.  */
10447
0
  hi_irelfn =
10448
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10449
0
         R_NDS32_HI20_RELA, laddr + first_size);
10450
0
  lo_irelfn =
10451
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10452
0
         R_NDS32_LO12S0_ORI_RELA,
10453
0
         laddr + first_size + 4);
10454
10455
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10456
0
    {
10457
0
      cond_irelfn =
10458
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10459
0
             checked_types[i], laddr);
10460
0
      if (cond_irelfn != irelend)
10461
0
  break;
10462
0
    }
10463
10464
0
  if (hi_irelfn == irelend
10465
0
      || lo_irelfn == irelend
10466
0
      || cond_irelfn == irelend)
10467
0
    {
10468
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10469
0
        (uint64_t) irel->r_offset);
10470
0
      return false;
10471
0
    }
10472
10473
  /* Get the value of the symbol referred to by the reloc.  */
10474
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10475
10476
0
  if (foff == 0
10477
0
      || foff < -CONSERVATIVE_24BIT_S1
10478
0
      || foff >= CONSERVATIVE_24BIT_S1)
10479
0
    return false;
10480
10481
  /* Get the all corresponding instructions.  */
10482
0
  if (first_size == 4)
10483
0
    {
10484
0
      insn = bfd_getb32 (contents + laddr);
10485
0
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10486
0
    }
10487
0
  else
10488
0
    {
10489
0
      insn16 = bfd_getb16 (contents + laddr);
10490
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10491
0
    }
10492
10493
  /* For simplicity of coding, we are going to modify the section
10494
     contents, the section relocs, and the BFD symbol table.  We
10495
     must tell the rest of the code not to free up this
10496
     information.  It would be possible to instead create a table
10497
     of changes which have to be made, as is done in coff-mips.c;
10498
     that would be more work, but would require less memory when
10499
     the linker is run.  */
10500
10501
0
  if (re_insn16
10502
0
      && foff >= -ACCURATE_8BIT_S1 - first_size
10503
0
      && foff < ACCURATE_8BIT_S1 - first_size)
10504
0
    {
10505
0
      if (!(seq_len & 0x2))
10506
0
  {
10507
    /* Don't convert it to 16-bit now, keep this as relaxable
10508
       for ``label reloc; INSN1a''6.  */
10509
    /* Save comp_insn32 to buffer.  */
10510
0
    bfd_putb32 (re_insn, contents + irel->r_offset);
10511
0
    *insn_len = 4;
10512
0
    reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10513
0
      R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10514
0
    cond_reloc = R_NDS32_INSN16;
10515
0
  }
10516
0
      else
10517
0
  {
10518
    /* Not optimize for speed; convert sequence to 16-bit.  */
10519
    /* Save comp_insn16 to buffer.  */
10520
0
    bfd_putb16 (re_insn16, contents + irel->r_offset);
10521
0
    *insn_len = 2;
10522
0
    reloc = R_NDS32_9_PCREL_RELA;
10523
0
    cond_reloc = R_NDS32_NONE;
10524
0
  }
10525
0
      cond_removed = 1;
10526
0
    }
10527
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
10528
0
     && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10529
0
         && foff < ACCURATE_14BIT_S1 - first_size))
10530
0
    {
10531
      /* beqs     label    ; 15_PCREL */
10532
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10533
0
      *insn_len = 4;
10534
0
      reloc = R_NDS32_15_PCREL_RELA;
10535
0
      cond_reloc = R_NDS32_NONE;
10536
0
      cond_removed = 1;
10537
0
    }
10538
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
10539
0
     && foff >= -CONSERVATIVE_16BIT_S1
10540
0
     && foff < CONSERVATIVE_16BIT_S1)
10541
0
    {
10542
      /* beqz     label ; 17_PCREL */
10543
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10544
0
      *insn_len = 4;
10545
0
      reloc = R_NDS32_17_PCREL_RELA;
10546
0
      cond_reloc = R_NDS32_NONE;
10547
0
      cond_removed = 1;
10548
0
    }
10549
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10550
0
     && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10551
0
    {
10552
      /* Relax to one of the following 3 variations
10553
10554
   case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10555
   for space
10556
   bnes38  rt, $1 ; LONGJUMP2
10557
   j       label  ; 25_PCREL
10558
   $1
10559
10560
   case 4-4; 1st insn not convertible, others don't care
10561
   bne   rt, ra, $1 ; LONGJUMP2
10562
   j     label      ; 25_PCREL
10563
   $1
10564
10565
   case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10566
   bne   rt, ra, $1 ; LONGJUMP2
10567
   j     label      ; 25_PCREL
10568
   $1 */
10569
10570
      /* Offset for first instruction.  */
10571
10572
      /* Use j label as second instruction.  */
10573
0
      *insn_len = 4 + first_size;
10574
0
      insn = INSN_J;
10575
0
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10576
0
      reloc = R_NDS32_LONGJUMP2;
10577
0
      cond_reloc = R_NDS32_25_PLTREL;
10578
0
    }
10579
0
    else
10580
0
      return false;
10581
10582
0
    if (cond_removed == 1)
10583
0
      {
10584
  /* Set all relocations.  */
10585
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10586
0
  irel->r_addend = hi_irelfn->r_addend;
10587
10588
0
  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10589
0
              cond_reloc);
10590
0
  cond_irelfn->r_addend = 0;
10591
0
  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10592
0
            R_NDS32_NONE);
10593
0
      }
10594
0
    else
10595
0
      {
10596
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10597
0
  irel->r_addend = irel->r_addend;
10598
0
  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10599
0
            cond_reloc);
10600
0
      }
10601
10602
0
  if ((seq_len ^ *insn_len ) & 0x2)
10603
0
    {
10604
0
      insn16 = NDS32_NOP16;
10605
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10606
0
      lo_irelfn->r_offset = *insn_len;
10607
0
      lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10608
0
          R_NDS32_INSN16);
10609
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10610
0
      *insn_len += 2;
10611
0
    }
10612
0
  else
10613
0
    lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10614
0
              R_NDS32_NONE);
10615
0
  return true;
10616
0
}
10617
10618
/* Relax LONGCALL4 relocation for nds32_elf_relax_section.  */
10619
10620
static bool
10621
nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10622
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10623
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10624
         Elf_Internal_Shdr *symtab_hdr)
10625
0
{
10626
  /* The pattern for LONGCALL4.  Support for function cse.
10627
     sethi ta, hi20(symbol) ; LONGCALL4/HI20
10628
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10629
     jral  ta     ; PTR_RES/EMPTY/INSN16  */
10630
10631
0
  bfd_vma laddr;
10632
0
  uint32_t insn;
10633
0
  Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10634
0
  Elf_Internal_Rela *irelend;
10635
0
  bfd_signed_vma foff;
10636
10637
0
  irelend = internal_relocs + sec->reloc_count;
10638
0
  laddr = irel->r_offset;
10639
10640
  /* Get the reloc for the address from which the register is
10641
     being loaded.  This reloc will tell us which function is
10642
     actually being called.  */
10643
0
  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10644
0
           R_NDS32_HI20_RELA, laddr);
10645
10646
0
  if (hi_irel == irelend)
10647
0
    {
10648
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10649
0
        (uint64_t) irel->r_offset);
10650
0
      return false;
10651
0
    }
10652
10653
  /* Get the value of the symbol referred to by the reloc.  */
10654
0
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10655
10656
  /* This condition only happened when symbol is undefined.  */
10657
0
  if (foff == 0
10658
0
      || foff < -CONSERVATIVE_24BIT_S1
10659
0
      || foff >= CONSERVATIVE_24BIT_S1)
10660
0
    return false;
10661
10662
  /* Relax to: jal symbol; 25_PCREL.  */
10663
  /* For simplicity of coding, we are going to modify the section
10664
     contents, the section relocs, and the BFD symbol table.  We
10665
     must tell the rest of the code not to free up this
10666
     information.  It would be possible to instead create a table
10667
     of changes which have to be made, as is done in coff-mips.c;
10668
     that would be more work, but would require less memory when
10669
     the linker is run.  */
10670
10671
0
  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10672
0
            R_NDS32_PTR_RESOLVED, irel->r_addend);
10673
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10674
0
            R_NDS32_EMPTY, irel->r_addend);
10675
10676
0
  if (ptr_irel == irelend || em_irel == irelend)
10677
0
    {
10678
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10679
0
        (uint64_t) irel->r_offset);
10680
0
      return false;
10681
0
    }
10682
  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10683
0
  insn = bfd_getb32 (contents + irel->r_addend);
10684
0
  if (insn & 0x80000000)
10685
0
    return false;
10686
10687
  /* Replace the long call with a jal.  */
10688
0
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10689
0
          R_NDS32_25_PCREL_RELA);
10690
0
  ptr_irel->r_addend = 1;
10691
10692
  /* We don't resolve this here but resolve it in relocate_section.  */
10693
0
  insn = INSN_JAL;
10694
0
  bfd_putb32 (insn, contents + em_irel->r_offset);
10695
10696
0
  irel->r_info =
10697
0
    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10698
10699
  /* If there is function cse, HI20 can not remove now.  */
10700
0
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10701
0
             R_NDS32_LONGCALL4, laddr);
10702
0
  if (call_irel == irelend)
10703
0
    {
10704
0
      *insn_len = 0;
10705
0
      hi_irel->r_info =
10706
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10707
0
    }
10708
10709
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10710
0
            R_NDS32_INSN16, irel->r_addend);
10711
0
  if (insn_irel != irelend)
10712
0
    insn_irel->r_info =
10713
0
      ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10714
10715
0
  return true;
10716
0
}
10717
10718
/* Relax LONGCALL5 relocation for nds32_elf_relax_section.  */
10719
10720
static bool
10721
nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10722
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10723
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10724
         Elf_Internal_Shdr *symtab_hdr)
10725
0
{
10726
  /* The pattern for LONGCALL5.
10727
     bltz  rt, .L1  ; LONGCALL5/17_PCREL
10728
     jal   symbol ; 25_PCREL
10729
     .L1:  */
10730
10731
0
  bfd_vma laddr;
10732
0
  uint32_t insn;
10733
0
  Elf_Internal_Rela *cond_irel, *irelend;
10734
0
  bfd_signed_vma foff;
10735
10736
0
  irelend = internal_relocs + sec->reloc_count;
10737
0
  laddr = irel->r_offset;
10738
0
  insn = bfd_getb32 (contents + laddr);
10739
10740
  /* Get the reloc for the address from which the register is
10741
     being loaded.  This reloc will tell us which function is
10742
     actually being called.  */
10743
0
  cond_irel =
10744
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10745
0
         R_NDS32_25_PCREL_RELA, irel->r_addend);
10746
0
  if (cond_irel == irelend)
10747
0
    {
10748
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10749
0
        (uint64_t) irel->r_offset);
10750
0
      return false;
10751
0
    }
10752
10753
  /* Get the value of the symbol referred to by the reloc.  */
10754
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10755
10756
0
  if (foff == 0
10757
0
      || foff < -CONSERVATIVE_16BIT_S1
10758
0
      || foff >= CONSERVATIVE_16BIT_S1)
10759
0
    return false;
10760
10761
  /* Relax to bgezal   rt, label ; 17_PCREL
10762
     or   bltzal   rt, label ; 17_PCREL.  */
10763
10764
  /* Convert to complimentary conditional call.  */
10765
0
  insn = CONVERT_CONDITION_CALL (insn);
10766
10767
  /* For simplicity of coding, we are going to modify the section
10768
     contents, the section relocs, and the BFD symbol table.  We
10769
     must tell the rest of the code not to free up this
10770
     information.  It would be possible to instead create a table
10771
     of changes which have to be made, as is done in coff-mips.c;
10772
     that would be more work, but would require less memory when
10773
     the linker is run.  */
10774
10775
  /* Modify relocation and contents.  */
10776
0
  cond_irel->r_info =
10777
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10778
10779
  /* Replace the long call with a bgezal.  */
10780
0
  bfd_putb32 (insn, contents + cond_irel->r_offset);
10781
0
  *insn_len = 0;
10782
10783
  /* Clean unnessary relocations.  */
10784
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10785
10786
0
  cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10787
0
             R_NDS32_17_PCREL_RELA, laddr);
10788
0
  cond_irel->r_info =
10789
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10790
10791
0
  return true;
10792
0
}
10793
10794
/* Relax LONGCALL6 relocation for nds32_elf_relax_section.  */
10795
10796
static bool
10797
nds32_elf_relax_longcall6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10798
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10799
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10800
         Elf_Internal_Shdr *symtab_hdr)
10801
0
{
10802
  /* The pattern for LONGCALL6.
10803
     bltz  rt,   .L1      ; LONGCALL6/17_PCREL
10804
     sethi ta,   hi20(symbol)   ; HI20/PTR
10805
     ori   ta, ta,  lo12(symbol)  ; LO12S0_ORI/PTR
10806
     jral  ta       ; PTR_RES/EMPTY/INSN16
10807
     .L1  */
10808
10809
0
  bfd_vma laddr;
10810
0
  uint32_t insn;
10811
0
  Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10812
0
  bfd_signed_vma foff;
10813
10814
0
  irelend = internal_relocs + sec->reloc_count;
10815
0
  laddr = irel->r_offset;
10816
10817
  /* Get the reloc for the address from which the register is
10818
     being loaded.  This reloc will tell us which function is
10819
     actually being called.  */
10820
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10821
0
           R_NDS32_EMPTY, irel->r_addend);
10822
10823
0
  if (em_irel == irelend)
10824
0
    {
10825
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10826
0
        (uint64_t) irel->r_offset);
10827
0
      return false;
10828
0
    }
10829
10830
  /* Get the value of the symbol referred to by the reloc.  */
10831
0
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10832
10833
0
  if (foff == 0
10834
0
      || foff < -CONSERVATIVE_24BIT_S1
10835
0
      || foff >= CONSERVATIVE_24BIT_S1)
10836
0
    return false;
10837
10838
  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10839
0
  insn = bfd_getb32 (contents + irel->r_addend);
10840
0
  if (insn & 0x80000000)
10841
0
    return false;
10842
10843
0
  insn = bfd_getb32 (contents + laddr);
10844
0
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10845
0
    {
10846
      /* Relax to  bgezal   rt, label ; 17_PCREL
10847
   or    bltzal   rt, label ; 17_PCREL.  */
10848
10849
      /* Convert to complimentary conditional call.  */
10850
0
      *insn_len = 0;
10851
0
      insn = CONVERT_CONDITION_CALL (insn);
10852
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
10853
10854
0
      em_irel->r_info =
10855
0
  ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10856
10857
      /* Set resolved relocation.  */
10858
0
      cond_irel =
10859
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10860
0
             R_NDS32_PTR_RESOLVED, irel->r_addend);
10861
0
      if (cond_irel == irelend)
10862
0
  {
10863
0
    _bfd_error_handler (unrecognized_reloc_msg, abfd,
10864
0
            "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10865
0
    return false;
10866
0
  }
10867
0
      cond_irel->r_addend = 1;
10868
10869
      /* Clear relocations.  */
10870
10871
0
      irel->r_info =
10872
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10873
10874
0
      cond_irel =
10875
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10876
0
             R_NDS32_17_PCREL_RELA, laddr);
10877
0
      if (cond_irel != irelend)
10878
0
  cond_irel->r_info =
10879
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10880
10881
0
      cond_irel =
10882
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10883
0
             R_NDS32_INSN16, irel->r_addend);
10884
0
      if (cond_irel != irelend)
10885
0
  cond_irel->r_info =
10886
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10887
10888
0
    }
10889
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10890
0
    {
10891
      /* Relax to the following instruction sequence
10892
   bltz  rt, .L1  ; LONGCALL2/17_PCREL
10893
   jal   symbol ; 25_PCREL/PTR_RES
10894
   .L1  */
10895
0
      *insn_len = 4;
10896
      /* Convert instruction.  */
10897
0
      insn = INSN_JAL;
10898
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
10899
10900
      /* Convert relocations.  */
10901
0
      em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10902
0
              R_NDS32_25_PCREL_RELA);
10903
0
      irel->r_info =
10904
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10905
10906
      /* Set resolved relocation.  */
10907
0
      cond_irel =
10908
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10909
0
             R_NDS32_PTR_RESOLVED, irel->r_addend);
10910
0
      if (cond_irel == irelend)
10911
0
  {
10912
0
    _bfd_error_handler (unrecognized_reloc_msg, abfd,
10913
0
            "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10914
0
    return false;
10915
0
  }
10916
0
      cond_irel->r_addend = 1;
10917
10918
0
      cond_irel =
10919
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10920
0
             R_NDS32_INSN16, irel->r_addend);
10921
0
      if (cond_irel != irelend)
10922
0
  cond_irel->r_info =
10923
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10924
0
    }
10925
0
  return true;
10926
0
}
10927
10928
/* Relax LONGJUMP4 relocation for nds32_elf_relax_section.  */
10929
10930
static bool
10931
nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10932
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10933
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10934
         Elf_Internal_Shdr *symtab_hdr)
10935
0
{
10936
  /* The pattern for LONGJUMP4.
10937
     sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10938
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10939
     jr    ta     ; PTR_RES/INSN16/EMPTY  */
10940
10941
0
  bfd_vma laddr;
10942
0
  int seq_len;  /* Original length of instruction sequence.  */
10943
0
  uint32_t insn;
10944
0
  Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10945
0
  bfd_signed_vma foff;
10946
10947
0
  irelend = internal_relocs + sec->reloc_count;
10948
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10949
0
  laddr = irel->r_offset;
10950
0
  *insn_len = seq_len;
10951
10952
  /* Get the reloc for the address from which the register is
10953
     being loaded.  This reloc will tell us which function is
10954
     actually being called.  */
10955
10956
0
  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10957
0
           R_NDS32_HI20_RELA, laddr);
10958
10959
0
  if (hi_irel == irelend)
10960
0
    {
10961
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10962
0
        (uint64_t) irel->r_offset);
10963
0
      return false;
10964
0
    }
10965
10966
  /* Get the value of the symbol referred to by the reloc.  */
10967
0
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10968
10969
0
  if (foff == 0
10970
0
      || foff >= CONSERVATIVE_24BIT_S1
10971
0
      || foff < -CONSERVATIVE_24BIT_S1)
10972
0
    return false;
10973
10974
  /* Convert it to "j label", it may be converted to j8 in the final
10975
     pass of relaxation.  Therefore, we do not consider this currently.  */
10976
0
  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10977
0
            R_NDS32_PTR_RESOLVED, irel->r_addend);
10978
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10979
0
           R_NDS32_EMPTY, irel->r_addend);
10980
10981
0
  if (ptr_irel == irelend || em_irel == irelend)
10982
0
    {
10983
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10984
0
        (uint64_t) irel->r_offset);
10985
0
      return false;
10986
0
    }
10987
10988
0
  em_irel->r_info =
10989
0
    ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10990
0
  ptr_irel->r_addend = 1;
10991
10992
  /* Write instruction.  */
10993
0
  insn = INSN_J;
10994
0
  bfd_putb32 (insn, contents + em_irel->r_offset);
10995
10996
  /* Clear relocations.  */
10997
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10998
10999
  /* If there is function cse, HI20 can not remove now.  */
11000
0
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11001
0
             R_NDS32_LONGJUMP4, laddr);
11002
0
  if (call_irel == irelend)
11003
0
    {
11004
0
      *insn_len = 0;
11005
0
      hi_irel->r_info =
11006
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
11007
0
    }
11008
11009
0
  return true;
11010
0
}
11011
11012
/* Relax LONGJUMP5 relocation for nds32_elf_relax_section.  */
11013
11014
static bool
11015
nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11016
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11017
         int *seq_len, bfd_byte *contents,
11018
         Elf_Internal_Sym *isymbuf,
11019
         Elf_Internal_Shdr *symtab_hdr)
11020
0
{
11021
  /* There are 2 variations for LONGJUMP5
11022
     case 2-4;  1st insn convertible, 16-bit on.
11023
     bnes38  rt, ra, .L1  ; LONGJUMP5/9_PCREL/INSN16
11024
     j       label    ; 25_PCREL/INSN16
11025
     $1:
11026
11027
     case 4-4; 1st insn not convertible
11028
     bne  rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11029
     j    label   ; 25_PCREL/INSN16
11030
     .L1:  */
11031
11032
0
  bfd_vma laddr;
11033
0
  Elf_Internal_Rela *cond_irel,  *irelend;
11034
0
  unsigned int i;
11035
0
  bfd_signed_vma foff;
11036
0
  uint32_t insn, re_insn = 0;
11037
0
  uint16_t insn16, re_insn16 = 0;
11038
0
  unsigned long reloc;
11039
11040
0
  enum elf_nds32_reloc_type checked_types[] =
11041
0
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11042
0
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11043
11044
0
  irelend = internal_relocs + sec->reloc_count;
11045
0
  laddr = irel->r_offset;
11046
11047
  /* Get the reloc for the address from which the register is
11048
     being loaded.  This reloc will tell us which function is
11049
     actually being called.  */
11050
11051
0
  cond_irel =
11052
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11053
0
         R_NDS32_25_PCREL_RELA, irel->r_addend);
11054
0
  if (cond_irel == irelend)
11055
0
    {
11056
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
11057
0
        (uint64_t) irel->r_offset);
11058
0
      return false;
11059
0
    }
11060
11061
  /* Get the value of the symbol referred to by the reloc.  */
11062
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11063
11064
0
  if (foff == 0
11065
0
      || foff < -CONSERVATIVE_16BIT_S1
11066
0
      || foff >= CONSERVATIVE_16BIT_S1)
11067
0
    return false;
11068
11069
  /* Get the all corresponding instructions.  */
11070
0
  insn = bfd_getb32 (contents + laddr);
11071
  /* Check instruction size.  */
11072
0
  if (insn & 0x80000000)
11073
0
    {
11074
0
      *seq_len = 0;
11075
0
      insn16 = insn >> 16;
11076
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11077
0
    }
11078
0
  else
11079
0
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11080
11081
0
  if (N32_OP6 (re_insn) == N32_OP6_BR1
11082
0
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11083
0
    {
11084
      /* beqs label ; 15_PCREL.  */
11085
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11086
0
      reloc = R_NDS32_15_PCREL_RELA;
11087
0
    }
11088
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
11089
0
     && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11090
0
    {
11091
      /* beqz label ; 17_PCREL.  */
11092
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11093
0
      reloc = R_NDS32_17_PCREL_RELA;
11094
0
    }
11095
0
  else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11096
0
     && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11097
0
    {
11098
      /* beqc label ; 9_PCREL.  */
11099
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11100
0
      reloc = R_NDS32_WORD_9_PCREL_RELA;
11101
0
    }
11102
0
  else
11103
0
    return false;
11104
11105
  /* Set all relocations.  */
11106
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
11107
11108
  /* Clean relocations.  */
11109
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11110
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11111
0
    {
11112
0
      cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11113
0
                 checked_types[i], laddr);
11114
0
      if (cond_irel != irelend)
11115
0
  {
11116
0
    if (*seq_len == 0
11117
0
        && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11118
0
      {
11119
        /* If the branch instruction is 2 byte, it cannot remove
11120
     directly.  Only convert it to nop16 and remove it after
11121
     checking alignment issue.  */
11122
0
        insn16 = NDS32_NOP16;
11123
0
        bfd_putb16 (insn16, contents + laddr);
11124
0
        cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11125
0
      }
11126
0
    else
11127
0
      cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11128
0
                R_NDS32_NONE);
11129
0
  }
11130
0
    }
11131
0
  *insn_len = 0;
11132
11133
0
  return true;
11134
0
}
11135
11136
/* Relax LONGJUMP6 relocation for nds32_elf_relax_section.  */
11137
11138
static bool
11139
nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11140
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11141
         int *seq_len, bfd_byte *contents,
11142
         Elf_Internal_Sym *isymbuf,
11143
         Elf_Internal_Shdr *symtab_hdr)
11144
0
{
11145
  /* There are 5 variations for LONGJUMP6
11146
     case : 2-4-4-4; 1st insn convertible, 16-bit on.
11147
     bnes38   rt, ra, .L1   ; LONGJUMP6/15_PCREL/INSN16
11148
     sethi    ta, hi20(symbol)    ; HI20/PTR
11149
     ori      ta, ta, lo12(symbol)  ; LO12S0_ORI/PTR
11150
     jr       ta      ; PTR_RES/INSN16/EMPTY
11151
     .L1:
11152
11153
     case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11154
     bne   rt, ra, .L1    ; LONGJUMP6/15_PCREL/INSN16
11155
     sethi ta, hi20(symbol) ; HI20/PTR
11156
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11157
     jr    ta     ; PTR_RES/INSN16/EMPTY
11158
     .L1:  */
11159
11160
0
  enum elf_nds32_reloc_type checked_types[] =
11161
0
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11162
0
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11163
11164
0
  int reloc_off = 0, cond_removed = 0;
11165
0
  bfd_vma laddr;
11166
0
  Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11167
0
  unsigned int i;
11168
0
  bfd_signed_vma foff;
11169
0
  uint32_t insn, re_insn = 0;
11170
0
  uint16_t insn16, re_insn16 = 0;
11171
0
  unsigned long reloc;
11172
11173
0
  irelend = internal_relocs + sec->reloc_count;
11174
0
  laddr = irel->r_offset;
11175
11176
  /* Get the reloc for the address from which the register is
11177
     being loaded.  This reloc will tell us which function is
11178
     actually being called.  */
11179
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11180
0
           R_NDS32_EMPTY, irel->r_addend);
11181
11182
0
  if (em_irel == irelend)
11183
0
    {
11184
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11185
0
        (uint64_t) irel->r_offset);
11186
0
      return false;
11187
0
    }
11188
11189
  /* Get the value of the symbol referred to by the reloc.  */
11190
0
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11191
11192
0
  if (foff == 0
11193
0
      || foff < -CONSERVATIVE_24BIT_S1
11194
0
      || foff >= CONSERVATIVE_24BIT_S1)
11195
0
    return false;
11196
11197
0
  insn = bfd_getb32 (contents + laddr);
11198
  /* Check instruction size.  */
11199
0
  if (insn & 0x80000000)
11200
0
    {
11201
0
      *seq_len = 0;
11202
0
      insn16 = insn >> 16;
11203
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11204
0
    }
11205
0
  else
11206
0
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11207
11208
  /* For simplicity of coding, we are going to modify the section
11209
     contents, the section relocs, and the BFD symbol table.  We
11210
     must tell the rest of the code not to free up this
11211
     information.  It would be possible to instead create a table
11212
     of changes which have to be made, as is done in coff-mips.c;
11213
     that would be more work, but would require less memory when
11214
     the linker is run.  */
11215
11216
0
  if (N32_OP6 (re_insn) == N32_OP6_BR1
11217
0
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11218
0
    {
11219
      /* beqs     label    ; 15_PCREL.  */
11220
0
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
11221
0
      reloc = R_NDS32_15_PCREL_RELA;
11222
0
      cond_removed = 1;
11223
0
    }
11224
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
11225
0
     && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11226
0
    {
11227
      /* beqz     label ; 17_PCREL.  */
11228
0
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
11229
0
      reloc = R_NDS32_17_PCREL_RELA;
11230
0
      cond_removed = 1;
11231
0
    }
11232
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11233
0
     && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11234
0
    {
11235
      /* Relax to one of the following 2 variations
11236
11237
   case 2-4;  1st insn convertible, 16-bit on.
11238
   bnes38  rt, ra, .L1  ; LONGJUMP5/9_PCREL/INSN16
11239
   j       label    ; 25_PCREL/INSN16
11240
   $1:
11241
11242
   case 4-4; 1st insn not convertible
11243
   bne  rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11244
   j    label   ; 25_PCREL/INSN16
11245
   .L1:  */
11246
11247
      /* Use j label as second instruction.  */
11248
0
      insn = INSN_J;
11249
0
      reloc = R_NDS32_25_PCREL_RELA;
11250
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
11251
0
    }
11252
0
  else
11253
0
    return false;
11254
11255
  /* Set all relocations.  */
11256
0
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11257
11258
0
  cond_irel =
11259
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11260
0
         R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11261
0
  cond_irel->r_addend = 1;
11262
11263
  /* Use INSN16 of first branch instruction to distinguish if keeping
11264
     INSN16 of final instruction or not.  */
11265
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11266
0
             R_NDS32_INSN16, irel->r_offset);
11267
0
  if (insn_irel == irelend)
11268
0
    {
11269
      /* Clean the final INSN16.  */
11270
0
      insn_irel =
11271
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
11272
0
             R_NDS32_INSN16, em_irel->r_offset);
11273
0
      insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11274
0
          R_NDS32_NONE);
11275
0
    }
11276
11277
0
  if (cond_removed == 1)
11278
0
    {
11279
0
      *insn_len = 0;
11280
11281
      /* Clear relocations.  */
11282
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11283
11284
0
      for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11285
0
  {
11286
0
    cond_irel =
11287
0
      find_relocs_at_address_addr (irel, internal_relocs, irelend,
11288
0
           checked_types[i], laddr);
11289
0
    if (cond_irel != irelend)
11290
0
      {
11291
0
        if (*seq_len == 0
11292
0
      && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11293
0
    {
11294
      /* If the branch instruction is 2 byte, it cannot remove
11295
         directly.  Only convert it to nop16 and remove it after
11296
         checking alignment issue.  */
11297
0
      insn16 = NDS32_NOP16;
11298
0
      bfd_putb16 (insn16, contents + laddr);
11299
0
      cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11300
0
    }
11301
0
        else
11302
0
    cond_irel->r_info =
11303
0
      ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11304
0
      }
11305
0
  }
11306
0
    }
11307
0
  else
11308
0
    {
11309
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11310
0
           R_NDS32_LONGJUMP5);
11311
0
    }
11312
11313
0
  return true;
11314
0
}
11315
11316
/* Relax LONGJUMP7 relocation for nds32_elf_relax_section.  */
11317
11318
static bool
11319
nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11320
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11321
         int *seq_len, bfd_byte *contents,
11322
         Elf_Internal_Sym *isymbuf,
11323
         Elf_Internal_Shdr *symtab_hdr)
11324
0
{
11325
  /* There are 2 variations for LONGJUMP5
11326
     case 2-4;  1st insn convertible, 16-bit on.
11327
     movi55  ta, imm11    ; LONGJUMP7/INSN16
11328
     beq     rt, ta, label  ; 15_PCREL
11329
11330
     case 4-4; 1st insn not convertible
11331
     movi55  ta, imm11    ; LONGJUMP7/INSN16
11332
     beq     rt, ta, label  ; 15_PCREL  */
11333
11334
0
  bfd_vma laddr;
11335
0
  Elf_Internal_Rela *cond_irel,  *irelend, *insn_irel;
11336
0
  bfd_signed_vma foff;
11337
0
  uint32_t insn, re_insn = 0;
11338
0
  uint16_t insn16;
11339
0
  uint32_t imm11;
11340
11341
0
  irelend = internal_relocs + sec->reloc_count;
11342
0
  laddr = irel->r_offset;
11343
11344
  /* Get the reloc for the address from which the register is
11345
     being loaded.  This reloc will tell us which function is
11346
     actually being called.  */
11347
11348
0
  cond_irel =
11349
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11350
0
         R_NDS32_15_PCREL_RELA, irel->r_addend);
11351
0
  if (cond_irel == irelend)
11352
0
    {
11353
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11354
0
        (uint64_t) irel->r_offset);
11355
0
      return false;
11356
0
    }
11357
11358
  /* Get the value of the symbol referred to by the reloc.  */
11359
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11360
11361
0
  if (foff == 0
11362
0
      || foff < -CONSERVATIVE_8BIT_S1
11363
0
      || foff >= CONSERVATIVE_8BIT_S1)
11364
0
    return false;
11365
11366
  /* Get the first instruction for its size.  */
11367
0
  insn = bfd_getb32 (contents + laddr);
11368
0
  if (insn & 0x80000000)
11369
0
    {
11370
0
      *seq_len = 0;
11371
      /* Get the immediate from movi55.  */
11372
0
      imm11 = N16_IMM5S (insn >> 16);
11373
0
    }
11374
0
  else
11375
0
    {
11376
      /* Get the immediate from movi.  */
11377
0
      imm11 = N32_IMM20S (insn);
11378
0
    }
11379
11380
  /* Get the branch instruction.  */
11381
0
  insn = bfd_getb32 (contents + irel->r_addend);
11382
  /* Convert instruction to BR3.  */
11383
0
  if ((insn >> 14) & 0x1)
11384
0
    re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11385
0
  else
11386
0
    re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11387
11388
0
  bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11389
11390
  /* Set all relocations.  */
11391
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11392
0
            R_NDS32_WORD_9_PCREL_RELA);
11393
11394
  /* Clean relocations.  */
11395
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11396
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11397
0
             R_NDS32_INSN16, irel->r_offset);
11398
0
  if (insn_irel != irelend)
11399
0
    {
11400
0
      if (*seq_len == 0)
11401
0
  {
11402
    /* If the first insntruction is 16bit, convert it to nop16.  */
11403
0
    insn16 = NDS32_NOP16;
11404
0
    bfd_putb16 (insn16, contents + laddr);
11405
0
    insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11406
0
  }
11407
0
      else
11408
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11409
0
            R_NDS32_NONE);
11410
0
    }
11411
0
  *insn_len = 0;
11412
11413
0
  return true;
11414
0
}
11415
11416
/* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11417
   for each relax round. But the gp may changed dramatically and then cause
11418
   the truncated to fit errors for the the converted gp instructions.
11419
   Therefore, we must reserve the minimum but safe enough size to prevent it.  */
11420
11421
static bool
11422
nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11423
           Elf_Internal_Rela *irel, bool *again,
11424
           bool init,
11425
           struct elf_nds32_link_hash_table *table,
11426
           Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11427
11428
0
{
11429
0
  int offset_to_gp;
11430
0
  static bool sec_pass = false;
11431
0
  static asection *first_sec = NULL, *sym_sec;
11432
  /* Record the number of instructions which may be removed.  */
11433
0
  static int count = 0, record_count;
11434
0
  Elf_Internal_Sym *isym;
11435
0
  struct elf_link_hash_entry *h = NULL;
11436
0
  int indx;
11437
0
  unsigned long r_symndx;
11438
0
  bfd *abfd = sec->owner;
11439
0
  static bfd_vma record_sda = 0;
11440
0
  int sda_offset = 0;
11441
11442
  /* Force doing relaxation when hyper-relax is high.  */
11443
0
  if (table->hyper_relax == 2)
11444
0
    return true;
11445
11446
  /* Do not relax the load/store patterns for the first
11447
     relax round.  */
11448
0
  if (init)
11449
0
    {
11450
0
      if (!first_sec)
11451
0
  first_sec = sec;
11452
0
      else if (first_sec == sec)
11453
0
  {
11454
0
    record_count = count;
11455
0
    count = 0;
11456
0
    sec_pass = true;
11457
0
  }
11458
11459
0
      if (!sec_pass)
11460
0
  *again = true;
11461
11462
0
      return true;
11463
0
    }
11464
11465
  /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11466
     DATA_SEGMENT_ALIGN size in the linker script may make it
11467
     get even bigger.  */
11468
0
  if (record_sda == 0)
11469
0
    record_sda = local_sda;
11470
0
  else if (local_sda > record_sda)
11471
0
    sda_offset = local_sda - record_sda;
11472
11473
  /* Assume the instruction will be removed in the best case.  */
11474
0
  count++;
11475
11476
  /* We record the offset to gp for each symbol, and then check
11477
     if it is changed dramatically after relaxing.
11478
     (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11479
     (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx].  */
11480
0
  r_symndx = ELF32_R_SYM (irel->r_info);
11481
0
  if (r_symndx >= symtab_hdr->sh_info)
11482
0
    {
11483
      /* Global symbols.  */
11484
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11485
0
      h = elf_sym_hashes (abfd)[indx];
11486
0
      sym_sec = h->root.u.def.section;
11487
0
      if (NDS32_GUARD_SEC_P (sym_sec->flags)
11488
0
    || bfd_is_abs_section (sym_sec))
11489
0
  {
11490
    /* Forbid doing relaxation when hyper-relax is low.  */
11491
0
    if (table->hyper_relax == 0)
11492
0
      return false;
11493
11494
0
    offset_to_gp = *access_addr - local_sda;
11495
0
    if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11496
0
      elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11497
0
    else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11498
0
       < abs (offset_to_gp) - sda_offset)
11499
0
      {
11500
        /* This may cause the error, so we reserve the
11501
     safe enough size for relaxing.  */
11502
0
        if (*access_addr >= local_sda)
11503
0
    *access_addr += (record_count * 4);
11504
0
        else
11505
0
    *access_addr -= (record_count * 4);
11506
0
      }
11507
0
    return sec_pass;
11508
0
  }
11509
0
    }
11510
0
  else
11511
0
    {
11512
      /* Local symbols.  */
11513
0
      if (!elf32_nds32_allocate_local_sym_info (abfd))
11514
0
  return false;
11515
0
      isym = isymbuf + r_symndx;
11516
11517
0
      sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11518
0
      if (NDS32_GUARD_SEC_P (sym_sec->flags))
11519
0
  {
11520
    /* Forbid doing relaxation when hyper-relax is low.  */
11521
0
    if (table->hyper_relax == 0)
11522
0
      return false;
11523
11524
0
    offset_to_gp = *access_addr - local_sda;
11525
0
    if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11526
0
      elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11527
0
    else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11528
0
       < abs (offset_to_gp) - sda_offset)
11529
0
      {
11530
        /* This may cause the error, so we reserve the
11531
     safe enough size for relaxing.  */
11532
0
        if (*access_addr >= local_sda)
11533
0
    *access_addr += (record_count * 4);
11534
0
        else
11535
0
    *access_addr -= (record_count * 4);
11536
0
      }
11537
0
    return sec_pass;
11538
0
  }
11539
0
    }
11540
11541
0
  return true;
11542
0
}
11543
11544
0
#define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11545
11546
/* Relax LOADSTORE relocation for nds32_elf_relax_section.  */
11547
11548
static bool
11549
nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11550
         asection *sec, Elf_Internal_Rela *irel,
11551
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11552
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11553
         Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11554
         struct elf_nds32_link_hash_table *table)
11555
0
{
11556
0
  int eliminate_sethi = 0, range_type;
11557
0
  unsigned int i;
11558
0
  bfd_vma local_sda, laddr;
11559
0
  int seq_len;  /* Original length of instruction sequence.  */
11560
0
  uint32_t insn;
11561
0
  Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11562
0
  bfd_vma access_addr = 0;
11563
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11564
0
  struct elf_link_hash_entry *h = NULL;
11565
0
  int indx;
11566
0
  enum elf_nds32_reloc_type checked_types[] =
11567
0
    { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11568
0
      R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11569
0
      R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11570
0
      R_NDS32_TLS_LE_HI20
11571
0
    };
11572
11573
0
  irelend = internal_relocs + sec->reloc_count;
11574
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
11575
0
  laddr = irel->r_offset;
11576
0
  *insn_len = seq_len;
11577
11578
  /* Get the high part relocation.  */
11579
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11580
0
    {
11581
0
      hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11582
0
                 checked_types[i], laddr);
11583
0
      if (hi_irelfn != irelend)
11584
0
  break;
11585
0
    }
11586
11587
0
  if (hi_irelfn == irelend)
11588
0
    {
11589
      /* Not R_NDS32_HI20_RELA.  */
11590
0
      if (i != 0)
11591
0
  _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11592
0
          (uint64_t) irel->r_offset);
11593
0
      return false;
11594
0
    }
11595
11596
0
  range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11597
0
  nds32_elf_final_sda_base (sec->output_section->owner,
11598
0
          link_info, &local_sda, false);
11599
11600
0
  switch (ELF32_R_TYPE (hi_irelfn->r_info))
11601
0
    {
11602
0
    case R_NDS32_HI20_RELA:
11603
0
      insn = bfd_getb32 (contents + laddr);
11604
0
      access_addr =
11605
0
  calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11606
11607
0
      if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11608
0
  {
11609
0
    indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11610
0
    h = elf_sym_hashes (abfd)[indx];
11611
0
  }
11612
11613
      /* Try movi.  */
11614
0
      if (range_type == NDS32_LOADSTORE_IMM
11615
0
    && access_addr < CONSERVATIVE_20BIT
11616
0
    && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11617
0
  {
11618
0
    eliminate_sethi = 1;
11619
0
    break;
11620
0
  }
11621
11622
0
      if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11623
0
  {
11624
0
    eliminate_sethi = 1;
11625
0
    break;
11626
0
  }
11627
0
      else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11628
0
               NULL, false, table, isymbuf, symtab_hdr))
11629
0
  return false;
11630
11631
0
      if (!load_store_relax)
11632
0
  return false;
11633
11634
      /* Case for set gp register.  */
11635
0
      if (N32_RT5 (insn) == N32_REG_GP)
11636
0
  return false;
11637
11638
0
      if (range_type == NDS32_LOADSTORE_FLOAT_S
11639
0
    || range_type == NDS32_LOADSTORE_FLOAT_D)
11640
0
  {
11641
0
    range_l = sdata_range[0][0];
11642
0
    range_h = sdata_range[0][1];
11643
0
  }
11644
0
      else
11645
0
  {
11646
0
    range_l = sdata_range[1][0];
11647
0
    range_h = sdata_range[1][1];
11648
0
  }
11649
0
      break;
11650
11651
0
    default:
11652
0
      return false;
11653
0
    }
11654
11655
  /* Delete sethi instruction.  */
11656
0
  if (eliminate_sethi == 1
11657
0
      || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11658
0
      || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11659
0
    {
11660
0
      hi_irelfn->r_info =
11661
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11662
0
      irel->r_info =
11663
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11664
0
      *insn_len = 0;
11665
0
      return true;
11666
0
    }
11667
11668
0
  return false;
11669
0
}
11670
11671
/* Relax LO12 relocation for nds32_elf_relax_section.  */
11672
11673
static void
11674
nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11675
          asection *sec, Elf_Internal_Rela *irel,
11676
          Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11677
          Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11678
          struct elf_nds32_link_hash_table *table)
11679
0
{
11680
0
  uint32_t insn;
11681
0
  bfd_vma local_sda, laddr;
11682
0
  unsigned long reloc;
11683
0
  bfd_vma access_addr;
11684
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11685
0
  Elf_Internal_Rela *irelfn = NULL, *irelend;
11686
0
  struct elf_link_hash_entry *h = NULL;
11687
0
  int indx;
11688
11689
  /* For SDA base relative relaxation.  */
11690
0
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11691
0
          &local_sda, false);
11692
11693
0
  irelend = internal_relocs + sec->reloc_count;
11694
0
  laddr = irel->r_offset;
11695
0
  insn = bfd_getb32 (contents + laddr);
11696
11697
0
  if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11698
0
    return;
11699
11700
0
  access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11701
11702
0
  if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11703
0
    {
11704
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11705
0
      h = elf_sym_hashes (abfd)[indx];
11706
0
    }
11707
11708
  /* Try movi.  */
11709
0
  if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11710
0
      && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11711
0
    {
11712
0
      reloc = R_NDS32_20_RELA;
11713
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11714
0
      insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11715
0
      bfd_putb32 (insn, contents + laddr);
11716
0
    }
11717
0
  else
11718
0
    {
11719
0
      if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11720
0
  {
11721
    /* Fall through.  */
11722
0
  }
11723
0
      else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11724
0
               false, table, isymbuf, symtab_hdr))
11725
0
  return;
11726
11727
0
      range_l = sdata_range[1][0];
11728
0
      range_h = sdata_range[1][1];
11729
0
      switch (ELF32_R_TYPE (irel->r_info))
11730
0
  {
11731
0
  case R_NDS32_LO12S0_RELA:
11732
0
    reloc = R_NDS32_SDA19S0_RELA;
11733
0
    break;
11734
0
  case R_NDS32_LO12S1_RELA:
11735
0
    reloc = R_NDS32_SDA18S1_RELA;
11736
0
    break;
11737
0
  case R_NDS32_LO12S2_RELA:
11738
0
    reloc = R_NDS32_SDA17S2_RELA;
11739
0
    break;
11740
0
  case R_NDS32_LO12S2_DP_RELA:
11741
0
    range_l = sdata_range[0][0];
11742
0
    range_h = sdata_range[0][1];
11743
0
    reloc = R_NDS32_SDA12S2_DP_RELA;
11744
0
    break;
11745
0
  case R_NDS32_LO12S2_SP_RELA:
11746
0
    range_l = sdata_range[0][0];
11747
0
    range_h = sdata_range[0][1];
11748
0
    reloc = R_NDS32_SDA12S2_SP_RELA;
11749
0
    break;
11750
0
  default:
11751
0
    return;
11752
0
  }
11753
11754
      /* There are range_h and range_l because linker has to promise
11755
   all sections move cross one page together.  */
11756
0
      if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11757
0
    || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11758
0
    || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11759
0
  {
11760
0
    if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == N32_REG_GP)
11761
0
      {
11762
        /* Maybe we should add R_NDS32_INSN16 reloc type here
11763
     or manually do some optimization.  sethi can't be
11764
     eliminated when updating $gp so the relative ori
11765
     needs to be preserved.  */
11766
0
        return;
11767
0
      }
11768
0
    if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11769
0
          &insn))
11770
0
      return;
11771
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11772
0
    bfd_putb32 (insn, contents + laddr);
11773
11774
0
    irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11775
0
             R_NDS32_INSN16);
11776
    /* SDA17 must keep INSN16 for converting fp_as_gp.  */
11777
0
    if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11778
0
      irelfn->r_info =
11779
0
        ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11780
11781
0
  }
11782
0
    }
11783
0
  return;
11784
0
}
11785
11786
/* Relax PTR relocation for nds32_elf_relax_section.  */
11787
11788
static bool
11789
nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11790
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11791
         int *seq_len, bfd_byte *contents)
11792
0
{
11793
0
  Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11794
11795
0
  irelend = internal_relocs + sec->reloc_count;
11796
11797
0
  re_irel =
11798
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11799
0
         R_NDS32_PTR_RESOLVED, irel->r_addend);
11800
11801
0
  if (re_irel == irelend)
11802
0
    {
11803
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11804
0
        (uint64_t) irel->r_offset);
11805
0
      return false;
11806
0
    }
11807
11808
0
  if (re_irel->r_addend != 1)
11809
0
    return false;
11810
11811
  /* Pointed target is relaxed and no longer needs this void *,
11812
     change the type to NONE.  */
11813
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11814
11815
  /* Find PTR_COUNT to decide remove it or not.  If PTR_COUNT does
11816
     not exist, it means only count 1 and remove it directly.  */
11817
  /* TODO: I hope we can obsolate R_NDS32_COUNT in the future.  */
11818
0
  count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11819
0
               R_NDS32_PTR_COUNT);
11820
0
  ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11821
0
             R_NDS32_PTR);
11822
0
  if (count_irel != irelend)
11823
0
    {
11824
0
      if (--count_irel->r_addend > 0)
11825
0
  return false;
11826
0
    }
11827
11828
0
  if (ptr_irel != irelend)
11829
0
    return false;
11830
11831
  /* If the PTR_COUNT is already 0, remove current instruction.  */
11832
0
  *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11833
0
  *insn_len = 0;
11834
0
  return true;
11835
0
}
11836
11837
/* Relax LWC relocation for nds32_elf_relax_section.  */
11838
11839
static void
11840
nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11841
          asection *sec, Elf_Internal_Rela *irel,
11842
          Elf_Internal_Rela *internal_relocs,
11843
          bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11844
          Elf_Internal_Shdr *symtab_hdr, bool *again)
11845
0
{
11846
  /* Pattern:
11847
     sethi    ra, hi20(symbol)      ; HI20/LOADSTORE
11848
     ori      ra, ra, lo12(symbol)  ; LO12S0/PTR/PTR/.../INSN16
11849
     flsi     fsa, [ra + offset1]   ; LSI/PTR_RESOLVED/INSN16
11850
     flsi     fsb, [ra + offset2]   ; LSI/PTR_RESOLVED/INSN16
11851
     ...  */
11852
11853
0
  uint32_t insn;
11854
0
  bfd_vma local_sda, laddr;
11855
0
  unsigned long reloc;
11856
0
  bfd_vma access_addr, flsi_offset;
11857
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11858
0
  Elf_Internal_Rela *irelend, *re_irel;
11859
0
  unsigned int opcode;
11860
11861
0
  irelend = internal_relocs + sec->reloc_count;
11862
0
  laddr = irel->r_offset;
11863
0
  insn = bfd_getb32 (contents + laddr);
11864
11865
0
  if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11866
0
    return;
11867
11868
  /* Can not do relaxation for bi format.  */
11869
0
  if ((insn & 0x1000))
11870
0
    return;
11871
11872
  /* Only deal with flsi, fssi, fldi, fsdi, so far.  */
11873
0
  opcode = N32_OP6 (insn);
11874
0
  if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11875
0
    reloc = R_NDS32_SDA12S2_SP_RELA;
11876
0
  else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11877
0
    reloc = R_NDS32_SDA12S2_DP_RELA;
11878
0
  else
11879
0
    return;
11880
11881
0
  re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11882
0
            R_NDS32_PTR_RESOLVED);
11883
0
  if (re_irel == irelend)
11884
0
    {
11885
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11886
0
        (uint64_t) irel->r_offset);
11887
0
      return;
11888
0
    }
11889
11890
  /* For SDA base relative relaxation.  */
11891
0
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11892
0
          &local_sda, false);
11893
0
  access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11894
0
  flsi_offset = (insn & 0xfff) << 2;
11895
0
  access_addr += flsi_offset;
11896
0
  range_l = sdata_range[0][0];
11897
0
  range_h = sdata_range[0][1];
11898
11899
0
  if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11900
0
      || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11901
0
    {
11902
      /* Turn flsi instruction into sda access format.  */
11903
0
      insn = (insn & 0x7ff07000) | (N32_REG_GP << 15);
11904
11905
      /* Add relocation type to flsi.  */
11906
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11907
0
      irel->r_addend += flsi_offset;
11908
0
      bfd_putb32 (insn, contents + re_irel->r_offset);
11909
11910
0
      re_irel->r_addend |= 1;
11911
0
      *again = true;
11912
0
    }
11913
0
}
11914
11915
static bool
11916
nds32_relax_adjust_label (bfd *abfd, asection *sec,
11917
        Elf_Internal_Rela *internal_relocs,
11918
        bfd_byte *contents,
11919
        nds32_elf_blank_t **relax_blank_list,
11920
        int optimize, int opt_size)
11921
0
{
11922
  /* This code block is used to adjust 4-byte alignment by relax a pair
11923
     of instruction a time.
11924
11925
     It recognizes three types of relocations.
11926
     1. R_NDS32_LABEL - a alignment.
11927
     2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11928
     3. is_16bit_NOP () - remove a 16-bit instruction.  */
11929
11930
  /* TODO: It seems currently implementation only support 4-byte alignment.
11931
     We should handle any-alignment.  */
11932
11933
0
  Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11934
0
  Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11935
0
  Elf_Internal_Rela rel_temp;
11936
0
  Elf_Internal_Rela *irelend;
11937
0
  bfd_vma address;
11938
0
  uint16_t insn16;
11939
11940
  /* Checking for branch relaxation relies on the relocations to
11941
     be sorted on 'r_offset'.  This is not guaranteed so we must sort.  */
11942
0
  nds32_insertion_sort (internal_relocs, sec->reloc_count,
11943
0
      sizeof (Elf_Internal_Rela), compar_reloc);
11944
11945
0
  irelend = internal_relocs + sec->reloc_count;
11946
11947
  /* Force R_NDS32_LABEL before R_NDS32_INSN16.  */
11948
  /* FIXME: Can we generate the right order in assembler?
11949
     So we don't have to swapping them here.  */
11950
11951
0
  for (label_rel = internal_relocs, insn_rel = internal_relocs;
11952
0
       label_rel < irelend; label_rel++)
11953
0
    {
11954
0
      if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11955
0
  continue;
11956
11957
      /* Find the first reloc has the same offset with label_rel.  */
11958
0
      while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11959
0
  insn_rel++;
11960
11961
0
      for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11962
0
     insn_rel++)
11963
  /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11964
     address.  */
11965
0
  if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11966
0
    break;
11967
11968
0
      if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11969
0
    && insn_rel < label_rel)
11970
0
  {
11971
    /* Swap the two reloc if the R_NDS32_INSN16 is
11972
       before R_NDS32_LABEL.  */
11973
0
    memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11974
0
    memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11975
0
    memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11976
0
  }
11977
0
    }
11978
11979
0
  label_rel = NULL;
11980
0
  insn_rel = NULL;
11981
  /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11982
     or higher, remove other R_NDS32_LABEL with lower alignment.
11983
     If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11984
     then the R_NDS32_LABEL sequence is broke.  */
11985
0
  for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11986
0
    {
11987
0
      if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11988
0
  {
11989
0
    if (label_rel == NULL)
11990
0
      {
11991
0
        if (tmp_rel->r_addend < 2)
11992
0
    label_rel = tmp_rel;
11993
0
        continue;
11994
0
      }
11995
0
    else if (tmp_rel->r_addend > 1)
11996
0
      {
11997
        /* Remove all LABEL relocation from label_rel to tmp_rel
11998
     including relocations with same offset as tmp_rel.  */
11999
0
        for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
12000
0
    {
12001
0
      if (tmp2_rel->r_offset == tmp_rel->r_offset)
12002
0
        break;
12003
12004
0
      if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
12005
0
          && tmp2_rel->r_addend < 2)
12006
0
        tmp2_rel->r_info =
12007
0
          ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
12008
0
            R_NDS32_NONE);
12009
0
    }
12010
0
        label_rel = NULL;
12011
0
      }
12012
0
  }
12013
0
      else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
12014
0
  {
12015
    /* A new INSN16 which can be converted, so clear label_rel.  */
12016
0
    if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
12017
0
           irelend, &insn16)
12018
0
        || is_16bit_NOP (abfd, sec, tmp_rel))
12019
0
      label_rel = NULL;
12020
0
  }
12021
0
    }
12022
12023
0
  label_rel = NULL;
12024
0
  insn_rel = NULL;
12025
  /* Optimized for speed and nothing has not been relaxed.
12026
     It's time to align labels.
12027
     We may convert a 16-bit instruction right before a label to
12028
     32-bit, in order to align the label if necessary
12029
     all reloc entries has been sorted by r_offset.  */
12030
0
  for (irel = internal_relocs;
12031
0
       irel < irelend && irel->r_offset < sec->size; irel++)
12032
0
    {
12033
0
      if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
12034
0
    && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
12035
0
  continue;
12036
12037
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
12038
0
  {
12039
    /* A new INSN16 found, resize the old one.  */
12040
0
    if (is_convert_32_to_16
12041
0
        (abfd, sec, irel, internal_relocs, irelend, &insn16)
12042
0
        || is_16bit_NOP (abfd, sec, irel))
12043
0
      {
12044
0
        if (insn_rel)
12045
0
    {
12046
      /* Previous INSN16 reloc exists, reduce its
12047
         size to 16-bit.  */
12048
0
      if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12049
0
             irelend, &insn16))
12050
0
        {
12051
0
          nds32_elf_write_16 (abfd, contents, insn_rel,
12052
0
            internal_relocs, irelend, insn16);
12053
12054
0
          if (!insert_nds32_elf_blank_recalc_total
12055
0
        (relax_blank_list, insn_rel->r_offset + 2, 2))
12056
0
      return false;
12057
0
        }
12058
0
      else if (is_16bit_NOP (abfd, sec, insn_rel))
12059
0
        {
12060
0
          if (!insert_nds32_elf_blank_recalc_total
12061
0
        (relax_blank_list, insn_rel->r_offset, 2))
12062
0
      return false;
12063
0
        }
12064
0
      insn_rel->r_info =
12065
0
        ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
12066
0
    }
12067
        /* Save the new one for later use.  */
12068
0
        insn_rel = irel;
12069
0
      }
12070
0
    else
12071
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
12072
0
           R_NDS32_NONE);
12073
0
  }
12074
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
12075
0
  {
12076
    /* Search for label.  */
12077
0
    int force_relax = 0;
12078
12079
    /* Label on 16-bit instruction or optimization
12080
       needless, just reset this reloc.  */
12081
0
    insn16 = bfd_getb16 (contents + irel->r_offset);
12082
0
    if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12083
0
      {
12084
0
        irel->r_info =
12085
0
    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12086
0
        continue;
12087
0
      }
12088
12089
0
    address =
12090
0
      irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12091
0
              irel->r_offset, 1);
12092
12093
0
    if (!insn_rel)
12094
0
      {
12095
        /* Check if there is case which can not be aligned.  */
12096
0
        if (irel->r_addend == 2 && address & 0x2)
12097
0
    return false;
12098
0
        continue;
12099
0
      }
12100
12101
    /* Try to align this label.  */
12102
12103
0
    if ((irel->r_addend & 0x1f) < 2)
12104
0
      {
12105
        /* Check if there is a INSN16 at the same address.
12106
     Label_rel always seats before insn_rel after
12107
     our sort.  */
12108
12109
        /* Search for INSN16 at LABEL location.  If INSN16 is at
12110
     same location and this LABEL alignment is lower than 2,
12111
     the INSN16 can be converted to 2-byte.  */
12112
0
        for (tmp_rel = irel;
12113
0
       tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12114
0
       tmp_rel++)
12115
0
    {
12116
0
      if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12117
0
          && (is_convert_32_to_16
12118
0
        (abfd, sec, tmp_rel, internal_relocs,
12119
0
         irelend, &insn16)
12120
0
        || is_16bit_NOP (abfd, sec, tmp_rel)))
12121
0
        {
12122
0
          force_relax = 1;
12123
0
          break;
12124
0
        }
12125
0
    }
12126
0
      }
12127
12128
0
    if (force_relax || irel->r_addend == 1 || address & 0x2)
12129
0
      {
12130
        /* Label not aligned.  */
12131
        /* Previous reloc exists, reduce its size to 16-bit.  */
12132
0
        if (is_convert_32_to_16 (abfd, sec, insn_rel,
12133
0
               internal_relocs, irelend, &insn16))
12134
0
    {
12135
0
      nds32_elf_write_16 (abfd, contents, insn_rel,
12136
0
              internal_relocs, irelend, insn16);
12137
12138
0
      if (!insert_nds32_elf_blank_recalc_total
12139
0
          (relax_blank_list, insn_rel->r_offset + 2, 2))
12140
0
        return false;
12141
0
    }
12142
0
        else if (is_16bit_NOP (abfd, sec, insn_rel))
12143
0
    {
12144
0
      if (!insert_nds32_elf_blank_recalc_total
12145
0
          (relax_blank_list, insn_rel->r_offset, 2))
12146
0
        return false;
12147
0
    }
12148
12149
0
      }
12150
    /* INSN16 reloc is used.  */
12151
0
    insn_rel = NULL;
12152
0
  }
12153
0
    }
12154
12155
0
  address =
12156
0
    sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12157
0
  if (insn_rel && (address & 0x2 || opt_size))
12158
0
    {
12159
0
      if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12160
0
             irelend, &insn16))
12161
0
  {
12162
0
    nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12163
0
            irelend, insn16);
12164
0
    if (!insert_nds32_elf_blank_recalc_total
12165
0
        (relax_blank_list, insn_rel->r_offset + 2, 2))
12166
0
      return false;
12167
0
    insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12168
0
             R_NDS32_NONE);
12169
0
  }
12170
0
      else if (is_16bit_NOP (abfd, sec, insn_rel))
12171
0
  {
12172
0
    if (!insert_nds32_elf_blank_recalc_total
12173
0
        (relax_blank_list, insn_rel->r_offset, 2))
12174
0
      return false;
12175
0
    insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12176
0
             R_NDS32_NONE);
12177
0
  }
12178
0
    }
12179
0
  insn_rel = NULL;
12180
0
  return true;
12181
0
}
12182
12183
static bool
12184
nds32_elf_relax_section (bfd *abfd, asection *sec,
12185
       struct bfd_link_info *link_info, bool *again)
12186
0
{
12187
0
  nds32_elf_blank_t *relax_blank_list = NULL;
12188
0
  Elf_Internal_Shdr *symtab_hdr;
12189
0
  Elf_Internal_Rela *internal_relocs;
12190
0
  Elf_Internal_Rela *irel;
12191
0
  Elf_Internal_Rela *irelend;
12192
0
  Elf_Internal_Sym *isymbuf = NULL;
12193
0
  bfd_byte *contents = NULL;
12194
0
  bool result = true;
12195
0
  int optimize = 0;
12196
0
  int opt_size = 0;
12197
0
  uint32_t insn;
12198
0
  uint16_t insn16;
12199
12200
  /* Target dependnet option.  */
12201
0
  struct elf_nds32_link_hash_table *table;
12202
0
  int load_store_relax;
12203
12204
0
  relax_blank_list = NULL;
12205
12206
0
  *again = false;
12207
12208
  /* Nothing to do for
12209
   * relocatable link or
12210
   * non-relocatable section or
12211
   * non-code section or
12212
   * empty content or
12213
   * no reloc entry.  */
12214
0
  if (bfd_link_relocatable (link_info)
12215
0
      || (sec->flags & SEC_RELOC) == 0
12216
0
      || (sec->flags & SEC_EXCLUDE) != 0
12217
0
      || (sec->flags & SEC_CODE) == 0
12218
0
      || sec->size == 0
12219
0
      || sec->reloc_count == 0)
12220
0
    return true;
12221
12222
  /* 09.12.11 Workaround.  */
12223
  /*  We have to adjust align for R_NDS32_LABEL if needed.
12224
      The adjust approach only can fix 2-byte align once.  */
12225
0
  if (sec->alignment_power > 2)
12226
0
    return true;
12227
12228
  /* Do TLS model conversion once at first.  */
12229
0
  nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12230
12231
  /* The optimization type to do.  */
12232
12233
0
  table = nds32_elf_hash_table (link_info);
12234
12235
  /* Save the first section for abs symbol relaxation.
12236
     This is used for checking gp relaxation in the
12237
     nds32_elf_relax_loadstore and nds32_elf_relax_lo12.  */
12238
0
  nds32_elf_relax_guard (NULL, 0, sec, NULL, again, true,
12239
0
       table, NULL, NULL);
12240
12241
  /* The begining of general relaxation.  */
12242
12243
0
  if (is_SDA_BASE_set == 0)
12244
0
    {
12245
0
      bfd_vma gp;
12246
0
      is_SDA_BASE_set = 1;
12247
0
      nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12248
0
        &gp, false);
12249
0
      relax_range_measurement (abfd, link_info);
12250
0
    }
12251
12252
0
  symtab_hdr = &elf_symtab_hdr (abfd);
12253
  /* Relocations MUST be kept in memory, because relaxation adjust them.  */
12254
0
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12255
0
                 true);
12256
0
  if (internal_relocs == NULL)
12257
0
    goto error_return;
12258
12259
0
  irelend = internal_relocs + sec->reloc_count;
12260
0
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
12261
0
         irelend, R_NDS32_RELAX_ENTRY);
12262
12263
0
  if (irel == irelend)
12264
0
    return true;
12265
12266
0
  if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12267
0
    {
12268
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12269
0
  return true;
12270
12271
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12272
0
  optimize = 1;
12273
12274
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12275
0
  opt_size = 1;
12276
0
    }
12277
12278
0
  load_store_relax = table->load_store_relax;
12279
12280
  /* Get symbol table and section content.  */
12281
0
  contents = NULL;
12282
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true)
12283
0
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
12284
0
    goto error_return;
12285
12286
  /* Do relax loop only when finalize is not done.
12287
     Take care of relaxable relocs except INSN16.  */
12288
0
  for (irel = internal_relocs; irel < irelend; irel++)
12289
0
    {
12290
0
      int seq_len;    /* Original length of instruction sequence.  */
12291
0
      int insn_len = 0;   /* Final length of instruction sequence.  */
12292
0
      bool removed;
12293
12294
0
      insn = 0;
12295
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12296
0
    && (irel->r_addend & 0x1f) >= 2)
12297
0
  optimize = 1;
12298
12299
      /* Relocation Types
12300
   R_NDS32_LONGCALL1  53
12301
   R_NDS32_LONGCALL2  54
12302
   R_NDS32_LONGCALL3  55
12303
   R_NDS32_LONGJUMP1  56
12304
   R_NDS32_LONGJUMP2  57
12305
   R_NDS32_LONGJUMP3  58
12306
   R_NDS32_LOADSTORE  59  */
12307
0
      if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12308
0
    && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12309
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
12310
12311
      /* Relocation Types
12312
   R_NDS32_LONGCALL4  107
12313
   R_NDS32_LONGCALL5  108
12314
   R_NDS32_LONGCALL6  109
12315
   R_NDS32_LONGJUMP4  110
12316
   R_NDS32_LONGJUMP5  111
12317
   R_NDS32_LONGJUMP6  112
12318
   R_NDS32_LONGJUMP7  113  */
12319
0
      else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12320
0
         && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12321
0
  seq_len = 4;
12322
12323
  /* Relocation Types
12324
   R_NDS32_LO12S0_RELA    30
12325
   R_NDS32_LO12S1_RELA    29
12326
   R_NDS32_LO12S2_RELA    28
12327
   R_NDS32_LO12S2_SP_RELA   71
12328
   R_NDS32_LO12S2_DP_RELA   70
12329
   R_NDS32_GOT_LO12   46
12330
   R_NDS32_GOTOFF_LO12    50
12331
   R_NDS32_PLTREL_LO12    65
12332
   R_NDS32_PLT_GOTREL_LO12  67
12333
   R_NDS32_17IFC_PCREL_RELA 96
12334
   R_NDS32_GOT_SUFF   193
12335
   R_NDS32_GOTOFF_SUFF    194
12336
   R_NDS32_PLT_GOT_SUFF   195
12337
   R_NDS32_MULCALL_SUFF   196
12338
   R_NDS32_PTR      197  */
12339
0
      else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12340
0
    && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12341
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12342
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12343
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12344
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12345
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12346
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12347
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12348
0
         || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12349
0
       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12350
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12351
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12352
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12353
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12354
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12355
0
  seq_len = 0;
12356
0
      else
12357
0
  continue;
12358
12359
0
      insn_len = seq_len;
12360
0
      removed = false;
12361
12362
0
      switch (ELF32_R_TYPE (irel->r_info))
12363
0
  {
12364
0
  case R_NDS32_LONGCALL1:
12365
0
    removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12366
0
                 &insn_len, contents, isymbuf,
12367
0
                 symtab_hdr);
12368
0
    break;
12369
0
  case R_NDS32_LONGCALL2:
12370
0
    removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12371
0
                 &insn_len, contents, isymbuf,
12372
0
                 symtab_hdr);
12373
0
    break;
12374
0
  case R_NDS32_LONGCALL3:
12375
0
    removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12376
0
                 &insn_len, contents, isymbuf,
12377
0
                 symtab_hdr);
12378
0
    break;
12379
0
  case R_NDS32_LONGJUMP1:
12380
0
    removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12381
0
                 &insn_len, contents, isymbuf,
12382
0
                 symtab_hdr);
12383
0
    break;
12384
0
  case R_NDS32_LONGJUMP2:
12385
0
    removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12386
0
                 &insn_len, contents, isymbuf,
12387
0
                 symtab_hdr);
12388
0
    break;
12389
0
  case R_NDS32_LONGJUMP3:
12390
0
    removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12391
0
                 &insn_len, contents, isymbuf,
12392
0
                 symtab_hdr);
12393
0
    break;
12394
0
  case R_NDS32_LONGCALL4:
12395
0
    removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12396
0
                 &insn_len, contents, isymbuf,
12397
0
                 symtab_hdr);
12398
0
    break;
12399
0
  case R_NDS32_LONGCALL5:
12400
0
    removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12401
0
                 &insn_len, contents, isymbuf,
12402
0
                 symtab_hdr);
12403
0
    break;
12404
0
  case R_NDS32_LONGCALL6:
12405
0
    removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12406
0
                 &insn_len, contents, isymbuf,
12407
0
                 symtab_hdr);
12408
0
    break;
12409
0
  case R_NDS32_LONGJUMP4:
12410
0
    removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12411
0
                 &insn_len, contents, isymbuf,
12412
0
                 symtab_hdr);
12413
0
    break;
12414
0
  case R_NDS32_LONGJUMP5:
12415
0
    removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12416
0
                 &insn_len, &seq_len, contents,
12417
0
                 isymbuf, symtab_hdr);
12418
0
    break;
12419
0
  case R_NDS32_LONGJUMP6:
12420
0
    removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12421
0
                 &insn_len, &seq_len, contents,
12422
0
                 isymbuf, symtab_hdr);
12423
0
    break;
12424
0
  case R_NDS32_LONGJUMP7:
12425
0
    removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12426
0
                 &insn_len, &seq_len, contents,
12427
0
                 isymbuf, symtab_hdr);
12428
0
    break;
12429
0
  case R_NDS32_LOADSTORE:
12430
0
    removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12431
0
                 internal_relocs, &insn_len,
12432
0
                 contents, isymbuf, symtab_hdr,
12433
0
                 load_store_relax, table);
12434
0
    break;
12435
0
  case R_NDS32_LO12S0_RELA:
12436
0
  case R_NDS32_LO12S1_RELA:
12437
0
  case R_NDS32_LO12S2_RELA:
12438
0
  case R_NDS32_LO12S2_DP_RELA:
12439
0
  case R_NDS32_LO12S2_SP_RELA:
12440
    /* Relax for low part.  */
12441
0
    nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12442
0
        contents, isymbuf, symtab_hdr, table);
12443
12444
    /* It is impossible to delete blank, so just continue.  */
12445
0
    continue;
12446
0
  case R_NDS32_PTR:
12447
0
    removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12448
0
           &insn_len, &seq_len, contents);
12449
0
    break;
12450
0
  case R_NDS32_LSI:
12451
0
    nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12452
0
        contents, isymbuf, symtab_hdr, again);
12453
0
    continue;
12454
0
  case R_NDS32_GOT_LO12:
12455
0
  case R_NDS32_GOTOFF_LO12:
12456
0
  case R_NDS32_PLTREL_LO12:
12457
0
  case R_NDS32_PLT_GOTREL_LO12:
12458
0
  case R_NDS32_GOTPC_LO12:
12459
0
  case R_NDS32_TLS_LE_LO12:
12460
0
  case R_NDS32_TLS_LE_ADD:
12461
0
  case R_NDS32_TLS_LE_LS:
12462
0
  case R_NDS32_PLT_GOT_SUFF:
12463
0
  case R_NDS32_GOT_SUFF:
12464
0
  case R_NDS32_GOTOFF_SUFF:
12465
0
    continue;
12466
0
  default:
12467
0
    continue;
12468
0
  }
12469
12470
0
      if (removed && seq_len - insn_len > 0)
12471
0
  {
12472
0
    if (!insert_nds32_elf_blank
12473
0
        (&relax_blank_list, irel->r_offset + insn_len,
12474
0
         seq_len - insn_len))
12475
0
      goto error_return;
12476
0
    *again = true;
12477
0
  }
12478
0
    }
12479
12480
0
  calc_nds32_blank_total (relax_blank_list);
12481
12482
0
  if (table->relax_fp_as_gp)
12483
0
    {
12484
0
      if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12485
0
         irelend, isymbuf))
12486
0
  goto error_return;
12487
12488
0
      if (!*again)
12489
0
  {
12490
0
    if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12491
0
                 irelend))
12492
0
      goto error_return;
12493
0
  }
12494
0
    }
12495
12496
0
  if (!*again)
12497
0
    {
12498
0
      if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12499
0
             &relax_blank_list, optimize, opt_size))
12500
0
  goto error_return;
12501
0
    }
12502
12503
  /* It doesn't matter optimize_for_space_no_align anymore.
12504
       If object file is assembled with flag '-Os',
12505
       the we don't adjust jump-destination on 4-byte boundary.  */
12506
12507
0
  if (relax_blank_list)
12508
0
    {
12509
0
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12510
0
      relax_blank_list = NULL;
12511
0
    }
12512
12513
0
  if (!*again)
12514
0
    {
12515
      /* Closing the section, so we don't relax it anymore.  */
12516
0
      bfd_vma sec_size_align;
12517
0
      Elf_Internal_Rela *tmp_rel;
12518
12519
      /* Pad to alignment boundary.  Only handle current section alignment.  */
12520
0
      sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12521
0
           & ((-1U) << sec->alignment_power);
12522
0
      if ((sec_size_align - sec->size) & 0x2)
12523
0
  {
12524
0
    insn16 = NDS32_NOP16;
12525
0
    bfd_putb16 (insn16, contents + sec->size);
12526
0
    sec->size += 2;
12527
0
  }
12528
12529
0
      while (sec_size_align != sec->size)
12530
0
  {
12531
0
    insn = NDS32_NOP32;
12532
0
    bfd_putb32 (insn, contents + sec->size);
12533
0
    sec->size += 4;
12534
0
  }
12535
12536
0
      tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12537
0
          irelend, R_NDS32_RELAX_ENTRY);
12538
0
      if (tmp_rel != irelend)
12539
0
  tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12540
12541
0
      clean_nds32_elf_blank ();
12542
0
    }
12543
12544
0
 finish:
12545
0
  if (elf_section_data (sec)->relocs != internal_relocs)
12546
0
    free (internal_relocs);
12547
12548
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
12549
0
    free (contents);
12550
12551
0
  if (symtab_hdr->contents != (bfd_byte *) isymbuf)
12552
0
    free (isymbuf);
12553
12554
0
  return result;
12555
12556
0
 error_return:
12557
0
  result = false;
12558
0
  goto finish;
12559
0
}
12560
12561
static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12562
{
12563
  {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12564
  {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12565
  {NULL, 0, 0, 0, 0}
12566
};
12567
12568
static bool
12569
nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
12570
3.67k
{
12571
3.67k
  const char *name = hdr->bfd_section->name;
12572
12573
3.67k
  if (startswith (name, ".sbss")
12574
3.63k
      || startswith (name, ".sdata"))
12575
99
    hdr->bfd_section->flags |= SEC_SMALL_DATA;
12576
12577
3.67k
  return true;
12578
3.67k
}
12579
12580
static bool
12581
nds32_elf_output_arch_syms (struct bfd_link_info *info,
12582
          void *finfo ATTRIBUTE_UNUSED,
12583
          int (*func) (void *, const char *,
12584
           Elf_Internal_Sym *,
12585
           asection *,
12586
           struct elf_link_hash_entry *)
12587
          ATTRIBUTE_UNUSED)
12588
0
{
12589
0
  FILE *sym_ld_script = NULL;
12590
0
  struct elf_nds32_link_hash_table *table;
12591
12592
0
  table = nds32_elf_hash_table (info);
12593
0
  sym_ld_script = table->sym_ld_script;
12594
12595
0
  if (check_start_export_sym)
12596
0
    fprintf (sym_ld_script, "}\n");
12597
12598
0
  return true;
12599
0
}
12600
12601
static enum elf_reloc_type_class
12602
nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12603
          const asection *rel_sec ATTRIBUTE_UNUSED,
12604
          const Elf_Internal_Rela *rela)
12605
0
{
12606
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
12607
0
    {
12608
0
    case R_NDS32_RELATIVE:
12609
0
      return reloc_class_relative;
12610
0
    case R_NDS32_JMP_SLOT:
12611
0
      return reloc_class_plt;
12612
0
    case R_NDS32_COPY:
12613
0
      return reloc_class_copy;
12614
0
    default:
12615
0
      return reloc_class_normal;
12616
0
    }
12617
0
}
12618
12619
/* Put target dependent option into info hash table.  */
12620
void
12621
bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12622
           int relax_fp_as_gp,
12623
           int eliminate_gc_relocs,
12624
           FILE * sym_ld_script,
12625
           int hyper_relax,
12626
           int tls_desc_trampoline,
12627
           int load_store_relax)
12628
0
{
12629
0
  struct elf_nds32_link_hash_table *table;
12630
12631
0
  table = nds32_elf_hash_table (link_info);
12632
0
  if (table == NULL)
12633
0
    return;
12634
12635
0
  table->relax_fp_as_gp = relax_fp_as_gp;
12636
0
  table->eliminate_gc_relocs = eliminate_gc_relocs;
12637
0
  table->sym_ld_script = sym_ld_script;
12638
0
  table->hyper_relax = hyper_relax;
12639
0
  table->tls_desc_trampoline = tls_desc_trampoline;
12640
0
  table ->load_store_relax = load_store_relax;
12641
0
}
12642

12643
12644
/* These functions and data-structures are used for fp-as-gp
12645
   optimization.  */
12646
12647
0
#define FAG_THRESHOLD 3  /* At least 3 gp-access.  */
12648
/* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12649
   the read-only section and read-write section.  */
12650
0
#define FAG_WINDOW  (508 - 32)
12651
12652
/* An nds32_fag represent a gp-relative access.
12653
   We find best fp-base by using a sliding window
12654
   to find a base address which can cover most gp-access.  */
12655
struct nds32_fag
12656
{
12657
  struct nds32_fag *next; /* NULL-teminated linked list.  */
12658
  bfd_vma addr;     /* The address of this fag.  */
12659
  Elf_Internal_Rela **relas;  /* The relocations associated with this fag.
12660
           It is used for applying FP7U2_FLAG.  */
12661
  int count;      /* How many times this address is referred.
12662
           There should be exactly `count' relocations
12663
           in relas.  */
12664
  int relas_capcity;    /* The buffer size of relas.
12665
           We use an array instead of linked-list,
12666
           and realloc is used to adjust buffer size.  */
12667
};
12668
12669
static void
12670
nds32_fag_init (struct nds32_fag *head)
12671
0
{
12672
0
  memset (head, 0, sizeof (struct nds32_fag));
12673
0
}
12674
12675
static void
12676
nds32_fag_verify (struct nds32_fag *head)
12677
0
{
12678
0
  struct nds32_fag *iter;
12679
0
  struct nds32_fag *prev;
12680
12681
0
  prev = NULL;
12682
0
  iter = head->next;
12683
0
  while (iter)
12684
0
    {
12685
0
      if (prev && prev->addr >= iter->addr)
12686
0
  puts ("Bug in fp-as-gp insertion.");
12687
0
      prev = iter;
12688
0
      iter = iter->next;
12689
0
    }
12690
0
}
12691
12692
/* Insert a fag in ascending order.
12693
   If a fag of the same address already exists,
12694
   they are chained by relas array.  */
12695
12696
static void
12697
nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12698
      Elf_Internal_Rela * rel)
12699
0
{
12700
0
  struct nds32_fag *iter;
12701
0
  struct nds32_fag *new_fag;
12702
0
  const int INIT_RELAS_CAP = 4;
12703
12704
0
  for (iter = head;
12705
0
       iter->next && iter->next->addr <= addr;
12706
0
       iter = iter->next)
12707
0
    /* Find somewhere to insert.  */ ;
12708
12709
  /* `iter' will be equal to `head' if the list is empty.  */
12710
0
  if (iter != head && iter->addr == addr)
12711
0
    {
12712
      /* The address exists in the list.
12713
   Insert `rel' into relocation list, relas.  */
12714
12715
      /* Check whether relas is big enough.  */
12716
0
      if (iter->count >= iter->relas_capcity)
12717
0
  {
12718
0
    iter->relas_capcity *= 2;
12719
0
    iter->relas = bfd_realloc
12720
0
      (iter->relas, iter->relas_capcity * sizeof (void *));
12721
0
  }
12722
0
      iter->relas[iter->count++] = rel;
12723
0
      return;
12724
0
    }
12725
12726
  /* This is a new address.  Create a fag node for it.  */
12727
0
  new_fag = bfd_malloc (sizeof (struct nds32_fag));
12728
0
  memset (new_fag, 0, sizeof (*new_fag));
12729
0
  new_fag->addr = addr;
12730
0
  new_fag->count = 1;
12731
0
  new_fag->next = iter->next;
12732
0
  new_fag->relas_capcity = INIT_RELAS_CAP;
12733
0
  new_fag->relas = (Elf_Internal_Rela **)
12734
0
    bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12735
0
  new_fag->relas[0] = rel;
12736
0
  iter->next = new_fag;
12737
12738
0
  nds32_fag_verify (head);
12739
0
}
12740
12741
static void
12742
nds32_fag_free_list (struct nds32_fag *head)
12743
0
{
12744
0
  struct nds32_fag *iter;
12745
12746
0
  iter = head->next;
12747
0
  while (iter)
12748
0
    {
12749
0
      struct nds32_fag *tmp = iter;
12750
0
      iter = iter->next;
12751
0
      free (tmp->relas);
12752
0
      tmp->relas = NULL;
12753
0
      free (tmp);
12754
0
    }
12755
0
}
12756
12757
/* Find the best fp-base address.
12758
   The relocation associated with that address is returned,
12759
   so we can track the symbol instead of a fixed address.
12760
12761
   When relaxation, the address of an datum may change,
12762
   because a text section is shrinked, so the data section
12763
   moves forward.  If the aligments of text and data section
12764
   are different, their distance may change too.
12765
   Therefore, tracking a fixed address is not appriate.  */
12766
12767
static int
12768
nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12769
0
{
12770
0
  struct nds32_fag *base; /* First fag in the window.  */
12771
0
  struct nds32_fag *last; /* First fag outside the window.  */
12772
0
  int accu = 0;     /* Usage accumulation.  */
12773
0
  struct nds32_fag *best; /* Best fag.  */
12774
0
  int baccu = 0;    /* Best accumulation.  */
12775
12776
  /* Use first fag for initial, and find the last fag in the window.
12777
12778
     In each iteration, we could simply subtract previous fag
12779
     and accumulate following fags which are inside the window,
12780
     untill we each the end.  */
12781
12782
0
  if (head->next == NULL)
12783
0
    {
12784
0
      *bestpp = NULL;
12785
0
      return 0;
12786
0
    }
12787
12788
  /* Initialize base.  */
12789
0
  base = head->next;
12790
0
  best = base;
12791
0
  for (last = base;
12792
0
       last && last->addr < base->addr + FAG_WINDOW;
12793
0
       last = last->next)
12794
0
    accu += last->count;
12795
12796
0
  baccu = accu;
12797
12798
  /* Record the best base in each iteration.  */
12799
0
  while (base->next)
12800
0
    {
12801
0
      accu -= base->count;
12802
0
      base = base->next;
12803
      /* Account fags in window.  */
12804
0
      for (/* Nothing.  */;
12805
0
     last && last->addr < base->addr + FAG_WINDOW;
12806
0
     last = last->next)
12807
0
  accu += last->count;
12808
12809
      /* A better fp-base?  */
12810
0
      if (accu > baccu)
12811
0
  {
12812
0
    best = base;
12813
0
    baccu = accu;
12814
0
  }
12815
0
    }
12816
12817
0
  if (bestpp)
12818
0
    *bestpp = best;
12819
0
  return baccu;
12820
0
}
12821
12822
/* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12823
   so we can convert it fo fp-relative access later.
12824
   `best_fag' is the best fp-base.  Only those inside the window
12825
   of best_fag is applied the flag.  */
12826
12827
static bool
12828
nds32_fag_mark_relax (struct bfd_link_info *link_info,
12829
          asection *sec, struct nds32_fag *best_fag,
12830
          Elf_Internal_Rela *internal_relocs,
12831
          Elf_Internal_Rela *irelend)
12832
0
{
12833
0
  struct nds32_fag *ifag;
12834
0
  bfd_vma best_fpbase, gp;
12835
0
  bfd *output_bfd;
12836
12837
0
  output_bfd = sec->output_section->owner;
12838
0
  nds32_elf_final_sda_base (output_bfd, link_info, &gp, false);
12839
0
  best_fpbase = best_fag->addr;
12840
12841
0
  if (best_fpbase > gp + sdata_range[1][1]
12842
0
      || best_fpbase < gp - sdata_range[1][0])
12843
0
    return false;
12844
12845
  /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12846
     so we know they can be converted to lwi37.fp.   */
12847
0
  for (ifag = best_fag;
12848
0
       ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12849
0
    {
12850
0
      int i;
12851
12852
0
      for (i = 0; i < ifag->count; i++)
12853
0
  {
12854
0
    Elf_Internal_Rela *insn16_rel;
12855
0
    Elf_Internal_Rela *fag_rel;
12856
12857
0
    fag_rel = ifag->relas[i];
12858
12859
    /* Only if this is within the WINDOWS, FP7U2_FLAG
12860
       is applied.  */
12861
12862
0
    insn16_rel = find_relocs_at_address
12863
0
      (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12864
12865
0
    if (insn16_rel != irelend)
12866
0
      insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12867
0
  }
12868
0
    }
12869
0
  return true;
12870
0
}
12871
12872
/* Reset INSN16 to clean fp as gp.  */
12873
12874
static void
12875
nds32_fag_unmark_relax (struct nds32_fag *fag,
12876
      Elf_Internal_Rela *internal_relocs,
12877
      Elf_Internal_Rela *irelend)
12878
0
{
12879
0
  struct nds32_fag *ifag;
12880
0
  int i;
12881
0
  Elf_Internal_Rela *insn16_rel;
12882
0
  Elf_Internal_Rela *fag_rel;
12883
12884
0
  for (ifag = fag; ifag; ifag = ifag->next)
12885
0
    {
12886
0
      for (i = 0; i < ifag->count; i++)
12887
0
  {
12888
0
    fag_rel = ifag->relas[i];
12889
12890
    /* Restore the INSN16 relocation.  */
12891
0
    insn16_rel = find_relocs_at_address
12892
0
      (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12893
12894
0
    if (insn16_rel != irelend)
12895
0
      insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12896
0
  }
12897
0
    }
12898
0
}
12899
12900
/* This is the main function of fp-as-gp optimization.
12901
   It should be called by relax_section.  */
12902
12903
static bool
12904
nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12905
          bfd *abfd, asection *sec,
12906
          Elf_Internal_Rela *internal_relocs,
12907
          Elf_Internal_Rela *irelend,
12908
          Elf_Internal_Sym *isymbuf)
12909
0
{
12910
0
  Elf_Internal_Rela *begin_rel = NULL;
12911
0
  Elf_Internal_Rela *irel;
12912
0
  struct nds32_fag fag_head;
12913
0
  Elf_Internal_Shdr *symtab_hdr;
12914
0
  bfd_byte *contents;
12915
0
  bool ifc_inside = false;
12916
12917
  /* FIXME: Can we bfd_elf_link_read_relocs for the relocs?  */
12918
12919
  /* Per-function fp-base selection.
12920
     1. Create a list for all the gp-relative access.
12921
     2. Base on those gp-relative address,
12922
  find a fp-base which can cover most access.
12923
     3. Use the fp-base for fp-as-gp relaxation.
12924
12925
     NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12926
     we should
12927
     1. delete the `la $fp, _FP_BASE_' instruction and
12928
     2. not convert lwi.gp to lwi37.fp.
12929
12930
     To delete the _FP_BASE_ instruction, we simply apply
12931
     R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12932
12933
     To suppress the conversion, we simply NOT to apply
12934
     R_NDS32_INSN16_FP7U2_FLAG flag.  */
12935
12936
0
  symtab_hdr = &elf_symtab_hdr (abfd);
12937
12938
0
  contents = NULL;
12939
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true)
12940
0
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
12941
0
    return false;
12942
12943
  /* Check whether it is worth for fp-as-gp optimization,
12944
     i.e., at least 3 gp-load.
12945
12946
     Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12947
     apply this optimization.  */
12948
12949
0
  for (irel = internal_relocs; irel < irelend; irel++)
12950
0
    {
12951
      /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12952
   One we enter the begin of the region, we track all the LW/ST
12953
   instructions, so when we leave the region, we try to find
12954
   the best fp-base address for those LW/ST instructions.  */
12955
12956
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12957
0
    && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12958
0
  {
12959
    /* Begin of the region.  */
12960
0
    if (begin_rel)
12961
      /* xgettext:c-format */
12962
0
      _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12963
12964
0
    begin_rel = irel;
12965
0
    nds32_fag_init (&fag_head);
12966
0
    ifc_inside = false;
12967
0
  }
12968
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12969
0
         && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12970
0
  {
12971
0
    int accu;
12972
0
    struct nds32_fag *best_fag, *tmp_fag;
12973
0
    int dist;
12974
12975
    /* End of the region.
12976
       Check whether it is worth to do fp-as-gp.  */
12977
12978
0
    if (begin_rel == NULL)
12979
0
      {
12980
        /* xgettext:c-format */
12981
0
        _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12982
0
          abfd, sec);
12983
0
        continue;
12984
0
      }
12985
12986
0
    accu = nds32_fag_find_base (&fag_head, &best_fag);
12987
12988
    /* Clean FP7U2_FLAG because they may set ever.  */
12989
0
    tmp_fag = fag_head.next;
12990
0
    nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12991
12992
    /* Check if it is worth, and FP_BASE is near enough to SDA_BASE.  */
12993
0
    if (accu < FAG_THRESHOLD
12994
0
        || !nds32_fag_mark_relax (link_info, sec, best_fag,
12995
0
          internal_relocs, irelend))
12996
0
      {
12997
        /* Not worth to do fp-as-gp.  */
12998
0
        begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12999
0
        begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13000
0
        irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
13001
0
        irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
13002
0
        nds32_fag_free_list (&fag_head);
13003
0
        begin_rel = NULL;
13004
0
        continue;
13005
0
      }
13006
13007
    /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
13008
       so we use it to record the distance to the reloction of best
13009
       fp-base.  */
13010
0
    dist = best_fag->relas[0] - begin_rel;
13011
0
    BFD_ASSERT (dist > 0 && dist < 0xffffff);
13012
    /* Use high 16 bits of addend to record the _FP_BASE_ matched
13013
       relocation.  And get the base value when relocating.  */
13014
0
    begin_rel->r_addend &= (0x1 << 16) - 1;
13015
0
    begin_rel->r_addend |= dist << 16;
13016
13017
0
    nds32_fag_free_list (&fag_head);
13018
0
    begin_rel = NULL;
13019
0
  }
13020
13021
0
      if (begin_rel == NULL || ifc_inside)
13022
  /* Skip if we are not in the region of fp-as-gp.  */
13023
0
  continue;
13024
13025
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
13026
0
    || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
13027
0
  {
13028
0
    bfd_vma addr;
13029
0
    uint32_t insn;
13030
13031
    /* A gp-relative access is found.  Insert it to the fag-list.  */
13032
13033
    /* Rt is necessary an RT3, so it can be converted to lwi37.fp.  */
13034
0
    insn = bfd_getb32 (contents + irel->r_offset);
13035
0
    if (!N32_IS_RT3 (insn))
13036
0
      continue;
13037
13038
0
    addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
13039
0
    nds32_fag_insert (&fag_head, addr, irel);
13040
0
  }
13041
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
13042
0
  {
13043
0
    begin_rel = NULL;
13044
0
  }
13045
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
13046
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
13047
0
  {
13048
    /* Suppress fp as gp when encounter ifc.  */
13049
0
    ifc_inside = true;
13050
0
  }
13051
0
    }
13052
13053
0
  return true;
13054
0
}
13055
13056
/* Remove unused `la $fp, _FD_BASE_' instruction.  */
13057
13058
static bool
13059
nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
13060
        Elf_Internal_Rela *internal_relocs,
13061
        Elf_Internal_Rela *irelend)
13062
0
{
13063
0
  Elf_Internal_Rela *irel;
13064
0
  Elf_Internal_Shdr *symtab_hdr;
13065
0
  bfd_byte *contents = NULL;
13066
0
  nds32_elf_blank_t *relax_blank_list = NULL;
13067
0
  bool result = true;
13068
0
  bool unused_region = false;
13069
13070
  /*
13071
     NOTE: Disable fp-as-gp if we encounter ifcall relocations:
13072
       R_NDS32_17IFC_PCREL_RELA
13073
       R_NDS32_10IFCU_PCREL_RELA.  */
13074
13075
0
  symtab_hdr = &elf_symtab_hdr (abfd);
13076
0
  nds32_get_section_contents (abfd, sec, &contents, true);
13077
13078
0
  for (irel = internal_relocs; irel < irelend; irel++)
13079
0
    {
13080
      /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13081
   we marked to in previous pass.
13082
   DO NOT scan relocations again, since we've alreadly decided it
13083
   and set the flag.  */
13084
0
      const char *syname;
13085
0
      int syndx;
13086
0
      uint32_t insn;
13087
13088
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13089
0
    && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13090
0
  unused_region = true;
13091
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13092
0
         && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13093
0
  unused_region = false;
13094
13095
      /* We're not in the region.  */
13096
0
      if (!unused_region)
13097
0
  continue;
13098
13099
      /* _FP_BASE_ must be a GLOBAL symbol.  */
13100
0
      syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13101
0
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13102
0
  continue;
13103
13104
      /* The symbol name must be _FP_BASE_.  */
13105
0
      syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13106
0
      if (strcmp (syname, FP_BASE_NAME) != 0)
13107
0
  continue;
13108
13109
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13110
0
  {
13111
    /* addi.gp  $fp, -256  */
13112
0
    insn = bfd_getb32 (contents + irel->r_offset);
13113
0
    if (insn != INSN_ADDIGP_TO_FP)
13114
0
      continue;
13115
0
  }
13116
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13117
0
  {
13118
    /* addi  $fp, $gp, -256  */
13119
0
    insn = bfd_getb32 (contents + irel->r_offset);
13120
0
    if (insn != INSN_ADDI_GP_TO_FP)
13121
0
      continue;
13122
0
  }
13123
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13124
0
  {
13125
    /* movi  $fp, FP_BASE  */
13126
0
    insn = bfd_getb32 (contents + irel->r_offset);
13127
0
    if (insn != INSN_MOVI_TO_FP)
13128
0
      continue;
13129
0
  }
13130
0
      else
13131
0
  continue;
13132
13133
      /* We got here because a FP_BASE instruction is found.  */
13134
0
      if (!insert_nds32_elf_blank_recalc_total
13135
0
    (&relax_blank_list, irel->r_offset, 4))
13136
0
  goto error_return;
13137
0
    }
13138
13139
0
 finish:
13140
0
  if (relax_blank_list)
13141
0
    {
13142
0
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13143
0
      relax_blank_list = NULL;
13144
0
    }
13145
0
  return result;
13146
13147
0
 error_return:
13148
0
  result = false;
13149
0
  goto finish;
13150
0
}
13151
13152
/* This is a version of bfd_generic_get_relocated_section_contents.
13153
   We need this variety because relaxation will modify the dwarf
13154
   infomation.  When there is undefined symbol reference error mesage,
13155
   linker need to dump line number where the symbol be used.  However
13156
   the address is be relaxed, it can not get the original dwarf contents.
13157
   The variety only modify function call for reading in the section.  */
13158
13159
static bfd_byte *
13160
nds32_elf_get_relocated_section_contents
13161
  (bfd *abfd,
13162
   struct bfd_link_info *link_info,
13163
   const struct bfd_link_order *link_order,
13164
   bfd_byte *data,
13165
   bool relocatable,
13166
   asymbol **symbols)
13167
57
{
13168
57
  bfd *input_bfd = link_order->u.indirect.section->owner;
13169
57
  asection *input_section = link_order->u.indirect.section;
13170
57
  long reloc_size;
13171
57
  arelent **reloc_vector;
13172
57
  long reloc_count;
13173
13174
57
  reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13175
57
  if (reloc_size < 0)
13176
2
    return NULL;
13177
13178
  /* Read in the section.  */
13179
55
  bfd_byte *orig_data = data;
13180
55
  if (!nds32_get_section_contents (input_bfd, input_section, &data, false))
13181
3
    return NULL;
13182
13183
52
  if (reloc_size == 0)
13184
0
    return data;
13185
13186
52
  reloc_vector = (arelent **) bfd_malloc (reloc_size);
13187
52
  if (reloc_vector == NULL)
13188
0
    goto error_return;
13189
13190
52
  reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13191
52
          reloc_vector, symbols);
13192
52
  if (reloc_count < 0)
13193
14
    goto error_return;
13194
13195
38
  if (reloc_count > 0)
13196
29
    {
13197
29
      arelent **parent;
13198
246
      for (parent = reloc_vector; *parent != NULL; parent++)
13199
241
  {
13200
241
    char *error_message = NULL;
13201
241
    asymbol *symbol;
13202
241
    bfd_reloc_status_type r;
13203
13204
241
    symbol = *(*parent)->sym_ptr_ptr;
13205
241
    if (symbol->section && discarded_section (symbol->section))
13206
0
      {
13207
0
        bfd_vma off;
13208
0
        static reloc_howto_type none_howto
13209
0
    = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13210
0
       "unused", false, 0, 0, false);
13211
13212
0
        off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13213
0
                input_section);
13214
0
        _bfd_clear_contents ((*parent)->howto, input_bfd,
13215
0
           input_section, data, off);
13216
0
        (*parent)->sym_ptr_ptr = &bfd_abs_section_ptr->symbol;
13217
0
        (*parent)->addend = 0;
13218
0
        (*parent)->howto = &none_howto;
13219
0
        r = bfd_reloc_ok;
13220
0
      }
13221
241
    else
13222
241
      r = bfd_perform_relocation (input_bfd, *parent, data,
13223
241
          input_section,
13224
241
          relocatable ? abfd : NULL,
13225
241
          &error_message);
13226
13227
241
    if (relocatable)
13228
0
      {
13229
0
        asection *os = input_section->output_section;
13230
13231
        /* A partial link, so keep the relocs.  */
13232
0
        os->orelocation[os->reloc_count] = *parent;
13233
0
        os->reloc_count++;
13234
0
      }
13235
13236
241
    if (r != bfd_reloc_ok)
13237
37
      {
13238
37
        _bfd_link_reloc_status_error (abfd, link_info, input_section,
13239
37
              *parent, error_message, r);
13240
37
        if (r == bfd_reloc_outofrange || r == bfd_reloc_notsupported)
13241
24
    goto error_return;
13242
37
      }
13243
241
  }
13244
29
    }
13245
13246
14
  free (reloc_vector);
13247
14
  return data;
13248
13249
38
 error_return:
13250
38
  free (reloc_vector);
13251
38
  if (orig_data == NULL)
13252
22
    free (data);
13253
38
  return NULL;
13254
38
}
13255
13256
/* Check target symbol.  */
13257
13258
static bool
13259
nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13260
1.52k
{
13261
1.52k
  if (!sym || !sym->name || sym->name[0] != '$')
13262
1.48k
    return false;
13263
38
  return true;
13264
1.52k
}
13265
13266
/* nds32 find maybe function sym.  Ignore target special symbol
13267
   first, and then go the general function.  */
13268
13269
static bfd_size_type
13270
nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13271
            bfd_vma *code_off)
13272
1.11k
{
13273
1.11k
  if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13274
28
    return 0;
13275
13276
1.08k
  return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13277
1.11k
}
13278

13279
13280
/* Do TLS model conversion.  */
13281
13282
typedef struct relax_group_list_t
13283
{
13284
  Elf_Internal_Rela *relo;
13285
  struct relax_group_list_t *next;
13286
  struct relax_group_list_t *next_sibling;
13287
  int id;
13288
} relax_group_list_t;
13289
13290
int
13291
list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13292
13293
int
13294
list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13295
13296
void
13297
dump_chain (relax_group_list_t *pHead);
13298
13299
int
13300
list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13301
0
{
13302
0
  relax_group_list_t *pNext = pHead;
13303
13304
  /* Find place.  */
13305
0
  while (pNext->next)
13306
0
    {
13307
0
      if (pNext->next->id > (int) pElem->r_addend)
13308
0
  break;
13309
13310
0
      pNext = pNext->next;
13311
0
    }
13312
13313
  /* Insert node.  */
13314
0
  relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13315
0
  if (!pNew)
13316
0
    return false;
13317
13318
0
  relax_group_list_t *tmp = pNext->next;
13319
0
  pNext->next = pNew;
13320
13321
0
  pNew->id = pElem->r_addend;
13322
0
  pNew->relo = pElem;
13323
0
  pNew->next = tmp;
13324
0
  pNew->next_sibling = NULL;
13325
13326
0
  return true;
13327
0
}
13328
13329
int
13330
list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13331
0
{
13332
0
  relax_group_list_t *pNext = pNode;
13333
13334
  /* Find place.  */
13335
0
  while (pNext->next_sibling)
13336
0
    {
13337
0
      pNext = pNext->next_sibling;
13338
0
    }
13339
13340
  /* Insert node.  */
13341
0
  relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13342
0
  if (!pNew)
13343
0
    return false;
13344
13345
0
  relax_group_list_t *tmp = pNext->next_sibling;
13346
0
  pNext->next_sibling = pNew;
13347
13348
0
  pNew->id = -1;
13349
0
  pNew->relo = pElem;
13350
0
  pNew->next = NULL;
13351
0
  pNew->next_sibling = tmp;
13352
13353
0
  return true;
13354
0
}
13355
13356
void
13357
dump_chain (relax_group_list_t *pHead)
13358
0
{
13359
0
  relax_group_list_t *pNext = pHead->next;
13360
0
  while (pNext)
13361
0
    {
13362
0
      printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13363
0
      relax_group_list_t *pNextSib = pNext->next_sibling;
13364
0
      while (pNextSib)
13365
0
  {
13366
0
    printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13367
0
    pNextSib = pNextSib->next_sibling;
13368
0
  }
13369
0
      pNext = pNext->next;
13370
0
      printf("\n");
13371
0
    }
13372
0
}
13373
13374
/* Check R_NDS32_RELAX_GROUP of each section.
13375
   There might be multiple sections in one object file.  */
13376
13377
int
13378
elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13379
0
{
13380
0
  elf32_nds32_relax_group_t *relax_group_ptr =
13381
0
    elf32_nds32_relax_group_ptr (abfd);
13382
13383
0
  int min_id = relax_group_ptr->min_id;
13384
0
  int max_id = relax_group_ptr->max_id;
13385
13386
0
  Elf_Internal_Rela *rel;
13387
0
  Elf_Internal_Rela *relend;
13388
0
  Elf_Internal_Rela *relocs;
13389
0
  enum elf_nds32_reloc_type rtype;
13390
13391
0
  do
13392
0
    {
13393
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13394
0
      relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL, true);
13395
0
      if (relocs == NULL)
13396
0
  break;
13397
13398
      /* Check R_NDS32_RELAX_GROUP.  */
13399
0
      relend = relocs + asec->reloc_count;
13400
0
      for (rel = relocs; rel < relend; rel++)
13401
0
  {
13402
0
    int id;
13403
0
    rtype = ELF32_R_TYPE (rel->r_info);
13404
0
    if (rtype != R_NDS32_RELAX_GROUP)
13405
0
      continue;
13406
13407
0
    id = rel->r_addend;
13408
0
    if (id < min_id)
13409
0
      min_id = id;
13410
0
    else if (id > max_id)
13411
0
      max_id = id;
13412
0
  }
13413
0
    }
13414
0
  while (false);
13415
13416
0
  if (elf_section_data (asec)->relocs != relocs)
13417
0
    free (relocs);
13418
13419
0
  if ((min_id != relax_group_ptr->min_id)
13420
0
      || (max_id != relax_group_ptr->max_id))
13421
0
    {
13422
0
      relax_group_ptr->count = max_id - min_id + 1;
13423
0
      BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13424
0
      relax_group_ptr->min_id = min_id;
13425
0
      BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13426
0
      relax_group_ptr->max_id = max_id;
13427
0
    }
13428
13429
0
  return relax_group_ptr->count;
13430
0
}
13431
13432
/* Reorder RELAX_GROUP ID when command line option '-r' is applied.  */
13433
static struct section_id_list_t *relax_group_section_id_list = NULL;
13434
13435
struct section_id_list_t *
13436
elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13437
0
{
13438
0
  struct section_id_list_t *result = NULL;
13439
0
  struct section_id_list_t *lst = *lst_ptr;
13440
13441
0
  if (NULL == lst)
13442
0
    {
13443
0
      result = (struct section_id_list_t *) calloc
13444
0
  (1, sizeof (struct section_id_list_t));
13445
0
      BFD_ASSERT (result); /* Feed me.  */
13446
0
      result->id = id;
13447
0
      *lst_ptr = result;
13448
0
    }
13449
0
  else
13450
0
    {
13451
0
      struct section_id_list_t *cur = lst;
13452
0
      struct section_id_list_t *prv = NULL;
13453
0
      struct section_id_list_t *sec = NULL;
13454
13455
0
      while (cur)
13456
0
  {
13457
0
    if (cur->id < id)
13458
0
      {
13459
0
        prv = cur;
13460
0
        cur = cur->next;
13461
0
        continue;
13462
0
      }
13463
13464
0
    if (cur->id > id)
13465
0
      {
13466
0
        cur = NULL; /* To insert after prv.  */
13467
0
        sec = cur;  /* In case prv == NULL.  */
13468
0
      }
13469
13470
0
    break;
13471
0
  }
13472
13473
0
      if (NULL == cur)
13474
0
  {
13475
    /* Insert after prv.  */
13476
0
    result = (struct section_id_list_t *) calloc
13477
0
      (1, sizeof (struct section_id_list_t));
13478
0
    BFD_ASSERT (result); /* Feed me.  */
13479
0
    result->id = id;
13480
0
    if (NULL != prv)
13481
0
      {
13482
0
        result->next = prv->next;
13483
0
        prv->next = result;
13484
0
      }
13485
0
    else
13486
0
      {
13487
0
        *lst_ptr = result;
13488
0
        result->next = sec;
13489
0
      }
13490
0
  }
13491
0
    }
13492
13493
0
  return result;
13494
0
}
13495
13496
int
13497
elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13498
0
{
13499
0
  static int next_relax_group_bias = 0;
13500
13501
0
  elf32_nds32_relax_group_t *relax_group_ptr =
13502
0
    elf32_nds32_relax_group_ptr (abfd);
13503
13504
0
  bool result = true;
13505
0
  Elf_Internal_Rela *rel;
13506
0
  Elf_Internal_Rela *relend;
13507
0
  Elf_Internal_Rela *relocs = NULL;
13508
0
  enum elf_nds32_reloc_type rtype;
13509
0
  struct section_id_list_t *node = NULL;
13510
13511
0
  do
13512
0
    {
13513
0
      if (0 == relax_group_ptr->count)
13514
0
  break;
13515
13516
      /* Check if this section has been handled.  */
13517
0
      node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13518
0
      if (NULL == node)
13519
0
  break; /* Hit, the section id has handled.  */
13520
13521
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13522
0
      relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL, true);
13523
0
      if (relocs == NULL)
13524
0
  {
13525
0
    BFD_ASSERT (0); /* feed me */
13526
0
    break;
13527
0
  }
13528
13529
      /* Allocate group id bias for this bfd!  */
13530
0
      if (0 == relax_group_ptr->init)
13531
0
  {
13532
0
    relax_group_ptr->bias = next_relax_group_bias;
13533
0
    next_relax_group_bias += relax_group_ptr->count;
13534
0
    relax_group_ptr->init = 1;
13535
0
  }
13536
13537
      /* Reorder relax group groups.  */
13538
0
      relend = relocs + asec->reloc_count;
13539
0
      for (rel = relocs; rel < relend; rel++)
13540
0
  {
13541
0
    rtype = ELF32_R_TYPE(rel->r_info);
13542
0
    if (rtype != R_NDS32_RELAX_GROUP)
13543
0
      continue;
13544
13545
    /* Change it.  */
13546
0
    rel->r_addend += relax_group_ptr->bias;
13547
0
  }
13548
0
    }
13549
0
  while (false);
13550
13551
0
  if (elf_section_data (asec)->relocs != relocs)
13552
0
    free (relocs);
13553
13554
0
  return result;
13555
0
}
13556
13557
int
13558
nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13559
         struct bfd_link_info *lnkinfo)
13560
0
{
13561
0
  bool result = true;
13562
0
  Elf_Internal_Rela *irel;
13563
0
  Elf_Internal_Rela *irelend;
13564
0
  Elf_Internal_Rela *internal_relocs;
13565
0
  unsigned long r_symndx;
13566
0
  enum elf_nds32_reloc_type r_type;
13567
13568
0
  Elf_Internal_Sym *local_syms = NULL;
13569
0
  bfd_byte *contents = NULL;
13570
13571
0
  relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13572
13573
0
  Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (inbfd);
13574
0
  struct elf_link_hash_entry **sym_hashes;
13575
0
  sym_hashes = elf_sym_hashes (inbfd);
13576
13577
  /* Reorder RELAX_GROUP when command line option '-r' is applied.  */
13578
0
  if (bfd_link_relocatable (lnkinfo))
13579
0
    {
13580
0
      elf32_nds32_unify_relax_group (inbfd, insec);
13581
0
      return result;
13582
0
    }
13583
13584
  /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13585
0
  internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL, true);
13586
0
  if (internal_relocs == NULL)
13587
0
    goto error_return;
13588
13589
0
  irelend = internal_relocs + insec->reloc_count;
13590
0
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
13591
0
         irelend, R_NDS32_RELAX_ENTRY);
13592
0
  if (irel == irelend)
13593
0
    goto finish;
13594
13595
  /* Chain/remove groups.  */
13596
0
  for (irel = internal_relocs; irel < irelend; irel++)
13597
0
    {
13598
0
      r_symndx = ELF32_R_SYM (irel->r_info);
13599
0
      r_type = ELF32_R_TYPE (irel->r_info);
13600
0
      if (r_type != R_NDS32_RELAX_GROUP)
13601
0
  continue;
13602
13603
      /* Remove it.  */
13604
0
      irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13605
      /* Chain it now.  */
13606
0
      if (!list_insert (&chain, irel))
13607
0
  goto error_return;
13608
0
    }
13609
13610
  /* Collect group relocations.  */
13611
  /* Presume relocations are sorted.  */
13612
0
  relax_group_list_t *pNext = chain.next;
13613
0
  while (pNext)
13614
0
    {
13615
0
      for (irel = internal_relocs; irel < irelend; irel++)
13616
0
  {
13617
0
    if (irel->r_offset == pNext->relo->r_offset)
13618
0
      {
13619
        /* Ignore Non-TLS relocation types.  */
13620
0
        r_type = ELF32_R_TYPE (irel->r_info);
13621
0
        if ((R_NDS32_TLS_LE_HI20 > r_type)
13622
0
      || (R_NDS32_RELAX_ENTRY == r_type))
13623
0
    continue;
13624
13625
0
        if (!list_insert_sibling (pNext, irel))
13626
0
    goto error_return;
13627
0
      }
13628
0
    else if (irel->r_offset > pNext->relo->r_offset)
13629
0
      {
13630
0
        pNext = pNext->next;
13631
0
        if (!pNext)
13632
0
    break;
13633
13634
0
        bfd_vma current_offset = pNext->relo->r_offset;
13635
0
        if (irel->r_offset > current_offset)
13636
0
    irel = internal_relocs; /* restart from head */
13637
0
        else
13638
0
    --irel; /* Check current irel again.  */
13639
0
        continue;
13640
0
      }
13641
0
    else
13642
0
      {
13643
        /* This shouldn't be reached.  */
13644
0
      }
13645
0
  }
13646
0
      if (pNext)
13647
0
  pNext = pNext->next;
13648
0
    }
13649
13650
#ifdef DUBUG_VERBOSE
13651
  dump_chain(&chain);
13652
#endif
13653
13654
  /* Get symbol table and section content.  */
13655
0
  if (incontents)
13656
0
    contents = incontents;
13657
0
  else if (!nds32_get_section_contents (inbfd, insec, &contents, true)
13658
0
     || !nds32_get_local_syms (inbfd, insec, &local_syms))
13659
0
    goto error_return;
13660
13661
0
  char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13662
13663
  /* Convert TLS model each group if necessary.  */
13664
0
  pNext = chain.next;
13665
13666
0
  int cur_grp_id = -1;
13667
0
  int sethi_rt = -1;
13668
0
  int add_rt = -1;
13669
0
  enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13670
13671
0
  tls_type = org_tls_type = eff_tls_type = 0;
13672
13673
0
  while (pNext)
13674
0
    {
13675
0
      relax_group_list_t *pNextSig = pNext->next_sibling;
13676
0
      while (pNextSig)
13677
0
  {
13678
0
    struct elf_link_hash_entry *h = NULL;
13679
13680
0
    irel = pNextSig->relo;
13681
0
    r_symndx = ELF32_R_SYM(irel->r_info);
13682
0
    r_type = ELF32_R_TYPE(irel->r_info);
13683
13684
0
    if (pNext->id != cur_grp_id)
13685
0
      {
13686
0
        cur_grp_id = pNext->id;
13687
0
        org_tls_type = get_tls_type (r_type, NULL);
13688
0
        if (r_symndx >= symtab_hdr->sh_info)
13689
0
    {
13690
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13691
0
      while (h->root.type == bfd_link_hash_indirect
13692
0
       || h->root.type == bfd_link_hash_warning)
13693
0
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
13694
0
      tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13695
0
    }
13696
0
        else
13697
0
    {
13698
0
      tls_type = local_got_tls_type
13699
0
        ? local_got_tls_type[r_symndx]
13700
0
        : GOT_NORMAL;
13701
0
    }
13702
13703
0
        eff_tls_type = 1 << (fls (tls_type) - 1);
13704
0
        sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13705
0
      }
13706
13707
0
    if (eff_tls_type != org_tls_type)
13708
0
      {
13709
0
        switch (org_tls_type)
13710
0
    {
13711
      /* DESC to IEGP/IE/LE.  */
13712
0
    case GOT_TLS_DESC:
13713
0
      switch (eff_tls_type)
13714
0
        {
13715
0
        case GOT_TLS_IE:
13716
0
          switch (r_type)
13717
0
      {
13718
0
      case R_NDS32_TLS_DESC_HI20:
13719
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13720
0
                  R_NDS32_TLS_IE_HI20);
13721
0
        break;
13722
0
      case R_NDS32_TLS_DESC_LO12:
13723
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13724
0
                  R_NDS32_TLS_IE_LO12);
13725
0
        break;
13726
0
      case R_NDS32_TLS_DESC_ADD:
13727
0
        {
13728
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13729
0
          add_rt = N32_RT5 (insn);
13730
0
          insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13731
0
          bfd_putb32 (insn, contents + irel->r_offset);
13732
13733
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13734
0
        }
13735
0
        break;
13736
0
      case R_NDS32_TLS_DESC_FUNC:
13737
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13738
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13739
0
                  R_NDS32_RELAX_REMOVE);
13740
0
        break;
13741
0
      case R_NDS32_TLS_DESC_CALL:
13742
0
        {
13743
0
          uint32_t insn = N32_ALU1(ADD, N32_REG_R0, add_rt,
13744
0
                 N32_REG_TP);
13745
0
          bfd_putb32 (insn, contents + irel->r_offset);
13746
13747
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13748
0
        }
13749
0
        break;
13750
0
      case R_NDS32_LOADSTORE:
13751
0
      case R_NDS32_PTR:
13752
0
      case R_NDS32_PTR_RESOLVED:
13753
0
      case R_NDS32_NONE:
13754
0
      case R_NDS32_LABEL:
13755
0
        break;
13756
0
      default:
13757
0
        BFD_ASSERT(0);
13758
0
        break;
13759
0
      }
13760
0
          break;
13761
0
        case GOT_TLS_IEGP:
13762
0
          switch (r_type)
13763
0
      {
13764
0
      case R_NDS32_TLS_DESC_HI20:
13765
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13766
0
                  R_NDS32_TLS_IEGP_HI20);
13767
0
        break;
13768
0
      case R_NDS32_TLS_DESC_LO12:
13769
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13770
0
                  R_NDS32_TLS_IEGP_LO12);
13771
0
        break;
13772
0
      case R_NDS32_TLS_DESC_ADD:
13773
0
        {
13774
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13775
0
          add_rt = N32_RT5 (insn);
13776
0
          insn = N32_MEM(LW, add_rt, sethi_rt,
13777
0
             N32_REG_GP, 0);
13778
0
          bfd_putb32 (insn, contents + irel->r_offset);
13779
13780
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13781
0
        }
13782
0
        break;
13783
0
      case R_NDS32_TLS_DESC_FUNC:
13784
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13785
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13786
0
                  R_NDS32_RELAX_REMOVE);
13787
0
        break;
13788
0
      case R_NDS32_TLS_DESC_CALL:
13789
0
        {
13790
0
          uint32_t insn = N32_ALU1(ADD, N32_REG_R0, add_rt,
13791
0
                 N32_REG_TP);
13792
0
          bfd_putb32 (insn, contents + irel->r_offset);
13793
13794
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13795
0
        }
13796
0
        break;
13797
0
      case R_NDS32_LOADSTORE:
13798
0
      case R_NDS32_PTR:
13799
0
      case R_NDS32_PTR_RESOLVED:
13800
0
      case R_NDS32_NONE:
13801
0
      case R_NDS32_LABEL:
13802
0
        break;
13803
0
      default:
13804
0
        BFD_ASSERT(0);
13805
0
        break;
13806
0
      }
13807
0
          break;
13808
0
        case GOT_TLS_LE:
13809
0
          switch (r_type)
13810
0
      {
13811
0
      case R_NDS32_TLS_DESC_HI20:
13812
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13813
0
        break;
13814
0
      case R_NDS32_TLS_DESC_LO12:
13815
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13816
0
        break;
13817
0
      case R_NDS32_TLS_DESC_ADD:
13818
0
        {
13819
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13820
13821
0
          add_rt = N32_RT5 (insn);
13822
0
          insn = N32_ALU1 (ADD, N32_REG_R0, sethi_rt,
13823
0
               N32_REG_TP);
13824
0
          bfd_putb32 (insn, contents + irel->r_offset);
13825
13826
0
          irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13827
0
        }
13828
0
        break;
13829
0
      case R_NDS32_TLS_DESC_FUNC:
13830
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13831
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13832
0
        break;
13833
0
      case R_NDS32_TLS_DESC_CALL:
13834
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13835
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13836
0
        break;
13837
0
      case R_NDS32_LOADSTORE:
13838
0
      case R_NDS32_PTR:
13839
0
      case R_NDS32_PTR_RESOLVED:
13840
0
      case R_NDS32_NONE:
13841
0
      case R_NDS32_LABEL:
13842
0
        break;
13843
0
      default:
13844
0
        BFD_ASSERT(0);
13845
0
        break;
13846
0
      }
13847
0
          break;
13848
0
        default:
13849
0
          break;
13850
0
        }
13851
0
      break;
13852
      /* IEGP to IE/LE.  */
13853
0
    case GOT_TLS_IEGP:
13854
0
      switch (eff_tls_type)
13855
0
        {
13856
0
        case GOT_TLS_IE:
13857
0
          switch (r_type)
13858
0
      {
13859
0
      case R_NDS32_TLS_IEGP_HI20:
13860
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13861
0
                  R_NDS32_TLS_IE_HI20);
13862
0
        break;
13863
0
      case R_NDS32_TLS_IEGP_LO12:
13864
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13865
0
                  R_NDS32_TLS_IE_LO12);
13866
0
        break;
13867
0
      case R_NDS32_PTR_RESOLVED:
13868
0
        {
13869
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13870
13871
0
          add_rt = N32_RT5 (insn);
13872
0
          insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13873
0
          bfd_putb32 (insn, contents + irel->r_offset);
13874
0
        }
13875
0
        break;
13876
0
      case R_NDS32_TLS_IEGP_LW:
13877
0
        break;
13878
0
      case R_NDS32_LOADSTORE:
13879
0
      case R_NDS32_PTR:
13880
0
      case R_NDS32_NONE:
13881
0
      case R_NDS32_LABEL:
13882
0
        break;
13883
0
      default:
13884
0
        BFD_ASSERT(0);
13885
0
        break;
13886
0
      }
13887
0
          break;
13888
0
        case GOT_TLS_LE:
13889
0
          switch (r_type)
13890
0
      {
13891
0
      case R_NDS32_TLS_IEGP_HI20:
13892
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13893
0
        break;
13894
0
      case R_NDS32_TLS_IEGP_LO12:
13895
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13896
0
        break;
13897
0
      case R_NDS32_TLS_IEGP_LW:
13898
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13899
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13900
0
        break;
13901
0
      case R_NDS32_LOADSTORE:
13902
0
      case R_NDS32_PTR:
13903
0
      case R_NDS32_NONE:
13904
0
      case R_NDS32_LABEL:
13905
0
      case R_NDS32_PTR_RESOLVED:
13906
0
        break;
13907
0
      default:
13908
0
        BFD_ASSERT(0);
13909
0
        break;
13910
0
      }
13911
0
          break;
13912
0
        default:
13913
0
          break;
13914
0
        }
13915
0
      break;
13916
      /* IE to LE. */
13917
0
    case GOT_TLS_IE:
13918
0
      switch (eff_tls_type)
13919
0
        {
13920
0
        case GOT_TLS_LE:
13921
0
          switch (r_type)
13922
0
      {
13923
0
      case R_NDS32_TLS_IE_HI20:
13924
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13925
0
        break;
13926
0
      case R_NDS32_TLS_IE_LO12S2:
13927
0
        {
13928
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13929
13930
0
          add_rt = N32_RT5 (insn);
13931
0
          insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13932
0
          bfd_putb32 (insn, contents + irel->r_offset);
13933
13934
0
          irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13935
0
        }
13936
0
        break;
13937
0
      case R_NDS32_LOADSTORE:
13938
0
      case R_NDS32_PTR:
13939
0
      case R_NDS32_NONE:
13940
0
      case R_NDS32_LABEL:
13941
0
        break;
13942
0
      default:
13943
0
        BFD_ASSERT(0);
13944
0
        break;
13945
0
      }
13946
0
          break;
13947
0
        default:
13948
0
          break;
13949
0
        }
13950
0
      break;
13951
0
    default:
13952
0
      break;
13953
0
    }
13954
0
      }
13955
0
    pNextSig = pNextSig->next_sibling;
13956
0
  }
13957
13958
0
#if 1
13959
0
      pNext = pNext->next;
13960
#else
13961
      while (pNext)
13962
  {
13963
    if (pNext->id != cur_grp_id)
13964
      break;
13965
    pNext = pNext->next;
13966
  }
13967
#endif
13968
0
    }
13969
13970
0
 finish:
13971
0
  if (incontents)
13972
0
    contents = NULL;
13973
13974
0
  if (elf_section_data (insec)->relocs != internal_relocs)
13975
0
    free (internal_relocs);
13976
13977
0
  if (elf_section_data (insec)->this_hdr.contents != contents)
13978
0
    free (contents);
13979
13980
0
  if (symtab_hdr->contents != (bfd_byte *) local_syms)
13981
0
    free (local_syms);
13982
13983
0
  if (chain.next)
13984
0
    {
13985
0
      pNext = chain.next;
13986
0
      relax_group_list_t *pDel;
13987
0
      while (pNext)
13988
0
  {
13989
0
    pDel = pNext;
13990
0
    pNext = pNext->next;
13991
0
    free (pDel);
13992
0
  }
13993
0
    }
13994
13995
0
  return result;
13996
13997
0
 error_return:
13998
  result = false;
13999
0
  goto finish;
14000
0
}
14001
14002
/* End TLS model conversion.  */
14003
14004
#define ELF_ARCH        bfd_arch_nds32
14005
#define ELF_MACHINE_CODE      EM_NDS32
14006
#define ELF_MAXPAGESIZE       0x1000
14007
#define ELF_TARGET_ID       NDS32_ELF_DATA
14008
14009
#define TARGET_BIG_SYM        nds32_elf32_be_vec
14010
#define TARGET_BIG_NAME       "elf32-nds32be"
14011
#define TARGET_LITTLE_SYM     nds32_elf32_le_vec
14012
#define TARGET_LITTLE_NAME      "elf32-nds32le"
14013
14014
#define elf_info_to_howto     nds32_info_to_howto
14015
#define elf_info_to_howto_rel     nds32_info_to_howto_rel
14016
14017
#define bfd_elf32_bfd_link_hash_table_create  nds32_elf_link_hash_table_create
14018
#define bfd_elf32_bfd_merge_private_bfd_data  nds32_elf_merge_private_bfd_data
14019
#define bfd_elf32_bfd_print_private_bfd_data  nds32_elf_print_private_bfd_data
14020
#define bfd_elf32_bfd_relax_section   nds32_elf_relax_section
14021
#define bfd_elf32_bfd_set_private_flags   nds32_elf_set_private_flags
14022
14023
#define bfd_elf32_mkobject      nds32_elf_mkobject
14024
#define bfd_elf32_new_section_hook    nds32_elf_new_section_hook
14025
#define bfd_elf32_bfd_free_cached_info    nds32_elf_free_cached_info
14026
#define elf_backend_action_discarded    nds32_elf_action_discarded
14027
#define elf_backend_add_symbol_hook   nds32_elf_add_symbol_hook
14028
#define elf_backend_check_relocs    nds32_elf_check_relocs
14029
#define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
14030
#define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
14031
#define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
14032
#define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
14033
#define elf_backend_late_size_sections    nds32_elf_late_size_sections
14034
#define elf_backend_relocate_section    nds32_elf_relocate_section
14035
#define elf_backend_gc_mark_hook    nds32_elf_gc_mark_hook
14036
#define elf_backend_grok_prstatus   nds32_elf_grok_prstatus
14037
#define elf_backend_grok_psinfo     nds32_elf_grok_psinfo
14038
#define elf_backend_reloc_type_class    nds32_elf_reloc_type_class
14039
#define elf_backend_copy_indirect_symbol  nds32_elf_copy_indirect_symbol
14040
#define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
14041
#define elf_backend_output_arch_syms    nds32_elf_output_arch_syms
14042
#define elf_backend_object_p      nds32_elf_object_p
14043
#define elf_backend_final_write_processing  nds32_elf_final_write_processing
14044
#define elf_backend_special_sections    nds32_elf_special_sections
14045
#define elf_backend_section_flags   nds32_elf_section_flags
14046
#define bfd_elf32_bfd_get_relocated_section_contents \
14047
        nds32_elf_get_relocated_section_contents
14048
#define bfd_elf32_bfd_is_target_special_symbol  nds32_elf_is_target_special_symbol
14049
#define elf_backend_maybe_function_sym    nds32_elf_maybe_function_sym
14050
14051
#define elf_backend_can_gc_sections   1
14052
#define elf_backend_can_refcount    1
14053
#define elf_backend_want_got_plt    1
14054
#define elf_backend_plt_readonly    1
14055
#define elf_backend_want_plt_sym    0
14056
#define elf_backend_got_header_size   12
14057
#define elf_backend_may_use_rel_p   1
14058
#define elf_backend_default_use_rela_p    1
14059
#define elf_backend_may_use_rela_p    1
14060
#define elf_backend_dtrel_excludes_plt    0
14061
14062
#include "elf32-target.h"
14063
14064
#undef ELF_MAXPAGESIZE
14065
#define ELF_MAXPAGESIZE       0x2000
14066
14067
#undef  TARGET_BIG_SYM
14068
#define TARGET_BIG_SYM        nds32_elf32_linux_be_vec
14069
#undef  TARGET_BIG_NAME
14070
#define TARGET_BIG_NAME       "elf32-nds32be-linux"
14071
#undef  TARGET_LITTLE_SYM
14072
#define TARGET_LITTLE_SYM     nds32_elf32_linux_le_vec
14073
#undef  TARGET_LITTLE_NAME
14074
#define TARGET_LITTLE_NAME      "elf32-nds32le-linux"
14075
#undef  elf32_bed
14076
#define elf32_bed       elf32_nds32_lin_bed
14077
14078
#include "elf32-target.h"