Coverage Report

Created: 2023-08-28 06:26

/src/binutils-gdb/bfd/elf32-nds32.c
Line
Count
Source (jump to first uncovered line)
1
/* NDS32-specific support for 32-bit ELF.
2
   Copyright (C) 2012-2023 Free Software Foundation, Inc.
3
   Contributed by Andes Technology Corporation.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
20
   02110-1301, USA.  */
21
22
23
#include "sysdep.h"
24
#include "bfd.h"
25
#include "bfdlink.h"
26
#include "libbfd.h"
27
#include "elf-bfd.h"
28
#include "libiberty.h"
29
#include "elf/nds32.h"
30
#include "opcode/nds32.h"
31
#include "elf32-nds32.h"
32
#include "opcode/cgen.h"
33
#include "../opcodes/nds32-opc.h"
34
35
/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
36
0
#define OCTETS_PER_BYTE(ABFD, SEC) 1
37
38
/* Relocation HOWTO functions.  */
39
static bfd_reloc_status_type nds32_elf_ignore_reloc
40
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
41
static bfd_reloc_status_type nds32_elf_9_pcrel_reloc
42
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
43
static bfd_reloc_status_type nds32_elf_hi20_reloc
44
  (bfd *, arelent *, asymbol *, void *,
45
   asection *, bfd *, char **);
46
static bfd_reloc_status_type nds32_elf_lo12_reloc
47
  (bfd *, arelent *, asymbol *, void *,
48
   asection *, bfd *, char **);
49
static bfd_reloc_status_type nds32_elf_generic_reloc
50
  (bfd *, arelent *, asymbol *, void *,
51
   asection *, bfd *, char **);
52
static bfd_reloc_status_type nds32_elf_sda15_reloc
53
  (bfd *, arelent *, asymbol *, void *,
54
   asection *, bfd *, char **);
55
56
/* Helper functions for HOWTO.  */
57
static bfd_reloc_status_type nds32_elf_do_9_pcrel_reloc
58
  (bfd *, reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
59
   asection *, bfd_vma, bfd_vma);
60
61
/* Nds32 helper functions.  */
62
static bfd_vma calculate_memory_address
63
  (bfd *, Elf_Internal_Rela *, Elf_Internal_Sym *, Elf_Internal_Shdr *);
64
static int nds32_get_section_contents (bfd *, asection *,
65
               bfd_byte **, bool);
66
static int nds32_get_local_syms (bfd *, asection *ATTRIBUTE_UNUSED,
67
         Elf_Internal_Sym **);
68
static bool  nds32_relax_fp_as_gp
69
  (struct bfd_link_info *link_info, bfd *abfd, asection *sec,
70
   Elf_Internal_Rela *internal_relocs, Elf_Internal_Rela *irelend,
71
   Elf_Internal_Sym *isymbuf);
72
static bool nds32_fag_remove_unused_fpbase
73
  (bfd *abfd, asection *sec, Elf_Internal_Rela *internal_relocs,
74
   Elf_Internal_Rela *irelend);
75
76
enum
77
{
78
  MACH_V1 = bfd_mach_n1h,
79
  MACH_V2 = bfd_mach_n1h_v2,
80
  MACH_V3 = bfd_mach_n1h_v3,
81
  MACH_V3M = bfd_mach_n1h_v3m
82
};
83
84
#define MIN(a, b) ((a) > (b) ? (b) : (a))
85
#define MAX(a, b) ((a) > (b) ? (a) : (b))
86
87
/* The name of the dynamic interpreter.  This is put in the .interp
88
   section.  */
89
0
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/ld.so.1"
90
91
0
#define NDS32_GUARD_SEC_P(flags) ((flags) & SEC_ALLOC \
92
0
          && (flags) & SEC_LOAD \
93
0
          && (flags) & SEC_READONLY)
94
95
/* The nop opcode we use.  */
96
0
#define NDS32_NOP32 0x40000009
97
0
#define NDS32_NOP16 0x9200
98
99
/* The size in bytes of an entry in the procedure linkage table.  */
100
0
#define PLT_ENTRY_SIZE 24
101
#define PLT_HEADER_SIZE 24
102
103
/* The first entry in a procedure linkage table are reserved,
104
   and the initial contents are unimportant (we zero them out).
105
   Subsequent entries look like this.  */
106
0
#define PLT0_ENTRY_WORD0  0x46f00000    /* sethi   r15, HI20(.got+4)    */
107
0
#define PLT0_ENTRY_WORD1  0x58f78000    /* ori     r15, r25, LO12(.got+4) */
108
0
#define PLT0_ENTRY_WORD2  0x05178000    /* lwi     r17, [r15+0]     */
109
0
#define PLT0_ENTRY_WORD3  0x04f78001    /* lwi     r15, [r15+4]     */
110
0
#define PLT0_ENTRY_WORD4  0x4a003c00    /* jr    r15        */
111
112
/* $ta is change to $r15 (from $r25).  */
113
0
#define PLT0_PIC_ENTRY_WORD0  0x46f00000  /* sethi   r15, HI20(got[1]@GOT)  */
114
0
#define PLT0_PIC_ENTRY_WORD1  0x58f78000  /* ori     r15, r15, LO12(got[1]@GOT) */
115
0
#define PLT0_PIC_ENTRY_WORD2  0x40f7f400  /* add     r15, gp, r15     */
116
0
#define PLT0_PIC_ENTRY_WORD3  0x05178000  /* lwi     r17, [r15+0]     */
117
0
#define PLT0_PIC_ENTRY_WORD4  0x04f78001  /* lwi     r15, [r15+4]     */
118
0
#define PLT0_PIC_ENTRY_WORD5  0x4a003c00  /* jr    r15        */
119
120
0
#define PLT_ENTRY_WORD0  0x46f00000    /* sethi   r15, HI20(&got[n+3])      */
121
0
#define PLT_ENTRY_WORD1  0x04f78000    /* lwi     r15, r15, LO12(&got[n+3]) */
122
0
#define PLT_ENTRY_WORD2  0x4a003c00    /* jr    r15           */
123
0
#define PLT_ENTRY_WORD3  0x45000000    /* movi    r16, sizeof(RELA) * n     */
124
0
#define PLT_ENTRY_WORD4  0x48000000    /* j    .plt0.         */
125
126
0
#define PLT_PIC_ENTRY_WORD0  0x46f00000    /* sethi  r15, HI20(got[n+3]@GOT)    */
127
0
#define PLT_PIC_ENTRY_WORD1  0x58f78000    /* ori    r15, r15,    LO12(got[n+3]@GOT) */
128
0
#define PLT_PIC_ENTRY_WORD2  0x38febc02    /* lw   r15, [gp+r15]        */
129
0
#define PLT_PIC_ENTRY_WORD3  0x4a003c00    /* jr   r15          */
130
0
#define PLT_PIC_ENTRY_WORD4  0x45000000    /* movi   r16, sizeof(RELA) * n      */
131
0
#define PLT_PIC_ENTRY_WORD5  0x48000000    /* j    .plt0          */
132
133
/* These are macros used to get the relocation accurate value.  */
134
0
#define ACCURATE_8BIT_S1  (0x100)
135
0
#define ACCURATE_U9BIT_S1 (0x400)
136
#define ACCURATE_12BIT_S1 (0x2000)
137
0
#define ACCURATE_14BIT_S1 (0x4000)
138
#define ACCURATE_19BIT    (0x40000)
139
140
/* These are macros used to get the relocation conservative value.  */
141
0
#define CONSERVATIVE_8BIT_S1  (0x100 - 4)
142
0
#define CONSERVATIVE_14BIT_S1 (0x4000 - 4)
143
0
#define CONSERVATIVE_16BIT_S1 (0x10000 - 4)
144
0
#define CONSERVATIVE_24BIT_S1 (0x1000000 - 4)
145
/* These must be more conservative because the address may be in
146
   different segment.  */
147
#define CONSERVATIVE_15BIT  (0x4000 - 0x1000)
148
#define CONSERVATIVE_15BIT_S1 (0x8000 - 0x1000)
149
#define CONSERVATIVE_15BIT_S2 (0x10000 - 0x1000)
150
#define CONSERVATIVE_19BIT  (0x40000 - 0x1000)
151
0
#define CONSERVATIVE_20BIT  (0x80000 - 0x1000)
152
153
/* Size of small data/bss sections, used to calculate SDA_BASE.  */
154
static long got_size = 0;
155
static int is_SDA_BASE_set = 0;
156
157
/* Convert ELF-VER in eflags to string for debugging purpose.  */
158
static const char *const nds32_elfver_strtab[] =
159
{
160
  "ELF-1.2",
161
  "ELF-1.3",
162
  "ELF-1.4",
163
};
164
165
/* The nds32 linker needs to keep track of the number of relocs that it
166
   decides to copy in check_relocs for each symbol.  This is so that
167
   it can discard PC relative relocs if it doesn't need them when
168
   linking with -Bsymbolic.  We store the information in a field
169
   extending the regular ELF linker hash table.  */
170
171
/* This structure keeps track of the number of PC relative relocs we
172
   have copied for a given symbol.  */
173
174
struct elf_nds32_pcrel_relocs_copied
175
{
176
  /* Next section.  */
177
  struct elf_nds32_pcrel_relocs_copied *next;
178
  /* A section in dynobj.  */
179
  asection *section;
180
  /* Number of relocs copied in this section.  */
181
  bfd_size_type count;
182
};
183
184
enum elf_nds32_tls_type
185
{
186
  GOT_UNKNOWN = (0),
187
  GOT_NORMAL = (1 << 0),
188
  GOT_TLS_LE = (1 << 1),
189
  GOT_TLS_IE = (1 << 2),
190
  GOT_TLS_IEGP = (1 << 3),
191
  GOT_TLS_LD = (1 << 4),
192
  GOT_TLS_GD = (1 << 5),
193
  GOT_TLS_DESC = (1 << 6),
194
};
195
196
/* Nds32 ELF linker hash entry.  */
197
198
struct elf_nds32_link_hash_entry
199
{
200
  struct elf_link_hash_entry root;
201
202
  /* For checking relocation type.  */
203
  enum elf_nds32_tls_type tls_type;
204
205
  int offset_to_gp;
206
};
207
208
/* Get the nds32 ELF linker hash table from a link_info structure.  */
209
210
0
#define FP_BASE_NAME "_FP_BASE_"
211
static int check_start_export_sym = 0;
212
213
/* The offset for executable tls relaxation.  */
214
0
#define TP_OFFSET 0x0
215
216
typedef struct
217
{
218
  int min_id;
219
  int max_id;
220
  int count;
221
  int bias;
222
  int init;
223
} elf32_nds32_relax_group_t;
224
225
struct elf_nds32_obj_tdata
226
{
227
  struct elf_obj_tdata root;
228
229
  /* tls_type for each local got entry.  */
230
  char *local_got_tls_type;
231
232
  /* GOTPLT entries for TLS descriptors.  */
233
  bfd_vma *local_tlsdesc_gotent;
234
235
  /* for R_NDS32_RELAX_GROUP handling.  */
236
  elf32_nds32_relax_group_t relax_group;
237
238
  unsigned int hdr_size;
239
  int* offset_to_gp;
240
};
241
242
#define elf_nds32_tdata(bfd) \
243
0
  ((struct elf_nds32_obj_tdata *) (bfd)->tdata.any)
244
245
#define elf32_nds32_local_got_tls_type(bfd) \
246
0
  (elf_nds32_tdata (bfd)->local_got_tls_type)
247
248
#define elf32_nds32_local_gp_offset(bfd) \
249
0
  (elf_nds32_tdata (bfd)->offset_to_gp)
250
251
0
#define elf32_nds32_hash_entry(ent) ((struct elf_nds32_link_hash_entry *)(ent))
252
253
#define elf32_nds32_relax_group_ptr(bfd) \
254
0
  &(elf_nds32_tdata (bfd)->relax_group)
255
256
static bool
257
nds32_elf_mkobject (bfd *abfd)
258
0
{
259
0
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_nds32_obj_tdata),
260
0
          NDS32_ELF_DATA);
261
0
}
262
263
/* Relocations used for relocation.  */
264
/* Define HOWTO2 (for relocation) and HOWTO3 (for relaxation) to
265
   initialize array nds32_elf_howto_table in any order. The benefit
266
   is that we can add any new relocations with any numbers and don't
267
   need to fill the gap by lots of EMPTY_HOWTO. */
268
#define HOWTO2(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
269
  [C] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
270
271
static reloc_howto_type nds32_elf_howto_table[] =
272
{
273
  /* This reloc does nothing.  */
274
  HOWTO2 (R_NDS32_NONE,   /* type  */
275
   0,     /* rightshift  */
276
   4,     /* size  */
277
   32,      /* bitsize  */
278
   false,     /* pc_relative  */
279
   0,     /* bitpos  */
280
   complain_overflow_bitfield,/* complain_on_overflow  */
281
   bfd_elf_generic_reloc, /* special_function  */
282
   "R_NDS32_NONE",  /* name  */
283
   false,     /* partial_inplace  */
284
   0,     /* src_mask  */
285
   0,     /* dst_mask  */
286
   false),    /* pcrel_offset  */
287
288
  /* A 16 bit absolute relocation.  */
289
  HOWTO2 (R_NDS32_16,   /* type  */
290
   0,     /* rightshift  */
291
   2,     /* size  */
292
   16,      /* bitsize  */
293
   false,     /* pc_relative  */
294
   0,     /* bitpos  */
295
   complain_overflow_bitfield,/* complain_on_overflow  */
296
   nds32_elf_generic_reloc,/* special_function  */
297
   "R_NDS32_16",    /* name  */
298
   false,     /* partial_inplace  */
299
   0xffff,    /* src_mask  */
300
   0xffff,    /* dst_mask  */
301
   false),    /* pcrel_offset  */
302
303
  /* A 32 bit absolute relocation.  */
304
  HOWTO2 (R_NDS32_32,   /* type  */
305
   0,     /* rightshift  */
306
   4,     /* size  */
307
   32,      /* bitsize  */
308
   false,     /* pc_relative  */
309
   0,     /* bitpos  */
310
   complain_overflow_bitfield,/* complain_on_overflow  */
311
   nds32_elf_generic_reloc,/* special_function  */
312
   "R_NDS32_32",    /* name  */
313
   false,     /* partial_inplace  */
314
   0xffffffff,    /* src_mask  */
315
   0xffffffff,    /* dst_mask  */
316
   false),    /* pcrel_offset  */
317
318
  /* A 20 bit address.  */
319
  HOWTO2 (R_NDS32_20,   /* type  */
320
   0,     /* rightshift  */
321
   4,     /* size  */
322
   20,      /* bitsize  */
323
   false,     /* pc_relative  */
324
   0,     /* bitpos  */
325
   complain_overflow_unsigned,/* complain_on_overflow  */
326
   nds32_elf_generic_reloc,/* special_function  */
327
   "R_NDS32_20",    /* name  */
328
   false,     /* partial_inplace  */
329
   0xfffff,   /* src_mask  */
330
   0xfffff,   /* dst_mask  */
331
   false),    /* pcrel_offset  */
332
333
  /* An PC Relative 9-bit relocation, shifted by 2.
334
     This reloc is complicated because relocations are relative to pc & -4.
335
     i.e. branches in the right insn slot use the address of the left insn
336
     slot for pc.  */
337
  /* It's not clear whether this should have partial_inplace set or not.
338
     Branch relaxing in the assembler can store the addend in the insn,
339
     and if bfd_install_relocation gets called the addend may get added
340
     again.  */
341
  HOWTO2 (R_NDS32_9_PCREL,  /* type  */
342
   1,     /* rightshift  */
343
   2,     /* size  */
344
   8,     /* bitsize  */
345
   true,      /* pc_relative  */
346
   0,     /* bitpos  */
347
   complain_overflow_signed,/* complain_on_overflow  */
348
   nds32_elf_9_pcrel_reloc,/* special_function  */
349
   "R_NDS32_9_PCREL", /* name  */
350
   false,     /* partial_inplace  */
351
   0xff,      /* src_mask  */
352
   0xff,      /* dst_mask  */
353
   true),     /* pcrel_offset  */
354
355
  /* A relative 15 bit relocation, right shifted by 1.  */
356
  HOWTO2 (R_NDS32_15_PCREL, /* type  */
357
   1,     /* rightshift  */
358
   4,     /* size  */
359
   14,      /* bitsize  */
360
   true,      /* pc_relative  */
361
   0,     /* bitpos  */
362
   complain_overflow_signed,/* complain_on_overflow  */
363
   bfd_elf_generic_reloc, /* special_function  */
364
   "R_NDS32_15_PCREL",  /* name  */
365
   false,     /* partial_inplace  */
366
   0x3fff,    /* src_mask  */
367
   0x3fff,    /* dst_mask  */
368
   true),     /* pcrel_offset  */
369
370
  /* A relative 17 bit relocation, right shifted by 1.  */
371
  HOWTO2 (R_NDS32_17_PCREL, /* type  */
372
   1,     /* rightshift  */
373
   4,     /* size  */
374
   16,      /* bitsize  */
375
   true,      /* pc_relative  */
376
   0,     /* bitpos  */
377
   complain_overflow_signed,/* complain_on_overflow  */
378
   bfd_elf_generic_reloc, /* special_function  */
379
   "R_NDS32_17_PCREL",  /* name  */
380
   false,     /* partial_inplace  */
381
   0xffff,    /* src_mask  */
382
   0xffff,    /* dst_mask  */
383
   true),     /* pcrel_offset  */
384
385
  /* A relative 25 bit relocation, right shifted by 1.  */
386
  /* It's not clear whether this should have partial_inplace set or not.
387
     Branch relaxing in the assembler can store the addend in the insn,
388
     and if bfd_install_relocation gets called the addend may get added
389
     again.  */
390
  HOWTO2 (R_NDS32_25_PCREL, /* type  */
391
   1,     /* rightshift  */
392
   4,     /* size  */
393
   24,      /* bitsize  */
394
   true,      /* pc_relative  */
395
   0,     /* bitpos  */
396
   complain_overflow_signed,/* complain_on_overflow  */
397
   bfd_elf_generic_reloc, /* special_function  */
398
   "R_NDS32_25_PCREL",  /* name  */
399
   false,     /* partial_inplace  */
400
   0xffffff,    /* src_mask  */
401
   0xffffff,    /* dst_mask  */
402
   true),     /* pcrel_offset  */
403
404
  /* High 20 bits of address when lower 12 is or'd in.  */
405
  HOWTO2 (R_NDS32_HI20,   /* type  */
406
   12,      /* rightshift  */
407
   4,     /* size  */
408
   20,      /* bitsize  */
409
   false,     /* pc_relative  */
410
   0,     /* bitpos  */
411
   complain_overflow_dont,/* complain_on_overflow  */
412
   nds32_elf_hi20_reloc,  /* special_function  */
413
   "R_NDS32_HI20",  /* name  */
414
   false,     /* partial_inplace  */
415
   0x000fffff,    /* src_mask  */
416
   0x000fffff,    /* dst_mask  */
417
   false),    /* pcrel_offset  */
418
419
  /* Lower 12 bits of address.  */
420
  HOWTO2 (R_NDS32_LO12S3, /* type  */
421
   3,     /* rightshift  */
422
   4,     /* size  */
423
   9,     /* bitsize  */
424
   false,     /* pc_relative  */
425
   0,     /* bitpos  */
426
   complain_overflow_dont,/* complain_on_overflow  */
427
   nds32_elf_lo12_reloc,  /* special_function  */
428
   "R_NDS32_LO12S3",  /* name  */
429
   false,     /* partial_inplace  */
430
   0x000001ff,    /* src_mask  */
431
   0x000001ff,    /* dst_mask  */
432
   false),    /* pcrel_offset  */
433
434
  /* Lower 12 bits of address.  */
435
  HOWTO2 (R_NDS32_LO12S2, /* type  */
436
   2,     /* rightshift  */
437
   4,     /* size  */
438
   10,      /* bitsize  */
439
   false,     /* pc_relative  */
440
   0,     /* bitpos  */
441
   complain_overflow_dont,/* complain_on_overflow  */
442
   nds32_elf_lo12_reloc,  /* special_function  */
443
   "R_NDS32_LO12S2",  /* name  */
444
   false,     /* partial_inplace  */
445
   0x000003ff,    /* src_mask  */
446
   0x000003ff,    /* dst_mask  */
447
   false),    /* pcrel_offset  */
448
449
  /* Lower 12 bits of address.  */
450
  HOWTO2 (R_NDS32_LO12S1, /* type  */
451
   1,     /* rightshift  */
452
   4,     /* size  */
453
   11,      /* bitsize  */
454
   false,     /* pc_relative  */
455
   0,     /* bitpos  */
456
   complain_overflow_dont,/* complain_on_overflow  */
457
   nds32_elf_lo12_reloc,  /* special_function  */
458
   "R_NDS32_LO12S1",  /* name  */
459
   false,     /* partial_inplace  */
460
   0x000007ff,    /* src_mask  */
461
   0x000007ff,    /* dst_mask  */
462
   false),    /* pcrel_offset  */
463
464
  /* Lower 12 bits of address.  */
465
  HOWTO2 (R_NDS32_LO12S0, /* type  */
466
   0,     /* rightshift  */
467
   4,     /* size  */
468
   12,      /* bitsize  */
469
   false,     /* pc_relative  */
470
   0,     /* bitpos  */
471
   complain_overflow_dont,/* complain_on_overflow  */
472
   nds32_elf_lo12_reloc,  /* special_function  */
473
   "R_NDS32_LO12S0",  /* name  */
474
   false,     /* partial_inplace  */
475
   0x00000fff,    /* src_mask  */
476
   0x00000fff,    /* dst_mask  */
477
   false),    /* pcrel_offset  */
478
479
  /* Small data area 15 bits offset.  */
480
  HOWTO2 (R_NDS32_SDA15S3,  /* type  */
481
   3,     /* rightshift  */
482
   4,     /* size  */
483
   15,      /* bitsize  */
484
   false,     /* pc_relative  */
485
   0,     /* bitpos  */
486
   complain_overflow_signed,/* complain_on_overflow  */
487
   nds32_elf_sda15_reloc, /* special_function  */
488
   "R_NDS32_SDA15S3", /* name  */
489
   false,     /* partial_inplace  */
490
   0x00007fff,    /* src_mask  */
491
   0x00007fff,    /* dst_mask  */
492
   false),    /* pcrel_offset  */
493
494
  /* Small data area 15 bits offset.  */
495
  HOWTO2 (R_NDS32_SDA15S2,  /* type  */
496
   2,     /* rightshift  */
497
   4,     /* size  */
498
   15,      /* bitsize  */
499
   false,     /* pc_relative  */
500
   0,     /* bitpos  */
501
   complain_overflow_signed,/* complain_on_overflow  */
502
   nds32_elf_sda15_reloc, /* special_function  */
503
   "R_NDS32_SDA15S2", /* name  */
504
   false,     /* partial_inplace  */
505
   0x00007fff,    /* src_mask  */
506
   0x00007fff,    /* dst_mask  */
507
   false),    /* pcrel_offset  */
508
509
  /* Small data area 15 bits offset.  */
510
  HOWTO2 (R_NDS32_SDA15S1,  /* type  */
511
   1,     /* rightshift  */
512
   4,     /* size  */
513
   15,      /* bitsize  */
514
   false,     /* pc_relative  */
515
   0,     /* bitpos  */
516
   complain_overflow_signed,/* complain_on_overflow  */
517
   nds32_elf_sda15_reloc, /* special_function  */
518
   "R_NDS32_SDA15S1", /* name  */
519
   false,     /* partial_inplace  */
520
   0x00007fff,    /* src_mask  */
521
   0x00007fff,    /* dst_mask  */
522
   false),    /* pcrel_offset  */
523
524
  /* Small data area 15 bits offset.  */
525
  HOWTO2 (R_NDS32_SDA15S0,  /* type  */
526
   0,     /* rightshift  */
527
   4,     /* size  */
528
   15,      /* bitsize  */
529
   false,     /* pc_relative  */
530
   0,     /* bitpos  */
531
   complain_overflow_signed,/* complain_on_overflow  */
532
   nds32_elf_sda15_reloc, /* special_function  */
533
   "R_NDS32_SDA15S0", /* name  */
534
   false,     /* partial_inplace  */
535
   0x00007fff,    /* src_mask  */
536
   0x00007fff,    /* dst_mask  */
537
   false),    /* pcrel_offset  */
538
539
  /* GNU extension to record C++ vtable hierarchy  */
540
  HOWTO2 (R_NDS32_GNU_VTINHERIT,/* type  */
541
   0,     /* rightshift  */
542
   4,     /* size  */
543
   0,     /* bitsize  */
544
   false,     /* pc_relative  */
545
   0,     /* bitpos  */
546
   complain_overflow_dont,/* complain_on_overflow  */
547
   NULL,      /* special_function  */
548
   "R_NDS32_GNU_VTINHERIT",/* name  */
549
   false,     /* partial_inplace  */
550
   0,     /* src_mask  */
551
   0,     /* dst_mask  */
552
   false),    /* pcrel_offset  */
553
554
  /* GNU extension to record C++ vtable member usage  */
555
  HOWTO2 (R_NDS32_GNU_VTENTRY,  /* type  */
556
   0,     /* rightshift  */
557
   4,     /* size  */
558
   0,     /* bitsize  */
559
   false,     /* pc_relative  */
560
   0,     /* bitpos  */
561
   complain_overflow_dont,/* complain_on_overflow  */
562
   _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
563
   "R_NDS32_GNU_VTENTRY", /* name  */
564
   false,     /* partial_inplace  */
565
   0,     /* src_mask  */
566
   0,     /* dst_mask  */
567
   false),    /* pcrel_offset  */
568
569
  /* A 16 bit absolute relocation.  */
570
  HOWTO2 (R_NDS32_16_RELA,  /* type  */
571
   0,     /* rightshift  */
572
   2,     /* size  */
573
   16,      /* bitsize  */
574
   false,     /* pc_relative  */
575
   0,     /* bitpos  */
576
   complain_overflow_bitfield,/* complain_on_overflow  */
577
   bfd_elf_generic_reloc, /* special_function  */
578
   "R_NDS32_16_RELA", /* name  */
579
   false,     /* partial_inplace  */
580
   0xffff,    /* src_mask  */
581
   0xffff,    /* dst_mask  */
582
   false),    /* pcrel_offset  */
583
584
  /* A 32 bit absolute relocation.  */
585
  HOWTO2 (R_NDS32_32_RELA,  /* type  */
586
   0,     /* rightshift  */
587
   4,     /* size  */
588
   32,      /* bitsize  */
589
   false,     /* pc_relative  */
590
   0,     /* bitpos  */
591
   complain_overflow_bitfield,/* complain_on_overflow  */
592
   bfd_elf_generic_reloc, /* special_function  */
593
   "R_NDS32_32_RELA", /* name  */
594
   false,     /* partial_inplace  */
595
   0xffffffff,    /* src_mask  */
596
   0xffffffff,    /* dst_mask  */
597
   false),    /* pcrel_offset  */
598
599
  /* A 20 bit address.  */
600
  HOWTO2 (R_NDS32_20_RELA,  /* type  */
601
   0,     /* rightshift  */
602
   4,     /* size  */
603
   20,      /* bitsize  */
604
   false,     /* pc_relative  */
605
   0,     /* bitpos  */
606
   complain_overflow_signed,/* complain_on_overflow  */
607
   bfd_elf_generic_reloc, /* special_function  */
608
   "R_NDS32_20_RELA", /* name  */
609
   false,     /* partial_inplace  */
610
   0xfffff,   /* src_mask  */
611
   0xfffff,   /* dst_mask  */
612
   false),    /* pcrel_offset  */
613
614
  HOWTO2 (R_NDS32_9_PCREL_RELA, /* type  */
615
   1,     /* rightshift  */
616
   2,     /* size  */
617
   8,     /* bitsize  */
618
   true,      /* pc_relative  */
619
   0,     /* bitpos  */
620
   complain_overflow_signed,/* complain_on_overflow  */
621
   bfd_elf_generic_reloc, /* special_function  */
622
   "R_NDS32_9_PCREL_RELA",/* name  */
623
   false,     /* partial_inplace  */
624
   0xff,      /* src_mask  */
625
   0xff,      /* dst_mask  */
626
   true),     /* pcrel_offset  */
627
628
  /* A relative 15 bit relocation, right shifted by 1.  */
629
  HOWTO2 (R_NDS32_15_PCREL_RELA,/* type  */
630
   1,     /* rightshift  */
631
   4,     /* size  */
632
   14,      /* bitsize  */
633
   true,      /* pc_relative  */
634
   0,     /* bitpos  */
635
   complain_overflow_signed,/* complain_on_overflow  */
636
   bfd_elf_generic_reloc, /* special_function  */
637
   "R_NDS32_15_PCREL_RELA",/* name  */
638
   false,     /* partial_inplace  */
639
   0x3fff,    /* src_mask  */
640
   0x3fff,    /* dst_mask  */
641
   true),     /* pcrel_offset  */
642
643
  /* A relative 17 bit relocation, right shifted by 1.  */
644
  HOWTO2 (R_NDS32_17_PCREL_RELA,/* type  */
645
   1,     /* rightshift  */
646
   4,     /* size  */
647
   16,      /* bitsize  */
648
   true,      /* pc_relative  */
649
   0,     /* bitpos  */
650
   complain_overflow_signed,/* complain_on_overflow  */
651
   bfd_elf_generic_reloc, /* special_function  */
652
   "R_NDS32_17_PCREL_RELA",/* name  */
653
   false,     /* partial_inplace  */
654
   0xffff,    /* src_mask  */
655
   0xffff,    /* dst_mask  */
656
   true),     /* pcrel_offset  */
657
658
  /* A relative 25 bit relocation, right shifted by 2.  */
659
  HOWTO2 (R_NDS32_25_PCREL_RELA,/* type  */
660
   1,     /* rightshift  */
661
   4,     /* size  */
662
   24,      /* bitsize  */
663
   true,      /* pc_relative  */
664
   0,     /* bitpos  */
665
   complain_overflow_signed,/* complain_on_overflow  */
666
   bfd_elf_generic_reloc, /* special_function  */
667
   "R_NDS32_25_PCREL_RELA",/* name  */
668
   false,     /* partial_inplace  */
669
   0xffffff,    /* src_mask  */
670
   0xffffff,    /* dst_mask  */
671
   true),     /* pcrel_offset  */
672
673
  /* High 20 bits of address when lower 16 is or'd in.  */
674
  HOWTO2 (R_NDS32_HI20_RELA,  /* type  */
675
   12,      /* rightshift  */
676
   4,     /* size  */
677
   20,      /* bitsize  */
678
   false,     /* pc_relative  */
679
   0,     /* bitpos  */
680
   complain_overflow_dont,/* complain_on_overflow  */
681
   bfd_elf_generic_reloc, /* special_function  */
682
   "R_NDS32_HI20_RELA", /* name  */
683
   false,     /* partial_inplace  */
684
   0x000fffff,    /* src_mask  */
685
   0x000fffff,    /* dst_mask  */
686
   false),    /* pcrel_offset  */
687
688
  /* Lower 12 bits of address.  */
689
  HOWTO2 (R_NDS32_LO12S3_RELA,  /* type  */
690
   3,     /* rightshift  */
691
   4,     /* size  */
692
   9,     /* bitsize  */
693
   false,     /* pc_relative  */
694
   0,     /* bitpos  */
695
   complain_overflow_dont,/* complain_on_overflow  */
696
   bfd_elf_generic_reloc, /* special_function  */
697
   "R_NDS32_LO12S3_RELA", /* name  */
698
   false,     /* partial_inplace  */
699
   0x000001ff,    /* src_mask  */
700
   0x000001ff,    /* dst_mask  */
701
   false),    /* pcrel_offset  */
702
703
  /* Lower 12 bits of address.  */
704
  HOWTO2 (R_NDS32_LO12S2_RELA,  /* type  */
705
   2,     /* rightshift  */
706
   4,     /* size  */
707
   10,      /* bitsize  */
708
   false,     /* pc_relative  */
709
   0,     /* bitpos  */
710
   complain_overflow_dont,/* complain_on_overflow  */
711
   bfd_elf_generic_reloc, /* special_function  */
712
   "R_NDS32_LO12S2_RELA", /* name  */
713
   false,     /* partial_inplace  */
714
   0x000003ff,    /* src_mask  */
715
   0x000003ff,    /* dst_mask  */
716
   false),    /* pcrel_offset  */
717
718
  /* Lower 12 bits of address.  */
719
  HOWTO2 (R_NDS32_LO12S1_RELA,  /* type  */
720
   1,     /* rightshift  */
721
   4,     /* size  */
722
   11,      /* bitsize  */
723
   false,     /* pc_relative  */
724
   0,     /* bitpos  */
725
   complain_overflow_dont,/* complain_on_overflow  */
726
   bfd_elf_generic_reloc, /* special_function  */
727
   "R_NDS32_LO12S1_RELA", /* name  */
728
   false,     /* partial_inplace  */
729
   0x000007ff,    /* src_mask  */
730
   0x000007ff,    /* dst_mask  */
731
   false),    /* pcrel_offset  */
732
733
  /* Lower 12 bits of address.  */
734
  HOWTO2 (R_NDS32_LO12S0_RELA,  /* type  */
735
   0,     /* rightshift  */
736
   4,     /* size  */
737
   12,      /* bitsize  */
738
   false,     /* pc_relative  */
739
   0,     /* bitpos  */
740
   complain_overflow_dont,/* complain_on_overflow  */
741
   bfd_elf_generic_reloc, /* special_function  */
742
   "R_NDS32_LO12S0_RELA", /* name  */
743
   false,     /* partial_inplace  */
744
   0x00000fff,    /* src_mask  */
745
   0x00000fff,    /* dst_mask  */
746
   false),    /* pcrel_offset  */
747
748
  /* Small data area 15 bits offset.  */
749
  HOWTO2 (R_NDS32_SDA15S3_RELA, /* type  */
750
   3,     /* rightshift  */
751
   4,     /* size  */
752
   15,      /* bitsize  */
753
   false,     /* pc_relative  */
754
   0,     /* bitpos  */
755
   complain_overflow_signed,/* complain_on_overflow  */
756
   bfd_elf_generic_reloc, /* special_function  */
757
   "R_NDS32_SDA15S3_RELA",/* name  */
758
   false,     /* partial_inplace  */
759
   0x00007fff,    /* src_mask  */
760
   0x00007fff,    /* dst_mask  */
761
   false),    /* pcrel_offset  */
762
763
  /* Small data area 15 bits offset.  */
764
  HOWTO2 (R_NDS32_SDA15S2_RELA, /* type  */
765
   2,     /* rightshift  */
766
   4,     /* size  */
767
   15,      /* bitsize  */
768
   false,     /* pc_relative  */
769
   0,     /* bitpos  */
770
   complain_overflow_signed,/* complain_on_overflow  */
771
   bfd_elf_generic_reloc, /* special_function  */
772
   "R_NDS32_SDA15S2_RELA",/* name  */
773
   false,     /* partial_inplace  */
774
   0x00007fff,    /* src_mask  */
775
   0x00007fff,    /* dst_mask  */
776
   false),    /* pcrel_offset  */
777
778
  HOWTO2 (R_NDS32_SDA15S1_RELA, /* type  */
779
   1,     /* rightshift  */
780
   4,     /* size  */
781
   15,      /* bitsize  */
782
   false,     /* pc_relative  */
783
   0,     /* bitpos  */
784
   complain_overflow_signed,/* complain_on_overflow  */
785
   bfd_elf_generic_reloc, /* special_function  */
786
   "R_NDS32_SDA15S1_RELA",/* name  */
787
   false,     /* partial_inplace  */
788
   0x00007fff,    /* src_mask  */
789
   0x00007fff,    /* dst_mask  */
790
   false),    /* pcrel_offset  */
791
792
  HOWTO2 (R_NDS32_SDA15S0_RELA, /* type  */
793
   0,     /* rightshift  */
794
   4,     /* size  */
795
   15,      /* bitsize  */
796
   false,     /* pc_relative  */
797
   0,     /* bitpos  */
798
   complain_overflow_signed,/* complain_on_overflow  */
799
   bfd_elf_generic_reloc, /* special_function  */
800
   "R_NDS32_SDA15S0_RELA",/* name  */
801
   false,     /* partial_inplace  */
802
   0x00007fff,    /* src_mask  */
803
   0x00007fff,    /* dst_mask  */
804
   false),    /* pcrel_offset  */
805
806
  /* GNU extension to record C++ vtable hierarchy  */
807
  HOWTO2 (R_NDS32_RELA_GNU_VTINHERIT,/* type  */
808
   0,     /* rightshift  */
809
   4,     /* size  */
810
   0,     /* bitsize  */
811
   false,     /* pc_relative  */
812
   0,     /* bitpos  */
813
   complain_overflow_dont,/* complain_on_overflow  */
814
   NULL,      /* special_function  */
815
   "R_NDS32_RELA_GNU_VTINHERIT",/* name  */
816
   false,     /* partial_inplace  */
817
   0,     /* src_mask  */
818
   0,     /* dst_mask  */
819
   false),    /* pcrel_offset  */
820
821
  /* GNU extension to record C++ vtable member usage  */
822
  HOWTO2 (R_NDS32_RELA_GNU_VTENTRY,/* type  */
823
   0,     /* rightshift  */
824
   4,     /* size  */
825
   0,     /* bitsize  */
826
   false,     /* pc_relative  */
827
   0,     /* bitpos  */
828
   complain_overflow_dont,/* complain_on_overflow  */
829
   _bfd_elf_rel_vtable_reloc_fn,/* special_function  */
830
   "R_NDS32_RELA_GNU_VTENTRY",/* name  */
831
   false,     /* partial_inplace  */
832
   0,     /* src_mask  */
833
   0,     /* dst_mask  */
834
   false),    /* pcrel_offset  */
835
836
  /* Like R_NDS32_20, but referring to the GOT table entry for
837
     the symbol.  */
838
  HOWTO2 (R_NDS32_GOT20,  /* type  */
839
   0,     /* rightshift  */
840
   4,     /* size  */
841
   20,      /* bitsize  */
842
   false,     /* pc_relative  */
843
   0,     /* bitpos  */
844
   complain_overflow_signed,/* complain_on_overflow  */
845
   bfd_elf_generic_reloc, /* special_function  */
846
   "R_NDS32_GOT20", /* name  */
847
   false,     /* partial_inplace  */
848
   0xfffff,   /* src_mask  */
849
   0xfffff,   /* dst_mask  */
850
   false),    /* pcrel_offset  */
851
852
  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
853
     entry for the symbol.  */
854
  HOWTO2 (R_NDS32_25_PLTREL,  /* type  */
855
   1,     /* rightshift  */
856
   4,     /* size  */
857
   24,      /* bitsize  */
858
   true,      /* pc_relative  */
859
   0,     /* bitpos  */
860
   complain_overflow_signed,/* complain_on_overflow  */
861
   bfd_elf_generic_reloc, /* special_function  */
862
   "R_NDS32_25_PLTREL", /* name  */
863
   false,     /* partial_inplace  */
864
   0xffffff,    /* src_mask  */
865
   0xffffff,    /* dst_mask  */
866
   true),     /* pcrel_offset  */
867
868
  /* This is used only by the dynamic linker.  The symbol should exist
869
     both in the object being run and in some shared library.  The
870
     dynamic linker copies the data addressed by the symbol from the
871
     shared library into the object, because the object being
872
     run has to have the data at some particular address.  */
873
  HOWTO2 (R_NDS32_COPY,   /* type  */
874
   0,     /* rightshift  */
875
   4,     /* size  */
876
   32,      /* bitsize  */
877
   false,     /* pc_relative  */
878
   0,     /* bitpos  */
879
   complain_overflow_bitfield,/* complain_on_overflow  */
880
   bfd_elf_generic_reloc, /* special_function  */
881
   "R_NDS32_COPY",  /* name  */
882
   false,     /* partial_inplace  */
883
   0xffffffff,    /* src_mask  */
884
   0xffffffff,    /* dst_mask  */
885
   false),    /* pcrel_offset  */
886
887
  /* Like R_NDS32_20, but used when setting global offset table
888
     entries.  */
889
  HOWTO2 (R_NDS32_GLOB_DAT, /* type  */
890
   0,     /* rightshift  */
891
   4,     /* size  */
892
   32,      /* bitsize  */
893
   false,     /* pc_relative  */
894
   0,     /* bitpos  */
895
   complain_overflow_bitfield,/* complain_on_overflow  */
896
   bfd_elf_generic_reloc, /* special_function  */
897
   "R_NDS32_GLOB_DAT",  /* name  */
898
   false,     /* partial_inplace  */
899
   0xffffffff,    /* src_mask  */
900
   0xffffffff,    /* dst_mask  */
901
   false),    /* pcrel_offset  */
902
903
  /* Marks a procedure linkage table entry for a symbol.  */
904
  HOWTO2 (R_NDS32_JMP_SLOT, /* type  */
905
   0,     /* rightshift  */
906
   4,     /* size  */
907
   32,      /* bitsize  */
908
   false,     /* pc_relative  */
909
   0,     /* bitpos  */
910
   complain_overflow_bitfield,/* complain_on_overflow  */
911
   bfd_elf_generic_reloc, /* special_function  */
912
   "R_NDS32_JMP_SLOT",  /* name  */
913
   false,     /* partial_inplace  */
914
   0xffffffff,    /* src_mask  */
915
   0xffffffff,    /* dst_mask  */
916
   false),    /* pcrel_offset  */
917
918
  /* Used only by the dynamic linker.  When the object is run, this
919
     longword is set to the load address of the object, plus the
920
     addend.  */
921
  HOWTO2 (R_NDS32_RELATIVE, /* type  */
922
   0,     /* rightshift  */
923
   4,     /* size  */
924
   32,      /* bitsize  */
925
   false,     /* pc_relative  */
926
   0,     /* bitpos  */
927
   complain_overflow_bitfield,/* complain_on_overflow  */
928
   bfd_elf_generic_reloc, /* special_function  */
929
   "R_NDS32_RELATIVE",  /* name  */
930
   false,     /* partial_inplace  */
931
   0xffffffff,    /* src_mask  */
932
   0xffffffff,    /* dst_mask  */
933
   false),    /* pcrel_offset  */
934
935
  HOWTO2 (R_NDS32_GOTOFF, /* type  */
936
   0,     /* rightshift  */
937
   4,     /* size  */
938
   20,      /* bitsize  */
939
   false,     /* pc_relative  */
940
   0,     /* bitpos  */
941
   complain_overflow_signed,/* complain_on_overflow  */
942
   bfd_elf_generic_reloc, /* special_function  */
943
   "R_NDS32_GOTOFF",  /* name  */
944
   false,     /* partial_inplace  */
945
   0xfffff,   /* src_mask  */
946
   0xfffff,   /* dst_mask  */
947
   false),    /* pcrel_offset  */
948
949
  /* An PC Relative 20-bit relocation used when setting PIC offset
950
     table register.  */
951
  HOWTO2 (R_NDS32_GOTPC20,  /* type  */
952
   0,     /* rightshift  */
953
   4,     /* size  */
954
   20,      /* bitsize  */
955
   true,      /* pc_relative  */
956
   0,     /* bitpos  */
957
   complain_overflow_signed,/* complain_on_overflow  */
958
   bfd_elf_generic_reloc, /* special_function  */
959
   "R_NDS32_GOTPC20", /* name  */
960
   false,     /* partial_inplace  */
961
   0xfffff,   /* src_mask  */
962
   0xfffff,   /* dst_mask  */
963
   true),     /* pcrel_offset  */
964
965
  /* Like R_NDS32_HI20, but referring to the GOT table entry for
966
     the symbol.  */
967
  HOWTO2 (R_NDS32_GOT_HI20, /* type  */
968
   12,      /* rightshift  */
969
   4,     /* size  */
970
   20,      /* bitsize  */
971
   false,     /* pc_relative  */
972
   0,     /* bitpos  */
973
   complain_overflow_dont,/* complain_on_overflow  */
974
   bfd_elf_generic_reloc, /* special_function  */
975
   "R_NDS32_GOT_HI20",  /* name  */
976
   false,     /* partial_inplace  */
977
   0x000fffff,    /* src_mask  */
978
   0x000fffff,    /* dst_mask  */
979
   false),    /* pcrel_offset  */
980
981
  HOWTO2 (R_NDS32_GOT_LO12, /* type  */
982
   0,     /* rightshift  */
983
   4,     /* size  */
984
   12,      /* bitsize  */
985
   false,     /* pc_relative  */
986
   0,     /* bitpos  */
987
   complain_overflow_dont,/* complain_on_overflow  */
988
   bfd_elf_generic_reloc, /* special_function  */
989
   "R_NDS32_GOT_LO12",  /* name  */
990
   false,     /* partial_inplace  */
991
   0x00000fff,    /* src_mask  */
992
   0x00000fff,    /* dst_mask  */
993
   false),    /* pcrel_offset  */
994
995
  /* An PC Relative relocation used when setting PIC offset table register.
996
     Like R_NDS32_HI20, but referring to the GOT table entry for
997
     the symbol.  */
998
  HOWTO2 (R_NDS32_GOTPC_HI20, /* type  */
999
   12,      /* rightshift  */
1000
   4,     /* size  */
1001
   20,      /* bitsize  */
1002
   false,     /* pc_relative  */
1003
   0,     /* bitpos  */
1004
   complain_overflow_dont,/* complain_on_overflow  */
1005
   bfd_elf_generic_reloc, /* special_function  */
1006
   "R_NDS32_GOTPC_HI20",  /* name  */
1007
   false,     /* partial_inplace  */
1008
   0x000fffff,    /* src_mask  */
1009
   0x000fffff,    /* dst_mask  */
1010
   true),     /* pcrel_offset  */
1011
1012
  HOWTO2 (R_NDS32_GOTPC_LO12, /* type  */
1013
   0,     /* rightshift  */
1014
   4,     /* size  */
1015
   12,      /* bitsize  */
1016
   false,     /* pc_relative  */
1017
   0,     /* bitpos  */
1018
   complain_overflow_dont,/* complain_on_overflow  */
1019
   bfd_elf_generic_reloc, /* special_function  */
1020
   "R_NDS32_GOTPC_LO12",  /* name  */
1021
   false,     /* partial_inplace  */
1022
   0x00000fff,    /* src_mask  */
1023
   0x00000fff,    /* dst_mask  */
1024
   true),     /* pcrel_offset  */
1025
1026
  HOWTO2 (R_NDS32_GOTOFF_HI20,  /* type  */
1027
   12,      /* rightshift  */
1028
   4,     /* size  */
1029
   20,      /* bitsize  */
1030
   false,     /* pc_relative  */
1031
   0,     /* bitpos  */
1032
   complain_overflow_dont,/* complain_on_overflow  */
1033
   bfd_elf_generic_reloc, /* special_function  */
1034
   "R_NDS32_GOTOFF_HI20", /* name  */
1035
   false,     /* partial_inplace  */
1036
   0x000fffff,    /* src_mask  */
1037
   0x000fffff,    /* dst_mask  */
1038
   false),    /* pcrel_offset  */
1039
1040
  HOWTO2 (R_NDS32_GOTOFF_LO12,  /* type  */
1041
   0,     /* rightshift  */
1042
   4,     /* size  */
1043
   12,      /* bitsize  */
1044
   false,     /* pc_relative  */
1045
   0,     /* bitpos  */
1046
   complain_overflow_dont,/* complain_on_overflow  */
1047
   bfd_elf_generic_reloc, /* special_function  */
1048
   "R_NDS32_GOTOFF_LO12", /* name  */
1049
   false,     /* partial_inplace  */
1050
   0x00000fff,    /* src_mask  */
1051
   0x00000fff,    /* dst_mask  */
1052
   false),    /* pcrel_offset  */
1053
1054
  /* Alignment hint for relaxable instruction.  This is used with
1055
     R_NDS32_LABEL as a pair.  Relax this instruction from 4 bytes to 2
1056
     in order to make next label aligned on word boundary.  */
1057
  HOWTO2 (R_NDS32_INSN16, /* type  */
1058
   0,     /* rightshift  */
1059
   4,     /* size  */
1060
   32,      /* bitsize  */
1061
   false,     /* pc_relative  */
1062
   0,     /* bitpos  */
1063
   complain_overflow_dont,/* complain_on_overflow  */
1064
   nds32_elf_ignore_reloc,/* special_function  */
1065
   "R_NDS32_INSN16",  /* name  */
1066
   false,     /* partial_inplace  */
1067
   0x00000fff,    /* src_mask  */
1068
   0x00000fff,    /* dst_mask  */
1069
   false),    /* pcrel_offset  */
1070
1071
  /* Alignment hint for label.  */
1072
  HOWTO2 (R_NDS32_LABEL,  /* type  */
1073
   0,     /* rightshift  */
1074
   4,     /* size  */
1075
   32,      /* bitsize  */
1076
   false,     /* pc_relative  */
1077
   0,     /* bitpos  */
1078
   complain_overflow_dont,/* complain_on_overflow  */
1079
   nds32_elf_ignore_reloc,/* special_function  */
1080
   "R_NDS32_LABEL", /* name  */
1081
   false,     /* partial_inplace  */
1082
   0xffffffff,    /* src_mask  */
1083
   0xffffffff,    /* dst_mask  */
1084
   false),    /* pcrel_offset  */
1085
1086
  /* Relax hint for unconditional call sequence  */
1087
  HOWTO2 (R_NDS32_LONGCALL1,  /* type  */
1088
   0,     /* rightshift  */
1089
   4,     /* size  */
1090
   32,      /* bitsize  */
1091
   false,     /* pc_relative  */
1092
   0,     /* bitpos  */
1093
   complain_overflow_dont,/* complain_on_overflow  */
1094
   nds32_elf_ignore_reloc,/* special_function  */
1095
   "R_NDS32_LONGCALL1", /* name  */
1096
   false,     /* partial_inplace  */
1097
   0xffffffff,    /* src_mask  */
1098
   0xffffffff,    /* dst_mask  */
1099
   false),    /* pcrel_offset  */
1100
1101
  /* Relax hint for conditional call sequence.  */
1102
  HOWTO2 (R_NDS32_LONGCALL2,  /* type  */
1103
   0,     /* rightshift  */
1104
   4,     /* size  */
1105
   32,      /* bitsize  */
1106
   false,     /* pc_relative  */
1107
   0,     /* bitpos  */
1108
   complain_overflow_dont,/* complain_on_overflow  */
1109
   nds32_elf_ignore_reloc,/* special_function  */
1110
   "R_NDS32_LONGCALL2", /* name  */
1111
   false,     /* partial_inplace  */
1112
   0xffffffff,    /* src_mask  */
1113
   0xffffffff,    /* dst_mask  */
1114
   false),    /* pcrel_offset  */
1115
1116
  /* Relax hint for conditional call sequence.  */
1117
  HOWTO2 (R_NDS32_LONGCALL3,  /* type  */
1118
   0,     /* rightshift  */
1119
   4,     /* size  */
1120
   32,      /* bitsize  */
1121
   false,     /* pc_relative  */
1122
   0,     /* bitpos  */
1123
   complain_overflow_dont,/* complain_on_overflow  */
1124
   nds32_elf_ignore_reloc,/* special_function  */
1125
   "R_NDS32_LONGCALL3", /* name  */
1126
   false,     /* partial_inplace  */
1127
   0xffffffff,    /* src_mask  */
1128
   0xffffffff,    /* dst_mask  */
1129
   false),    /* pcrel_offset  */
1130
1131
  /* Relax hint for unconditional branch sequence.  */
1132
  HOWTO2 (R_NDS32_LONGJUMP1,  /* type  */
1133
   0,     /* rightshift  */
1134
   4,     /* size  */
1135
   32,      /* bitsize  */
1136
   false,     /* pc_relative  */
1137
   0,     /* bitpos  */
1138
   complain_overflow_dont,/* complain_on_overflow  */
1139
   nds32_elf_ignore_reloc,/* special_function  */
1140
   "R_NDS32_LONGJUMP1", /* name  */
1141
   false,     /* partial_inplace  */
1142
   0xffffffff,    /* src_mask  */
1143
   0xffffffff,    /* dst_mask  */
1144
   false),    /* pcrel_offset  */
1145
1146
  /* Relax hint for conditional branch sequence.  */
1147
  HOWTO2 (R_NDS32_LONGJUMP2,  /* type  */
1148
   0,     /* rightshift  */
1149
   4,     /* size  */
1150
   32,      /* bitsize  */
1151
   false,     /* pc_relative  */
1152
   0,     /* bitpos  */
1153
   complain_overflow_dont,/* complain_on_overflow  */
1154
   nds32_elf_ignore_reloc,/* special_function  */
1155
   "R_NDS32_LONGJUMP2", /* name  */
1156
   false,     /* partial_inplace  */
1157
   0xffffffff,    /* src_mask  */
1158
   0xffffffff,    /* dst_mask  */
1159
   false),    /* pcrel_offset  */
1160
1161
  /* Relax hint for conditional branch sequence.  */
1162
  HOWTO2 (R_NDS32_LONGJUMP3,  /* type  */
1163
   0,     /* rightshift  */
1164
   4,     /* size  */
1165
   32,      /* bitsize  */
1166
   false,     /* pc_relative  */
1167
   0,     /* bitpos  */
1168
   complain_overflow_dont,/* complain_on_overflow  */
1169
   nds32_elf_ignore_reloc,/* special_function  */
1170
   "R_NDS32_LONGJUMP3", /* name  */
1171
   false,     /* partial_inplace  */
1172
   0xffffffff,    /* src_mask  */
1173
   0xffffffff,    /* dst_mask  */
1174
   false),    /* pcrel_offset  */
1175
1176
  /* Relax hint for load/store sequence.   */
1177
  HOWTO2 (R_NDS32_LOADSTORE,  /* type  */
1178
   0,     /* rightshift  */
1179
   4,     /* size  */
1180
   32,      /* bitsize  */
1181
   false,     /* pc_relative  */
1182
   0,     /* bitpos  */
1183
   complain_overflow_dont,/* complain_on_overflow  */
1184
   nds32_elf_ignore_reloc,/* special_function  */
1185
   "R_NDS32_LOADSTORE", /* name  */
1186
   false,     /* partial_inplace  */
1187
   0xffffffff,    /* src_mask  */
1188
   0xffffffff,    /* dst_mask  */
1189
   false),    /* pcrel_offset  */
1190
1191
  /* Relax hint for load/store sequence.  */
1192
  HOWTO2 (R_NDS32_9_FIXED_RELA, /* type  */
1193
   0,     /* rightshift  */
1194
   2,     /* size  */
1195
   16,      /* bitsize  */
1196
   false,     /* pc_relative  */
1197
   0,     /* bitpos  */
1198
   complain_overflow_dont,/* complain_on_overflow  */
1199
   nds32_elf_ignore_reloc,/* special_function  */
1200
   "R_NDS32_9_FIXED_RELA",/* name  */
1201
   false,     /* partial_inplace  */
1202
   0x000000ff,    /* src_mask  */
1203
   0x000000ff,    /* dst_mask  */
1204
   false),    /* pcrel_offset  */
1205
1206
  /* Relax hint for load/store sequence.  */
1207
  HOWTO2 (R_NDS32_15_FIXED_RELA,/* type  */
1208
   0,     /* rightshift  */
1209
   4,     /* size  */
1210
   32,      /* bitsize  */
1211
   false,     /* pc_relative  */
1212
   0,     /* bitpos  */
1213
   complain_overflow_dont,/* complain_on_overflow  */
1214
   nds32_elf_ignore_reloc,/* special_function  */
1215
   "R_NDS32_15_FIXED_RELA",/* name  */
1216
   false,     /* partial_inplace  */
1217
   0x00003fff,    /* src_mask  */
1218
   0x00003fff,    /* dst_mask  */
1219
   false),    /* pcrel_offset  */
1220
1221
  /* Relax hint for load/store sequence.  */
1222
  HOWTO2 (R_NDS32_17_FIXED_RELA,/* type  */
1223
   0,     /* rightshift  */
1224
   4,     /* size  */
1225
   32,      /* bitsize  */
1226
   false,     /* pc_relative  */
1227
   0,     /* bitpos  */
1228
   complain_overflow_dont,/* complain_on_overflow  */
1229
   nds32_elf_ignore_reloc,/* special_function  */
1230
   "R_NDS32_17_FIXED_RELA",/* name  */
1231
   false,     /* partial_inplace  */
1232
   0x0000ffff,    /* src_mask  */
1233
   0x0000ffff,    /* dst_mask  */
1234
   false),    /* pcrel_offset  */
1235
1236
  /* Relax hint for load/store sequence.  */
1237
  HOWTO2 (R_NDS32_25_FIXED_RELA,/* type  */
1238
   0,     /* rightshift  */
1239
   4,     /* size  */
1240
   32,      /* bitsize  */
1241
   false,     /* pc_relative  */
1242
   0,     /* bitpos  */
1243
   complain_overflow_dont,/* complain_on_overflow  */
1244
   nds32_elf_ignore_reloc,/* special_function  */
1245
   "R_NDS32_25_FIXED_RELA",/* name  */
1246
   false,     /* partial_inplace  */
1247
   0x00ffffff,    /* src_mask  */
1248
   0x00ffffff,    /* dst_mask  */
1249
   false),    /* pcrel_offset  */
1250
1251
  /* High 20 bits of PLT symbol offset relative to PC.  */
1252
  HOWTO2 (R_NDS32_PLTREL_HI20,  /* type  */
1253
   12,      /* rightshift  */
1254
   4,     /* size  */
1255
   20,      /* bitsize  */
1256
   false,     /* pc_relative  */
1257
   0,     /* bitpos  */
1258
   complain_overflow_dont,/* complain_on_overflow  */
1259
   bfd_elf_generic_reloc, /* special_function  */
1260
   "R_NDS32_PLTREL_HI20", /* name  */
1261
   false,     /* partial_inplace  */
1262
   0x000fffff,    /* src_mask  */
1263
   0x000fffff,    /* dst_mask  */
1264
   false),    /* pcrel_offset  */
1265
1266
  /* Low 12 bits of PLT symbol offset relative to PC.  */
1267
  HOWTO2 (R_NDS32_PLTREL_LO12,  /* type  */
1268
   0,     /* rightshift  */
1269
   4,     /* size  */
1270
   12,      /* bitsize  */
1271
   false,     /* pc_relative  */
1272
   0,     /* bitpos  */
1273
   complain_overflow_dont,/* complain_on_overflow  */
1274
   bfd_elf_generic_reloc, /* special_function  */
1275
   "R_NDS32_PLTREL_LO12", /* name  */
1276
   false,     /* partial_inplace  */
1277
   0x00000fff,    /* src_mask  */
1278
   0x00000fff,    /* dst_mask  */
1279
   false),    /* pcrel_offset  */
1280
1281
  /* High 20 bits of PLT symbol offset relative to GOT (GP).  */
1282
  HOWTO2 (R_NDS32_PLT_GOTREL_HI20,  /* type  */
1283
   12,      /* rightshift  */
1284
   4,     /* size  */
1285
   20,      /* bitsize  */
1286
   false,     /* pc_relative  */
1287
   0,     /* bitpos  */
1288
   complain_overflow_dont,/* complain_on_overflow  */
1289
   bfd_elf_generic_reloc, /* special_function  */
1290
   "R_NDS32_PLT_GOTREL_HI20",/* name  */
1291
   false,     /* partial_inplace  */
1292
   0x000fffff,    /* src_mask  */
1293
   0x000fffff,    /* dst_mask  */
1294
   false),    /* pcrel_offset  */
1295
1296
  /* Low 12 bits of PLT symbol offset relative to GOT (GP).  */
1297
  HOWTO2 (R_NDS32_PLT_GOTREL_LO12,/* type  */
1298
   0,     /* rightshift  */
1299
   4,     /* size  */
1300
   12,      /* bitsize  */
1301
   false,     /* pc_relative  */
1302
   0,     /* bitpos  */
1303
   complain_overflow_dont,/* complain_on_overflow  */
1304
   bfd_elf_generic_reloc, /* special_function  */
1305
   "R_NDS32_PLT_GOTREL_LO12",/* name  */
1306
   false,     /* partial_inplace  */
1307
   0x00000fff,    /* src_mask  */
1308
   0x00000fff,    /* dst_mask  */
1309
   false),    /* pcrel_offset  */
1310
1311
  /* Small data area 12 bits offset.  */
1312
  HOWTO2 (R_NDS32_SDA12S2_DP_RELA,/* type  */
1313
   2,     /* rightshift  */
1314
   4,     /* size  */
1315
   12,      /* bitsize  */
1316
   false,     /* pc_relative  */
1317
   0,     /* bitpos  */
1318
   complain_overflow_signed,/* complain_on_overflow  */
1319
   bfd_elf_generic_reloc, /* special_function  */
1320
   "R_NDS32_SDA12S2_DP_RELA",/* name  */
1321
   false,     /* partial_inplace  */
1322
   0x00000fff,    /* src_mask  */
1323
   0x00000fff,    /* dst_mask  */
1324
   false),    /* pcrel_offset  */
1325
1326
  /* Small data area 12 bits offset.  */
1327
  HOWTO2 (R_NDS32_SDA12S2_SP_RELA,/* type  */
1328
   2,     /* rightshift  */
1329
   4,     /* size  */
1330
   12,      /* bitsize  */
1331
   false,     /* pc_relative  */
1332
   0,     /* bitpos  */
1333
   complain_overflow_signed,/* complain_on_overflow  */
1334
   bfd_elf_generic_reloc, /* special_function  */
1335
   "R_NDS32_SDA12S2_SP_RELA",/* name  */
1336
   false,     /* partial_inplace  */
1337
   0x00000fff,    /* src_mask  */
1338
   0x00000fff,    /* dst_mask  */
1339
   false),    /* pcrel_offset  */
1340
  /* Lower 12 bits of address.  */
1341
1342
  HOWTO2 (R_NDS32_LO12S2_DP_RELA, /* type  */
1343
   2,     /* rightshift  */
1344
   4,     /* size  */
1345
   10,      /* bitsize  */
1346
   false,     /* pc_relative  */
1347
   0,     /* bitpos  */
1348
   complain_overflow_dont,/* complain_on_overflow  */
1349
   bfd_elf_generic_reloc, /* special_function  */
1350
   "R_NDS32_LO12S2_DP_RELA",/* name  */
1351
   false,     /* partial_inplace  */
1352
   0x000003ff,    /* src_mask  */
1353
   0x000003ff,    /* dst_mask  */
1354
   false),    /* pcrel_offset  */
1355
1356
  /* Lower 12 bits of address.  */
1357
  HOWTO2 (R_NDS32_LO12S2_SP_RELA,/* type  */
1358
   2,     /* rightshift  */
1359
   4,     /* size  */
1360
   10,      /* bitsize  */
1361
   false,     /* pc_relative  */
1362
   0,     /* bitpos  */
1363
   complain_overflow_dont,/* complain_on_overflow  */
1364
   bfd_elf_generic_reloc, /* special_function  */
1365
   "R_NDS32_LO12S2_SP_RELA",/* name  */
1366
   false,     /* partial_inplace  */
1367
   0x000003ff,    /* src_mask  */
1368
   0x000003ff,    /* dst_mask  */
1369
   false),    /* pcrel_offset  */
1370
1371
  /* Lower 12 bits of address.  Special identity for or case.  */
1372
  HOWTO2 (R_NDS32_LO12S0_ORI_RELA,/* type  */
1373
   0,     /* rightshift  */
1374
   4,     /* size  */
1375
   12,      /* bitsize  */
1376
   false,     /* pc_relative  */
1377
   0,     /* bitpos  */
1378
   complain_overflow_dont,/* complain_on_overflow  */
1379
   bfd_elf_generic_reloc, /* special_function  */
1380
   "R_NDS32_LO12S0_ORI_RELA",/* name  */
1381
   false,     /* partial_inplace  */
1382
   0x00000fff,    /* src_mask  */
1383
   0x00000fff,    /* dst_mask  */
1384
   false),    /* pcrel_offset  */
1385
1386
  /* Small data area 19 bits offset.  */
1387
  HOWTO2 (R_NDS32_SDA16S3_RELA, /* type  */
1388
   3,     /* rightshift  */
1389
   4,     /* size  */
1390
   16,      /* bitsize  */
1391
   false,     /* pc_relative  */
1392
   0,     /* bitpos  */
1393
   complain_overflow_signed,/* complain_on_overflow  */
1394
   bfd_elf_generic_reloc, /* special_function  */
1395
   "R_NDS32_SDA16S3_RELA",/* name  */
1396
   false,     /* partial_inplace  */
1397
   0x0000ffff,    /* src_mask  */
1398
   0x0000ffff,    /* dst_mask  */
1399
   false),    /* pcrel_offset  */
1400
1401
  /* Small data area 15 bits offset.  */
1402
  HOWTO2 (R_NDS32_SDA17S2_RELA, /* type  */
1403
   2,     /* rightshift  */
1404
   4,     /* size  */
1405
   17,      /* bitsize  */
1406
   false,     /* pc_relative  */
1407
   0,     /* bitpos  */
1408
   complain_overflow_signed,/* complain_on_overflow  */
1409
   bfd_elf_generic_reloc, /* special_function  */
1410
   "R_NDS32_SDA17S2_RELA",/* name  */
1411
   false,     /* partial_inplace  */
1412
   0x0001ffff,    /* src_mask  */
1413
   0x0001ffff,    /* dst_mask  */
1414
   false),    /* pcrel_offset  */
1415
1416
  HOWTO2 (R_NDS32_SDA18S1_RELA, /* type  */
1417
   1,     /* rightshift  */
1418
   4,     /* size  */
1419
   18,      /* bitsize  */
1420
   false,     /* pc_relative  */
1421
   0,     /* bitpos  */
1422
   complain_overflow_signed,/* complain_on_overflow  */
1423
   bfd_elf_generic_reloc, /* special_function  */
1424
   "R_NDS32_SDA18S1_RELA",/* name  */
1425
   false,     /* partial_inplace  */
1426
   0x0003ffff,    /* src_mask  */
1427
   0x0003ffff,    /* dst_mask  */
1428
   false),    /* pcrel_offset  */
1429
1430
  HOWTO2 (R_NDS32_SDA19S0_RELA, /* type  */
1431
   0,     /* rightshift  */
1432
   4,     /* size  */
1433
   19,      /* bitsize  */
1434
   false,     /* pc_relative  */
1435
   0,     /* bitpos  */
1436
   complain_overflow_signed,/* complain_on_overflow  */
1437
   bfd_elf_generic_reloc, /* special_function  */
1438
   "R_NDS32_SDA19S0_RELA",/* name  */
1439
   false,     /* partial_inplace  */
1440
   0x0007ffff,    /* src_mask  */
1441
   0x0007ffff,    /* dst_mask  */
1442
   false),    /* pcrel_offset  */
1443
  HOWTO2 (R_NDS32_DWARF2_OP1_RELA,/* type  */
1444
   0,     /* rightshift  */
1445
   1,     /* size  */
1446
   8,     /* bitsize  */
1447
   false,     /* pc_relative  */
1448
   0,     /* bitpos  */
1449
   complain_overflow_dont,/* complain_on_overflow  */
1450
   nds32_elf_ignore_reloc,/* special_function  */
1451
   "R_NDS32_DWARF2_OP1_RELA",/* name  */
1452
   false,     /* partial_inplace  */
1453
   0xff,      /* src_mask  */
1454
   0xff,      /* dst_mask  */
1455
   false),    /* pcrel_offset  */
1456
1457
  HOWTO2 (R_NDS32_DWARF2_OP2_RELA,/* type  */
1458
   0,     /* rightshift  */
1459
   2,     /* size  */
1460
   16,      /* bitsize  */
1461
   false,     /* pc_relative  */
1462
   0,     /* bitpos  */
1463
   complain_overflow_dont,/* complain_on_overflow  */
1464
   nds32_elf_ignore_reloc,/* special_function  */
1465
   "R_NDS32_DWARF2_OP2_RELA",/* name  */
1466
   false,     /* partial_inplace  */
1467
   0xffff,    /* src_mask  */
1468
   0xffff,    /* dst_mask  */
1469
   false),    /* pcrel_offset  */
1470
1471
  HOWTO2 (R_NDS32_DWARF2_LEB_RELA,/* type  */
1472
   0,     /* rightshift  */
1473
   4,     /* size  */
1474
   32,      /* bitsize  */
1475
   false,     /* pc_relative  */
1476
   0,     /* bitpos  */
1477
   complain_overflow_dont,/* complain_on_overflow  */
1478
   nds32_elf_ignore_reloc,/* special_function  */
1479
   "R_NDS32_DWARF2_LEB_RELA",/* name  */
1480
   false,     /* partial_inplace  */
1481
   0xffffffff,    /* src_mask  */
1482
   0xffffffff,    /* dst_mask  */
1483
   false),    /* pcrel_offset  */
1484
1485
  HOWTO2 (R_NDS32_UPDATE_TA_RELA,/* type  */
1486
   0,     /* rightshift  */
1487
   2,     /* size  */
1488
   16,      /* bitsize  */
1489
   false,     /* pc_relative  */
1490
   0,     /* bitpos  */
1491
   complain_overflow_dont,/* complain_on_overflow  */
1492
   nds32_elf_ignore_reloc,/* special_function  */
1493
   "R_NDS32_UPDATE_TA_RELA",/* name  */
1494
   false,     /* partial_inplace  */
1495
   0xffff,    /* src_mask  */
1496
   0xffff,    /* dst_mask  */
1497
   false),    /* pcrel_offset  */
1498
1499
  /* Like R_NDS32_PCREL, but referring to the procedure linkage table
1500
     entry for the symbol.  */
1501
  HOWTO2 (R_NDS32_9_PLTREL, /* type  */
1502
   1,     /* rightshift  */
1503
   2,     /* size  */
1504
   8,     /* bitsize  */
1505
   true,      /* pc_relative  */
1506
   0,     /* bitpos  */
1507
   complain_overflow_signed,/* complain_on_overflow  */
1508
   bfd_elf_generic_reloc, /* special_function  */
1509
   "R_NDS32_9_PLTREL",  /* name  */
1510
   false,     /* partial_inplace  */
1511
   0xff,      /* src_mask  */
1512
   0xff,      /* dst_mask  */
1513
   true),     /* pcrel_offset  */
1514
1515
  /* Low 20 bits of PLT symbol offset relative to GOT (GP).  */
1516
  HOWTO2 (R_NDS32_PLT_GOTREL_LO20,/* type  */
1517
   0,     /* rightshift  */
1518
   4,     /* size  */
1519
   20,      /* bitsize  */
1520
   false,     /* pc_relative  */
1521
   0,     /* bitpos  */
1522
   complain_overflow_dont,/* complain_on_overflow  */
1523
   bfd_elf_generic_reloc, /* special_function  */
1524
   "R_NDS32_PLT_GOTREL_LO20",/* name  */
1525
   false,     /* partial_inplace  */
1526
   0x000fffff,    /* src_mask  */
1527
   0x000fffff,    /* dst_mask  */
1528
   false),    /* pcrel_offset  */
1529
1530
  /* low 15 bits of PLT symbol offset relative to GOT (GP)  */
1531
  HOWTO2 (R_NDS32_PLT_GOTREL_LO15,/* type  */
1532
   0,     /* rightshift  */
1533
   4,     /* size  */
1534
   15,      /* bitsize  */
1535
   false,     /* pc_relative  */
1536
   0,     /* bitpos  */
1537
   complain_overflow_dont,/* complain_on_overflow  */
1538
   bfd_elf_generic_reloc, /* special_function  */
1539
   "R_NDS32_PLT_GOTREL_LO15",/* name  */
1540
   false,     /* partial_inplace  */
1541
   0x00007fff,    /* src_mask  */
1542
   0x00007fff,    /* dst_mask  */
1543
   false),    /* pcrel_offset  */
1544
1545
  /* Low 19 bits of PLT symbol offset relative to GOT (GP).  */
1546
  HOWTO2 (R_NDS32_PLT_GOTREL_LO19,/* type  */
1547
   0,     /* rightshift  */
1548
   4,     /* size  */
1549
   19,      /* bitsize  */
1550
   false,     /* pc_relative  */
1551
   0,     /* bitpos  */
1552
   complain_overflow_dont,/* complain_on_overflow  */
1553
   bfd_elf_generic_reloc, /* special_function  */
1554
   "R_NDS32_PLT_GOTREL_LO19",/* name  */
1555
   false,     /* partial_inplace  */
1556
   0x0007ffff,    /* src_mask  */
1557
   0x0007ffff,    /* dst_mask  */
1558
   false),    /* pcrel_offset  */
1559
1560
  HOWTO2 (R_NDS32_GOT_LO15, /* type  */
1561
   0,     /* rightshift  */
1562
   4,     /* size  */
1563
   15,      /* bitsize  */
1564
   false,     /* pc_relative  */
1565
   0,     /* bitpos  */
1566
   complain_overflow_dont,/* complain_on_overflow  */
1567
   bfd_elf_generic_reloc, /* special_function  */
1568
   "R_NDS32_GOT_LO15",  /* name  */
1569
   false,     /* partial_inplace  */
1570
   0x00007fff,    /* src_mask  */
1571
   0x00007fff,    /* dst_mask  */
1572
   false),    /* pcrel_offset  */
1573
1574
  HOWTO2 (R_NDS32_GOT_LO19, /* type  */
1575
   0,     /* rightshift  */
1576
   4,     /* size  */
1577
   19,      /* bitsize  */
1578
   false,     /* pc_relative  */
1579
   0,     /* bitpos  */
1580
   complain_overflow_dont,/* complain_on_overflow  */
1581
   bfd_elf_generic_reloc, /* special_function  */
1582
   "R_NDS32_GOT_LO19",  /* name  */
1583
   false,     /* partial_inplace  */
1584
   0x0007ffff,    /* src_mask  */
1585
   0x0007ffff,    /* dst_mask  */
1586
   false),    /* pcrel_offset  */
1587
1588
  HOWTO2 (R_NDS32_GOTOFF_LO15,  /* type  */
1589
   0,     /* rightshift  */
1590
   4,     /* size  */
1591
   15,      /* bitsize  */
1592
   false,     /* pc_relative  */
1593
   0,     /* bitpos  */
1594
   complain_overflow_dont,/* complain_on_overflow  */
1595
   bfd_elf_generic_reloc, /* special_function  */
1596
   "R_NDS32_GOTOFF_LO15", /* name  */
1597
   false,     /* partial_inplace  */
1598
   0x00007fff,    /* src_mask  */
1599
   0x00007fff,    /* dst_mask  */
1600
   false),    /* pcrel_offset  */
1601
1602
  HOWTO2 (R_NDS32_GOTOFF_LO19,  /* type  */
1603
   0,     /* rightshift  */
1604
   4,     /* size  */
1605
   19,      /* bitsize  */
1606
   false,     /* pc_relative  */
1607
   0,     /* bitpos  */
1608
   complain_overflow_dont,/* complain_on_overflow  */
1609
   bfd_elf_generic_reloc, /* special_function  */
1610
   "R_NDS32_GOTOFF_LO19", /* name  */
1611
   false,     /* partial_inplace  */
1612
   0x0007ffff,    /* src_mask  */
1613
   0x0007ffff,    /* dst_mask  */
1614
   false),    /* pcrel_offset  */
1615
1616
  /* GOT 15 bits offset.  */
1617
  HOWTO2 (R_NDS32_GOT15S2_RELA, /* type  */
1618
   2,     /* rightshift  */
1619
   4,     /* size  */
1620
   15,      /* bitsize  */
1621
   false,     /* pc_relative  */
1622
   0,     /* bitpos  */
1623
   complain_overflow_signed,/* complain_on_overflow  */
1624
   bfd_elf_generic_reloc, /* special_function  */
1625
   "R_NDS32_GOT15S2_RELA",/* name  */
1626
   false,     /* partial_inplace  */
1627
   0x00007fff,    /* src_mask  */
1628
   0x00007fff,    /* dst_mask  */
1629
   false),    /* pcrel_offset  */
1630
1631
  /* GOT 17 bits offset.  */
1632
  HOWTO2 (R_NDS32_GOT17S2_RELA, /* type  */
1633
   2,     /* rightshift  */
1634
   4,     /* size  */
1635
   17,      /* bitsize  */
1636
   false,     /* pc_relative  */
1637
   0,     /* bitpos  */
1638
   complain_overflow_signed,/* complain_on_overflow  */
1639
   bfd_elf_generic_reloc, /* special_function  */
1640
   "R_NDS32_GOT17S2_RELA",/* name  */
1641
   false,     /* partial_inplace  */
1642
   0x0001ffff,    /* src_mask  */
1643
   0x0001ffff,    /* dst_mask  */
1644
   false),    /* pcrel_offset  */
1645
1646
  /* A 5 bit address.  */
1647
  HOWTO2 (R_NDS32_5_RELA, /* type  */
1648
   0,     /* rightshift  */
1649
   2,     /* size  */
1650
   5,     /* bitsize  */
1651
   false,     /* pc_relative  */
1652
   0,     /* bitpos  */
1653
   complain_overflow_signed,/* complain_on_overflow  */
1654
   bfd_elf_generic_reloc, /* special_function  */
1655
   "R_NDS32_5_RELA",  /* name  */
1656
   false,     /* partial_inplace  */
1657
   0x1f,      /* src_mask  */
1658
   0x1f,      /* dst_mask  */
1659
   false),    /* pcrel_offset  */
1660
1661
  HOWTO2 (R_NDS32_10_UPCREL_RELA,/* type  */
1662
   1,     /* rightshift  */
1663
   2,     /* size  */
1664
   9,     /* bitsize  */
1665
   true,      /* pc_relative  */
1666
   0,     /* bitpos  */
1667
   complain_overflow_unsigned,/* complain_on_overflow  */
1668
   bfd_elf_generic_reloc, /* special_function  */
1669
   "R_NDS32_10_UPCREL_RELA",/* name  */
1670
   false,     /* partial_inplace  */
1671
   0x1ff,     /* src_mask  */
1672
   0x1ff,     /* dst_mask  */
1673
   true),     /* pcrel_offset  */
1674
1675
  HOWTO2 (R_NDS32_SDA_FP7U2_RELA,/* type  */
1676
   2,     /* rightshift  */
1677
   2,     /* size  */
1678
   7,     /* bitsize  */
1679
   false,     /* pc_relative  */
1680
   0,     /* bitpos  */
1681
   complain_overflow_unsigned,/* complain_on_overflow  */
1682
   bfd_elf_generic_reloc, /* special_function  */
1683
   "R_NDS32_SDA_FP7U2_RELA",/* name  */
1684
   false,     /* partial_inplace  */
1685
   0x0000007f,    /* src_mask  */
1686
   0x0000007f,    /* dst_mask  */
1687
   false),    /* pcrel_offset  */
1688
1689
  HOWTO2 (R_NDS32_WORD_9_PCREL_RELA,/* type  */
1690
   1,     /* rightshift  */
1691
   4,     /* size  */
1692
   8,     /* bitsize  */
1693
   true,      /* pc_relative  */
1694
   0,     /* bitpos  */
1695
   complain_overflow_signed,/* complain_on_overflow  */
1696
   bfd_elf_generic_reloc, /* special_function  */
1697
   "R_NDS32_WORD_9_PCREL_RELA",/* name  */
1698
   false,     /* partial_inplace  */
1699
   0xff,      /* src_mask  */
1700
   0xff,      /* dst_mask  */
1701
   true),     /* pcrel_offset  */
1702
1703
  HOWTO2 (R_NDS32_25_ABS_RELA,  /* type  */
1704
   1,     /* rightshift  */
1705
   4,     /* size  */
1706
   24,      /* bitsize  */
1707
   false,     /* pc_relative  */
1708
   0,     /* bitpos  */
1709
   complain_overflow_dont,/* complain_on_overflow  */
1710
   bfd_elf_generic_reloc, /* special_function  */
1711
   "R_NDS32_25_ABS_RELA", /* name  */
1712
   false,     /* partial_inplace  */
1713
   0xffffff,    /* src_mask  */
1714
   0xffffff,    /* dst_mask  */
1715
   false),    /* pcrel_offset  */
1716
1717
  /* A relative 17 bit relocation for ifc, right shifted by 1.  */
1718
  HOWTO2 (R_NDS32_17IFC_PCREL_RELA,/* type  */
1719
   1,     /* rightshift  */
1720
   4,     /* size  */
1721
   16,      /* bitsize  */
1722
   true,      /* pc_relative  */
1723
   0,     /* bitpos  */
1724
   complain_overflow_signed,/* complain_on_overflow  */
1725
   bfd_elf_generic_reloc, /* special_function  */
1726
   "R_NDS32_17IFC_PCREL_RELA",/* name  */
1727
   false,     /* partial_inplace  */
1728
   0xffff,    /* src_mask  */
1729
   0xffff,    /* dst_mask  */
1730
   true),     /* pcrel_offset  */
1731
1732
  /* A relative unsigned 10 bit relocation for ifc, right shifted by 1.  */
1733
  HOWTO2 (R_NDS32_10IFCU_PCREL_RELA,/* type  */
1734
   1,     /* rightshift  */
1735
   2,     /* size  */
1736
   9,     /* bitsize  */
1737
   true,      /* pc_relative  */
1738
   0,     /* bitpos  */
1739
   complain_overflow_unsigned,/* complain_on_overflow  */
1740
   bfd_elf_generic_reloc, /* special_function  */
1741
   "R_NDS32_10IFCU_PCREL_RELA",/* name  */
1742
   false,     /* partial_inplace  */
1743
   0x1ff,     /* src_mask  */
1744
   0x1ff,     /* dst_mask  */
1745
   true),     /* pcrel_offset  */
1746
1747
  /* Like R_NDS32_HI20, but referring to the TLS LE entry for the symbol.  */
1748
  HOWTO2 (R_NDS32_TLS_LE_HI20,  /* type  */
1749
   12,      /* rightshift  */
1750
   4,     /* size  */
1751
   20,      /* bitsize  */
1752
   false,     /* pc_relative  */
1753
   0,     /* bitpos  */
1754
   complain_overflow_dont,/* complain_on_overflow  */
1755
   bfd_elf_generic_reloc, /* special_function  */
1756
   "R_NDS32_TLS_LE_HI20", /* name  */
1757
   false,     /* partial_inplace  */
1758
   0x000fffff,    /* src_mask  */
1759
   0x000fffff,    /* dst_mask  */
1760
   false),    /* pcrel_offset  */
1761
1762
  HOWTO2 (R_NDS32_TLS_LE_LO12,  /* type  */
1763
   0,     /* rightshift  */
1764
   4,     /* size  */
1765
   12,      /* bitsize  */
1766
   false,     /* pc_relative  */
1767
   0,     /* bitpos  */
1768
   complain_overflow_dont,/* complain_on_overflow  */
1769
   bfd_elf_generic_reloc, /* special_function  */
1770
   "R_NDS32_TLS_LE_LO12", /* name  */
1771
   false,     /* partial_inplace  */
1772
   0x00000fff,    /* src_mask  */
1773
   0x00000fff,    /* dst_mask  */
1774
   false),    /* pcrel_offset  */
1775
1776
  /* Like R_NDS32_HI20, but referring to the TLS IE entry for the symbol.  */
1777
  HOWTO2 (R_NDS32_TLS_IE_HI20,  /* type  */
1778
   12,      /* rightshift  */
1779
   4,     /* size  */
1780
   20,      /* bitsize  */
1781
   false,     /* pc_relative  */
1782
   0,     /* bitpos  */
1783
   complain_overflow_dont,/* complain_on_overflow  */
1784
   bfd_elf_generic_reloc, /* special_function  */
1785
   "R_NDS32_TLS_IE_HI20", /* name  */
1786
   false,     /* partial_inplace  */
1787
   0x000fffff,    /* src_mask  */
1788
   0x000fffff,    /* dst_mask  */
1789
   false),    /* pcrel_offset  */
1790
1791
  HOWTO2 (R_NDS32_TLS_IE_LO12S2,/* type  */
1792
   2,     /* rightshift  */
1793
   4,     /* size  */
1794
   10,      /* bitsize  */
1795
   false,     /* pc_relative  */
1796
   0,     /* bitpos  */
1797
   complain_overflow_dont,/* complain_on_overflow  */
1798
   bfd_elf_generic_reloc, /* special_function  */
1799
   "R_NDS32_TLS_IE_LO12S2",/* name  */
1800
   false,     /* partial_inplace  */
1801
   0x000003ff,    /* src_mask  */
1802
   0x000003ff,    /* dst_mask  */
1803
   false),    /* pcrel_offset  */
1804
1805
  /* TLS LE TP offset relocation  */
1806
  HOWTO2 (R_NDS32_TLS_TPOFF,  /* type  */
1807
   0,     /* rightshift  */
1808
   4,     /* size  */
1809
   32,      /* bitsize  */
1810
   false,     /* pc_relative  */
1811
   0,     /* bitpos  */
1812
   complain_overflow_bitfield,/* complain_on_overflow  */
1813
   bfd_elf_generic_reloc, /* special_function  */
1814
   "R_NDS32_TLS_TPOFF", /* name  */
1815
   false,     /* partial_inplace  */
1816
   0xffffffff,    /* src_mask  */
1817
   0xffffffff,    /* dst_mask  */
1818
   false),    /* pcrel_offset  */
1819
1820
  /* A 20 bit address.  */
1821
  HOWTO2 (R_NDS32_TLS_LE_20,  /* type  */
1822
   0,     /* rightshift  */
1823
   4,     /* size  */
1824
   20,      /* bitsize  */
1825
   false,     /* pc_relative  */
1826
   0,     /* bitpos  */
1827
   complain_overflow_signed,/* complain_on_overflow  */
1828
   bfd_elf_generic_reloc, /* special_function  */
1829
   "R_NDS32_TLS_LE_20", /* name  */
1830
   false,     /* partial_inplace  */
1831
   0xfffff,   /* src_mask  */
1832
   0xfffff,   /* dst_mask  */
1833
   false),    /* pcrel_offset  */
1834
1835
  HOWTO2 (R_NDS32_TLS_LE_15S0,  /* type  */
1836
   0,     /* rightshift  */
1837
   4,     /* size  */
1838
   15,      /* bitsize  */
1839
   false,     /* pc_relative  */
1840
   0,     /* bitpos  */
1841
   complain_overflow_signed,/* complain_on_overflow  */
1842
   bfd_elf_generic_reloc, /* special_function  */
1843
   "R_NDS32_TLS_LE_15S0", /* name  */
1844
   false,     /* partial_inplace  */
1845
   0x7fff,    /* src_mask  */
1846
   0x7fff,    /* dst_mask  */
1847
   false),    /* pcrel_offset  */
1848
1849
  HOWTO2 (R_NDS32_TLS_LE_15S1,  /* type  */
1850
   1,     /* rightshift  */
1851
   4,     /* size  */
1852
   15,      /* bitsize  */
1853
   false,     /* pc_relative  */
1854
   0,     /* bitpos  */
1855
   complain_overflow_signed,/* complain_on_overflow  */
1856
   bfd_elf_generic_reloc, /* special_function  */
1857
   "R_NDS32_TLS_LE_15S1", /* name  */
1858
   false,     /* partial_inplace  */
1859
   0x7fff,    /* src_mask  */
1860
   0x7fff,    /* dst_mask  */
1861
   false),    /* pcrel_offset  */
1862
1863
  HOWTO2 (R_NDS32_TLS_LE_15S2,  /* type  */
1864
   2,     /* rightshift  */
1865
   4,     /* size  */
1866
   15,      /* bitsize  */
1867
   false,     /* pc_relative  */
1868
   0,     /* bitpos  */
1869
   complain_overflow_signed,/* complain_on_overflow  */
1870
   bfd_elf_generic_reloc, /* special_function  */
1871
   "R_NDS32_TLS_LE_15S2", /* name  */
1872
   false,     /* partial_inplace  */
1873
   0x7fff,    /* src_mask  */
1874
   0x7fff,    /* dst_mask  */
1875
   false),    /* pcrel_offset  */
1876
1877
  /* Relax hint for unconditional call sequence  */
1878
  HOWTO2 (R_NDS32_LONGCALL4,  /* type  */
1879
   0,     /* rightshift  */
1880
   4,     /* size  */
1881
   32,      /* bitsize  */
1882
   false,     /* pc_relative  */
1883
   0,     /* bitpos  */
1884
   complain_overflow_dont,/* complain_on_overflow  */
1885
   nds32_elf_ignore_reloc,/* special_function  */
1886
   "R_NDS32_LONGCALL4", /* name  */
1887
   false,     /* partial_inplace  */
1888
   0xffffffff,    /* src_mask  */
1889
   0xffffffff,    /* dst_mask  */
1890
   false),    /* pcrel_offset  */
1891
1892
  /* Relax hint for conditional call sequence.  */
1893
  HOWTO2 (R_NDS32_LONGCALL5,  /* type  */
1894
   0,     /* rightshift  */
1895
   4,     /* size  */
1896
   32,      /* bitsize  */
1897
   false,     /* pc_relative  */
1898
   0,     /* bitpos  */
1899
   complain_overflow_dont,/* complain_on_overflow  */
1900
   nds32_elf_ignore_reloc,/* special_function  */
1901
   "R_NDS32_LONGCALL5", /* name  */
1902
   false,     /* partial_inplace  */
1903
   0xffffffff,    /* src_mask  */
1904
   0xffffffff,    /* dst_mask  */
1905
   false),    /* pcrel_offset  */
1906
1907
  /* Relax hint for conditional call sequence.  */
1908
  HOWTO2 (R_NDS32_LONGCALL6,  /* type  */
1909
   0,     /* rightshift  */
1910
   4,     /* size  */
1911
   32,      /* bitsize  */
1912
   false,     /* pc_relative  */
1913
   0,     /* bitpos  */
1914
   complain_overflow_dont,/* complain_on_overflow  */
1915
   nds32_elf_ignore_reloc,/* special_function  */
1916
   "R_NDS32_LONGCALL6", /* name  */
1917
   false,     /* partial_inplace  */
1918
   0xffffffff,    /* src_mask  */
1919
   0xffffffff,    /* dst_mask  */
1920
   false),    /* pcrel_offset  */
1921
1922
  /* Relax hint for unconditional branch sequence.  */
1923
  HOWTO2 (R_NDS32_LONGJUMP4,  /* type  */
1924
   0,     /* rightshift  */
1925
   4,     /* size  */
1926
   32,      /* bitsize  */
1927
   false,     /* pc_relative  */
1928
   0,     /* bitpos  */
1929
   complain_overflow_dont,/* complain_on_overflow  */
1930
   nds32_elf_ignore_reloc,/* special_function  */
1931
   "R_NDS32_LONGJUMP4", /* name  */
1932
   false,     /* partial_inplace  */
1933
   0xffffffff,    /* src_mask  */
1934
   0xffffffff,    /* dst_mask  */
1935
   false),    /* pcrel_offset  */
1936
1937
  /* Relax hint for conditional branch sequence.  */
1938
  HOWTO2 (R_NDS32_LONGJUMP5,  /* type  */
1939
   0,     /* rightshift  */
1940
   4,     /* size  */
1941
   32,      /* bitsize  */
1942
   false,     /* pc_relative  */
1943
   0,     /* bitpos  */
1944
   complain_overflow_dont,/* complain_on_overflow  */
1945
   nds32_elf_ignore_reloc,/* special_function  */
1946
   "R_NDS32_LONGJUMP5", /* name  */
1947
   false,     /* partial_inplace  */
1948
   0xffffffff,    /* src_mask  */
1949
   0xffffffff,    /* dst_mask  */
1950
   false),    /* pcrel_offset  */
1951
1952
  /* Relax hint for conditional branch sequence.  */
1953
  HOWTO2 (R_NDS32_LONGJUMP6,  /* type  */
1954
   0,     /* rightshift  */
1955
   4,     /* size  */
1956
   32,      /* bitsize  */
1957
   false,     /* pc_relative  */
1958
   0,     /* bitpos  */
1959
   complain_overflow_dont,/* complain_on_overflow  */
1960
   nds32_elf_ignore_reloc,/* special_function  */
1961
   "R_NDS32_LONGJUMP6", /* name  */
1962
   false,     /* partial_inplace  */
1963
   0xffffffff,    /* src_mask  */
1964
   0xffffffff,    /* dst_mask  */
1965
   false),    /* pcrel_offset  */
1966
1967
  /* Relax hint for conditional branch sequence.  */
1968
  HOWTO2 (R_NDS32_LONGJUMP7,  /* type  */
1969
   0,     /* rightshift  */
1970
   4,     /* size  */
1971
   32,      /* bitsize  */
1972
   false,     /* pc_relative  */
1973
   0,     /* bitpos  */
1974
   complain_overflow_dont,/* complain_on_overflow  */
1975
   nds32_elf_ignore_reloc,/* special_function  */
1976
   "R_NDS32_LONGJUMP7", /* name  */
1977
   false,     /* partial_inplace  */
1978
   0xffffffff,    /* src_mask  */
1979
   0xffffffff,    /* dst_mask  */
1980
   false),    /* pcrel_offset  */
1981
1982
  EMPTY_HOWTO (114),
1983
1984
  HOWTO2 (R_NDS32_TLS_IE_LO12,  /* type  */
1985
   0,     /* rightshift  */
1986
   4,     /* size  */
1987
   12,      /* bitsize  */
1988
   false,     /* pc_relative  */
1989
   0,     /* bitpos  */
1990
   complain_overflow_dont,/* complain_on_overflow  */
1991
   bfd_elf_generic_reloc, /* special_function  */
1992
   "R_NDS32_TLS_IE_LO12", /* name  */
1993
   false,     /* partial_inplace  */
1994
   0x00000fff,    /* src_mask  */
1995
   0x00000fff,    /* dst_mask  */
1996
   false),    /* pcrel_offset  */
1997
1998
  /* Like R_NDS32_HI20, but referring to the TLS IE (PIE)
1999
     entry for the symbol.  */
2000
  HOWTO2 (R_NDS32_TLS_IEGP_HI20,/* type  */
2001
   12,      /* rightshift  */
2002
   4,     /* size  */
2003
   20,      /* bitsize  */
2004
   false,     /* pc_relative  */
2005
   0,     /* bitpos  */
2006
   complain_overflow_dont,/* complain_on_overflow  */
2007
   bfd_elf_generic_reloc, /* special_function  */
2008
   "R_NDS32_TLS_IEGP_HI20",/* name  */
2009
   false,     /* partial_inplace  */
2010
   0x000fffff,    /* src_mask  */
2011
   0x000fffff,    /* dst_mask  */
2012
   false),    /* pcrel_offset  */
2013
2014
  HOWTO2 (R_NDS32_TLS_IEGP_LO12,/* type  */
2015
   0,     /* rightshift  */
2016
   4,     /* size  */
2017
   12,      /* bitsize  */
2018
   false,     /* pc_relative  */
2019
   0,     /* bitpos  */
2020
   complain_overflow_dont,/* complain_on_overflow  */
2021
   bfd_elf_generic_reloc, /* special_function  */
2022
   "R_NDS32_TLS_IEGP_LO12",/* name  */
2023
   false,     /* partial_inplace  */
2024
   0x00000fff,    /* src_mask  */
2025
   0x00000fff,    /* dst_mask  */
2026
   false),    /* pcrel_offset  */
2027
2028
  HOWTO2 (R_NDS32_TLS_IEGP_LO12S2,/* type  */
2029
   2,     /* rightshift  */
2030
   4,     /* size  */
2031
   10,      /* bitsize  */
2032
   false,     /* pc_relative  */
2033
   0,     /* bitpos  */
2034
   complain_overflow_dont,/* complain_on_overflow  */
2035
   bfd_elf_generic_reloc, /* special_function  */
2036
   "R_NDS32_TLS_IEGP_LO12S2",/* name  */
2037
   false,     /* partial_inplace  */
2038
   0x000003ff,    /* src_mask  */
2039
   0x000003ff,    /* dst_mask  */
2040
   false),    /* pcrel_offset  */
2041
2042
  /* TLS description relocation  */
2043
  HOWTO2 (R_NDS32_TLS_DESC, /* type  */
2044
   12,      /* rightshift  */
2045
   4,     /* size  */
2046
   20,      /* bitsize  */
2047
   false,     /* pc_relative  */
2048
   0,     /* bitpos  */
2049
   complain_overflow_dont,/* complain_on_overflow  */
2050
   nds32_elf_hi20_reloc,  /* special_function  */
2051
   "R_NDS32_TLS_DESC_HI20",/* name  */
2052
   false,     /* partial_inplace  */
2053
   0x000fffff,    /* src_mask  */
2054
   0x000fffff,    /* dst_mask  */
2055
   false),    /* pcrel_offset  */
2056
2057
  /* TLS GD/LD description offset high part.  */
2058
  HOWTO2 (R_NDS32_TLS_DESC_HI20,/* type  */
2059
   12,      /* rightshift  */
2060
   4,     /* size  */
2061
   20,      /* bitsize  */
2062
   false,     /* pc_relative  */
2063
   0,     /* bitpos  */
2064
   complain_overflow_dont,/* complain_on_overflow  */
2065
   nds32_elf_hi20_reloc,  /* special_function  */
2066
   "R_NDS32_TLS_DESC_HI20",/* name  */
2067
   false,     /* partial_inplace  */
2068
   0x000fffff,    /* src_mask  */
2069
   0x000fffff,    /* dst_mask  */
2070
   false),    /* pcrel_offset  */
2071
2072
  /* TLS GD/LD description offset low part.  */
2073
  HOWTO2 (R_NDS32_TLS_DESC_LO12,/* type  */
2074
   0,     /* rightshift  */
2075
   4,     /* size  */
2076
   12,      /* bitsize  */
2077
   false,     /* pc_relative  */
2078
   0,     /* bitpos  */
2079
   complain_overflow_dont,/* complain_on_overflow  */
2080
   nds32_elf_lo12_reloc,  /* special_function  */
2081
   "R_NDS32_TLS_DESC_LO12",/* name  */
2082
   false,     /* partial_inplace  */
2083
   0x00000fff,    /* src_mask  */
2084
   0x00000fff,    /* dst_mask  */
2085
   false),    /* pcrel_offset  */
2086
2087
  /* TLS GD/LD description offset set (movi).  */
2088
  HOWTO2 (R_NDS32_TLS_DESC_20,  /* type  */
2089
   0,     /* rightshift  */
2090
   4,     /* size  */
2091
   20,      /* bitsize  */
2092
   false,     /* pc_relative  */
2093
   0,     /* bitpos  */
2094
   complain_overflow_signed,/* complain_on_overflow  */
2095
   bfd_elf_generic_reloc, /* special_function  */
2096
   "R_NDS32_TLS_DESC_20", /* name  */
2097
   false,     /* partial_inplace  */
2098
   0x000fffff,    /* src_mask  */
2099
   0x000fffff,    /* dst_mask  */
2100
   false),    /* pcrel_offset  */
2101
2102
  /* TLS GD/LD description offset set (lwi.gp).  */
2103
  HOWTO2 (R_NDS32_TLS_DESC_SDA17S2,/* type  */
2104
   2,     /* rightshift  */
2105
   4,     /* size  */
2106
   17,      /* bitsize  */
2107
   false,     /* pc_relative  */
2108
   0,     /* bitpos  */
2109
   complain_overflow_signed,/* complain_on_overflow  */
2110
   bfd_elf_generic_reloc, /* special_function  */
2111
   "R_NDS32_TLS_DESC_SDA17S2",/* name  */
2112
   false,     /* partial_inplace  */
2113
   0x0001ffff,    /* src_mask  */
2114
   0x0001ffff,    /* dst_mask  */
2115
   false),    /* pcrel_offset  */
2116
};
2117
2118
/* Relocations used for relaxation.  */
2119
#define HOWTO3(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC) \
2120
  [C-R_NDS32_RELAX_ENTRY] = HOWTO(C, R, S, B, P, BI, O, SF, NAME, INPLACE, MASKSRC, MASKDST, PC)
2121
2122
static reloc_howto_type nds32_elf_relax_howto_table[] = {
2123
  HOWTO3 (R_NDS32_RELAX_ENTRY,  /* type  */
2124
   0,     /* rightshift  */
2125
   4,     /* size  */
2126
   32,      /* bitsize  */
2127
   false,     /* pc_relative  */
2128
   0,     /* bitpos  */
2129
   complain_overflow_dont,/* complain_on_overflow  */
2130
   nds32_elf_ignore_reloc,/* special_function  */
2131
   "R_NDS32_RELAX_ENTRY", /* name  */
2132
   false,     /* partial_inplace  */
2133
   0xffffffff,    /* src_mask  */
2134
   0xffffffff,    /* dst_mask  */
2135
   false),    /* pcrel_offset  */
2136
2137
  HOWTO3 (R_NDS32_GOT_SUFF, /* type  */
2138
   0,     /* rightshift  */
2139
   4,     /* size  */
2140
   32,      /* bitsize  */
2141
   false,     /* pc_relative  */
2142
   0,     /* bitpos  */
2143
   complain_overflow_dont,/* complain_on_overflow  */
2144
   nds32_elf_ignore_reloc,/* special_function  */
2145
   "R_NDS32_GOT_SUFF",  /* name  */
2146
   false,     /* partial_inplace  */
2147
   0xffffffff,    /* src_mask  */
2148
   0xffffffff,    /* dst_mask  */
2149
   false),    /* pcrel_offset  */
2150
2151
  HOWTO3 (R_NDS32_GOTOFF_SUFF,  /* type  */
2152
   0,     /* rightshift  */
2153
   4,     /* size  */
2154
   32,      /* bitsize  */
2155
   false,     /* pc_relative  */
2156
   0,     /* bitpos  */
2157
   complain_overflow_bitfield,/* complain_on_overflow  */
2158
   nds32_elf_ignore_reloc,/* special_function  */
2159
   "R_NDS32_GOTOFF_SUFF", /* name  */
2160
   false,     /* partial_inplace  */
2161
   0xffffffff,    /* src_mask  */
2162
   0xffffffff,    /* dst_mask  */
2163
   false),    /* pcrel_offset  */
2164
2165
  HOWTO3 (R_NDS32_PLT_GOT_SUFF, /* type  */
2166
   0,     /* rightshift  */
2167
   4,     /* size  */
2168
   32,      /* bitsize  */
2169
   false,     /* pc_relative  */
2170
   0,     /* bitpos  */
2171
   complain_overflow_dont,/* complain_on_overflow  */
2172
   nds32_elf_ignore_reloc,/* special_function  */
2173
   "R_NDS32_PLT_GOT_SUFF",/* name  */
2174
   false,     /* partial_inplace  */
2175
   0xffffffff,    /* src_mask  */
2176
   0xffffffff,    /* dst_mask  */
2177
   false),    /* pcrel_offset  */
2178
2179
  HOWTO3 (R_NDS32_MULCALL_SUFF, /* type  */
2180
   0,     /* rightshift  */
2181
   4,     /* size  */
2182
   32,      /* bitsize  */
2183
   false,     /* pc_relative  */
2184
   0,     /* bitpos  */
2185
   complain_overflow_dont,/* complain_on_overflow  */
2186
   nds32_elf_ignore_reloc,/* special_function  */
2187
   "R_NDS32_MULCALL_SUFF",/* name  */
2188
   false,     /* partial_inplace  */
2189
   0xffffffff,    /* src_mask  */
2190
   0xffffffff,    /* dst_mask  */
2191
   false),    /* pcrel_offset  */
2192
2193
  HOWTO3 (R_NDS32_PTR,    /* type  */
2194
   0,     /* rightshift  */
2195
   4,     /* size  */
2196
   32,      /* bitsize  */
2197
   false,     /* pc_relative  */
2198
   0,     /* bitpos  */
2199
   complain_overflow_dont,/* complain_on_overflow  */
2200
   nds32_elf_ignore_reloc,/* special_function  */
2201
   "R_NDS32_PTR",   /* name  */
2202
   false,     /* partial_inplace  */
2203
   0xffffffff,    /* src_mask  */
2204
   0xffffffff,    /* dst_mask  */
2205
   false),    /* pcrel_offset  */
2206
2207
  HOWTO3 (R_NDS32_PTR_COUNT,  /* type  */
2208
   0,     /* rightshift  */
2209
   4,     /* size  */
2210
   32,      /* bitsize  */
2211
   false,     /* pc_relative  */
2212
   0,     /* bitpos  */
2213
   complain_overflow_dont,/* complain_on_overflow  */
2214
   nds32_elf_ignore_reloc,/* special_function  */
2215
   "R_NDS32_PTR_COUNT", /* name  */
2216
   false,     /* partial_inplace  */
2217
   0xffffffff,    /* src_mask  */
2218
   0xffffffff,    /* dst_mask  */
2219
   false),    /* pcrel_offset  */
2220
2221
  HOWTO3 (R_NDS32_PTR_RESOLVED, /* type  */
2222
   0,     /* rightshift  */
2223
   4,     /* size  */
2224
   32,      /* bitsize  */
2225
   false,     /* pc_relative  */
2226
   0,     /* bitpos  */
2227
   complain_overflow_dont,/* complain_on_overflow  */
2228
   nds32_elf_ignore_reloc,/* special_function  */
2229
   "R_NDS32_PTR_RESOLVED",/* name  */
2230
   false,     /* partial_inplace  */
2231
   0xffffffff,    /* src_mask  */
2232
   0xffffffff,    /* dst_mask  */
2233
   false),    /* pcrel_offset  */
2234
2235
  HOWTO3 (R_NDS32_PLTBLOCK, /* type  */
2236
   0,     /* rightshift  */
2237
   4,     /* size  */
2238
   32,      /* bitsize  */
2239
   false,     /* pc_relative  */
2240
   0,     /* bitpos  */
2241
   complain_overflow_dont,/* complain_on_overflow  */
2242
   nds32_elf_ignore_reloc,/* special_function  */
2243
   "R_NDS32_PLTBLOCK",  /* name  */
2244
   false,     /* partial_inplace  */
2245
   0xffffffff,    /* src_mask  */
2246
   0xffffffff,    /* dst_mask  */
2247
   false),    /* pcrel_offset  */
2248
2249
  HOWTO3 (R_NDS32_RELAX_REGION_BEGIN,/* type  */
2250
   0,     /* rightshift  */
2251
   4,     /* size  */
2252
   32,      /* bitsize  */
2253
   false,     /* pc_relative  */
2254
   0,     /* bitpos  */
2255
   complain_overflow_dont,/* complain_on_overflow  */
2256
   nds32_elf_ignore_reloc,/* special_function  */
2257
   "R_NDS32_RELAX_REGION_BEGIN",/* name  */
2258
   false,     /* partial_inplace  */
2259
   0xffffffff,    /* src_mask  */
2260
   0xffffffff,    /* dst_mask  */
2261
   false),    /* pcrel_offset  */
2262
2263
  HOWTO3 (R_NDS32_RELAX_REGION_END,/* type  */
2264
   0,     /* rightshift  */
2265
   4,     /* size  */
2266
   32,      /* bitsize  */
2267
   false,     /* pc_relative  */
2268
   0,     /* bitpos  */
2269
   complain_overflow_dont,/* complain_on_overflow  */
2270
   nds32_elf_ignore_reloc,/* special_function  */
2271
   "R_NDS32_RELAX_REGION_END",/* name  */
2272
   false,     /* partial_inplace  */
2273
   0xffffffff,    /* src_mask  */
2274
   0xffffffff,    /* dst_mask  */
2275
   false),    /* pcrel_offset  */
2276
2277
  HOWTO3 (R_NDS32_MINUEND,  /* type  */
2278
   0,     /* rightshift  */
2279
   4,     /* size  */
2280
   32,      /* bitsize  */
2281
   false,     /* pc_relative  */
2282
   0,     /* bitpos  */
2283
   complain_overflow_dont,/* complain_on_overflow  */
2284
   nds32_elf_ignore_reloc,/* special_function  */
2285
   "R_NDS32_MINUEND", /* name  */
2286
   false,     /* partial_inplace  */
2287
   0xffffffff,    /* src_mask  */
2288
   0xffffffff,    /* dst_mask  */
2289
   false),    /* pcrel_offset  */
2290
2291
  HOWTO3 (R_NDS32_SUBTRAHEND, /* type  */
2292
   0,     /* rightshift  */
2293
   4,     /* size  */
2294
   32,      /* bitsize  */
2295
   false,     /* pc_relative  */
2296
   0,     /* bitpos  */
2297
   complain_overflow_dont,/* complain_on_overflow  */
2298
   nds32_elf_ignore_reloc,/* special_function  */
2299
   "R_NDS32_SUBTRAHEND",  /* name  */
2300
   false,     /* partial_inplace  */
2301
   0xffffffff,    /* src_mask  */
2302
   0xffffffff,    /* dst_mask  */
2303
   false),    /* pcrel_offset  */
2304
2305
  HOWTO3 (R_NDS32_DIFF8,  /* type  */
2306
   0,     /* rightshift  */
2307
   1,     /* size  */
2308
   8,     /* bitsize  */
2309
   false,     /* pc_relative  */
2310
   0,     /* bitpos  */
2311
   complain_overflow_dont,/* complain_on_overflow  */
2312
   nds32_elf_ignore_reloc,/* special_function  */
2313
   "R_NDS32_DIFF8", /* name  */
2314
   false,     /* partial_inplace  */
2315
   0x000000ff,    /* src_mask  */
2316
   0x000000ff,    /* dst_mask  */
2317
   false),    /* pcrel_offset  */
2318
2319
  HOWTO3 (R_NDS32_DIFF16, /* type  */
2320
   0,     /* rightshift  */
2321
   2,     /* size  */
2322
   16,      /* bitsize  */
2323
   false,     /* pc_relative  */
2324
   0,     /* bitpos  */
2325
   complain_overflow_dont,/* complain_on_overflow  */
2326
   nds32_elf_ignore_reloc,/* special_function  */
2327
   "R_NDS32_DIFF16",  /* name  */
2328
   false,     /* partial_inplace  */
2329
   0x0000ffff,    /* src_mask  */
2330
   0x0000ffff,    /* dst_mask  */
2331
   false),    /* pcrel_offset  */
2332
2333
  HOWTO3 (R_NDS32_DIFF32, /* type  */
2334
   0,     /* rightshift  */
2335
   4,     /* size  */
2336
   32,      /* bitsize  */
2337
   false,     /* pc_relative  */
2338
   0,     /* bitpos  */
2339
   complain_overflow_dont,/* complain_on_overflow  */
2340
   nds32_elf_ignore_reloc,/* special_function  */
2341
   "R_NDS32_DIFF32",  /* name  */
2342
   false,     /* partial_inplace  */
2343
   0xffffffff,    /* src_mask  */
2344
   0xffffffff,    /* dst_mask  */
2345
   false),    /* pcrel_offset  */
2346
2347
  HOWTO3 (R_NDS32_DIFF_ULEB128, /* type  */
2348
   0,     /* rightshift  */
2349
   1,     /* size  */
2350
   0,     /* bitsize  */
2351
   false,     /* pc_relative  */
2352
   0,     /* bitpos  */
2353
   complain_overflow_dont,/* complain_on_overflow  */
2354
   nds32_elf_ignore_reloc,/* special_function  */
2355
   "R_NDS32_DIFF_ULEB128",/* name  */
2356
   false,     /* partial_inplace  */
2357
   0xffffffff,    /* src_mask  */
2358
   0xffffffff,    /* dst_mask  */
2359
   false),    /* pcrel_offset  */
2360
2361
  HOWTO3 (R_NDS32_DATA,   /* type  */
2362
   0,     /* rightshift  */
2363
   4,     /* size  */
2364
   32,      /* bitsize  */
2365
   false,     /* pc_relative  */
2366
   0,     /* bitpos  */
2367
   complain_overflow_dont,/* complain_on_overflow  */
2368
   nds32_elf_ignore_reloc,/* special_function  */
2369
   "R_NDS32_DATA",  /* name  */
2370
   false,     /* partial_inplace  */
2371
   0xffffffff,    /* src_mask  */
2372
   0xffffffff,    /* dst_mask  */
2373
   false),    /* pcrel_offset  */
2374
2375
  HOWTO3 (R_NDS32_TRAN,   /* type  */
2376
   0,     /* rightshift  */
2377
   4,     /* size  */
2378
   32,      /* bitsize  */
2379
   false,     /* pc_relative  */
2380
   0,     /* bitpos  */
2381
   complain_overflow_dont,/* complain_on_overflow  */
2382
   nds32_elf_ignore_reloc,/* special_function  */
2383
   "R_NDS32_TRAN",  /* name  */
2384
   false,     /* partial_inplace  */
2385
   0xffffffff,    /* src_mask  */
2386
   0xffffffff,    /* dst_mask  */
2387
   false),    /* pcrel_offset  */
2388
2389
  HOWTO3 (R_NDS32_TLS_LE_ADD, /* type  */
2390
   0,     /* rightshift  */
2391
   4,     /* size  */
2392
   32,      /* bitsize  */
2393
   false,     /* pc_relative  */
2394
   0,     /* bitpos  */
2395
   complain_overflow_dont,/* complain_on_overflow  */
2396
   nds32_elf_ignore_reloc,/* special_function  */
2397
   "R_NDS32_TLS_LE_ADD",  /* name  */
2398
   false,     /* partial_inplace  */
2399
   0xffffffff,    /* src_mask  */
2400
   0xffffffff,    /* dst_mask  */
2401
   false),    /* pcrel_offset  */
2402
2403
  HOWTO3 (R_NDS32_TLS_LE_LS,  /* type  */
2404
   0,     /* rightshift  */
2405
   4,     /* size  */
2406
   32,      /* bitsize  */
2407
   false,     /* pc_relative  */
2408
   0,     /* bitpos  */
2409
   complain_overflow_dont,/* complain_on_overflow  */
2410
   nds32_elf_ignore_reloc,/* special_function  */
2411
   "R_NDS32_TLS_LE_LS", /* name  */
2412
   false,     /* partial_inplace  */
2413
   0xffffffff,    /* src_mask  */
2414
   0xffffffff,    /* dst_mask  */
2415
   false),    /* pcrel_offset  */
2416
2417
  HOWTO3 (R_NDS32_EMPTY,  /* type  */
2418
   0,     /* rightshift  */
2419
   4,     /* size  */
2420
   32,      /* bitsize  */
2421
   false,     /* pc_relative  */
2422
   0,     /* bitpos  */
2423
   complain_overflow_dont,/* complain_on_overflow  */
2424
   nds32_elf_ignore_reloc,/* special_function  */
2425
   "R_NDS32_EMPTY", /* name  */
2426
   false,     /* partial_inplace  */
2427
   0xffffffff,    /* src_mask  */
2428
   0xffffffff,    /* dst_mask  */
2429
   false),    /* pcrel_offset  */
2430
2431
  /* TLS GD/LD description address base addition.  */
2432
  HOWTO3 (R_NDS32_TLS_DESC_ADD, /* type  */
2433
   0,     /* rightshift  */
2434
   4,     /* size  */
2435
   32,      /* bitsize  */
2436
   false,     /* pc_relative  */
2437
   0,     /* bitpos  */
2438
   complain_overflow_dont,/* complain_on_overflow  */
2439
   nds32_elf_ignore_reloc,/* special_function  */
2440
   "R_NDS32_TLS_DESC_ADD",/* name  */
2441
   false,     /* partial_inplace  */
2442
   0xffffffff,    /* src_mask  */
2443
   0xffffffff,    /* dst_mask  */
2444
   false),    /* pcrel_offset  */
2445
2446
  /* TLS GD/LD description function load.  */
2447
  HOWTO3 (R_NDS32_TLS_DESC_FUNC,/* type  */
2448
   0,     /* rightshift  */
2449
   4,     /* size  */
2450
   32,      /* bitsize  */
2451
   false,     /* pc_relative  */
2452
   0,     /* bitpos  */
2453
   complain_overflow_dont,/* complain_on_overflow  */
2454
   nds32_elf_ignore_reloc,/* special_function  */
2455
   "R_NDS32_TLS_DESC_FUNC",/* name  */
2456
   false,     /* partial_inplace  */
2457
   0xffffffff,    /* src_mask  */
2458
   0xffffffff,    /* dst_mask  */
2459
   false),    /* pcrel_offset  */
2460
2461
  /* TLS DESC resolve function call.  */
2462
  HOWTO3 (R_NDS32_TLS_DESC_CALL,/* type  */
2463
   0,     /* rightshift  */
2464
   4,     /* size  */
2465
   32,      /* bitsize  */
2466
   false,     /* pc_relative  */
2467
   0,     /* bitpos  */
2468
   complain_overflow_dont,/* complain_on_overflow  */
2469
   nds32_elf_ignore_reloc,/* special_function  */
2470
   "R_NDS32_TLS_DESC_CALL",/* name  */
2471
   false,     /* partial_inplace  */
2472
   0xffffffff,    /* src_mask  */
2473
   0xffffffff,    /* dst_mask  */
2474
   false),    /* pcrel_offset  */
2475
2476
  /* TLS DESC variable access.  */
2477
  HOWTO3 (R_NDS32_TLS_DESC_MEM, /* type  */
2478
   0,     /* rightshift  */
2479
   4,     /* size  */
2480
   32,      /* bitsize  */
2481
   false,     /* pc_relative  */
2482
   0,     /* bitpos  */
2483
   complain_overflow_dont,/* complain_on_overflow  */
2484
   nds32_elf_ignore_reloc,/* special_function  */
2485
   "R_NDS32_TLS_DESC_MEM",/* name  */
2486
   false,     /* partial_inplace  */
2487
   0xffffffff,    /* src_mask  */
2488
   0xffffffff,    /* dst_mask  */
2489
   false),    /* pcrel_offset  */
2490
2491
  /* TLS GD/LD description mark (@tlsdec).  */
2492
  HOWTO3 (R_NDS32_RELAX_REMOVE, /* type  */
2493
   0,     /* rightshift  */
2494
   4,     /* size  */
2495
   32,      /* bitsize  */
2496
   false,     /* pc_relative  */
2497
   0,     /* bitpos  */
2498
   complain_overflow_dont,/* complain_on_overflow  */
2499
   nds32_elf_ignore_reloc,/* special_function  */
2500
   "R_NDS32_REMOVE",  /* name  */
2501
   false,     /* partial_inplace  */
2502
   0xffffffff,    /* src_mask  */
2503
   0xffffffff,    /* dst_mask  */
2504
   false),    /* pcrel_offset  */
2505
2506
  /* TLS GD/LD description mark (@tlsdec).  */
2507
  HOWTO3 (R_NDS32_RELAX_GROUP,  /* type  */
2508
   0,     /* rightshift  */
2509
   4,     /* size  */
2510
   32,      /* bitsize  */
2511
   false,     /* pc_relative  */
2512
   0,     /* bitpos  */
2513
   complain_overflow_dont,/* complain_on_overflow  */
2514
   nds32_elf_ignore_reloc,/* special_function  */
2515
   "R_NDS32_GROUP", /* name  */
2516
   false,     /* partial_inplace  */
2517
   0xffffffff,    /* src_mask  */
2518
   0xffffffff,    /* dst_mask  */
2519
   false),    /* pcrel_offset  */
2520
2521
  HOWTO3 (R_NDS32_TLS_IEGP_LW,  /* type  */
2522
   0,     /* rightshift  */
2523
   4,     /* size  */
2524
   32,      /* bitsize  */
2525
   false,     /* pc_relative  */
2526
   0,     /* bitpos  */
2527
   complain_overflow_dont,/* complain_on_overflow  */
2528
   nds32_elf_ignore_reloc,/* special_function  */
2529
   "R_NDS32_TLS_IEGP_LW", /* name  */
2530
   false,     /* partial_inplace  */
2531
   0xffffffff,    /* src_mask  */
2532
   0xffffffff,    /* dst_mask  */
2533
   false),    /* pcrel_offset  */
2534
2535
  /* LA and FLSI relaxation.  */
2536
  HOWTO3 (R_NDS32_LSI,    /* type  */
2537
   0,     /* rightshift  */
2538
   4,     /* size  */
2539
   32,      /* bitsize  */
2540
   false,     /* pc_relative  */
2541
   0,     /* bitpos  */
2542
   complain_overflow_dont,/* complain_on_overflow  */
2543
   nds32_elf_ignore_reloc,/* special_function  */
2544
   "R_NDS32_LSI",   /* name  */
2545
   false,     /* partial_inplace  */
2546
   0xffffffff,    /* src_mask  */
2547
   0xffffffff,    /* dst_mask  */
2548
   false),
2549
};
2550
2551
static unsigned long dl_tlsdesc_lazy_trampoline[] =
2552
{
2553
  0x46200000,     /* sethi $r2,#0x0      */
2554
  0x58210000,     /* ori $r2,$r2,#0x0    */
2555
  0x40217400,     /* add $r2,$r2,$gp     */
2556
  0x04210000,     /* lwi $r2,[$r2+#0x0]  */
2557
  0x46300000,     /* sethi $r3,#0x0      */
2558
  0x58318000,     /* ori $r3,$r3,#0x0    */
2559
  0x4031f400,     /* add $r3,$r3,$gp     */
2560
  0x4a000800,     /* jr $r2              */
2561
};
2562
2563
static void
2564
nds32_put_trampoline (void *contents, const unsigned long *template,
2565
          unsigned count)
2566
0
{
2567
0
  unsigned ix;
2568
2569
0
  for (ix = 0; ix != count; ix++)
2570
0
    {
2571
0
      unsigned long insn = template[ix];
2572
0
      bfd_putb32 (insn, (char *) contents + ix * 4);
2573
0
    }
2574
0
}
2575
2576
/* nds32_insertion_sort sorts an array with nmemb elements of size size.
2577
   This prototype is the same as qsort ().  */
2578
2579
static void
2580
nds32_insertion_sort (void *base, size_t nmemb, size_t size,
2581
          int (*compar) (const void *lhs, const void *rhs))
2582
0
{
2583
0
  char *ptr = (char *) base;
2584
0
  int i, j;
2585
0
  char tmp[sizeof (Elf_Internal_Rela)];
2586
2587
0
  BFD_ASSERT (size <= sizeof (tmp));
2588
2589
  /* If i is less than j, i is inserted before j.
2590
2591
     |---- j ----- i --------------|
2592
      \    / \      /
2593
   sorted   unsorted
2594
   */
2595
2596
0
  for (i = 1; i < (int) nmemb; i++)
2597
0
    {
2598
0
      for (j = (i - 1); j >= 0; j--)
2599
0
  if (compar (ptr + i * size, ptr + j * size) >= 0)
2600
0
    break;
2601
2602
0
      j++;
2603
2604
0
      if (i == j)
2605
0
  continue; /* i is in order.  */
2606
2607
0
      memcpy (tmp, ptr + i * size, size);
2608
0
      memmove (ptr + (j + 1) * size, ptr + j * size, (i - j) * size);
2609
0
      memcpy (ptr + j * size, tmp, size);
2610
0
    }
2611
0
}
2612
2613
/* Sort relocation by r_offset.
2614
2615
   We didn't use qsort () in stdlib, because quick-sort is not a stable sorting
2616
   algorithm.  Relocations at the same r_offset must keep their order.
2617
   For example, RELAX_ENTRY must be the very first relocation entry.
2618
2619
   Currently, this function implements insertion-sort.
2620
2621
   FIXME: If we already sort them in assembler, why bother sort them
2622
    here again?  */
2623
2624
static int
2625
compar_reloc (const void *lhs, const void *rhs)
2626
0
{
2627
0
  const Elf_Internal_Rela *l = (const Elf_Internal_Rela *) lhs;
2628
0
  const Elf_Internal_Rela *r = (const Elf_Internal_Rela *) rhs;
2629
2630
0
  if (l->r_offset > r->r_offset)
2631
0
    return 1;
2632
0
  else if (l->r_offset == r->r_offset)
2633
0
    return 0;
2634
0
  else
2635
0
    return -1;
2636
0
}
2637
2638
/* Functions listed below are only used for old relocs.
2639
     nds32_elf_9_pcrel_reloc
2640
     nds32_elf_do_9_pcrel_reloc
2641
     nds32_elf_hi20_reloc
2642
     nds32_elf_relocate_hi20
2643
     nds32_elf_lo12_reloc
2644
     nds32_elf_sda15_reloc
2645
     nds32_elf_generic_reloc.  */
2646
2647
/* Handle the R_NDS32_9_PCREL & R_NDS32_9_PCREL_RELA reloc.  */
2648
2649
static bfd_reloc_status_type
2650
nds32_elf_9_pcrel_reloc (bfd *       abfd,
2651
       arelent *   reloc_entry,
2652
       asymbol *   symbol,
2653
       void *      data,
2654
       asection *  input_section,
2655
       bfd *       output_bfd,
2656
       char **     error_message ATTRIBUTE_UNUSED)
2657
0
{
2658
  /* This part is from bfd_elf_generic_reloc.  */
2659
0
  if (output_bfd != (bfd *) NULL
2660
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
2661
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
2662
0
    {
2663
0
      reloc_entry->address += input_section->output_offset;
2664
0
      return bfd_reloc_ok;
2665
0
    }
2666
2667
0
  if (output_bfd != NULL)
2668
0
    {
2669
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
2670
0
      return bfd_reloc_continue;
2671
0
    }
2672
2673
0
  return nds32_elf_do_9_pcrel_reloc (abfd, reloc_entry->howto,
2674
0
             input_section,
2675
0
             data, reloc_entry->address,
2676
0
             symbol->section,
2677
0
             (symbol->value
2678
0
              + symbol->section->output_section->vma
2679
0
              + symbol->section->output_offset),
2680
0
             reloc_entry->addend);
2681
0
}
2682
2683
/* Utility to actually perform an R_NDS32_9_PCREL reloc.  */
2684
0
#define N_ONES(n) (((((bfd_vma) 1 << ((n) - 1)) - 1) << 1) | 1)
2685
2686
static bfd_reloc_status_type
2687
nds32_elf_do_9_pcrel_reloc (bfd *               abfd,
2688
          reloc_howto_type *  howto,
2689
          asection *          input_section,
2690
          bfd_byte *          data,
2691
          bfd_vma             offset,
2692
          asection *          symbol_section ATTRIBUTE_UNUSED,
2693
          bfd_vma             symbol_value,
2694
          bfd_vma             addend)
2695
0
{
2696
0
  bfd_signed_vma relocation;
2697
0
  unsigned short x;
2698
0
  bfd_reloc_status_type status;
2699
2700
  /* Sanity check the address (offset in section).  */
2701
0
  if (offset > bfd_get_section_limit (abfd, input_section))
2702
0
    return bfd_reloc_outofrange;
2703
2704
0
  relocation = symbol_value + addend;
2705
  /* Make it pc relative.  */
2706
0
  relocation -= (input_section->output_section->vma
2707
0
     + input_section->output_offset);
2708
  /* These jumps mask off the lower two bits of the current address
2709
     before doing pcrel calculations.  */
2710
0
  relocation -= (offset & -(bfd_vma) 2);
2711
2712
0
  if (relocation < -ACCURATE_8BIT_S1 || relocation >= ACCURATE_8BIT_S1)
2713
0
    status = bfd_reloc_overflow;
2714
0
  else
2715
0
    status = bfd_reloc_ok;
2716
2717
0
  x = bfd_getb16 (data + offset);
2718
2719
0
  relocation >>= howto->rightshift;
2720
0
  relocation <<= howto->bitpos;
2721
0
  x = (x & ~howto->dst_mask)
2722
0
      | (((x & howto->src_mask) + relocation) & howto->dst_mask);
2723
2724
0
  bfd_putb16 ((bfd_vma) x, data + offset);
2725
2726
0
  return status;
2727
0
}
2728
2729
/* Handle the R_NDS32_HI20_[SU]LO relocs.
2730
   HI20_SLO is for the add3 and load/store with displacement instructions.
2731
   HI20 is for the or3 instruction.
2732
   For R_NDS32_HI20_SLO, the lower 16 bits are sign extended when added to
2733
   the high 16 bytes so if the lower 16 bits are negative (bit 15 == 1) then
2734
   we must add one to the high 16 bytes (which will get subtracted off when
2735
   the low 16 bits are added).
2736
   These relocs have to be done in combination with an R_NDS32_LO12 reloc
2737
   because there is a carry from the LO12 to the HI20.  Here we just save
2738
   the information we need; we do the actual relocation when we see the LO12.
2739
   This code is copied from the elf32-mips.c.  We also support an arbitrary
2740
   number of HI20 relocs to be associated with a single LO12 reloc.  The
2741
   assembler sorts the relocs to ensure each HI20 immediately precedes its
2742
   LO12.  However if there are multiple copies, the assembler may not find
2743
   the real LO12 so it picks the first one it finds.  */
2744
2745
struct nds32_hi20
2746
{
2747
  struct nds32_hi20 *next;
2748
  bfd_byte *addr;
2749
  bfd_vma addend;
2750
};
2751
2752
static struct nds32_hi20 *nds32_hi20_list;
2753
2754
static bfd_reloc_status_type
2755
nds32_elf_hi20_reloc (bfd *abfd ATTRIBUTE_UNUSED,
2756
          arelent *reloc_entry,
2757
          asymbol *symbol,
2758
          void *data,
2759
          asection *input_section,
2760
          bfd *output_bfd,
2761
          char **error_message ATTRIBUTE_UNUSED)
2762
0
{
2763
0
  bfd_reloc_status_type ret;
2764
0
  bfd_vma relocation;
2765
0
  struct nds32_hi20 *n;
2766
2767
  /* This part is from bfd_elf_generic_reloc.
2768
     If we're relocating, and this an external symbol, we don't want
2769
     to change anything.  */
2770
0
  if (output_bfd != (bfd *) NULL
2771
0
      && (symbol->flags & BSF_SECTION_SYM) == 0 && reloc_entry->addend == 0)
2772
0
    {
2773
0
      reloc_entry->address += input_section->output_offset;
2774
0
      return bfd_reloc_ok;
2775
0
    }
2776
2777
  /* Sanity check the address (offset in section).  */
2778
0
  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
2779
0
    return bfd_reloc_outofrange;
2780
2781
0
  ret = bfd_reloc_ok;
2782
0
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2783
0
    ret = bfd_reloc_undefined;
2784
2785
0
  if (bfd_is_com_section (symbol->section))
2786
0
    relocation = 0;
2787
0
  else
2788
0
    relocation = symbol->value;
2789
2790
0
  relocation += symbol->section->output_section->vma;
2791
0
  relocation += symbol->section->output_offset;
2792
0
  relocation += reloc_entry->addend;
2793
2794
  /* Save the information, and let LO12 do the actual relocation.  */
2795
0
  n = (struct nds32_hi20 *) bfd_malloc ((bfd_size_type) sizeof *n);
2796
0
  if (n == NULL)
2797
0
    return bfd_reloc_outofrange;
2798
2799
0
  n->addr = (bfd_byte *) data + reloc_entry->address;
2800
0
  n->addend = relocation;
2801
0
  n->next = nds32_hi20_list;
2802
0
  nds32_hi20_list = n;
2803
2804
0
  if (output_bfd != (bfd *) NULL)
2805
0
    reloc_entry->address += input_section->output_offset;
2806
2807
0
  return ret;
2808
0
}
2809
2810
/* Handle an NDS32 ELF HI20 reloc.  */
2811
2812
static void
2813
nds32_elf_relocate_hi20 (bfd *input_bfd ATTRIBUTE_UNUSED,
2814
       int type ATTRIBUTE_UNUSED,
2815
       Elf_Internal_Rela *relhi,
2816
       Elf_Internal_Rela *rello,
2817
       bfd_byte *contents,
2818
       bfd_vma addend)
2819
0
{
2820
0
  unsigned long insn;
2821
0
  bfd_vma addlo;
2822
2823
0
  insn = bfd_getb32 (contents + relhi->r_offset);
2824
2825
0
  addlo = bfd_getb32 (contents + rello->r_offset);
2826
0
  addlo &= 0xfff;
2827
2828
0
  addend += ((insn & 0xfffff) << 20) + addlo;
2829
2830
0
  insn = (insn & 0xfff00000) | ((addend >> 12) & 0xfffff);
2831
0
  bfd_putb32 (insn, contents + relhi->r_offset);
2832
0
}
2833
2834
/* Do an R_NDS32_LO12 relocation.  This is a straightforward 12 bit
2835
   inplace relocation; this function exists in order to do the
2836
   R_NDS32_HI20_[SU]LO relocation described above.  */
2837
2838
static bfd_reloc_status_type
2839
nds32_elf_lo12_reloc (bfd *input_bfd, arelent *reloc_entry, asymbol *symbol,
2840
          void *data, asection *input_section, bfd *output_bfd,
2841
          char **error_message)
2842
0
{
2843
  /* This part is from bfd_elf_generic_reloc.
2844
     If we're relocating, and this an external symbol, we don't want
2845
     to change anything.  */
2846
0
  if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2847
0
      && reloc_entry->addend == 0)
2848
0
    {
2849
0
      reloc_entry->address += input_section->output_offset;
2850
0
      return bfd_reloc_ok;
2851
0
    }
2852
2853
0
  if (nds32_hi20_list != NULL)
2854
0
    {
2855
0
      struct nds32_hi20 *l;
2856
2857
0
      l = nds32_hi20_list;
2858
0
      while (l != NULL)
2859
0
  {
2860
0
    unsigned long insn;
2861
0
    unsigned long val;
2862
0
    unsigned long vallo;
2863
0
    struct nds32_hi20 *next;
2864
2865
    /* Do the HI20 relocation.  Note that we actually don't need
2866
       to know anything about the LO12 itself, except where to
2867
       find the low 12 bits of the addend needed by the LO12.  */
2868
0
    insn = bfd_getb32 (l->addr);
2869
0
    vallo = bfd_getb32 ((bfd_byte *) data + reloc_entry->address);
2870
0
    vallo &= 0xfff;
2871
0
    switch (reloc_entry->howto->type)
2872
0
      {
2873
0
      case R_NDS32_LO12S3:
2874
0
        vallo <<= 3;
2875
0
        break;
2876
2877
0
      case R_NDS32_LO12S2:
2878
0
        vallo <<= 2;
2879
0
        break;
2880
2881
0
      case R_NDS32_LO12S1:
2882
0
        vallo <<= 1;
2883
0
        break;
2884
2885
0
      case R_NDS32_LO12S0:
2886
0
        vallo <<= 0;
2887
0
        break;
2888
0
      }
2889
2890
0
    val = ((insn & 0xfffff) << 12) + vallo;
2891
0
    val += l->addend;
2892
2893
0
    insn = (insn & ~(bfd_vma) 0xfffff) | ((val >> 12) & 0xfffff);
2894
0
    bfd_putb32 ((bfd_vma) insn, l->addr);
2895
2896
0
    next = l->next;
2897
0
    free (l);
2898
0
    l = next;
2899
0
  }
2900
2901
0
      nds32_hi20_list = NULL;
2902
0
    }
2903
2904
  /* Now do the LO12 reloc in the usual way.
2905
     ??? It would be nice to call bfd_elf_generic_reloc here,
2906
     but we have partial_inplace set.  bfd_elf_generic_reloc will
2907
     pass the handling back to bfd_install_relocation which will install
2908
     a section relative addend which is wrong.  */
2909
0
  return nds32_elf_generic_reloc (input_bfd, reloc_entry, symbol, data,
2910
0
          input_section, output_bfd, error_message);
2911
0
}
2912
2913
/* Do generic partial_inplace relocation.
2914
   This is a local replacement for bfd_elf_generic_reloc.  */
2915
2916
static bfd_reloc_status_type
2917
nds32_elf_generic_reloc (bfd *input_bfd, arelent *reloc_entry,
2918
       asymbol *symbol, void *data, asection *input_section,
2919
       bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
2920
0
{
2921
0
  bfd_reloc_status_type ret;
2922
0
  bfd_vma relocation;
2923
0
  bfd_byte *inplace_address;
2924
2925
  /* This part is from bfd_elf_generic_reloc.
2926
     If we're relocating, and this an external symbol, we don't want
2927
     to change anything.  */
2928
0
  if (output_bfd != NULL && (symbol->flags & BSF_SECTION_SYM) == 0
2929
0
      && reloc_entry->addend == 0)
2930
0
    {
2931
0
      reloc_entry->address += input_section->output_offset;
2932
0
      return bfd_reloc_ok;
2933
0
    }
2934
2935
  /* Now do the reloc in the usual way.
2936
     ??? It would be nice to call bfd_elf_generic_reloc here,
2937
     but we have partial_inplace set.  bfd_elf_generic_reloc will
2938
     pass the handling back to bfd_install_relocation which will install
2939
     a section relative addend which is wrong.  */
2940
2941
  /* Sanity check the address (offset in section).  */
2942
0
  if (reloc_entry->address > bfd_get_section_limit (input_bfd, input_section))
2943
0
    return bfd_reloc_outofrange;
2944
2945
0
  ret = bfd_reloc_ok;
2946
0
  if (bfd_is_und_section (symbol->section) && output_bfd == (bfd *) NULL)
2947
0
    ret = bfd_reloc_undefined;
2948
2949
0
  if (bfd_is_com_section (symbol->section) || output_bfd != (bfd *) NULL)
2950
0
    relocation = 0;
2951
0
  else
2952
0
    relocation = symbol->value;
2953
2954
  /* Only do this for a final link.  */
2955
0
  if (output_bfd == (bfd *) NULL)
2956
0
    {
2957
0
      relocation += symbol->section->output_section->vma;
2958
0
      relocation += symbol->section->output_offset;
2959
0
    }
2960
2961
0
  relocation += reloc_entry->addend;
2962
0
  switch (reloc_entry->howto->type)
2963
0
    {
2964
0
    case R_NDS32_LO12S3:
2965
0
      relocation >>= 3;
2966
0
      break;
2967
2968
0
    case R_NDS32_LO12S2:
2969
0
      relocation >>= 2;
2970
0
      break;
2971
2972
0
    case R_NDS32_LO12S1:
2973
0
      relocation >>= 1;
2974
0
      break;
2975
2976
0
    case R_NDS32_LO12S0:
2977
0
    default:
2978
0
      relocation >>= 0;
2979
0
      break;
2980
0
    }
2981
2982
0
  inplace_address = (bfd_byte *) data + reloc_entry->address;
2983
2984
0
#define DOIT(x)           \
2985
0
  x = ((x & ~reloc_entry->howto->dst_mask) |    \
2986
0
  (((x & reloc_entry->howto->src_mask) +  relocation) & \
2987
0
  reloc_entry->howto->dst_mask))
2988
2989
0
  switch (bfd_get_reloc_size (reloc_entry->howto))
2990
0
    {
2991
0
    case 2:
2992
0
      {
2993
0
  short x = bfd_getb16 (inplace_address);
2994
2995
0
  DOIT (x);
2996
0
  bfd_putb16 ((bfd_vma) x, inplace_address);
2997
0
      }
2998
0
      break;
2999
0
    case 4:
3000
0
      {
3001
0
  unsigned long x = bfd_getb32 (inplace_address);
3002
3003
0
  DOIT (x);
3004
0
  bfd_putb32 ((bfd_vma) x, inplace_address);
3005
0
      }
3006
0
      break;
3007
0
    default:
3008
0
      BFD_ASSERT (0);
3009
0
    }
3010
3011
0
  if (output_bfd != (bfd *) NULL)
3012
0
    reloc_entry->address += input_section->output_offset;
3013
3014
0
  return ret;
3015
0
}
3016
3017
/* Handle the R_NDS32_SDA15 reloc.
3018
   This reloc is used to compute the address of objects in the small data area
3019
   and to perform loads and stores from that area.
3020
   The lower 15 bits are sign extended and added to the register specified
3021
   in the instruction, which is assumed to point to _SDA_BASE_.
3022
3023
   Since the lower 15 bits offset is left-shifted 0, 1 or 2 bits depending on
3024
   the access size, this must be taken care of.  */
3025
3026
static bfd_reloc_status_type
3027
nds32_elf_sda15_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3028
           asymbol *symbol, void *data ATTRIBUTE_UNUSED,
3029
           asection *input_section, bfd *output_bfd,
3030
           char **error_message ATTRIBUTE_UNUSED)
3031
0
{
3032
  /* This part is from bfd_elf_generic_reloc.  */
3033
0
  if (output_bfd != (bfd *) NULL
3034
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
3035
0
      && (!reloc_entry->howto->partial_inplace || reloc_entry->addend == 0))
3036
0
    {
3037
0
      reloc_entry->address += input_section->output_offset;
3038
0
      return bfd_reloc_ok;
3039
0
    }
3040
3041
0
  if (output_bfd != NULL)
3042
0
    {
3043
      /* FIXME: See bfd_perform_relocation.  Is this right?  */
3044
0
      return bfd_reloc_continue;
3045
0
    }
3046
3047
  /* FIXME: not sure what to do here yet.  But then again, the linker
3048
     may never call us.  */
3049
0
  abort ();
3050
0
}
3051
3052
/* nds32_elf_ignore_reloc is the special function for
3053
   relocation types which don't need to be relocated
3054
   like relaxation relocation types.
3055
   This function simply return bfd_reloc_ok when it is
3056
   invoked.  */
3057
3058
static bfd_reloc_status_type
3059
nds32_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
3060
      asymbol *symbol ATTRIBUTE_UNUSED,
3061
      void *data ATTRIBUTE_UNUSED, asection *input_section,
3062
      bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
3063
0
{
3064
0
  if (output_bfd != NULL)
3065
0
    reloc_entry->address += input_section->output_offset;
3066
3067
0
  return bfd_reloc_ok;
3068
0
}
3069

3070
3071
/* Map BFD reloc types to NDS32 ELF reloc types.  */
3072
3073
struct nds32_reloc_map_entry
3074
{
3075
  bfd_reloc_code_real_type bfd_reloc_val;
3076
  unsigned char elf_reloc_val;
3077
};
3078
3079
static const struct nds32_reloc_map_entry nds32_reloc_map[] =
3080
{
3081
  {BFD_RELOC_NONE, R_NDS32_NONE},
3082
  {BFD_RELOC_16, R_NDS32_16_RELA},
3083
  {BFD_RELOC_32, R_NDS32_32_RELA},
3084
  {BFD_RELOC_VTABLE_INHERIT, R_NDS32_RELA_GNU_VTINHERIT},
3085
  {BFD_RELOC_VTABLE_ENTRY, R_NDS32_RELA_GNU_VTENTRY},
3086
3087
  {BFD_RELOC_NDS32_20, R_NDS32_20_RELA},
3088
  {BFD_RELOC_NDS32_9_PCREL, R_NDS32_9_PCREL_RELA},
3089
  {BFD_RELOC_NDS32_WORD_9_PCREL, R_NDS32_WORD_9_PCREL_RELA},
3090
  {BFD_RELOC_NDS32_15_PCREL, R_NDS32_15_PCREL_RELA},
3091
  {BFD_RELOC_NDS32_17_PCREL, R_NDS32_17_PCREL_RELA},
3092
  {BFD_RELOC_NDS32_25_PCREL, R_NDS32_25_PCREL_RELA},
3093
  {BFD_RELOC_NDS32_HI20, R_NDS32_HI20_RELA},
3094
  {BFD_RELOC_NDS32_LO12S3, R_NDS32_LO12S3_RELA},
3095
  {BFD_RELOC_NDS32_LO12S2, R_NDS32_LO12S2_RELA},
3096
  {BFD_RELOC_NDS32_LO12S1, R_NDS32_LO12S1_RELA},
3097
  {BFD_RELOC_NDS32_LO12S0, R_NDS32_LO12S0_RELA},
3098
  {BFD_RELOC_NDS32_LO12S0_ORI, R_NDS32_LO12S0_ORI_RELA},
3099
  {BFD_RELOC_NDS32_SDA15S3, R_NDS32_SDA15S3_RELA},
3100
  {BFD_RELOC_NDS32_SDA15S2, R_NDS32_SDA15S2_RELA},
3101
  {BFD_RELOC_NDS32_SDA15S1, R_NDS32_SDA15S1_RELA},
3102
  {BFD_RELOC_NDS32_SDA15S0, R_NDS32_SDA15S0_RELA},
3103
  {BFD_RELOC_NDS32_SDA16S3, R_NDS32_SDA16S3_RELA},
3104
  {BFD_RELOC_NDS32_SDA17S2, R_NDS32_SDA17S2_RELA},
3105
  {BFD_RELOC_NDS32_SDA18S1, R_NDS32_SDA18S1_RELA},
3106
  {BFD_RELOC_NDS32_SDA19S0, R_NDS32_SDA19S0_RELA},
3107
  {BFD_RELOC_NDS32_GOT20, R_NDS32_GOT20},
3108
  {BFD_RELOC_NDS32_9_PLTREL, R_NDS32_9_PLTREL},
3109
  {BFD_RELOC_NDS32_25_PLTREL, R_NDS32_25_PLTREL},
3110
  {BFD_RELOC_NDS32_COPY, R_NDS32_COPY},
3111
  {BFD_RELOC_NDS32_GLOB_DAT, R_NDS32_GLOB_DAT},
3112
  {BFD_RELOC_NDS32_JMP_SLOT, R_NDS32_JMP_SLOT},
3113
  {BFD_RELOC_NDS32_RELATIVE, R_NDS32_RELATIVE},
3114
  {BFD_RELOC_NDS32_GOTOFF, R_NDS32_GOTOFF},
3115
  {BFD_RELOC_NDS32_GOTOFF_HI20, R_NDS32_GOTOFF_HI20},
3116
  {BFD_RELOC_NDS32_GOTOFF_LO12, R_NDS32_GOTOFF_LO12},
3117
  {BFD_RELOC_NDS32_GOTPC20, R_NDS32_GOTPC20},
3118
  {BFD_RELOC_NDS32_GOT_HI20, R_NDS32_GOT_HI20},
3119
  {BFD_RELOC_NDS32_GOT_LO12, R_NDS32_GOT_LO12},
3120
  {BFD_RELOC_NDS32_GOTPC_HI20, R_NDS32_GOTPC_HI20},
3121
  {BFD_RELOC_NDS32_GOTPC_LO12, R_NDS32_GOTPC_LO12},
3122
  {BFD_RELOC_NDS32_INSN16, R_NDS32_INSN16},
3123
  {BFD_RELOC_NDS32_LABEL, R_NDS32_LABEL},
3124
  {BFD_RELOC_NDS32_LONGCALL1, R_NDS32_LONGCALL1},
3125
  {BFD_RELOC_NDS32_LONGCALL2, R_NDS32_LONGCALL2},
3126
  {BFD_RELOC_NDS32_LONGCALL3, R_NDS32_LONGCALL3},
3127
  {BFD_RELOC_NDS32_LONGJUMP1, R_NDS32_LONGJUMP1},
3128
  {BFD_RELOC_NDS32_LONGJUMP2, R_NDS32_LONGJUMP2},
3129
  {BFD_RELOC_NDS32_LONGJUMP3, R_NDS32_LONGJUMP3},
3130
  {BFD_RELOC_NDS32_LOADSTORE, R_NDS32_LOADSTORE},
3131
  {BFD_RELOC_NDS32_9_FIXED, R_NDS32_9_FIXED_RELA},
3132
  {BFD_RELOC_NDS32_15_FIXED, R_NDS32_15_FIXED_RELA},
3133
  {BFD_RELOC_NDS32_17_FIXED, R_NDS32_17_FIXED_RELA},
3134
  {BFD_RELOC_NDS32_25_FIXED, R_NDS32_25_FIXED_RELA},
3135
  {BFD_RELOC_NDS32_LONGCALL4, R_NDS32_LONGCALL4},
3136
  {BFD_RELOC_NDS32_LONGCALL5, R_NDS32_LONGCALL5},
3137
  {BFD_RELOC_NDS32_LONGCALL6, R_NDS32_LONGCALL6},
3138
  {BFD_RELOC_NDS32_LONGJUMP4, R_NDS32_LONGJUMP4},
3139
  {BFD_RELOC_NDS32_LONGJUMP5, R_NDS32_LONGJUMP5},
3140
  {BFD_RELOC_NDS32_LONGJUMP6, R_NDS32_LONGJUMP6},
3141
  {BFD_RELOC_NDS32_LONGJUMP7, R_NDS32_LONGJUMP7},
3142
  {BFD_RELOC_NDS32_PLTREL_HI20, R_NDS32_PLTREL_HI20},
3143
  {BFD_RELOC_NDS32_PLTREL_LO12, R_NDS32_PLTREL_LO12},
3144
  {BFD_RELOC_NDS32_PLT_GOTREL_HI20, R_NDS32_PLT_GOTREL_HI20},
3145
  {BFD_RELOC_NDS32_PLT_GOTREL_LO12, R_NDS32_PLT_GOTREL_LO12},
3146
  {BFD_RELOC_NDS32_SDA12S2_DP, R_NDS32_SDA12S2_DP_RELA},
3147
  {BFD_RELOC_NDS32_SDA12S2_SP, R_NDS32_SDA12S2_SP_RELA},
3148
  {BFD_RELOC_NDS32_LO12S2_DP, R_NDS32_LO12S2_DP_RELA},
3149
  {BFD_RELOC_NDS32_LO12S2_SP, R_NDS32_LO12S2_SP_RELA},
3150
  {BFD_RELOC_NDS32_DWARF2_OP1, R_NDS32_DWARF2_OP1_RELA},
3151
  {BFD_RELOC_NDS32_DWARF2_OP2, R_NDS32_DWARF2_OP2_RELA},
3152
  {BFD_RELOC_NDS32_DWARF2_LEB, R_NDS32_DWARF2_LEB_RELA},
3153
  {BFD_RELOC_NDS32_UPDATE_TA, R_NDS32_UPDATE_TA_RELA},
3154
  {BFD_RELOC_NDS32_PLT_GOTREL_LO20, R_NDS32_PLT_GOTREL_LO20},
3155
  {BFD_RELOC_NDS32_PLT_GOTREL_LO15, R_NDS32_PLT_GOTREL_LO15},
3156
  {BFD_RELOC_NDS32_PLT_GOTREL_LO19, R_NDS32_PLT_GOTREL_LO19},
3157
  {BFD_RELOC_NDS32_GOT_LO15, R_NDS32_GOT_LO15},
3158
  {BFD_RELOC_NDS32_GOT_LO19, R_NDS32_GOT_LO19},
3159
  {BFD_RELOC_NDS32_GOTOFF_LO15, R_NDS32_GOTOFF_LO15},
3160
  {BFD_RELOC_NDS32_GOTOFF_LO19, R_NDS32_GOTOFF_LO19},
3161
  {BFD_RELOC_NDS32_GOT15S2, R_NDS32_GOT15S2_RELA},
3162
  {BFD_RELOC_NDS32_GOT17S2, R_NDS32_GOT17S2_RELA},
3163
  {BFD_RELOC_NDS32_5, R_NDS32_5_RELA},
3164
  {BFD_RELOC_NDS32_10_UPCREL, R_NDS32_10_UPCREL_RELA},
3165
  {BFD_RELOC_NDS32_SDA_FP7U2_RELA, R_NDS32_SDA_FP7U2_RELA},
3166
  {BFD_RELOC_NDS32_RELAX_ENTRY, R_NDS32_RELAX_ENTRY},
3167
  {BFD_RELOC_NDS32_GOT_SUFF, R_NDS32_GOT_SUFF},
3168
  {BFD_RELOC_NDS32_GOTOFF_SUFF, R_NDS32_GOTOFF_SUFF},
3169
  {BFD_RELOC_NDS32_PLT_GOT_SUFF, R_NDS32_PLT_GOT_SUFF},
3170
  {BFD_RELOC_NDS32_MULCALL_SUFF, R_NDS32_MULCALL_SUFF},
3171
  {BFD_RELOC_NDS32_PTR, R_NDS32_PTR},
3172
  {BFD_RELOC_NDS32_PTR_COUNT, R_NDS32_PTR_COUNT},
3173
  {BFD_RELOC_NDS32_PTR_RESOLVED, R_NDS32_PTR_RESOLVED},
3174
  {BFD_RELOC_NDS32_PLTBLOCK, R_NDS32_PLTBLOCK},
3175
  {BFD_RELOC_NDS32_RELAX_REGION_BEGIN, R_NDS32_RELAX_REGION_BEGIN},
3176
  {BFD_RELOC_NDS32_RELAX_REGION_END, R_NDS32_RELAX_REGION_END},
3177
  {BFD_RELOC_NDS32_MINUEND, R_NDS32_MINUEND},
3178
  {BFD_RELOC_NDS32_SUBTRAHEND, R_NDS32_SUBTRAHEND},
3179
  {BFD_RELOC_NDS32_DIFF8, R_NDS32_DIFF8},
3180
  {BFD_RELOC_NDS32_DIFF16, R_NDS32_DIFF16},
3181
  {BFD_RELOC_NDS32_DIFF32, R_NDS32_DIFF32},
3182
  {BFD_RELOC_NDS32_DIFF_ULEB128, R_NDS32_DIFF_ULEB128},
3183
  {BFD_RELOC_NDS32_EMPTY, R_NDS32_EMPTY},
3184
  {BFD_RELOC_NDS32_25_ABS, R_NDS32_25_ABS_RELA},
3185
  {BFD_RELOC_NDS32_DATA, R_NDS32_DATA},
3186
  {BFD_RELOC_NDS32_TRAN, R_NDS32_TRAN},
3187
  {BFD_RELOC_NDS32_17IFC_PCREL, R_NDS32_17IFC_PCREL_RELA},
3188
  {BFD_RELOC_NDS32_10IFCU_PCREL, R_NDS32_10IFCU_PCREL_RELA},
3189
  /* Not sure.  */
3190
  {BFD_RELOC_NDS32_TPOFF, R_NDS32_TLS_TPOFF},
3191
  /* Missing: BFD_RELOC_NDS32_GOTTPOFF.  */
3192
  {BFD_RELOC_NDS32_TLS_LE_HI20, R_NDS32_TLS_LE_HI20},
3193
  {BFD_RELOC_NDS32_TLS_LE_LO12, R_NDS32_TLS_LE_LO12},
3194
  {BFD_RELOC_NDS32_TLS_LE_20, R_NDS32_TLS_LE_20},
3195
  {BFD_RELOC_NDS32_TLS_LE_15S0, R_NDS32_TLS_LE_15S0},
3196
  {BFD_RELOC_NDS32_TLS_LE_15S1, R_NDS32_TLS_LE_15S1},
3197
  {BFD_RELOC_NDS32_TLS_LE_15S2, R_NDS32_TLS_LE_15S2},
3198
  {BFD_RELOC_NDS32_TLS_LE_ADD, R_NDS32_TLS_LE_ADD},
3199
  {BFD_RELOC_NDS32_TLS_LE_LS, R_NDS32_TLS_LE_LS},
3200
  {BFD_RELOC_NDS32_TLS_IE_HI20, R_NDS32_TLS_IE_HI20},
3201
  {BFD_RELOC_NDS32_TLS_IE_LO12, R_NDS32_TLS_IE_LO12},
3202
  {BFD_RELOC_NDS32_TLS_IE_LO12S2, R_NDS32_TLS_IE_LO12S2},
3203
  {BFD_RELOC_NDS32_TLS_IEGP_HI20, R_NDS32_TLS_IEGP_HI20},
3204
  {BFD_RELOC_NDS32_TLS_IEGP_LO12, R_NDS32_TLS_IEGP_LO12},
3205
  {BFD_RELOC_NDS32_TLS_IEGP_LO12S2, R_NDS32_TLS_IEGP_LO12S2},
3206
  {BFD_RELOC_NDS32_TLS_IEGP_LW, R_NDS32_TLS_IEGP_LW},
3207
  {BFD_RELOC_NDS32_TLS_DESC, R_NDS32_TLS_DESC},
3208
  {BFD_RELOC_NDS32_TLS_DESC_HI20, R_NDS32_TLS_DESC_HI20},
3209
  {BFD_RELOC_NDS32_TLS_DESC_LO12, R_NDS32_TLS_DESC_LO12},
3210
  {BFD_RELOC_NDS32_TLS_DESC_20, R_NDS32_TLS_DESC_20},
3211
  {BFD_RELOC_NDS32_TLS_DESC_SDA17S2, R_NDS32_TLS_DESC_SDA17S2},
3212
  {BFD_RELOC_NDS32_TLS_DESC_ADD, R_NDS32_TLS_DESC_ADD},
3213
  {BFD_RELOC_NDS32_TLS_DESC_FUNC, R_NDS32_TLS_DESC_FUNC},
3214
  {BFD_RELOC_NDS32_TLS_DESC_CALL, R_NDS32_TLS_DESC_CALL},
3215
  {BFD_RELOC_NDS32_TLS_DESC_MEM, R_NDS32_TLS_DESC_MEM},
3216
  {BFD_RELOC_NDS32_REMOVE, R_NDS32_RELAX_REMOVE},
3217
  {BFD_RELOC_NDS32_GROUP, R_NDS32_RELAX_GROUP},
3218
  {BFD_RELOC_NDS32_LSI, R_NDS32_LSI},
3219
};
3220
3221
/* Patch tag.  */
3222
3223
static reloc_howto_type *
3224
bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3225
         const char *r_name)
3226
0
{
3227
0
  unsigned int i;
3228
3229
0
  for (i = 0; i < ARRAY_SIZE (nds32_elf_howto_table); i++)
3230
0
    if (nds32_elf_howto_table[i].name != NULL
3231
0
  && strcasecmp (nds32_elf_howto_table[i].name, r_name) == 0)
3232
0
      return &nds32_elf_howto_table[i];
3233
3234
0
  for (i = 0; i < ARRAY_SIZE (nds32_elf_relax_howto_table); i++)
3235
0
    if (nds32_elf_relax_howto_table[i].name != NULL
3236
0
  && strcasecmp (nds32_elf_relax_howto_table[i].name, r_name) == 0)
3237
0
      return &nds32_elf_relax_howto_table[i];
3238
3239
0
  return NULL;
3240
0
}
3241
3242
static reloc_howto_type *
3243
bfd_elf32_bfd_reloc_type_table_lookup (unsigned int code)
3244
0
{
3245
0
  if (code < R_NDS32_RELAX_ENTRY)
3246
0
    {
3247
0
      if (code < ARRAY_SIZE (nds32_elf_howto_table))
3248
0
  return &nds32_elf_howto_table[code];
3249
0
    }
3250
0
  else
3251
0
    {
3252
0
      if (code - R_NDS32_RELAX_ENTRY < ARRAY_SIZE (nds32_elf_relax_howto_table))
3253
0
  return &nds32_elf_relax_howto_table[code - R_NDS32_RELAX_ENTRY];
3254
0
    }
3255
0
  return NULL;
3256
0
}
3257
3258
static reloc_howto_type *
3259
bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
3260
         bfd_reloc_code_real_type code)
3261
0
{
3262
0
  unsigned int i;
3263
3264
0
  for (i = 0; i < ARRAY_SIZE (nds32_reloc_map); i++)
3265
0
    {
3266
0
      if (nds32_reloc_map[i].bfd_reloc_val == code)
3267
0
  return bfd_elf32_bfd_reloc_type_table_lookup
3268
0
    (nds32_reloc_map[i].elf_reloc_val);
3269
0
    }
3270
3271
0
  return NULL;
3272
0
}
3273
3274
/* Set the howto pointer for an NDS32 ELF reloc.  */
3275
3276
static bool
3277
nds32_info_to_howto_rel (bfd *abfd, arelent *cache_ptr,
3278
       Elf_Internal_Rela *dst)
3279
0
{
3280
0
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3281
3282
0
  cache_ptr->howto = NULL;
3283
0
  if (r_type <= R_NDS32_GNU_VTENTRY)
3284
0
    cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3285
0
  if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3286
0
    {
3287
      /* xgettext:c-format */
3288
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3289
0
        abfd, r_type);
3290
0
      bfd_set_error (bfd_error_bad_value);
3291
0
      return false;
3292
0
    }
3293
0
  return true;
3294
0
}
3295
3296
static bool
3297
nds32_info_to_howto (bfd *abfd, arelent *cache_ptr,
3298
         Elf_Internal_Rela *dst)
3299
0
{
3300
0
  unsigned int r_type = ELF32_R_TYPE (dst->r_info);
3301
3302
0
  cache_ptr->howto = NULL;
3303
0
  if (r_type == R_NDS32_NONE
3304
0
      || r_type > R_NDS32_GNU_VTENTRY)
3305
0
    cache_ptr->howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
3306
0
  if (cache_ptr->howto == NULL || cache_ptr->howto->name == NULL)
3307
0
    {
3308
      /* xgettext:c-format */
3309
0
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
3310
0
        abfd, r_type);
3311
0
      bfd_set_error (bfd_error_bad_value);
3312
0
      return false;
3313
0
    }
3314
0
  return true;
3315
0
}
3316
3317
/* Support for core dump NOTE sections.
3318
   Reference to include/linux/elfcore.h in Linux.  */
3319
3320
static bool
3321
nds32_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
3322
0
{
3323
0
  int offset;
3324
0
  size_t size;
3325
3326
0
  switch (note->descsz)
3327
0
    {
3328
0
    case 0x114:
3329
      /* Linux/NDS32 32-bit, ABI1.  */
3330
3331
      /* pr_cursig */
3332
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3333
3334
      /* pr_pid */
3335
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3336
3337
      /* pr_reg */
3338
0
      offset = 72;
3339
0
      size = 200;
3340
0
      break;
3341
3342
0
    case 0xfc:
3343
      /* Linux/NDS32 32-bit.  */
3344
3345
      /* pr_cursig */
3346
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
3347
3348
      /* pr_pid */
3349
0
      elf_tdata (abfd)->core->pid = bfd_get_32 (abfd, note->descdata + 24);
3350
3351
      /* pr_reg */
3352
0
      offset = 72;
3353
0
      size = 176;
3354
0
      break;
3355
3356
0
    default:
3357
0
      return false;
3358
0
    }
3359
3360
  /* Make a ".reg" section.  */
3361
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
3362
0
            size, note->descpos + offset);
3363
0
}
3364
3365
static bool
3366
nds32_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
3367
0
{
3368
0
  switch (note->descsz)
3369
0
    {
3370
0
    case 124:
3371
      /* Linux/NDS32.  */
3372
3373
      /* __kernel_uid_t, __kernel_gid_t are short on NDS32 platform.  */
3374
0
      elf_tdata (abfd)->core->program =
3375
0
  _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
3376
0
      elf_tdata (abfd)->core->command =
3377
0
  _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
3378
0
      break;
3379
3380
0
    default:
3381
0
      return false;
3382
0
    }
3383
3384
  /* Note that for some reason, a spurious space is tacked
3385
     onto the end of the args in some (at least one anyway)
3386
     implementations, so strip it off if it exists.  */
3387
0
  {
3388
0
    char *command = elf_tdata (abfd)->core->command;
3389
0
    int n = strlen (command);
3390
3391
0
    if (0 < n && command[n - 1] == ' ')
3392
0
      command[n - 1] = '\0';
3393
0
  }
3394
3395
0
  return true;
3396
0
}
3397
3398
/* Hook called by the linker routine which adds symbols from an object
3399
   file.  We must handle the special NDS32 section numbers here.
3400
   We also keep watching for whether we need to create the sdata special
3401
   linker sections.  */
3402
3403
static bool
3404
nds32_elf_add_symbol_hook (bfd *abfd,
3405
         struct bfd_link_info *info ATTRIBUTE_UNUSED,
3406
         Elf_Internal_Sym *sym,
3407
         const char **namep ATTRIBUTE_UNUSED,
3408
         flagword *flagsp ATTRIBUTE_UNUSED,
3409
         asection **secp, bfd_vma *valp)
3410
0
{
3411
0
  switch (sym->st_shndx)
3412
0
    {
3413
0
    case SHN_COMMON:
3414
      /* Common symbols less than the GP size are automatically
3415
   treated as SHN_MIPS_SCOMMON symbols.  */
3416
0
      if (sym->st_size > elf_gp_size (abfd)
3417
0
    || ELF_ST_TYPE (sym->st_info) == STT_TLS)
3418
0
  break;
3419
3420
      /* st_value is the alignment constraint.
3421
   That might be its actual size if it is an array or structure.  */
3422
0
      switch (sym->st_value)
3423
0
  {
3424
0
  case 1:
3425
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_b");
3426
0
    break;
3427
0
  case 2:
3428
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_h");
3429
0
    break;
3430
0
  case 4:
3431
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_w");
3432
0
    break;
3433
0
  case 8:
3434
0
    *secp = bfd_make_section_old_way (abfd, ".scommon_d");
3435
0
    break;
3436
0
  default:
3437
0
    return true;
3438
0
  }
3439
3440
0
      (*secp)->flags |= SEC_IS_COMMON | SEC_SMALL_DATA;
3441
0
      *valp = sym->st_size;
3442
0
      break;
3443
0
    }
3444
3445
0
  return true;
3446
0
}
3447
3448
/* This function can figure out the best location for a base register to access
3449
   data relative to this base register
3450
   INPUT:
3451
   sda_d0: size of first DOUBLE WORD data section
3452
   sda_w0: size of first WORD data section
3453
   sda_h0: size of first HALF WORD data section
3454
   sda_b : size of BYTE data section
3455
   sda_hi: size of second HALF WORD data section
3456
   sda_w1: size of second WORD data section
3457
   sda_d1: size of second DOUBLE WORD data section
3458
   OUTPUT:
3459
   offset (always positive) from the beginning of sda_d0 if OK
3460
   a negative error value if fail
3461
   NOTE:
3462
   these 7 sections have to be located back to back if exist
3463
   a pass in 0 value for non-existing section   */
3464
3465
/* Due to the interpretation of simm15 field of load/store depending on
3466
   data accessing size, the organization of base register relative data shall
3467
   like the following figure
3468
   -------------------------------------------
3469
   |  DOUBLE WORD sized data (range +/- 128K)
3470
   -------------------------------------------
3471
   |  WORD sized data (range +/- 64K)
3472
   -------------------------------------------
3473
   |  HALF WORD sized data (range +/- 32K)
3474
   -------------------------------------------
3475
   |  BYTE sized data (range +/- 16K)
3476
   -------------------------------------------
3477
   |  HALF WORD sized data (range +/- 32K)
3478
   -------------------------------------------
3479
   |  WORD sized data (range +/- 64K)
3480
   -------------------------------------------
3481
   |  DOUBLE WORD sized data (range +/- 128K)
3482
   -------------------------------------------
3483
   Its base register shall be set to access these data freely.  */
3484
3485
/* We have to figure out the SDA_BASE value, so that we can adjust the
3486
   symbol value correctly.  We look up the symbol _SDA_BASE_ in the output
3487
   BFD.  If we can't find it, we're stuck.  We cache it in the ELF
3488
   target data.  We don't need to adjust the symbol value for an
3489
   external symbol if we are producing relocatable output.  */
3490
3491
static asection *sda_rela_sec = NULL;
3492
3493
#define SDA_SECTION_NUM 10
3494
3495
static bfd_reloc_status_type
3496
nds32_elf_final_sda_base (bfd *output_bfd,
3497
        struct bfd_link_info *info,
3498
        bfd_vma *psb,
3499
        bool add_symbol)
3500
0
{
3501
0
  int relax_fp_as_gp;
3502
0
  struct elf_nds32_link_hash_table *table;
3503
0
  struct bfd_link_hash_entry *h, *h2;
3504
0
  long unsigned int total = 0;
3505
0
  asection *first = NULL, *final = NULL, *temp;
3506
0
  bfd_vma sda_base = 0;
3507
3508
0
  h = bfd_link_hash_lookup (info->hash, "_SDA_BASE_", false, false, true);
3509
0
  if (!h || (h->type != bfd_link_hash_defined
3510
0
       && h->type != bfd_link_hash_defweak))
3511
0
    {
3512
      /* The first section must be 4-byte aligned to promise _SDA_BASE_ being
3513
   4 byte-aligned.  Therefore, it has to set the first section ".data"
3514
   4 byte-aligned.  */
3515
0
      static const char sec_name[SDA_SECTION_NUM][10] =
3516
0
  {
3517
0
    ".data", ".got", ".sdata_d", ".sdata_w", ".sdata_h", ".sdata_b",
3518
0
    ".sbss_b", ".sbss_h", ".sbss_w", ".sbss_d"
3519
0
  };
3520
0
      size_t i = 0;
3521
3522
0
      if (output_bfd->sections == NULL)
3523
0
  {
3524
0
    *psb = elf_gp (output_bfd);
3525
0
    return bfd_reloc_ok;
3526
0
  }
3527
3528
      /* Get the first and final section.  */
3529
0
      while (i < ARRAY_SIZE (sec_name))
3530
0
  {
3531
0
    temp = bfd_get_section_by_name (output_bfd, sec_name[i]);
3532
0
    if (temp && !first && (temp->size != 0 || temp->rawsize != 0))
3533
0
      first = temp;
3534
0
    if (temp && (temp->size != 0 || temp->rawsize != 0))
3535
0
      final = temp;
3536
3537
    /* Summarize the sections in order to check if joining .bss.  */
3538
0
    if (temp && temp->size != 0)
3539
0
      total += temp->size;
3540
0
    else if (temp && temp->rawsize != 0)
3541
0
      total += temp->rawsize;
3542
3543
0
    i++;
3544
0
  }
3545
3546
      /* Check .bss size.  */
3547
0
      temp = bfd_get_section_by_name (output_bfd, ".bss");
3548
0
      if (temp)
3549
0
  {
3550
0
    if (temp->size != 0)
3551
0
      total += temp->size;
3552
0
    else if (temp->rawsize != 0)
3553
0
      total += temp->rawsize;
3554
3555
0
    if (total < 0x80000)
3556
0
      {
3557
0
        if (!first && (temp->size != 0 || temp->rawsize != 0))
3558
0
    first = temp;
3559
0
        if ((temp->size != 0 || temp->rawsize != 0))
3560
0
    final = temp;
3561
0
      }
3562
0
  }
3563
3564
0
      if (first && final)
3565
0
  {
3566
    /* The middle of data region.  */
3567
0
    sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3568
3569
    /* Find the section sda_base located.  */
3570
0
    i = 0;
3571
0
    while (i < ARRAY_SIZE (sec_name))
3572
0
      {
3573
0
        final = bfd_get_section_by_name (output_bfd, sec_name[i]);
3574
0
        if (final && (final->size != 0 || final->rawsize != 0)
3575
0
      && sda_base >= final->vma)
3576
0
    {
3577
0
      first = final;
3578
0
      i++;
3579
0
    }
3580
0
        else
3581
0
    break;
3582
0
      }
3583
0
  }
3584
0
      else
3585
0
  {
3586
    /* If there is not any default data section in output bfd, try to find
3587
       the first data section.  If no data section be found, just simplily
3588
       choose the first output section.  */
3589
0
    temp = output_bfd->sections;
3590
0
    while (temp)
3591
0
      {
3592
0
        if (temp->flags & SEC_ALLOC
3593
0
      && (((temp->flags & SEC_DATA)
3594
0
           && ((temp->flags & SEC_READONLY) == 0))
3595
0
          || (temp->flags & SEC_LOAD) == 0)
3596
0
      && (temp->size != 0 || temp->rawsize != 0))
3597
0
    {
3598
0
      if (!first)
3599
0
        first = temp;
3600
0
      final = temp;
3601
0
    }
3602
0
        temp = temp->next;
3603
0
      }
3604
3605
    /* There is no data or bss section.  */
3606
0
    if (!first || (first->size == 0 && first->rawsize == 0))
3607
0
      {
3608
0
        first = output_bfd->sections;
3609
0
        while (first && first->size == 0 && first->rawsize == 0)
3610
0
    first = first->next;
3611
0
      }
3612
3613
    /* There is no concrete section.  */
3614
0
    if (!first)
3615
0
      {
3616
0
        *psb = elf_gp (output_bfd);
3617
0
        return bfd_reloc_ok;
3618
0
      }
3619
3620
0
    if (final && (final->vma + final->rawsize - first->vma) <= 0x4000)
3621
0
      sda_base = final->vma / 2 + final->rawsize / 2 + first->vma / 2;
3622
0
    else
3623
0
      sda_base = first->vma + 0x2000;
3624
0
  }
3625
3626
0
      sda_base -= first->vma;
3627
0
      sda_base = sda_base & (~7);
3628
3629
0
      if (!_bfd_generic_link_add_one_symbol
3630
0
    (info, output_bfd, "_SDA_BASE_", BSF_GLOBAL | BSF_WEAK, first,
3631
0
     (bfd_vma) sda_base, (const char *) NULL, false,
3632
0
     get_elf_backend_data (output_bfd)->collect, &h))
3633
0
  return false;
3634
3635
0
      sda_rela_sec = first;
3636
0
    }
3637
3638
  /* Set _FP_BASE_ to _SDA_BASE_.  */
3639
0
  table = nds32_elf_hash_table (info);
3640
0
  relax_fp_as_gp = table->relax_fp_as_gp;
3641
0
  h2 = bfd_link_hash_lookup (info->hash, FP_BASE_NAME, false, false, false);
3642
  /* _SDA_BASE_ is difined in linker script.  */
3643
0
  if (!first)
3644
0
    {
3645
0
      first = h->u.def.section;
3646
0
      sda_base = h->u.def.value;
3647
0
    }
3648
3649
0
  if (relax_fp_as_gp && h2
3650
0
      && (h2->type == bfd_link_hash_undefweak
3651
0
    || h2->type == bfd_link_hash_undefined))
3652
0
    {
3653
      /* Define a weak FP_BASE_NAME here to prevent the undefined symbol.
3654
   And set FP equal to SDA_BASE to do relaxation for
3655
   la $fp, _FP_BASE_.  */
3656
0
      if (!_bfd_generic_link_add_one_symbol
3657
0
    (info, output_bfd, FP_BASE_NAME, BSF_GLOBAL | BSF_WEAK,
3658
0
     first, sda_base, (const char *) NULL,
3659
0
     false, get_elf_backend_data (output_bfd)->collect, &h2))
3660
0
  return false;
3661
0
    }
3662
3663
0
  if (add_symbol)
3664
0
    {
3665
0
      if (h)
3666
0
  {
3667
    /* Now set gp.  */
3668
0
    elf_gp (output_bfd) = (h->u.def.value
3669
0
         + h->u.def.section->output_section->vma
3670
0
         + h->u.def.section->output_offset);
3671
0
  }
3672
0
      else
3673
0
  {
3674
0
    _bfd_error_handler (_("error: can't find symbol: %s"), "_SDA_BASE_");
3675
0
    return bfd_reloc_dangerous;
3676
0
  }
3677
0
    }
3678
3679
0
  *psb = h->u.def.value
3680
0
    + h->u.def.section->output_section->vma
3681
0
    + h->u.def.section->output_offset;
3682
0
  return bfd_reloc_ok;
3683
0
}
3684

3685
3686
/* Return size of a PLT entry.  */
3687
#define elf_nds32_sizeof_plt(info) PLT_ENTRY_SIZE
3688
3689
/* Create an entry in an nds32 ELF linker hash table.  */
3690
3691
static struct bfd_hash_entry *
3692
nds32_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
3693
           struct bfd_hash_table *table,
3694
           const char *string)
3695
0
{
3696
0
  struct elf_nds32_link_hash_entry *ret;
3697
3698
0
  ret = (struct elf_nds32_link_hash_entry *) entry;
3699
3700
  /* Allocate the structure if it has not already been allocated by a
3701
     subclass.  */
3702
0
  if (ret == NULL)
3703
0
    ret = (struct elf_nds32_link_hash_entry *)
3704
0
       bfd_hash_allocate (table, sizeof (struct elf_nds32_link_hash_entry));
3705
3706
0
  if (ret == NULL)
3707
0
    return (struct bfd_hash_entry *) ret;
3708
3709
  /* Call the allocation method of the superclass.  */
3710
0
  ret = (struct elf_nds32_link_hash_entry *)
3711
0
    _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret, table, string);
3712
3713
0
  if (ret != NULL)
3714
0
    {
3715
0
      struct elf_nds32_link_hash_entry *eh;
3716
3717
0
      eh = (struct elf_nds32_link_hash_entry *) ret;
3718
0
      eh->tls_type = GOT_UNKNOWN;
3719
0
      eh->offset_to_gp = 0;
3720
0
    }
3721
3722
0
  return (struct bfd_hash_entry *) ret;
3723
0
}
3724
3725
/* Create an nds32 ELF linker hash table.  */
3726
3727
static struct bfd_link_hash_table *
3728
nds32_elf_link_hash_table_create (bfd *abfd)
3729
0
{
3730
0
  struct elf_nds32_link_hash_table *ret;
3731
3732
0
  size_t amt = sizeof (struct elf_nds32_link_hash_table);
3733
3734
0
  ret = (struct elf_nds32_link_hash_table *) bfd_zmalloc (amt);
3735
0
  if (ret == NULL)
3736
0
    return NULL;
3737
3738
  /* Patch tag.  */
3739
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
3740
0
              nds32_elf_link_hash_newfunc,
3741
0
              sizeof (struct elf_nds32_link_hash_entry),
3742
0
              NDS32_ELF_DATA))
3743
0
    {
3744
0
      free (ret);
3745
0
      return NULL;
3746
0
    }
3747
3748
0
  ret->sym_ld_script = NULL;
3749
3750
0
  return &ret->root.root;
3751
0
}
3752
3753
/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
3754
   shortcuts to them in our hash table.  */
3755
3756
static bool
3757
create_got_section (bfd *dynobj, struct bfd_link_info *info)
3758
0
{
3759
0
  struct elf_link_hash_table *ehtab;
3760
3761
0
  if (!_bfd_elf_create_got_section (dynobj, info))
3762
0
    return false;
3763
3764
0
  ehtab = elf_hash_table (info);
3765
0
  ehtab->sgot = bfd_get_section_by_name (dynobj, ".got");
3766
0
  ehtab->sgotplt = bfd_get_section_by_name (dynobj, ".got.plt");
3767
0
  if (!ehtab->sgot || !ehtab->sgotplt)
3768
0
    abort ();
3769
3770
  /* _bfd_elf_create_got_section will create it for us.  */
3771
0
  ehtab->srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
3772
0
  if (ehtab->srelgot == NULL
3773
0
      || !bfd_set_section_flags (ehtab->srelgot,
3774
0
         (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
3775
0
          | SEC_IN_MEMORY | SEC_LINKER_CREATED
3776
0
          | SEC_READONLY))
3777
0
      || !bfd_set_section_alignment (ehtab->srelgot, 2))
3778
0
    return false;
3779
3780
0
  return true;
3781
0
}
3782
3783
/* Create dynamic sections when linking against a dynamic object.  */
3784
3785
static bool
3786
nds32_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3787
0
{
3788
0
  struct elf_link_hash_table *ehtab;
3789
0
  struct elf_nds32_link_hash_table *htab;
3790
0
  flagword flags, pltflags;
3791
0
  register asection *s;
3792
0
  const struct elf_backend_data *bed;
3793
0
  int ptralign = 2;   /* 32-bit  */
3794
0
  const char *secname;
3795
0
  char *relname;
3796
0
  flagword secflags;
3797
0
  asection *sec;
3798
3799
0
  bed = get_elf_backend_data (abfd);
3800
0
  ehtab = elf_hash_table (info);
3801
0
  htab = nds32_elf_hash_table (info);
3802
3803
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3804
     .rel[a].bss sections.  */
3805
3806
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3807
0
     | SEC_LINKER_CREATED);
3808
3809
0
  pltflags = flags;
3810
0
  pltflags |= SEC_CODE;
3811
0
  if (bed->plt_not_loaded)
3812
0
    pltflags &= ~(SEC_LOAD | SEC_HAS_CONTENTS);
3813
0
  if (bed->plt_readonly)
3814
0
    pltflags |= SEC_READONLY;
3815
3816
0
  s = bfd_make_section (abfd, ".plt");
3817
0
  ehtab->splt = s;
3818
0
  if (s == NULL
3819
0
      || !bfd_set_section_flags (s, pltflags)
3820
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
3821
0
    return false;
3822
3823
0
  if (bed->want_plt_sym)
3824
0
    {
3825
      /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3826
   .plt section.  */
3827
0
      struct bfd_link_hash_entry *bh = NULL;
3828
0
      struct elf_link_hash_entry *h;
3829
3830
0
      if (!(_bfd_generic_link_add_one_symbol
3831
0
      (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
3832
0
       (bfd_vma) 0, (const char *) NULL, false,
3833
0
       get_elf_backend_data (abfd)->collect, &bh)))
3834
0
  return false;
3835
3836
0
      h = (struct elf_link_hash_entry *) bh;
3837
0
      h->def_regular = 1;
3838
0
      h->type = STT_OBJECT;
3839
3840
0
      if (bfd_link_pic (info) && !bfd_elf_link_record_dynamic_symbol (info, h))
3841
0
  return false;
3842
0
    }
3843
3844
0
  s = bfd_make_section (abfd,
3845
0
      bed->default_use_rela_p ? ".rela.plt" : ".rel.plt");
3846
0
  ehtab->srelplt = s;
3847
0
  if (s == NULL
3848
0
      || !bfd_set_section_flags (s, flags | SEC_READONLY)
3849
0
      || !bfd_set_section_alignment (s, ptralign))
3850
0
    return false;
3851
3852
0
  if (ehtab->sgot == NULL && !create_got_section (abfd, info))
3853
0
    return false;
3854
3855
0
  for (sec = abfd->sections; sec; sec = sec->next)
3856
0
    {
3857
0
      secflags = bfd_section_flags (sec);
3858
0
      if ((secflags & (SEC_DATA | SEC_LINKER_CREATED))
3859
0
    || ((secflags & SEC_HAS_CONTENTS) != SEC_HAS_CONTENTS))
3860
0
  continue;
3861
0
      secname = bfd_section_name (sec);
3862
0
      relname = (char *) bfd_malloc ((bfd_size_type) strlen (secname) + 6);
3863
0
      strcpy (relname, ".rela");
3864
0
      strcat (relname, secname);
3865
0
      if (bfd_get_section_by_name (abfd, secname))
3866
0
  continue;
3867
0
      s = bfd_make_section (abfd, relname);
3868
0
      if (s == NULL
3869
0
    || !bfd_set_section_flags (s, flags | SEC_READONLY)
3870
0
    || !bfd_set_section_alignment (s, ptralign))
3871
0
  return false;
3872
0
    }
3873
3874
0
  if (bed->want_dynbss)
3875
0
    {
3876
      /* The .dynbss section is a place to put symbols which are defined
3877
   by dynamic objects, are referenced by regular objects, and are
3878
   not functions.  We must allocate space for them in the process
3879
   image and use a R_*_COPY reloc to tell the dynamic linker to
3880
   initialize them at run time.  The linker script puts the .dynbss
3881
   section into the .bss section of the final image.  */
3882
0
      s = bfd_make_section (abfd, ".dynbss");
3883
0
      htab->root.sdynbss = s;
3884
0
      if (s == NULL
3885
0
    || !bfd_set_section_flags (s, SEC_ALLOC | SEC_LINKER_CREATED))
3886
0
  return false;
3887
      /* The .rel[a].bss section holds copy relocs.  This section is not
3888
   normally needed.  We need to create it here, though, so that the
3889
   linker will map it to an output section.  We can't just create it
3890
   only if we need it, because we will not know whether we need it
3891
   until we have seen all the input files, and the first time the
3892
   main linker code calls BFD after examining all the input files
3893
   (size_dynamic_sections) the input sections have already been
3894
   mapped to the output sections.  If the section turns out not to
3895
   be needed, we can discard it later.  We will never need this
3896
   section when generating a shared object, since they do not use
3897
   copy relocs.  */
3898
0
      if (!bfd_link_pic (info))
3899
0
  {
3900
0
    s = bfd_make_section (abfd, (bed->default_use_rela_p
3901
0
               ? ".rela.bss" : ".rel.bss"));
3902
0
    htab->root.srelbss = s;
3903
0
    if (s == NULL
3904
0
        || !bfd_set_section_flags (s, flags | SEC_READONLY)
3905
0
        || !bfd_set_section_alignment (s, ptralign))
3906
0
      return false;
3907
0
  }
3908
0
    }
3909
3910
0
  return true;
3911
0
}
3912
3913
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
3914
static void
3915
nds32_elf_copy_indirect_symbol (struct bfd_link_info *info,
3916
        struct elf_link_hash_entry *dir,
3917
        struct elf_link_hash_entry *ind)
3918
0
{
3919
0
  struct elf_nds32_link_hash_entry *edir, *eind;
3920
3921
0
  edir = (struct elf_nds32_link_hash_entry *) dir;
3922
0
  eind = (struct elf_nds32_link_hash_entry *) ind;
3923
3924
0
  if (ind->root.type == bfd_link_hash_indirect)
3925
0
    {
3926
0
      if (dir->got.refcount <= 0)
3927
0
  {
3928
0
    edir->tls_type = eind->tls_type;
3929
0
    eind->tls_type = GOT_UNKNOWN;
3930
0
  }
3931
0
    }
3932
3933
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
3934
0
}
3935

3936
/* Adjust a symbol defined by a dynamic object and referenced by a
3937
   regular object.  The current definition is in some section of the
3938
   dynamic object, but we're not including those sections.  We have to
3939
   change the definition to something the rest of the link can
3940
   understand.  */
3941
3942
static bool
3943
nds32_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
3944
         struct elf_link_hash_entry *h)
3945
0
{
3946
0
  struct elf_nds32_link_hash_table *htab;
3947
0
  bfd *dynobj;
3948
0
  asection *s;
3949
0
  unsigned int power_of_two;
3950
3951
0
  dynobj = elf_hash_table (info)->dynobj;
3952
3953
  /* Make sure we know what is going on here.  */
3954
0
  BFD_ASSERT (dynobj != NULL
3955
0
        && (h->needs_plt
3956
0
      || h->is_weakalias
3957
0
      || (h->def_dynamic && h->ref_regular && !h->def_regular)));
3958
3959
3960
  /* If this is a function, put it in the procedure linkage table.  We
3961
     will fill in the contents of the procedure linkage table later,
3962
     when we know the address of the .got section.  */
3963
0
  if (h->type == STT_FUNC || h->needs_plt)
3964
0
    {
3965
0
      if (!bfd_link_pic (info)
3966
0
    && !h->def_dynamic
3967
0
    && !h->ref_dynamic
3968
0
    && h->root.type != bfd_link_hash_undefweak
3969
0
    && h->root.type != bfd_link_hash_undefined)
3970
0
  {
3971
    /* This case can occur if we saw a PLT reloc in an input
3972
       file, but the symbol was never referred to by a dynamic
3973
       object.  In such a case, we don't actually need to build
3974
       a procedure linkage table, and we can just do a PCREL
3975
       reloc instead.  */
3976
0
    h->plt.offset = (bfd_vma) - 1;
3977
0
    h->needs_plt = 0;
3978
0
  }
3979
3980
0
      return true;
3981
0
    }
3982
0
  else
3983
0
    h->plt.offset = (bfd_vma) - 1;
3984
3985
  /* If this is a weak symbol, and there is a real definition, the
3986
     processor independent code will have arranged for us to see the
3987
     real definition first, and we can just use the same value.  */
3988
0
  if (h->is_weakalias)
3989
0
    {
3990
0
      struct elf_link_hash_entry *def = weakdef (h);
3991
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3992
0
      h->root.u.def.section = def->root.u.def.section;
3993
0
      h->root.u.def.value = def->root.u.def.value;
3994
0
      return true;
3995
0
    }
3996
3997
  /* This is a reference to a symbol defined by a dynamic object which
3998
     is not a function.  */
3999
4000
  /* If we are creating a shared library, we must presume that the
4001
     only references to the symbol are via the global offset table.
4002
     For such cases we need not do anything here; the relocations will
4003
     be handled correctly by relocate_section.  */
4004
0
  if (bfd_link_pic (info))
4005
0
    return true;
4006
4007
  /* If there are no references to this symbol that do not use the
4008
     GOT, we don't need to generate a copy reloc.  */
4009
0
  if (!h->non_got_ref)
4010
0
    return true;
4011
4012
  /* If -z nocopyreloc was given, we won't generate them either.  */
4013
0
  if (0 && info->nocopyreloc)
4014
0
    {
4015
0
      h->non_got_ref = 0;
4016
0
      return true;
4017
0
    }
4018
4019
  /* If we don't find any dynamic relocs in read-only sections, then
4020
     we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
4021
0
  if (!_bfd_elf_readonly_dynrelocs (h))
4022
0
    {
4023
0
      h->non_got_ref = 0;
4024
0
      return true;
4025
0
    }
4026
4027
  /* We must allocate the symbol in our .dynbss section, which will
4028
     become part of the .bss section of the executable.  There will be
4029
     an entry for this symbol in the .dynsym section.  The dynamic
4030
     object will contain position independent code, so all references
4031
     from the dynamic object to this symbol will go through the global
4032
     offset table.  The dynamic linker will use the .dynsym entry to
4033
     determine the address it must put in the global offset table, so
4034
     both the dynamic object and the regular object will refer to the
4035
     same memory location for the variable.  */
4036
4037
0
  htab = nds32_elf_hash_table (info);
4038
0
  s = htab->root.sdynbss;
4039
0
  BFD_ASSERT (s != NULL);
4040
4041
  /* We must generate a R_NDS32_COPY reloc to tell the dynamic linker
4042
     to copy the initial value out of the dynamic object and into the
4043
     runtime process image.  We need to remember the offset into the
4044
     .rela.bss section we are going to use.  */
4045
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
4046
0
    {
4047
0
      asection *srel;
4048
4049
0
      srel = htab->root.srelbss;
4050
0
      BFD_ASSERT (srel != NULL);
4051
0
      srel->size += sizeof (Elf32_External_Rela);
4052
0
      h->needs_copy = 1;
4053
0
    }
4054
4055
  /* We need to figure out the alignment required for this symbol.  I
4056
     have no idea how ELF linkers handle this.  */
4057
0
  power_of_two = bfd_log2 (h->size);
4058
0
  if (power_of_two > 3)
4059
0
    power_of_two = 3;
4060
4061
  /* Apply the required alignment.  */
4062
0
  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
4063
0
  if (power_of_two > bfd_section_alignment (s))
4064
0
    {
4065
0
      if (!bfd_set_section_alignment (s, power_of_two))
4066
0
  return false;
4067
0
    }
4068
4069
  /* Define the symbol as being at this point in the section.  */
4070
0
  h->root.u.def.section = s;
4071
0
  h->root.u.def.value = s->size;
4072
4073
  /* Increment the section size to make room for the symbol.  */
4074
0
  s->size += h->size;
4075
4076
0
  return true;
4077
0
}
4078
4079
/* Allocate space in .plt, .got and associated reloc sections for
4080
   dynamic relocs.  */
4081
4082
static bool
4083
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
4084
0
{
4085
0
  struct bfd_link_info *info;
4086
0
  struct elf_link_hash_table *ehtab;
4087
0
  struct elf_nds32_link_hash_table *htab;
4088
0
  struct elf_dyn_relocs *p;
4089
4090
0
  if (h->root.type == bfd_link_hash_indirect)
4091
0
    return true;
4092
4093
  /* When warning symbols are created, they **replace** the "real"
4094
     entry in the hash table, thus we never get to see the real
4095
     symbol in a hash traversal. So look at it now.  */
4096
0
  if (h->root.type == bfd_link_hash_warning)
4097
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
4098
4099
0
  info = (struct bfd_link_info *) inf;
4100
0
  ehtab = elf_hash_table (info);
4101
0
  htab = nds32_elf_hash_table (info);
4102
0
  if (htab == NULL)
4103
0
    return false;
4104
4105
0
  if ((htab->root.dynamic_sections_created || h->type == STT_GNU_IFUNC)
4106
0
      && h->plt.refcount > 0
4107
0
      && !(bfd_link_pie (info) && h->def_regular))
4108
0
    {
4109
      /* Make sure this symbol is output as a dynamic symbol.
4110
   Undefined weak syms won't yet be marked as dynamic.  */
4111
0
      if (h->dynindx == -1 && !h->forced_local)
4112
0
  {
4113
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
4114
0
      return false;
4115
0
  }
4116
4117
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, bfd_link_pic (info), h))
4118
0
  {
4119
0
    asection *s = ehtab->splt;
4120
4121
    /* If this is the first .plt entry, make room for the special
4122
       first entry.  */
4123
0
    if (s->size == 0)
4124
0
      s->size += PLT_ENTRY_SIZE;
4125
4126
0
    h->plt.offset = s->size;
4127
4128
    /* If this symbol is not defined in a regular file, and we are
4129
       not generating a shared library, then set the symbol to this
4130
       location in the .plt.  This is required to make function
4131
       pointers compare as equal between the normal executable and
4132
       the shared library.  */
4133
0
    if (!bfd_link_pic (info) && !h->def_regular)
4134
0
      {
4135
0
        h->root.u.def.section = s;
4136
0
        h->root.u.def.value = h->plt.offset;
4137
0
      }
4138
4139
    /* Make room for this entry.  */
4140
0
    s->size += PLT_ENTRY_SIZE;
4141
4142
    /* We also need to make an entry in the .got.plt section, which
4143
       will be placed in the .got section by the linker script.  */
4144
0
    ehtab->sgotplt->size += 4;
4145
4146
    /* We also need to make an entry in the .rel.plt section.  */
4147
0
    ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4148
0
    if (htab->tls_desc_trampoline)
4149
0
      htab->next_tls_desc_index++;
4150
0
  }
4151
0
      else
4152
0
  {
4153
0
    h->plt.offset = (bfd_vma) - 1;
4154
0
    h->needs_plt = 0;
4155
0
  }
4156
0
    }
4157
0
  else
4158
0
    {
4159
0
      h->plt.offset = (bfd_vma) - 1;
4160
0
      h->needs_plt = 0;
4161
0
    }
4162
4163
0
  if (h->got.refcount > 0)
4164
0
    {
4165
0
      asection *sgot;
4166
0
      bool dyn;
4167
0
      int tls_type = elf32_nds32_hash_entry (h)->tls_type;
4168
4169
      /* Make sure this symbol is output as a dynamic symbol.
4170
   Undefined weak syms won't yet be marked as dynamic.  */
4171
0
      if (h->dynindx == -1 && !h->forced_local)
4172
0
  {
4173
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
4174
0
      return false;
4175
0
  }
4176
4177
0
      sgot = elf_hash_table (info)->sgot;
4178
0
      h->got.offset = sgot->size;
4179
4180
0
      if (tls_type == GOT_UNKNOWN)
4181
0
  abort ();
4182
4183
      /* Non-TLS symbols, and TLS_IE need one GOT slot.  */
4184
0
      if (tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4185
0
  sgot->size += 4;
4186
0
      else
4187
0
  {
4188
    /* TLS_DESC, TLS_GD, and TLS_LD need 2 consecutive GOT slots.  */
4189
0
    if (tls_type & GOT_TLS_DESC)
4190
0
      sgot->size += 8;
4191
0
  }
4192
4193
0
      dyn = htab->root.dynamic_sections_created;
4194
4195
0
      if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h))
4196
0
  {
4197
0
    if (tls_type == GOT_TLS_DESC && htab->tls_desc_trampoline)
4198
0
      {
4199
        /* TLS_DESC with trampoline needs a relocation slot
4200
     within .rela.plt.  */
4201
0
        htab->num_tls_desc++;
4202
0
        ehtab->srelplt->size += sizeof (Elf32_External_Rela);
4203
0
        htab->tls_trampoline = -1;
4204
0
      }
4205
0
    else
4206
0
      {
4207
        /* other relocations, including TLS_DESC without trampoline, need
4208
     a relocation slot within .rela.got.  */
4209
0
        ehtab->srelgot->size += sizeof (Elf32_External_Rela);
4210
0
      }
4211
0
  }
4212
0
    }
4213
0
  else
4214
0
    h->got.offset = (bfd_vma)-1;
4215
4216
0
  if (h->dyn_relocs == NULL)
4217
0
    return true;
4218
4219
  /* In the shared -Bsymbolic case, discard space allocated for
4220
     dynamic pc-relative relocs against symbols which turn out to be
4221
     defined in regular objects.  For the normal shared case, discard
4222
     space for pc-relative relocs that have become local due to symbol
4223
     visibility changes.  */
4224
4225
0
  if (bfd_link_pic (info))
4226
0
    {
4227
0
      if (h->def_regular && (h->forced_local || info->symbolic))
4228
0
  {
4229
0
    struct elf_dyn_relocs **pp;
4230
4231
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
4232
0
      {
4233
0
        p->count -= p->pc_count;
4234
0
        p->pc_count = 0;
4235
0
        if (p->count == 0)
4236
0
    *pp = p->next;
4237
0
        else
4238
0
    pp = &p->next;
4239
0
      }
4240
0
  }
4241
0
    }
4242
0
  else
4243
0
    {
4244
      /* For the non-shared case, discard space for relocs against
4245
   symbols which turn out to need copy relocs or are not dynamic.  */
4246
4247
0
      if (!h->non_got_ref
4248
0
    && ((h->def_dynamic
4249
0
         && !h->def_regular)
4250
0
        || (htab->root.dynamic_sections_created
4251
0
      && (h->root.type == bfd_link_hash_undefweak
4252
0
          || h->root.type == bfd_link_hash_undefined))))
4253
0
  {
4254
    /* Make sure this symbol is output as a dynamic symbol.
4255
       Undefined weak syms won't yet be marked as dynamic.  */
4256
0
    if (h->dynindx == -1 && !h->forced_local)
4257
0
      {
4258
0
        if (!bfd_elf_link_record_dynamic_symbol (info, h))
4259
0
    return false;
4260
0
      }
4261
4262
    /* If that succeeded, we know we'll be keeping all the
4263
       relocs.  */
4264
0
    if (h->dynindx != -1)
4265
0
      goto keep;
4266
0
  }
4267
4268
0
      h->dyn_relocs = NULL;
4269
4270
0
    keep:;
4271
0
    }
4272
4273
  /* Finally, allocate space.  */
4274
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
4275
0
    {
4276
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
4277
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
4278
0
    }
4279
4280
0
  return true;
4281
0
}
4282
4283
/* Add relocation REL to the end of relocation section SRELOC.  */
4284
4285
static void
4286
elf32_nds32_add_dynreloc (bfd *output_bfd,
4287
        struct bfd_link_info *info ATTRIBUTE_UNUSED,
4288
        asection *sreloc, Elf_Internal_Rela *rel)
4289
0
{
4290
0
  bfd_byte *loc;
4291
0
  if (sreloc == NULL)
4292
0
    abort ();
4293
4294
0
  loc = sreloc->contents;
4295
0
  loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
4296
0
  if (sreloc->reloc_count * sizeof (Elf32_External_Rela) > sreloc->size)
4297
0
    abort ();
4298
4299
0
  bfd_elf32_swap_reloca_out (output_bfd, rel, loc);
4300
0
}
4301
4302
/* Set the sizes of the dynamic sections.  */
4303
4304
static bool
4305
nds32_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
4306
         struct bfd_link_info *info)
4307
0
{
4308
0
  struct elf_nds32_link_hash_table *htab;
4309
0
  bfd *dynobj;
4310
0
  asection *s;
4311
0
  bool relocs;
4312
0
  bfd *ibfd;
4313
4314
0
  htab = nds32_elf_hash_table (info);
4315
0
  if (htab == NULL)
4316
0
    return false;
4317
4318
0
  dynobj = elf_hash_table (info)->dynobj;
4319
0
  BFD_ASSERT (dynobj != NULL);
4320
4321
0
  if (elf_hash_table (info)->dynamic_sections_created)
4322
0
    {
4323
      /* Set the contents of the .interp section to the interpreter.  */
4324
0
      if (bfd_link_executable (info) && !info->nointerp)
4325
0
  {
4326
0
    s = bfd_get_section_by_name (dynobj, ".interp");
4327
0
    BFD_ASSERT (s != NULL);
4328
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4329
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4330
0
  }
4331
0
    }
4332
4333
  /* Set up .got offsets for local syms, and space for local dynamic
4334
     relocs.  */
4335
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4336
0
    {
4337
0
      bfd_signed_vma *local_got;
4338
0
      bfd_signed_vma *end_local_got;
4339
0
      bfd_size_type locsymcount;
4340
0
      Elf_Internal_Shdr *symtab_hdr;
4341
0
      asection *sgot;
4342
0
      char *local_tls_type;
4343
0
      unsigned long symndx;
4344
0
      bfd_vma *local_tlsdesc_gotent;
4345
4346
0
      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4347
0
  continue;
4348
4349
0
      for (s = ibfd->sections; s != NULL; s = s->next)
4350
0
  {
4351
0
    struct elf_dyn_relocs *p;
4352
4353
0
    for (p = ((struct elf_dyn_relocs *)
4354
0
        elf_section_data (s)->local_dynrel);
4355
0
         p != NULL; p = p->next)
4356
0
      {
4357
0
        if (!bfd_is_abs_section (p->sec)
4358
0
      && bfd_is_abs_section (p->sec->output_section))
4359
0
    {
4360
      /* Input section has been discarded, either because
4361
         it is a copy of a linkonce section or due to
4362
         linker script /DISCARD/, so we'll be discarding
4363
         the relocs too.  */
4364
0
    }
4365
0
        else if (p->count != 0)
4366
0
    {
4367
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
4368
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
4369
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4370
0
        info->flags |= DF_TEXTREL;
4371
0
    }
4372
0
      }
4373
0
  }
4374
4375
0
      local_got = elf_local_got_refcounts (ibfd);
4376
0
      if (!local_got)
4377
0
  continue;
4378
4379
0
      symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
4380
0
      locsymcount = symtab_hdr->sh_info;
4381
0
      end_local_got = local_got + locsymcount;
4382
0
      sgot = elf_hash_table (info)->sgot;
4383
0
      local_tls_type = elf32_nds32_local_got_tls_type (ibfd);
4384
0
      local_tlsdesc_gotent = elf32_nds32_local_tlsdesc_gotent (ibfd);
4385
0
      for (symndx = 0; local_got < end_local_got;
4386
0
     ++local_got, ++local_tls_type, ++local_tlsdesc_gotent, ++symndx)
4387
0
  {
4388
0
    if (*local_got > 0)
4389
0
      {
4390
0
        int num_of_got_entry_needed = 0;
4391
0
        *local_got = sgot->size;
4392
0
        *local_tlsdesc_gotent = sgot->size;
4393
4394
        /* TLS_NORMAL, and TLS_IE need one slot in .got.  */
4395
0
        if (*local_tls_type & (GOT_NORMAL | GOT_TLS_IE | GOT_TLS_IEGP))
4396
0
    num_of_got_entry_needed = 1;
4397
        /* TLS_GD, TLS_LD, and TLS_DESC need an 8-byte structure in the GOT.  */
4398
0
        else if (*local_tls_type & GOT_TLS_DESC)
4399
0
    num_of_got_entry_needed = 2;
4400
4401
0
        sgot->size += (num_of_got_entry_needed << 2);
4402
4403
        /* non-relax-able TLS_DESCs need a slot in .rela.plt.
4404
     others need a slot in .rela.got.  */
4405
0
        if (*local_tls_type == GOT_TLS_DESC)
4406
0
    {
4407
0
      if (bfd_link_pic (info))
4408
0
        {
4409
0
          if (htab->tls_desc_trampoline)
4410
0
      {
4411
0
        htab->num_tls_desc++;
4412
0
        htab->root.srelplt->size += sizeof (Elf32_External_Rela);
4413
0
        htab->tls_trampoline = -1;
4414
0
      }
4415
0
          else
4416
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4417
0
        }
4418
0
      else
4419
0
        {
4420
          /* TLS_DESC -> TLS_LE  */
4421
0
        }
4422
0
    }
4423
0
        else
4424
0
    {
4425
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
4426
0
    }
4427
0
      }
4428
0
    else
4429
0
      {
4430
0
        *local_got = (bfd_vma) -1;
4431
0
        *local_tlsdesc_gotent = (bfd_vma) -1;
4432
0
      }
4433
0
  }
4434
0
    }
4435
4436
  /* Allocate global sym .plt and .got entries, and space for global
4437
     sym dynamic relocs.  */
4438
0
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, (void *) info);
4439
4440
  /* For every jump slot reserved in the sgotplt, reloc_count is
4441
     incremented.  However, when we reserve space for TLS descriptors,
4442
     it's not incremented, so in order to compute the space reserved
4443
     for them, it suffices to multiply the reloc count by the jump
4444
     slot size.  */
4445
0
  if (htab->tls_desc_trampoline && htab->root.srelplt)
4446
0
    htab->sgotplt_jump_table_size = elf32_nds32_compute_jump_table_size (htab);
4447
4448
0
  if (htab->tls_trampoline)
4449
0
    {
4450
0
      htab->tls_trampoline = htab->root.splt->size;
4451
4452
      /* If we're not using lazy TLS relocations, don't generate the
4453
   PLT and GOT entries they require.  */
4454
0
      if ((info->flags & DF_BIND_NOW))
4455
0
  htab->root.tlsdesc_plt = 0;
4456
0
      else
4457
0
  {
4458
0
    htab->root.tlsdesc_got = htab->root.sgot->size;
4459
0
    htab->root.sgot->size += 4;
4460
4461
0
    htab->root.tlsdesc_plt = htab->root.splt->size;
4462
0
    htab->root.splt->size += 4 * ARRAY_SIZE (dl_tlsdesc_lazy_trampoline);
4463
0
  }
4464
0
    }
4465
4466
  /* We now have determined the sizes of the various dynamic sections.
4467
     Allocate memory for them.  */
4468
  /* The check_relocs and adjust_dynamic_symbol entry points have
4469
     determined the sizes of the various dynamic sections.  Allocate
4470
     memory for them.  */
4471
0
  relocs = false;
4472
0
  for (s = dynobj->sections; s != NULL; s = s->next)
4473
0
    {
4474
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
4475
0
  continue;
4476
4477
0
      if (s == htab->root.splt)
4478
0
  {
4479
    /* Strip this section if we don't need it; see the
4480
       comment below.  */
4481
0
    ;
4482
0
  }
4483
0
      else if (s == elf_hash_table (info)->sgot)
4484
0
  {
4485
0
    got_size += s->size;
4486
0
  }
4487
0
      else if (s == elf_hash_table (info)->sgotplt)
4488
0
  {
4489
0
    got_size += s->size;
4490
0
  }
4491
0
      else if (startswith (bfd_section_name (s), ".rela"))
4492
0
  {
4493
0
    if (s->size != 0 && s != elf_hash_table (info)->srelplt)
4494
0
      relocs = true;
4495
4496
    /* We use the reloc_count field as a counter if we need
4497
       to copy relocs into the output file.  */
4498
0
    s->reloc_count = 0;
4499
0
  }
4500
0
      else
4501
0
  {
4502
    /* It's not one of our sections, so don't allocate space.  */
4503
0
    continue;
4504
0
  }
4505
4506
0
      if (s->size == 0)
4507
0
  {
4508
    /* If we don't need this section, strip it from the
4509
       output file.  This is mostly to handle .rela.bss and
4510
       .rela.plt.  We must create both sections in
4511
       create_dynamic_sections, because they must be created
4512
       before the linker maps input sections to output
4513
       sections.  The linker does that before
4514
       adjust_dynamic_symbol is called, and it is that
4515
       function which decides whether anything needs to go
4516
       into these sections.  */
4517
0
    s->flags |= SEC_EXCLUDE;
4518
0
    continue;
4519
0
  }
4520
4521
      /* Allocate memory for the section contents.  We use bfd_zalloc
4522
   here in case unused entries are not reclaimed before the
4523
   section's contents are written out.  This should not happen,
4524
   but this way if it does, we get a R_NDS32_NONE reloc instead
4525
   of garbage.  */
4526
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4527
0
      if (s->contents == NULL)
4528
0
  return false;
4529
0
    }
4530
4531
0
  return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
4532
0
}
4533
4534
static bfd_reloc_status_type
4535
nds32_relocate_contents (reloc_howto_type *howto, bfd *input_bfd,
4536
       bfd_vma relocation, bfd_byte *location)
4537
0
{
4538
0
  int size;
4539
0
  bfd_vma x = 0;
4540
0
  bfd_reloc_status_type flag;
4541
0
  unsigned int rightshift = howto->rightshift;
4542
0
  unsigned int bitpos = howto->bitpos;
4543
4544
0
  if (howto->negate)
4545
0
    relocation = -relocation;
4546
4547
  /* Get the value we are going to relocate.  */
4548
0
  size = bfd_get_reloc_size (howto);
4549
0
  switch (size)
4550
0
    {
4551
0
    default:
4552
0
      abort ();
4553
0
      break;
4554
0
    case 0:
4555
0
      return bfd_reloc_ok;
4556
0
    case 2:
4557
0
      x = bfd_getb16 (location);
4558
0
      break;
4559
0
    case 4:
4560
0
      x = bfd_getb32 (location);
4561
0
      break;
4562
0
    }
4563
4564
  /* Check for overflow.  FIXME: We may drop bits during the addition
4565
     which we don't check for.  We must either check at every single
4566
     operation, which would be tedious, or we must do the computations
4567
     in a type larger than bfd_vma, which would be inefficient.  */
4568
0
  flag = bfd_reloc_ok;
4569
0
  if (howto->complain_on_overflow != complain_overflow_dont)
4570
0
    {
4571
0
      bfd_vma addrmask, fieldmask, signmask, ss;
4572
0
      bfd_vma a, b, sum;
4573
4574
      /* Get the values to be added together.  For signed and unsigned
4575
   relocations, we assume that all values should be truncated to
4576
   the size of an address.  For bitfields, all the bits matter.
4577
   See also bfd_check_overflow.  */
4578
0
      fieldmask = N_ONES (howto->bitsize);
4579
0
      signmask = ~fieldmask;
4580
0
      addrmask = N_ONES (bfd_arch_bits_per_address (input_bfd)) | fieldmask;
4581
0
      a = (relocation & addrmask) >> rightshift;
4582
0
      b = (x & howto->src_mask & addrmask) >> bitpos;
4583
4584
0
      switch (howto->complain_on_overflow)
4585
0
  {
4586
0
  case complain_overflow_signed:
4587
    /* If any sign bits are set, all sign bits must be set.
4588
       That is, A must be a valid negative address after
4589
       shifting.  */
4590
0
    signmask = ~(fieldmask >> 1);
4591
    /* Fall through.  */
4592
4593
0
  case complain_overflow_bitfield:
4594
    /* Much like the signed check, but for a field one bit
4595
       wider.  We allow a bitfield to represent numbers in the
4596
       range -2**n to 2**n-1, where n is the number of bits in the
4597
       field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
4598
       can't overflow, which is exactly what we want.  */
4599
0
    ss = a & signmask;
4600
0
    if (ss != 0 && ss != ((addrmask >> rightshift) & signmask))
4601
0
      flag = bfd_reloc_overflow;
4602
4603
    /* We only need this next bit of code if the sign bit of B
4604
       is below the sign bit of A.  This would only happen if
4605
       SRC_MASK had fewer bits than BITSIZE.  Note that if
4606
       SRC_MASK has more bits than BITSIZE, we can get into
4607
       trouble; we would need to verify that B is in range, as
4608
       we do for A above.  */
4609
0
    ss = ((~howto->src_mask) >> 1) & howto->src_mask;
4610
0
    ss >>= bitpos;
4611
4612
    /* Set all the bits above the sign bit.  */
4613
0
    b = (b ^ ss) - ss;
4614
4615
    /* Now we can do the addition.  */
4616
0
    sum = a + b;
4617
4618
    /* See if the result has the correct sign.  Bits above the
4619
       sign bit are junk now; ignore them.  If the sum is
4620
       positive, make sure we did not have all negative inputs;
4621
       if the sum is negative, make sure we did not have all
4622
       positive inputs.  The test below looks only at the sign
4623
       bits, and it really just
4624
       SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
4625
4626
       We mask with addrmask here to explicitly allow an address
4627
       wrap-around.  The Linux kernel relies on it, and it is
4628
       the only way to write assembler code which can run when
4629
       loaded at a location 0x80000000 away from the location at
4630
       which it is linked.  */
4631
0
    if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
4632
0
      flag = bfd_reloc_overflow;
4633
4634
0
    break;
4635
4636
0
  case complain_overflow_unsigned:
4637
    /* Checking for an unsigned overflow is relatively easy:
4638
       trim the addresses and add, and trim the result as well.
4639
       Overflow is normally indicated when the result does not
4640
       fit in the field.  However, we also need to consider the
4641
       case when, e.g., fieldmask is 0x7fffffff or smaller, an
4642
       input is 0x80000000, and bfd_vma is only 32 bits; then we
4643
       will get sum == 0, but there is an overflow, since the
4644
       inputs did not fit in the field.  Instead of doing a
4645
       separate test, we can check for this by or-ing in the
4646
       operands when testing for the sum overflowing its final
4647
       field.  */
4648
0
    sum = (a + b) & addrmask;
4649
0
    if ((a | b | sum) & signmask)
4650
0
      flag = bfd_reloc_overflow;
4651
0
    break;
4652
4653
0
  default:
4654
0
    abort ();
4655
0
  }
4656
0
    }
4657
4658
  /* Put RELOCATION in the right bits.  */
4659
0
  relocation >>= (bfd_vma) rightshift;
4660
0
  relocation <<= (bfd_vma) bitpos;
4661
4662
  /* Add RELOCATION to the right bits of X.  */
4663
  /* FIXME : 090616
4664
     Because the relaxation may generate duplicate relocation at one address,
4665
     an addition to immediate in the instruction may cause the relocation added
4666
     several times.
4667
     This bug should be fixed in assembler, but a check is also needed here.  */
4668
0
  if (howto->partial_inplace)
4669
0
    x = ((x & ~howto->dst_mask)
4670
0
   | (((x & howto->src_mask) + relocation) & howto->dst_mask));
4671
0
  else
4672
0
    x = ((x & ~howto->dst_mask) | ((relocation) & howto->dst_mask));
4673
4674
4675
  /* Put the relocated value back in the object file.  */
4676
0
  switch (size)
4677
0
    {
4678
0
    default:
4679
0
    case 0:
4680
0
    case 1:
4681
0
    case 8:
4682
0
      abort ();
4683
0
      break;
4684
0
    case 2:
4685
0
      bfd_putb16 (x, location);
4686
0
      break;
4687
0
    case 4:
4688
0
      bfd_putb32 (x, location);
4689
0
      break;
4690
0
    }
4691
4692
0
  return flag;
4693
0
}
4694
4695
static bfd_reloc_status_type
4696
nds32_elf_final_link_relocate (reloc_howto_type *howto, bfd *input_bfd,
4697
             asection *input_section, bfd_byte *contents,
4698
             bfd_vma address, bfd_vma value, bfd_vma addend)
4699
0
{
4700
0
  bfd_vma relocation;
4701
4702
  /* Sanity check the address.  */
4703
0
  if (address > bfd_get_section_limit (input_bfd, input_section))
4704
0
    return bfd_reloc_outofrange;
4705
4706
  /* This function assumes that we are dealing with a basic relocation
4707
     against a symbol.  We want to compute the value of the symbol to
4708
     relocate to.  This is just VALUE, the value of the symbol, plus
4709
     ADDEND, any addend associated with the reloc.  */
4710
0
  relocation = value + addend;
4711
4712
  /* If the relocation is PC relative, we want to set RELOCATION to
4713
     the distance between the symbol (currently in RELOCATION) and the
4714
     location we are relocating.  If pcrel_offset is FALSE we do not
4715
     need to subtract out the offset of the location within the
4716
     section (which is just ADDRESS).  */
4717
0
  if (howto->pc_relative)
4718
0
    {
4719
0
      relocation -= (input_section->output_section->vma
4720
0
         + input_section->output_offset);
4721
0
      if (howto->pcrel_offset)
4722
0
  relocation -= address;
4723
0
    }
4724
4725
0
  return nds32_relocate_contents (howto, input_bfd, relocation,
4726
0
          contents + address);
4727
0
}
4728
4729
static int
4730
nds32_elf_output_symbol_hook (struct bfd_link_info *info,
4731
            const char *name,
4732
            Elf_Internal_Sym *elfsym ATTRIBUTE_UNUSED,
4733
            asection *input_sec,
4734
            struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
4735
0
{
4736
0
  const char *source;
4737
0
  FILE *sym_ld_script = NULL;
4738
0
  struct elf_nds32_link_hash_table *table;
4739
4740
0
  table = nds32_elf_hash_table (info);
4741
0
  sym_ld_script = table->sym_ld_script;
4742
0
  if (!sym_ld_script)
4743
0
    return true;
4744
4745
0
  if (!h || !name || *name == '\0')
4746
0
    return true;
4747
4748
0
  if (input_sec->flags & SEC_EXCLUDE)
4749
0
    return true;
4750
4751
0
  if (!check_start_export_sym)
4752
0
    {
4753
0
      fprintf (sym_ld_script, "SECTIONS\n{\n");
4754
0
      check_start_export_sym = 1;
4755
0
    }
4756
4757
0
  if (h->root.type == bfd_link_hash_defined
4758
0
      || h->root.type == bfd_link_hash_defweak)
4759
0
    {
4760
0
      if (!h->root.u.def.section->output_section)
4761
0
  return true;
4762
4763
0
      if (bfd_is_const_section (input_sec))
4764
0
  source = input_sec->name;
4765
0
      else
4766
0
  source = bfd_get_filename (input_sec->owner);
4767
4768
0
      fprintf (sym_ld_script, "\t%s = 0x%08lx;\t /* %s */\n",
4769
0
         h->root.root.string,
4770
0
         (long) (h->root.u.def.value
4771
0
    + h->root.u.def.section->output_section->vma
4772
0
    + h->root.u.def.section->output_offset), source);
4773
0
    }
4774
4775
0
  return true;
4776
0
}
4777
4778
/* Relocate an NDS32/D ELF section.
4779
   There is some attempt to make this function usable for many architectures,
4780
   both for RELA and REL type relocs, if only to serve as a learning tool.
4781
4782
   The RELOCATE_SECTION function is called by the new ELF backend linker
4783
   to handle the relocations for a section.
4784
4785
   The relocs are always passed as Rela structures; if the section
4786
   actually uses Rel structures, the r_addend field will always be
4787
   zero.
4788
4789
   This function is responsible for adjust the section contents as
4790
   necessary, and (if using Rela relocs and generating a
4791
   relocatable output file) adjusting the reloc addend as
4792
   necessary.
4793
4794
   This function does not have to worry about setting the reloc
4795
   address or the reloc symbol index.
4796
4797
   LOCAL_SYMS is a pointer to the swapped in local symbols.
4798
4799
   LOCAL_SECTIONS is an array giving the section in the input file
4800
   corresponding to the st_shndx field of each local symbol.
4801
4802
   The global hash table entry for the global symbols can be found
4803
   via elf_sym_hashes (input_bfd).
4804
4805
   When generating relocatable output, this function must handle
4806
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
4807
   going to be the section symbol corresponding to the output
4808
   section, which means that the addend must be adjusted
4809
   accordingly.  */
4810
4811
/* Return the base VMA address which should be subtracted from real addresses
4812
   when resolving @dtpoff relocation.
4813
   This is PT_TLS segment p_vaddr.  */
4814
4815
/* Return the relocation value for @tpoff relocation
4816
   if STT_TLS virtual address is ADDRESS.  */
4817
4818
/* Return the relocation value for @gottpoff relocation
4819
   if STT_TLS virtual address is ADDRESS.  */
4820
4821
static bfd_vma
4822
gottpoff (struct bfd_link_info *info, bfd_vma address)
4823
0
{
4824
0
  bfd_vma tp_base;
4825
0
  bfd_vma tp_offset;
4826
4827
  /* If tls_sec is NULL, we should have signalled an error already.  */
4828
0
  if (elf_hash_table (info)->tls_sec == NULL)
4829
0
    return 0;
4830
4831
0
  tp_base = elf_hash_table (info)->tls_sec->vma;
4832
0
  tp_offset = address - tp_base;
4833
4834
0
  return tp_offset;
4835
0
}
4836
4837
static bool
4838
patch_tls_desc_to_ie (bfd_byte *contents, Elf_Internal_Rela *rel, bfd *ibfd)
4839
0
{
4840
  /* TLS_GD/TLS_LD model #1
4841
     46 00 00 00 sethi $r0,#0x0
4842
     58 00 00 00 ori $r0,$r0,#0x0
4843
     40 00 74 00 add $r0,$r0,$gp
4844
     04 10 00 00 lwi $r1,[$r0+#0x0]
4845
     4b e0 04 01 jral $lp,$r1  */
4846
4847
  /* TLS_GD/TLS_LD model #2
4848
     46 00 00 00 sethi $r0,#0x0
4849
     58 00 00 00 ori $r0,$r0,#0x0
4850
     38 10 74 02 lw $r1,[$r0+($gp<<#0x0)]
4851
     40 00 74 00 add $r0,$r0,$gp
4852
     4b e0 04 01 jral $lp,$r1  */
4853
4854
  /* TLS_IE model (non-PIC)
4855
     46 00 00 00 sethi $r0,#0x0
4856
     04 00 00 00 lwi $r0,[$r0+#0x0]
4857
     38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4858
4859
  /* TLS_IE model (PIC)
4860
     46 00 00 00 sethi $r0,#0x0
4861
     58 00 00 00 ori $r0,$r0,#0x0
4862
     38 00 74 02 lw $r0,[$r0+($gp<<#0x0)]
4863
     38 00 64 02 lw $r0,[$r0+($r25<<#0x0)]  */
4864
4865
  /* TLS_GD_TO_IE model
4866
     46 00 00 00 sethi $r0,#0x0
4867
     58 00 00 00 ori $r0,$r0,#0x0
4868
     40 00 74 00 add $r0,$rM,$gp
4869
     04 00 00 01 lwi $r0,[$r0+#0x4]
4870
     40 00 64 00 add $r0,$r0,$r25  */
4871
4872
0
  bool rz = false;
4873
4874
0
  typedef struct
4875
0
    {
4876
0
      uint32_t opcode;
4877
0
      uint32_t mask;
4878
0
    } pat_t;
4879
4880
0
  uint32_t patch[3] =
4881
0
    {
4882
0
      0x40007400, /* add $r0,$rM,$gp     */
4883
0
      0x04000001, /* lwi $r0,[$r0+#0x4]  */
4884
0
      0x40006400, /* add $r0,$r0,$r25    */
4885
0
    };
4886
4887
0
  pat_t mode0[3] =
4888
0
    {
4889
0
  { 0x40000000, 0xfe0003ff },
4890
0
  { 0x04000000, 0xfe000000 },
4891
0
  { 0x4be00001, 0xffff83ff },
4892
0
    };
4893
4894
0
  pat_t mode1[3] =
4895
0
    {
4896
0
  { 0x38007402, 0xfe007fff },
4897
0
  { 0x40007400, 0xfe007fff },
4898
0
  { 0x4be00001, 0xffff83ff },
4899
0
    };
4900
4901
0
  unsigned char *p = contents + rel->r_offset;
4902
4903
0
  uint32_t insn;
4904
0
  uint32_t regidx = 0;
4905
0
  insn = bfd_getb32 (p);
4906
0
  if (INSN_SETHI == (0xfe0fffffu & insn))
4907
0
    {
4908
0
      regidx = 0x1f & (insn >> 20);
4909
0
      p += 4;
4910
0
    }
4911
4912
0
  insn = bfd_getb32 (p);
4913
0
  if (INSN_ORI == (0xfe007fffu & insn))
4914
0
    {
4915
0
      regidx = 0x1f & (insn >> 20);
4916
0
      p += 4;
4917
0
    }
4918
4919
0
  if (patch[2] == bfd_getb32 (p + 8)) /* Character instruction.  */
4920
0
    {
4921
      /* already patched?  */
4922
0
      if ((patch[0] == (0xfff07fffu & bfd_getb32 (p + 0))) &&
4923
0
    (patch[1] == bfd_getb32 (p + 4)))
4924
0
  rz = true;
4925
0
    }
4926
0
  else if (mode0[0].opcode == (mode0[0].mask & bfd_getb32 (p + 0)))
4927
0
    {
4928
0
      if ((mode0[1].opcode == (mode0[1].mask & bfd_getb32 (p + 4))) &&
4929
0
    (mode0[2].opcode == (mode0[2].mask & bfd_getb32 (p + 8))))
4930
0
  {
4931
0
    bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4932
0
    bfd_putb32 (patch[1], p + 4);
4933
0
    bfd_putb32 (patch[2], p + 8);
4934
0
    rz = true;
4935
0
  }
4936
0
    }
4937
0
  else if (mode1[0].opcode == (mode1[0].mask & bfd_getb32 (p + 0)))
4938
0
    {
4939
0
      if ((mode1[1].opcode == (mode1[1].mask & bfd_getb32 (p + 4))) &&
4940
0
    (mode1[2].opcode == (mode1[2].mask & bfd_getb32 (p + 8))))
4941
0
  {
4942
0
    bfd_putb32 (patch[0] | (regidx << 15), p + 0);
4943
0
    bfd_putb32 (patch[1], p + 4);
4944
0
    bfd_putb32 (patch[2], p + 8);
4945
0
    rz = true;
4946
0
  }
4947
0
    }
4948
4949
0
  if (!rz)
4950
0
    {
4951
0
      printf ("%s: %s @ 0x%08x\n", __func__, bfd_get_filename (ibfd),
4952
0
        (int) rel->r_offset);
4953
0
      BFD_ASSERT(0); /* Unsupported pattern.  */
4954
0
    }
4955
4956
0
  return rz;
4957
0
}
4958
4959
static enum elf_nds32_tls_type
4960
get_tls_type (enum elf_nds32_reloc_type r_type, struct elf_link_hash_entry *h);
4961
4962
static unsigned int
4963
ones32 (register unsigned int x)
4964
0
{
4965
  /* 32-bit recursive reduction using SWAR...
4966
     but first step is mapping 2-bit values
4967
     into sum of 2 1-bit values in sneaky way.  */
4968
0
  x -= ((x >> 1) & 0x55555555);
4969
0
  x = (((x >> 2) & 0x33333333) + (x & 0x33333333));
4970
0
  x = (((x >> 4) + x) & 0x0f0f0f0f);
4971
0
  x += (x >> 8);
4972
0
  x += (x >> 16);
4973
0
  return (x & 0x0000003f);
4974
0
}
4975
4976
#if !HAVE_FLS
4977
static unsigned int
4978
fls (register unsigned int x)
4979
0
{
4980
0
  return ffs (x & (-x));
4981
0
}
4982
#endif /* !HAVE_FLS */
4983
4984
#define nds32_elf_local_tlsdesc_gotent(bfd) \
4985
  (elf_nds32_tdata (bfd)->local_tlsdesc_gotent)
4986
4987
static int
4988
nds32_elf_relocate_section (bfd *      output_bfd ATTRIBUTE_UNUSED,
4989
          struct bfd_link_info * info,
4990
          bfd *      input_bfd,
4991
          asection *       input_section,
4992
          bfd_byte *       contents,
4993
          Elf_Internal_Rela *    relocs,
4994
          Elf_Internal_Sym *     local_syms,
4995
          asection **      local_sections)
4996
0
{
4997
0
  Elf_Internal_Shdr *symtab_hdr;
4998
0
  struct elf_link_hash_entry **sym_hashes;
4999
0
  Elf_Internal_Rela *rel, *relend;
5000
0
  bool ret = true;    /* Assume success.  */
5001
0
  int align = 0;
5002
0
  bfd_reloc_status_type r;
5003
0
  const char *errmsg = NULL;
5004
0
  bfd_vma gp;
5005
0
  struct elf_link_hash_table *ehtab;
5006
0
  struct elf_nds32_link_hash_table *htab;
5007
0
  bfd *dynobj;
5008
0
  bfd_vma *local_got_offsets;
5009
0
  asection *sgot, *splt, *sreloc;
5010
0
  bfd_vma high_address;
5011
0
  struct elf_nds32_link_hash_table *table;
5012
0
  int eliminate_gc_relocs;
5013
0
  bfd_vma fpbase_addr;
5014
5015
0
  symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
5016
0
  sym_hashes = elf_sym_hashes (input_bfd);
5017
0
  ehtab = elf_hash_table (info);
5018
0
  htab = nds32_elf_hash_table (info);
5019
0
  high_address = bfd_get_section_limit (input_bfd, input_section);
5020
5021
0
  dynobj = htab->root.dynobj;
5022
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
5023
5024
0
  sgot = ehtab->sgot;
5025
0
  splt = ehtab->splt;
5026
0
  sreloc = NULL;
5027
5028
0
  rel = relocs;
5029
0
  relend = relocs + input_section->reloc_count;
5030
5031
0
  table = nds32_elf_hash_table (info);
5032
0
  eliminate_gc_relocs = table->eliminate_gc_relocs;
5033
5034
  /* By this time, we can adjust the value of _SDA_BASE_.  */
5035
  /* Explain _SDA_BASE_  */
5036
0
  if ((!bfd_link_relocatable (info)))
5037
0
    {
5038
0
      is_SDA_BASE_set = 1;
5039
0
      r = nds32_elf_final_sda_base (output_bfd, info, &gp, true);
5040
0
      if (r != bfd_reloc_ok)
5041
0
  return false;
5042
0
    }
5043
5044
  /* Do TLS model conversion once at first.  */
5045
0
  nds32_elf_unify_tls_model (input_bfd, input_section, contents, info);
5046
5047
  /* Use gp as fp to prevent truncated fit.  Because in relaxation time
5048
     the fp value is set as gp, and it has be reverted for instruction
5049
     setting fp.  */
5050
0
  fpbase_addr = elf_gp (output_bfd);
5051
5052
  /* Deal with (dynamic) relocations.  */
5053
0
  for (rel = relocs; rel < relend; rel++)
5054
0
    {
5055
0
      enum elf_nds32_reloc_type r_type;
5056
0
      reloc_howto_type *howto = NULL;
5057
0
      unsigned long r_symndx;
5058
0
      struct elf_link_hash_entry *h = NULL;
5059
0
      Elf_Internal_Sym *sym = NULL;
5060
0
      asection *sec;
5061
0
      bfd_vma relocation;
5062
0
      bfd_vma relocation_sym = 0xdeadbeef;
5063
0
      Elf_Internal_Rela *lorel;
5064
0
      bfd_vma off;
5065
5066
      /* We can't modify r_addend here as elf_link_input_bfd has an assert to
5067
   ensure it's zero (we use REL relocs, not RELA).  Therefore this
5068
   should be assigning zero to `addend', but for clarity we use
5069
   `r_addend'.  */
5070
5071
0
      bfd_vma addend = rel->r_addend;
5072
0
      bfd_vma offset = rel->r_offset;
5073
5074
0
      r_type = ELF32_R_TYPE (rel->r_info);
5075
0
      if (r_type >= R_NDS32_max)
5076
0
  {
5077
    /* xgettext:c-format */
5078
0
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
5079
0
            input_bfd, r_type);
5080
0
    bfd_set_error (bfd_error_bad_value);
5081
0
    ret = false;
5082
0
    continue;
5083
0
  }
5084
5085
0
      if (r_type == R_NDS32_GNU_VTENTRY
5086
0
    || r_type == R_NDS32_GNU_VTINHERIT
5087
0
    || r_type == R_NDS32_NONE
5088
0
    || r_type == R_NDS32_RELA_GNU_VTENTRY
5089
0
    || r_type == R_NDS32_RELA_GNU_VTINHERIT
5090
0
    || (r_type >= R_NDS32_INSN16 && r_type <= R_NDS32_25_FIXED_RELA)
5091
0
    || r_type == R_NDS32_DATA
5092
0
    || r_type == R_NDS32_TRAN)
5093
0
  continue;
5094
5095
      /* If we enter the fp-as-gp region.  Resolve the address
5096
   of best fp-base.  */
5097
0
      if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_BEGIN
5098
0
    && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5099
0
  {
5100
0
    int dist;
5101
5102
    /* Distance to relocation of best fp-base is encoded in R_SYM.  */
5103
0
    dist =  rel->r_addend >> 16;
5104
0
    fpbase_addr = calculate_memory_address (input_bfd, rel + dist,
5105
0
              local_syms, symtab_hdr);
5106
0
  }
5107
0
      else if (ELF32_R_TYPE (rel->r_info) == R_NDS32_RELAX_REGION_END
5108
0
         && (rel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
5109
0
  {
5110
0
    fpbase_addr = elf_gp (output_bfd);
5111
0
  }
5112
5113
      /* Skip the relocations used for relaxation.  */
5114
      /* We have to update LONGCALL and LONGJUMP
5115
   relocations when generating the relocatable files.  */
5116
0
      if (!bfd_link_relocatable (info)
5117
0
    && (r_type >= R_NDS32_RELAX_ENTRY
5118
0
        || (r_type >= R_NDS32_LONGCALL4
5119
0
      && r_type <= R_NDS32_LONGJUMP7)))
5120
0
  continue;
5121
5122
0
      howto = bfd_elf32_bfd_reloc_type_table_lookup (r_type);
5123
0
      r_symndx = ELF32_R_SYM (rel->r_info);
5124
5125
      /* This is a final link.  */
5126
0
      sym = NULL;
5127
0
      sec = NULL;
5128
0
      h = NULL;
5129
5130
0
      if (r_symndx < symtab_hdr->sh_info)
5131
0
  {
5132
    /* Local symbol.  */
5133
0
    sym = local_syms + r_symndx;
5134
0
    sec = local_sections[r_symndx];
5135
5136
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
5137
0
    addend = rel->r_addend;
5138
5139
    /* keep symbol location for static TLS_IE GOT entry  */
5140
0
    relocation_sym = relocation;
5141
0
    if (bfd_link_relocatable (info))
5142
0
      {
5143
        /* This is a relocatable link.  We don't have to change
5144
     anything, unless the reloc is against a section symbol,
5145
     in which case we have to adjust according to where the
5146
     section symbol winds up in the output section.  */
5147
0
        if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
5148
0
    rel->r_addend += sec->output_offset + sym->st_value;
5149
5150
0
        continue;
5151
0
      }
5152
0
  }
5153
0
      else
5154
0
  {
5155
    /* External symbol.  */
5156
0
    if (bfd_link_relocatable (info))
5157
0
      continue;
5158
0
    bool warned, ignored, unresolved_reloc;
5159
0
    int symndx = r_symndx - symtab_hdr->sh_info;
5160
5161
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
5162
0
           r_symndx, symtab_hdr, sym_hashes, h, sec,
5163
0
           relocation, unresolved_reloc, warned,
5164
0
           ignored);
5165
5166
    /* keep symbol location for static TLS_IE GOT entry  */
5167
0
    relocation_sym = relocation;
5168
5169
    /* la $fp, _FP_BASE_ is per-function (region).
5170
       Handle it specially.  */
5171
0
    switch ((int) r_type)
5172
0
      {
5173
0
      case R_NDS32_HI20_RELA:
5174
0
      case R_NDS32_LO12S0_RELA:
5175
0
        if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5176
0
        FP_BASE_NAME) == 0)
5177
0
    {
5178
0
      if (!bfd_link_pie (info))
5179
0
        {
5180
0
          _bfd_error_handler
5181
0
      ("%pB: warning: _FP_BASE_ setting insns relaxation failed.",
5182
0
       input_bfd);
5183
0
        }
5184
0
      relocation = fpbase_addr;
5185
0
    }
5186
0
        break;
5187
0
      case R_NDS32_SDA19S0_RELA:
5188
0
      case R_NDS32_SDA15S0_RELA:
5189
0
      case R_NDS32_20_RELA:
5190
0
        if (strcmp (elf_sym_hashes (input_bfd)[symndx]->root.root.string,
5191
0
        FP_BASE_NAME) == 0)
5192
0
    {
5193
0
      relocation = fpbase_addr;
5194
0
      break;
5195
0
    }
5196
0
      }
5197
0
  }
5198
5199
      /* Sanity check the address.  */
5200
0
      if (offset > high_address)
5201
0
  {
5202
0
    r = bfd_reloc_outofrange;
5203
0
    goto check_reloc;
5204
0
  }
5205
5206
0
      if (r_type >= R_NDS32_RELAX_ENTRY)
5207
0
  continue;
5208
5209
0
      switch ((int) r_type)
5210
0
  {
5211
0
  case R_NDS32_GOTOFF:
5212
    /* Relocation is relative to the start of the global offset
5213
       table (for ld24 rx, #uimm24), e.g. access at label+addend
5214
5215
       ld24 rx. #label@GOTOFF + addend
5216
       sub  rx, r12.  */
5217
0
  case R_NDS32_GOTOFF_HI20:
5218
0
  case R_NDS32_GOTOFF_LO12:
5219
0
  case R_NDS32_GOTOFF_LO15:
5220
0
  case R_NDS32_GOTOFF_LO19:
5221
0
    BFD_ASSERT (sgot != NULL);
5222
5223
0
    relocation -= elf_gp (output_bfd);
5224
0
    break;
5225
5226
0
  case R_NDS32_9_PLTREL:
5227
0
  case R_NDS32_25_PLTREL:
5228
    /* Relocation is to the entry for this symbol in the
5229
       procedure linkage table.  */
5230
5231
    /* The native assembler will generate a 25_PLTREL reloc
5232
       for a local symbol if you assemble a call from one
5233
       section to another when using -K pic.  */
5234
0
    if (h == NULL)
5235
0
      break;
5236
5237
0
    if (h->forced_local)
5238
0
      break;
5239
5240
    /* We didn't make a PLT entry for this symbol.  This
5241
       happens when statically linking PIC code, or when
5242
       using -Bsymbolic.  */
5243
0
    if (h->plt.offset == (bfd_vma) - 1)
5244
0
      break;
5245
5246
0
    relocation = (splt->output_section->vma
5247
0
      + splt->output_offset + h->plt.offset);
5248
0
    break;
5249
5250
0
  case R_NDS32_PLT_GOTREL_HI20:
5251
0
  case R_NDS32_PLT_GOTREL_LO12:
5252
0
  case R_NDS32_PLT_GOTREL_LO15:
5253
0
  case R_NDS32_PLT_GOTREL_LO19:
5254
0
  case R_NDS32_PLT_GOTREL_LO20:
5255
0
    if (h == NULL
5256
0
        || h->forced_local
5257
0
        || h->plt.offset == (bfd_vma) -1
5258
0
        || (bfd_link_pie (info) && h->def_regular))
5259
0
      {
5260
        /* Maybe we should find better checking to optimize
5261
     PIE PLT relocations.  */
5262
        /* We didn't make a PLT entry for this symbol.  This
5263
     happens when statically linking PIC code, or when
5264
     using -Bsymbolic.  */
5265
0
        if (h)
5266
0
    h->plt.offset = (bfd_vma) -1;   /* Cancel PLT trampoline.  */
5267
0
        relocation -= elf_gp (output_bfd);
5268
0
        break;
5269
0
      }
5270
5271
0
    relocation = (splt->output_section->vma
5272
0
      + splt->output_offset + h->plt.offset);
5273
5274
0
    relocation -= elf_gp (output_bfd);
5275
0
    break;
5276
5277
0
  case R_NDS32_PLTREL_HI20:
5278
0
  case R_NDS32_PLTREL_LO12:
5279
5280
    /* Relocation is to the entry for this symbol in the
5281
       procedure linkage table.  */
5282
5283
    /* The native assembler will generate a 25_PLTREL reloc
5284
       for a local symbol if you assemble a call from one
5285
       section to another when using -K pic.  */
5286
0
    if (h == NULL)
5287
0
      break;
5288
5289
0
    if (h->forced_local)
5290
0
      break;
5291
5292
0
    if (h->plt.offset == (bfd_vma) - 1)
5293
      /* We didn't make a PLT entry for this symbol.  This
5294
         happens when statically linking PIC code, or when
5295
         using -Bsymbolic.  */
5296
0
      break;
5297
5298
0
    if (splt == NULL)
5299
0
      break;
5300
5301
0
    relocation = (splt->output_section->vma
5302
0
      + splt->output_offset
5303
0
      + h->plt.offset + 4)
5304
0
           - (input_section->output_section->vma
5305
0
        + input_section->output_offset
5306
0
        + rel->r_offset);
5307
5308
0
    break;
5309
5310
0
  case R_NDS32_GOTPC20:
5311
    /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5312
       ld24 rx,#_GLOBAL_OFFSET_TABLE_  */
5313
0
    relocation = elf_gp (output_bfd);
5314
0
    break;
5315
5316
0
  case R_NDS32_GOTPC_HI20:
5317
0
  case R_NDS32_GOTPC_LO12:
5318
    /* .got(_GLOBAL_OFFSET_TABLE_) - pc relocation
5319
       bl .+4
5320
       seth rx,#high(_GLOBAL_OFFSET_TABLE_)
5321
       or3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)
5322
       or
5323
       bl .+4
5324
       seth rx,#shigh(_GLOBAL_OFFSET_TABLE_)
5325
       add3 rx,rx,#low(_GLOBAL_OFFSET_TABLE_ +4)  */
5326
0
    relocation = elf_gp (output_bfd);
5327
0
    relocation -= (input_section->output_section->vma
5328
0
       + input_section->output_offset + rel->r_offset);
5329
0
    break;
5330
5331
0
  case R_NDS32_GOT20:
5332
    /* Fall through.  */
5333
0
  case R_NDS32_GOT_HI20:
5334
0
  case R_NDS32_GOT_LO12:
5335
0
  case R_NDS32_GOT_LO15:
5336
0
  case R_NDS32_GOT_LO19:
5337
    /* Relocation is to the entry for this symbol in the global
5338
       offset table.  */
5339
0
    BFD_ASSERT (sgot != NULL);
5340
5341
0
    if (h != NULL)
5342
0
      {
5343
        /* External symbol  */
5344
0
        bool dyn;
5345
5346
0
        off = h->got.offset;
5347
0
        BFD_ASSERT (off != (bfd_vma) - 1);
5348
0
        dyn = htab->root.dynamic_sections_created;
5349
0
        if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
5350
0
                bfd_link_pic (info),
5351
0
                h)
5352
0
      || (bfd_link_pic (info)
5353
0
          && (info->symbolic
5354
0
        || h->dynindx == -1
5355
0
        || h->forced_local) && h->def_regular))
5356
0
    {
5357
      /* This is actually a static link, or it is a
5358
         -Bsymbolic link and the symbol is defined
5359
         locally, or the symbol was forced to be local
5360
         because of a version file.  We must initialize
5361
         this entry in the global offset table.  Since the
5362
         offset must always be a multiple of 4, we use the
5363
         least significant bit to record whether we have
5364
         initialized it already.
5365
5366
         When doing a dynamic link, we create a .rela.got
5367
         relocation entry to initialize the value.  This
5368
         is done in the finish_dynamic_symbol routine.  */
5369
0
      if ((off & 1) != 0) /* clear LSB  */
5370
0
        off &= ~1;
5371
0
      else
5372
0
        {
5373
0
          bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5374
0
          h->got.offset |= 1;
5375
0
        }
5376
0
    }
5377
0
        relocation = sgot->output_section->vma + sgot->output_offset + off
5378
0
         - elf_gp (output_bfd);
5379
0
      }
5380
0
    else
5381
0
      {
5382
        /* Local symbol  */
5383
0
        bfd_byte *loc;
5384
5385
0
        BFD_ASSERT (local_got_offsets != NULL
5386
0
        && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5387
5388
0
        off = local_got_offsets[r_symndx];
5389
5390
        /* The offset must always be a multiple of 4.  We use
5391
     the least significant bit to record whether we have
5392
     already processed this entry.  */
5393
0
        if ((off & 1) != 0) /* clear LSB  */
5394
0
    off &= ~1;
5395
0
        else
5396
0
    {
5397
0
      bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5398
5399
0
      if (bfd_link_pic (info))
5400
0
        {
5401
0
          asection *srelgot;
5402
0
          Elf_Internal_Rela outrel;
5403
5404
          /* We need to generate a R_NDS32_RELATIVE reloc
5405
       for the dynamic linker.  */
5406
0
          srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5407
0
          BFD_ASSERT (srelgot != NULL);
5408
5409
0
          outrel.r_offset = (elf_gp (output_bfd)
5410
0
           + sgot->output_offset + off);
5411
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5412
0
          outrel.r_addend = relocation;
5413
0
          loc = srelgot->contents;
5414
0
          loc +=
5415
0
      srelgot->reloc_count * sizeof (Elf32_External_Rela);
5416
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5417
0
          ++srelgot->reloc_count;
5418
0
        }
5419
0
      local_got_offsets[r_symndx] |= 1;
5420
0
    }
5421
0
        relocation = sgot->output_section->vma + sgot->output_offset + off
5422
0
         - elf_gp (output_bfd);
5423
0
      }
5424
5425
0
    break;
5426
5427
0
  case R_NDS32_16_RELA:
5428
0
  case R_NDS32_20_RELA:
5429
0
  case R_NDS32_5_RELA:
5430
0
  case R_NDS32_32_RELA:
5431
0
  case R_NDS32_9_PCREL_RELA:
5432
0
  case R_NDS32_WORD_9_PCREL_RELA:
5433
0
  case R_NDS32_10_UPCREL_RELA:
5434
0
  case R_NDS32_15_PCREL_RELA:
5435
0
  case R_NDS32_17_PCREL_RELA:
5436
0
  case R_NDS32_25_PCREL_RELA:
5437
0
  case R_NDS32_HI20_RELA:
5438
0
  case R_NDS32_LO12S3_RELA:
5439
0
  case R_NDS32_LO12S2_RELA:
5440
0
  case R_NDS32_LO12S2_DP_RELA:
5441
0
  case R_NDS32_LO12S2_SP_RELA:
5442
0
  case R_NDS32_LO12S1_RELA:
5443
0
  case R_NDS32_LO12S0_RELA:
5444
0
  case R_NDS32_LO12S0_ORI_RELA:
5445
0
    if (bfd_link_pic (info) && r_symndx != 0
5446
0
        && (input_section->flags & SEC_ALLOC) != 0
5447
0
        && (eliminate_gc_relocs == 0
5448
0
      || (sec && (sec->flags & SEC_EXCLUDE) == 0))
5449
0
        && ((r_type != R_NDS32_9_PCREL_RELA
5450
0
       && r_type != R_NDS32_WORD_9_PCREL_RELA
5451
0
       && r_type != R_NDS32_10_UPCREL_RELA
5452
0
       && r_type != R_NDS32_15_PCREL_RELA
5453
0
       && r_type != R_NDS32_17_PCREL_RELA
5454
0
       && r_type != R_NDS32_25_PCREL_RELA
5455
0
       && !(r_type == R_NDS32_32_RELA
5456
0
      && strcmp (input_section->name, ".eh_frame") == 0))
5457
0
      || (h != NULL && h->dynindx != -1
5458
0
          && (!info->symbolic || !h->def_regular))))
5459
0
      {
5460
0
        Elf_Internal_Rela outrel;
5461
0
        bool skip, relocate;
5462
0
        bfd_byte *loc;
5463
5464
        /* When generating a shared object, these relocations
5465
     are copied into the output file to be resolved at run
5466
     time.  */
5467
5468
0
        if (sreloc == NULL)
5469
0
    {
5470
0
      const char *name;
5471
5472
0
      name = bfd_elf_string_from_elf_section
5473
0
        (input_bfd, elf_elfheader (input_bfd)->e_shstrndx,
5474
0
         elf_section_data (input_section)->rela.hdr->sh_name);
5475
0
      if (name == NULL)
5476
0
        return false;
5477
5478
0
      BFD_ASSERT (startswith (name, ".rela")
5479
0
            && strcmp (bfd_section_name (input_section),
5480
0
           name + 5) == 0);
5481
5482
0
      sreloc = bfd_get_section_by_name (dynobj, name);
5483
0
      BFD_ASSERT (sreloc != NULL);
5484
0
    }
5485
5486
0
        skip = false;
5487
0
        relocate = false;
5488
5489
0
        outrel.r_offset = _bfd_elf_section_offset (output_bfd,
5490
0
               info,
5491
0
               input_section,
5492
0
               rel->r_offset);
5493
0
        if (outrel.r_offset == (bfd_vma) - 1)
5494
0
    skip = true;
5495
0
        else if (outrel.r_offset == (bfd_vma) - 2)
5496
0
    skip = true, relocate = true;
5497
0
        outrel.r_offset += (input_section->output_section->vma
5498
0
          + input_section->output_offset);
5499
5500
0
        if (skip)
5501
0
    memset (&outrel, 0, sizeof outrel);
5502
0
        else if (r_type == R_NDS32_17_PCREL_RELA
5503
0
           || r_type == R_NDS32_15_PCREL_RELA
5504
0
           || r_type == R_NDS32_25_PCREL_RELA)
5505
0
    {
5506
0
      BFD_ASSERT (h != NULL && h->dynindx != -1);
5507
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5508
0
      outrel.r_addend = rel->r_addend;
5509
0
    }
5510
0
        else
5511
0
    {
5512
      /* h->dynindx may be -1 if this symbol was marked to
5513
         become local.  */
5514
0
      if (h == NULL
5515
0
          || ((info->symbolic || h->dynindx == -1)
5516
0
        && h->def_regular)
5517
0
          || (bfd_link_pie (info) && h->def_regular))
5518
0
        {
5519
0
          relocate = true;
5520
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5521
0
          outrel.r_addend = relocation + rel->r_addend;
5522
5523
0
          if (h)
5524
0
      {
5525
0
        h->plt.offset = (bfd_vma) -1;   /* cancel PLT trampoline.  */
5526
5527
0
        BFD_ASSERT (sgot != NULL);
5528
        /* If we did not allocate got entry for the symbol,
5529
           we can not fill the nonexistent got entry.  */
5530
0
        if (h->got.offset != (bfd_vma) -1
5531
0
            && (h->got.offset & 1) == 0)
5532
0
          {
5533
0
            bfd_put_32 (output_bfd, outrel.r_addend,
5534
0
            sgot->contents + h->got.offset);
5535
0
          }
5536
0
      }
5537
0
        }
5538
0
      else
5539
0
        {
5540
0
          if (h->dynindx == -1)
5541
0
      {
5542
0
        _bfd_error_handler
5543
0
          (_("%pB: relocation %s against `%s' can not be used when "
5544
0
             "making a shared object; recompile with -fPIC"),
5545
0
           input_bfd, nds32_elf_howto_table[r_type].name, h->root.root.string);
5546
0
        bfd_set_error (bfd_error_bad_value);
5547
0
        return false;
5548
0
      }
5549
5550
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
5551
0
          outrel.r_addend = rel->r_addend;
5552
0
        }
5553
0
    }
5554
5555
0
        loc = sreloc->contents;
5556
0
        loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
5557
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5558
0
        ++sreloc->reloc_count;
5559
5560
        /* If this reloc is against an external symbol, we do
5561
     not want to fiddle with the addend.  Otherwise, we
5562
     need to include the symbol value so that it becomes
5563
     an addend for the dynamic reloc.  */
5564
0
        if (!relocate)
5565
0
    continue;
5566
0
      }
5567
0
    break;
5568
5569
0
  case R_NDS32_25_ABS_RELA:
5570
0
    if (bfd_link_pic (info))
5571
0
      {
5572
0
        _bfd_error_handler
5573
0
    (_("%pB: warning: %s unsupported in shared mode"),
5574
0
     input_bfd, "R_NDS32_25_ABS_RELA");
5575
0
        return false;
5576
0
      }
5577
0
    break;
5578
5579
0
  case R_NDS32_9_PCREL:
5580
0
    r = nds32_elf_do_9_pcrel_reloc (input_bfd, howto, input_section,
5581
0
            contents, offset,
5582
0
            sec, relocation, addend);
5583
0
    goto check_reloc;
5584
5585
0
  case R_NDS32_HI20:
5586
    /* We allow an arbitrary number of HI20 relocs before the
5587
       LO12 reloc.  This permits gcc to emit the HI and LO relocs
5588
       itself.  */
5589
0
    for (lorel = rel + 1;
5590
0
         (lorel < relend
5591
0
    && ELF32_R_TYPE (lorel->r_info) == R_NDS32_HI20); lorel++)
5592
0
      continue;
5593
0
    if (lorel < relend
5594
0
        && (ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S3
5595
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S2
5596
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S1
5597
0
      || ELF32_R_TYPE (lorel->r_info) == R_NDS32_LO12S0))
5598
0
      {
5599
0
        nds32_elf_relocate_hi20 (input_bfd, r_type, rel, lorel,
5600
0
               contents, relocation + addend);
5601
0
        r = bfd_reloc_ok;
5602
0
      }
5603
0
    else
5604
0
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5605
0
            contents, offset, relocation,
5606
0
            addend);
5607
0
    goto check_reloc;
5608
5609
0
  case R_NDS32_GOT17S2_RELA:
5610
0
  case R_NDS32_GOT15S2_RELA:
5611
0
    BFD_ASSERT (sgot != NULL);
5612
5613
0
    if (h != NULL)
5614
0
      {
5615
0
        bool dyn;
5616
5617
0
        off = h->got.offset;
5618
0
        BFD_ASSERT (off != (bfd_vma) - 1);
5619
5620
0
        dyn = htab->root.dynamic_sections_created;
5621
0
        if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL
5622
0
      (dyn, bfd_link_pic (info), h)
5623
0
      || (bfd_link_pic (info)
5624
0
          && (info->symbolic
5625
0
        || h->dynindx == -1
5626
0
        || h->forced_local)
5627
0
          && h->def_regular))
5628
0
    {
5629
      /* This is actually a static link, or it is a
5630
         -Bsymbolic link and the symbol is defined
5631
         locally, or the symbol was forced to be local
5632
         because of a version file.  We must initialize
5633
         this entry in the global offset table.  Since the
5634
         offset must always be a multiple of 4, we use the
5635
         least significant bit to record whether we have
5636
         initialized it already.
5637
5638
         When doing a dynamic link, we create a .rela.got
5639
         relocation entry to initialize the value.  This
5640
         is done in the finish_dynamic_symbol routine.  */
5641
0
      if ((off & 1) != 0)
5642
0
        off &= ~1;
5643
0
      else
5644
0
        {
5645
0
          bfd_put_32 (output_bfd, relocation,
5646
0
          sgot->contents + off);
5647
0
          h->got.offset |= 1;
5648
0
        }
5649
0
    }
5650
0
      }
5651
0
    else
5652
0
      {
5653
0
        bfd_byte *loc;
5654
5655
0
        BFD_ASSERT (local_got_offsets != NULL
5656
0
        && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5657
5658
0
        off = local_got_offsets[r_symndx];
5659
5660
        /* The offset must always be a multiple of 4.  We use
5661
     the least significant bit to record whether we have
5662
     already processed this entry.  */
5663
0
        if ((off & 1) != 0)
5664
0
    off &= ~1;
5665
0
        else
5666
0
    {
5667
0
      bfd_put_32 (output_bfd, relocation, sgot->contents + off);
5668
5669
0
      if (bfd_link_pic (info))
5670
0
        {
5671
0
          asection *srelgot;
5672
0
          Elf_Internal_Rela outrel;
5673
5674
          /* We need to generate a R_NDS32_RELATIVE reloc
5675
       for the dynamic linker.  */
5676
0
          srelgot = bfd_get_section_by_name (dynobj, ".rela.got");
5677
0
          BFD_ASSERT (srelgot != NULL);
5678
5679
0
          outrel.r_offset = (elf_gp (output_bfd)
5680
0
           + sgot->output_offset + off);
5681
0
          outrel.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
5682
0
          outrel.r_addend = relocation;
5683
0
          loc = srelgot->contents;
5684
0
          loc +=
5685
0
      srelgot->reloc_count * sizeof (Elf32_External_Rela);
5686
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5687
0
          ++srelgot->reloc_count;
5688
0
        }
5689
0
      local_got_offsets[r_symndx] |= 1;
5690
0
    }
5691
0
      }
5692
0
    relocation = sgot->output_section->vma + sgot->output_offset + off
5693
0
      - elf_gp (output_bfd);
5694
5695
0
    if (relocation & align)
5696
0
      {
5697
        /* Incorrect alignment.  */
5698
0
        _bfd_error_handler
5699
0
    (_("%pB: warning: unaligned access to GOT entry"), input_bfd);
5700
0
        ret = false;
5701
0
        r = bfd_reloc_dangerous;
5702
0
        goto check_reloc;
5703
0
      }
5704
0
    break;
5705
5706
0
  case R_NDS32_SDA16S3_RELA:
5707
0
  case R_NDS32_SDA15S3_RELA:
5708
0
  case R_NDS32_SDA15S3:
5709
0
    align = 0x7;
5710
0
    goto handle_sda;
5711
5712
0
  case R_NDS32_SDA17S2_RELA:
5713
0
  case R_NDS32_SDA15S2_RELA:
5714
0
  case R_NDS32_SDA12S2_SP_RELA:
5715
0
  case R_NDS32_SDA12S2_DP_RELA:
5716
0
  case R_NDS32_SDA15S2:
5717
0
  case R_NDS32_SDA_FP7U2_RELA:
5718
0
    align = 0x3;
5719
0
    goto handle_sda;
5720
5721
0
  case R_NDS32_SDA18S1_RELA:
5722
0
  case R_NDS32_SDA15S1_RELA:
5723
0
  case R_NDS32_SDA15S1:
5724
0
    align = 0x1;
5725
0
    goto handle_sda;
5726
5727
0
  case R_NDS32_SDA19S0_RELA:
5728
0
  case R_NDS32_SDA15S0_RELA:
5729
0
  case R_NDS32_SDA15S0:
5730
0
    align = 0x0;
5731
0
  handle_sda:
5732
0
    BFD_ASSERT (sec != NULL);
5733
5734
    /* If the symbol is in the abs section, the out_bfd will be null.
5735
       This happens when the relocation has a symbol@GOTOFF.  */
5736
0
    r = nds32_elf_final_sda_base (output_bfd, info, &gp, false);
5737
0
    if (r != bfd_reloc_ok)
5738
0
      {
5739
0
        _bfd_error_handler
5740
0
    (_("%pB: warning: relocate SDA_BASE failed"), input_bfd);
5741
0
        ret = false;
5742
0
        goto check_reloc;
5743
0
      }
5744
5745
    /* At this point `relocation' contains the object's
5746
       address.  */
5747
0
    if (r_type == R_NDS32_SDA_FP7U2_RELA)
5748
0
      {
5749
0
        relocation -= fpbase_addr;
5750
0
      }
5751
0
    else
5752
0
      relocation -= gp;
5753
    /* Now it contains the offset from _SDA_BASE_.  */
5754
5755
    /* Make sure alignment is correct.  */
5756
5757
0
    if (relocation & align)
5758
0
      {
5759
        /* Incorrect alignment.  */
5760
0
        _bfd_error_handler
5761
    /* xgettext:c-format */
5762
0
    (_("%pB(%pA): warning: unaligned small data access"
5763
0
       " of type %d"),
5764
0
     input_bfd, input_section, r_type);
5765
0
        ret = false;
5766
0
        goto check_reloc;
5767
0
      }
5768
0
    break;
5769
5770
0
  case R_NDS32_17IFC_PCREL_RELA:
5771
0
  case R_NDS32_10IFCU_PCREL_RELA:
5772
    /* Do nothing.  */
5773
0
    break;
5774
5775
0
  case R_NDS32_TLS_LE_HI20:
5776
0
  case R_NDS32_TLS_LE_LO12:
5777
0
  case R_NDS32_TLS_LE_20:
5778
0
  case R_NDS32_TLS_LE_15S0:
5779
0
  case R_NDS32_TLS_LE_15S1:
5780
0
  case R_NDS32_TLS_LE_15S2:
5781
    /* We do not have garbage collection for got entries.
5782
       Therefore, IE to LE may have one empty entry, and DESC to
5783
       LE may have two.  */
5784
0
    if (elf_hash_table (info)->tls_sec != NULL)
5785
0
      relocation -= (elf_hash_table (info)->tls_sec->vma + TP_OFFSET);
5786
0
    break;
5787
5788
0
  case R_NDS32_TLS_IE_HI20:
5789
0
  case R_NDS32_TLS_IE_LO12S2:
5790
0
  case R_NDS32_TLS_DESC_HI20:
5791
0
  case R_NDS32_TLS_DESC_LO12:
5792
0
  case R_NDS32_TLS_IE_LO12:
5793
0
  case R_NDS32_TLS_IEGP_HI20:
5794
0
  case R_NDS32_TLS_IEGP_LO12:
5795
0
  case R_NDS32_TLS_IEGP_LO12S2:
5796
0
    {
5797
      /* Relocation is to the entry for this symbol in the global
5798
         offset table.  */
5799
0
      enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
5800
0
      asection *srelgot;
5801
0
      Elf_Internal_Rela outrel;
5802
0
      bfd_byte *loc;
5803
0
      int indx = 0;
5804
5805
0
      eff_tls_type = org_tls_type = get_tls_type (r_type, h);
5806
5807
0
      BFD_ASSERT (sgot != NULL);
5808
0
      if (h != NULL)
5809
0
        {
5810
0
    bool dyn;
5811
5812
0
    off = h->got.offset;
5813
0
    BFD_ASSERT (off != (bfd_vma) -1);
5814
0
    dyn = htab->root.dynamic_sections_created;
5815
0
    tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
5816
0
    if (WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
5817
0
        && (!bfd_link_pic (info)
5818
0
      || !SYMBOL_REFERENCES_LOCAL (info, h)))
5819
0
      indx = h->dynindx;
5820
0
        }
5821
0
      else
5822
0
        {
5823
0
    BFD_ASSERT (local_got_offsets != NULL
5824
0
          && local_got_offsets[r_symndx] != (bfd_vma) - 1);
5825
0
    off = local_got_offsets[r_symndx];
5826
0
    tls_type = elf32_nds32_local_got_tls_type (input_bfd)[r_symndx];
5827
0
        }
5828
5829
0
      relocation = sgot->output_section->vma + sgot->output_offset + off;
5830
5831
0
      if (1 < ones32 (tls_type))
5832
0
        {
5833
0
    eff_tls_type = 1 << (fls (tls_type) - 1);
5834
    /* TLS model shall be handled in nds32_elf_unify_tls_model ().  */
5835
5836
    /* TLS model X -> LE is not implement yet!
5837
       workaround here!  */
5838
0
    if (eff_tls_type == GOT_TLS_LE)
5839
0
      {
5840
0
        eff_tls_type = 1 << (fls (tls_type ^ eff_tls_type) - 1);
5841
0
      }
5842
0
        }
5843
5844
      /* The offset must always be a multiple of 4.  We use
5845
         the least significant bit to record whether we have
5846
         already processed this entry.  */
5847
0
      bool need_relocs = false;
5848
0
      srelgot = ehtab->srelgot;
5849
0
      if ((bfd_link_pic (info) || indx != 0)
5850
0
    && (h == NULL || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
5851
0
        || h->root.type != bfd_link_hash_undefweak))
5852
0
        {
5853
0
    need_relocs = true;
5854
0
    BFD_ASSERT (srelgot != NULL);
5855
0
        }
5856
5857
0
      if (off & 1)
5858
0
        {
5859
0
    off &= ~1;
5860
0
    relocation &= ~1;
5861
5862
0
    if (eff_tls_type & GOT_TLS_DESC)
5863
0
      {
5864
0
        relocation -= elf_gp (output_bfd);
5865
0
        if ((R_NDS32_TLS_DESC_HI20 == r_type) && (!need_relocs))
5866
0
          {
5867
      /* TLS model shall be converted.  */
5868
0
      BFD_ASSERT(0);
5869
0
          }
5870
0
      }
5871
0
    else if (eff_tls_type & GOT_TLS_IEGP)
5872
0
      {
5873
0
        relocation -= elf_gp (output_bfd);
5874
0
      }
5875
0
        }
5876
0
      else
5877
0
        {
5878
0
    if ((eff_tls_type & GOT_TLS_LE) && (tls_type ^ eff_tls_type))
5879
0
      {
5880
        /* TLS model workaround shall be applied.  */
5881
0
        BFD_ASSERT(0);
5882
0
      }
5883
0
    else if (eff_tls_type & (GOT_TLS_IE | GOT_TLS_IEGP))
5884
0
      {
5885
0
        if (eff_tls_type & GOT_TLS_IEGP)
5886
0
          relocation -= elf_gp(output_bfd);
5887
5888
0
        if (need_relocs)
5889
0
          {
5890
0
      if (indx == 0)
5891
0
        outrel.r_addend = gottpoff (info, relocation_sym);
5892
0
      else
5893
0
        outrel.r_addend = 0;
5894
0
      outrel.r_offset = (sgot->output_section->vma
5895
0
             + sgot->output_offset + off);
5896
0
      outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_TPOFF);
5897
5898
0
      elf32_nds32_add_dynreloc (output_bfd, info, srelgot,
5899
0
              &outrel);
5900
0
          }
5901
0
        else
5902
0
          {
5903
0
      bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5904
0
            sgot->contents + off);
5905
0
          }
5906
0
      }
5907
0
    else if (eff_tls_type & GOT_TLS_DESC)
5908
0
      {
5909
0
        relocation -= elf_gp (output_bfd);
5910
0
        if (need_relocs)
5911
0
          {
5912
0
      if (indx == 0)
5913
0
        outrel.r_addend = gottpoff (info, relocation_sym);
5914
0
      else
5915
0
        outrel.r_addend = 0;
5916
0
      outrel.r_offset = (sgot->output_section->vma
5917
0
             + sgot->output_offset + off);
5918
0
      outrel.r_info = ELF32_R_INFO (indx, R_NDS32_TLS_DESC);
5919
5920
0
      if (htab->tls_desc_trampoline)
5921
0
        {
5922
0
          asection *srelplt;
5923
0
          srelplt = ehtab->srelplt;
5924
0
          loc = srelplt->contents;
5925
0
          loc += htab->next_tls_desc_index++ * sizeof (Elf32_External_Rela);
5926
0
          BFD_ASSERT (loc + sizeof (Elf32_External_Rela)
5927
0
          <= srelplt->contents + srelplt->size);
5928
5929
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5930
0
        }
5931
0
      else
5932
0
        {
5933
0
          loc = srelgot->contents;
5934
0
          loc += srelgot->reloc_count * sizeof (Elf32_External_Rela);
5935
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5936
0
          ++srelgot->reloc_count;
5937
0
        }
5938
0
          }
5939
0
        else
5940
0
          {
5941
      /* feed me!  */
5942
0
      bfd_put_32 (output_bfd, 0xdeadbeef,
5943
0
            sgot->contents + off);
5944
0
      bfd_put_32 (output_bfd, gottpoff (info, relocation_sym),
5945
0
            sgot->contents + off + 4);
5946
0
      patch_tls_desc_to_ie (contents, rel, input_bfd);
5947
0
      BFD_ASSERT(0);
5948
0
          }
5949
0
      }
5950
0
    else
5951
0
      {
5952
        /* TLS model workaround shall be applied.  */
5953
0
        BFD_ASSERT(0);
5954
0
      }
5955
5956
0
    if (h != NULL)
5957
0
      h->got.offset |= 1;
5958
0
    else
5959
0
      local_got_offsets[r_symndx] |= 1;
5960
0
        }
5961
0
    }
5962
0
  break;
5963
    /* DON'T fall through.  */
5964
5965
0
  default:
5966
    /* OLD_NDS32_RELOC.  */
5967
5968
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
5969
0
          contents, offset, relocation, addend);
5970
0
    goto check_reloc;
5971
0
  }
5972
5973
0
      switch ((int) r_type)
5974
0
  {
5975
0
  case R_NDS32_20_RELA:
5976
0
  case R_NDS32_5_RELA:
5977
0
  case R_NDS32_9_PCREL_RELA:
5978
0
  case R_NDS32_WORD_9_PCREL_RELA:
5979
0
  case R_NDS32_10_UPCREL_RELA:
5980
0
  case R_NDS32_15_PCREL_RELA:
5981
0
  case R_NDS32_17_PCREL_RELA:
5982
0
  case R_NDS32_25_PCREL_RELA:
5983
0
  case R_NDS32_25_ABS_RELA:
5984
0
  case R_NDS32_HI20_RELA:
5985
0
  case R_NDS32_LO12S3_RELA:
5986
0
  case R_NDS32_LO12S2_RELA:
5987
0
  case R_NDS32_LO12S2_DP_RELA:
5988
0
  case R_NDS32_LO12S2_SP_RELA:
5989
0
  case R_NDS32_LO12S1_RELA:
5990
0
  case R_NDS32_LO12S0_RELA:
5991
0
  case R_NDS32_LO12S0_ORI_RELA:
5992
0
  case R_NDS32_SDA16S3_RELA:
5993
0
  case R_NDS32_SDA17S2_RELA:
5994
0
  case R_NDS32_SDA18S1_RELA:
5995
0
  case R_NDS32_SDA19S0_RELA:
5996
0
  case R_NDS32_SDA15S3_RELA:
5997
0
  case R_NDS32_SDA15S2_RELA:
5998
0
  case R_NDS32_SDA12S2_DP_RELA:
5999
0
  case R_NDS32_SDA12S2_SP_RELA:
6000
0
  case R_NDS32_SDA15S1_RELA:
6001
0
  case R_NDS32_SDA15S0_RELA:
6002
0
  case R_NDS32_SDA_FP7U2_RELA:
6003
0
  case R_NDS32_9_PLTREL:
6004
0
  case R_NDS32_25_PLTREL:
6005
0
  case R_NDS32_GOT20:
6006
0
  case R_NDS32_GOT_HI20:
6007
0
  case R_NDS32_GOT_LO12:
6008
0
  case R_NDS32_GOT_LO15:
6009
0
  case R_NDS32_GOT_LO19:
6010
0
  case R_NDS32_GOT15S2_RELA:
6011
0
  case R_NDS32_GOT17S2_RELA:
6012
0
  case R_NDS32_GOTPC20:
6013
0
  case R_NDS32_GOTPC_HI20:
6014
0
  case R_NDS32_GOTPC_LO12:
6015
0
  case R_NDS32_GOTOFF:
6016
0
  case R_NDS32_GOTOFF_HI20:
6017
0
  case R_NDS32_GOTOFF_LO12:
6018
0
  case R_NDS32_GOTOFF_LO15:
6019
0
  case R_NDS32_GOTOFF_LO19:
6020
0
  case R_NDS32_PLTREL_HI20:
6021
0
  case R_NDS32_PLTREL_LO12:
6022
0
  case R_NDS32_PLT_GOTREL_HI20:
6023
0
  case R_NDS32_PLT_GOTREL_LO12:
6024
0
  case R_NDS32_PLT_GOTREL_LO15:
6025
0
  case R_NDS32_PLT_GOTREL_LO19:
6026
0
  case R_NDS32_PLT_GOTREL_LO20:
6027
0
  case R_NDS32_17IFC_PCREL_RELA:
6028
0
  case R_NDS32_10IFCU_PCREL_RELA:
6029
0
  case R_NDS32_TLS_LE_HI20:
6030
0
  case R_NDS32_TLS_LE_LO12:
6031
0
  case R_NDS32_TLS_IE_HI20:
6032
0
  case R_NDS32_TLS_IE_LO12S2:
6033
0
  case R_NDS32_TLS_LE_20:
6034
0
  case R_NDS32_TLS_LE_15S0:
6035
0
  case R_NDS32_TLS_LE_15S1:
6036
0
  case R_NDS32_TLS_LE_15S2:
6037
0
  case R_NDS32_TLS_DESC_HI20:
6038
0
  case R_NDS32_TLS_DESC_LO12:
6039
0
  case R_NDS32_TLS_IE_LO12:
6040
0
  case R_NDS32_TLS_IEGP_HI20:
6041
0
  case R_NDS32_TLS_IEGP_LO12:
6042
0
  case R_NDS32_TLS_IEGP_LO12S2:
6043
    /* Instruction related relocs must handle endian properly.  */
6044
    /* NOTE: PIC IS NOT HANDLE YET; DO IT LATER.  */
6045
0
    r = nds32_elf_final_link_relocate (howto, input_bfd,
6046
0
               input_section, contents,
6047
0
               rel->r_offset, relocation,
6048
0
               rel->r_addend);
6049
0
    break;
6050
6051
0
  default:
6052
    /* All other relocs can use default handler.  */
6053
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
6054
0
          contents, rel->r_offset,
6055
0
          relocation, rel->r_addend);
6056
0
    break;
6057
0
  }
6058
6059
0
    check_reloc:
6060
6061
0
      if (r != bfd_reloc_ok)
6062
0
  {
6063
    /* FIXME: This should be generic enough to go in a utility.  */
6064
0
    const char *name;
6065
6066
0
    if (h != NULL)
6067
0
      name = h->root.root.string;
6068
0
    else
6069
0
      {
6070
0
        name = bfd_elf_string_from_elf_section
6071
0
          (input_bfd, symtab_hdr->sh_link, sym->st_name);
6072
0
        if (name == NULL || *name == '\0')
6073
0
    name = bfd_section_name (sec);
6074
0
      }
6075
6076
0
    if (errmsg != NULL)
6077
0
      goto common_error;
6078
6079
0
    switch (r)
6080
0
      {
6081
0
      case bfd_reloc_overflow:
6082
0
        (*info->callbacks->reloc_overflow)
6083
0
    (info, (h ? &h->root : NULL), name, howto->name,
6084
0
     (bfd_vma) 0, input_bfd, input_section, offset);
6085
0
        break;
6086
6087
0
      case bfd_reloc_undefined:
6088
0
        (*info->callbacks->undefined_symbol)
6089
0
    (info, name, input_bfd, input_section, offset, true);
6090
0
        break;
6091
6092
0
      case bfd_reloc_outofrange:
6093
0
        errmsg = _("internal error: out of range error");
6094
0
        goto common_error;
6095
6096
0
      case bfd_reloc_notsupported:
6097
0
        errmsg = _("internal error: unsupported relocation error");
6098
0
        goto common_error;
6099
6100
0
      case bfd_reloc_dangerous:
6101
0
        errmsg = _("internal error: dangerous error");
6102
0
        goto common_error;
6103
6104
0
      default:
6105
0
        errmsg = _("internal error: unknown error");
6106
        /* Fall through.  */
6107
6108
0
      common_error:
6109
0
        (*info->callbacks->warning) (info, errmsg, name, input_bfd,
6110
0
             input_section, offset);
6111
0
        break;
6112
0
      }
6113
0
  }
6114
0
    }
6115
6116
  /* Resotre header size to avoid overflow load.  */
6117
0
  if (elf_nds32_tdata (input_bfd)->hdr_size != 0)
6118
0
    symtab_hdr->sh_size = elf_nds32_tdata (input_bfd)->hdr_size;
6119
6120
0
  return ret;
6121
0
}
6122
6123
/* Finish up dynamic symbol handling.  We set the contents of various
6124
   dynamic sections here.  */
6125
6126
static bool
6127
nds32_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
6128
         struct elf_link_hash_entry *h, Elf_Internal_Sym *sym)
6129
0
{
6130
0
  struct elf_link_hash_table *ehtab;
6131
0
  struct elf_nds32_link_hash_entry *hent;
6132
0
  bfd_byte *loc;
6133
6134
0
  ehtab = elf_hash_table (info);
6135
0
  hent = (struct elf_nds32_link_hash_entry *) h;
6136
6137
0
  if (h->plt.offset != (bfd_vma) - 1)
6138
0
    {
6139
0
      asection *splt;
6140
0
      asection *sgot;
6141
0
      asection *srela;
6142
6143
0
      bfd_vma plt_index;
6144
0
      bfd_vma got_offset;
6145
0
      bfd_vma local_plt_offset;
6146
0
      Elf_Internal_Rela rela;
6147
6148
      /* This symbol has an entry in the procedure linkage table.  Set
6149
   it up.  */
6150
6151
0
      BFD_ASSERT (h->dynindx != -1);
6152
6153
0
      splt = ehtab->splt;
6154
0
      sgot = ehtab->sgotplt;
6155
0
      srela = ehtab->srelplt;
6156
0
      BFD_ASSERT (splt != NULL && sgot != NULL && srela != NULL);
6157
6158
      /* Get the index in the procedure linkage table which
6159
   corresponds to this symbol.  This is the index of this symbol
6160
   in all the symbols for which we are making plt entries.  The
6161
   first entry in the procedure linkage table is reserved.  */
6162
0
      plt_index = h->plt.offset / PLT_ENTRY_SIZE - 1;
6163
6164
      /* Get the offset into the .got table of the entry that
6165
   corresponds to this function.  Each .got entry is 4 bytes.
6166
   The first three are reserved.  */
6167
0
      got_offset = (plt_index + 3) * 4;
6168
6169
      /* Fill in the entry in the procedure linkage table.  */
6170
0
      if (!bfd_link_pic (info))
6171
0
  {
6172
0
    unsigned long insn;
6173
6174
0
    insn = PLT_ENTRY_WORD0 + (((sgot->output_section->vma
6175
0
              + sgot->output_offset + got_offset) >> 12)
6176
0
            & 0xfffff);
6177
0
    bfd_putb32 (insn, splt->contents + h->plt.offset);
6178
6179
0
    insn = PLT_ENTRY_WORD1 + (((sgot->output_section->vma
6180
0
              + sgot->output_offset + got_offset) & 0x0fff)
6181
0
            >> 2);
6182
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6183
6184
0
    insn = PLT_ENTRY_WORD2;
6185
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6186
6187
0
    insn = PLT_ENTRY_WORD3 + (plt_index & 0x7ffff);
6188
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6189
6190
0
    insn = PLT_ENTRY_WORD4
6191
0
     + (((unsigned int) ((-(h->plt.offset + 16)) >> 1)) & 0xffffff);
6192
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6193
0
    local_plt_offset = 12;
6194
0
  }
6195
0
      else
6196
0
  {
6197
    /* sda_base must be set at this time.  */
6198
0
    unsigned long insn;
6199
0
    long offset;
6200
6201
0
    offset = sgot->output_section->vma + sgot->output_offset + got_offset
6202
0
       - elf_gp (output_bfd);
6203
0
    insn = PLT_PIC_ENTRY_WORD0 + ((offset >> 12) & 0xfffff);
6204
0
    bfd_putb32 (insn, splt->contents + h->plt.offset);
6205
6206
0
    insn = PLT_PIC_ENTRY_WORD1 + (offset & 0xfff);
6207
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 4);
6208
6209
0
    insn = PLT_PIC_ENTRY_WORD2;
6210
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 8);
6211
6212
0
    insn = PLT_PIC_ENTRY_WORD3;
6213
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 12);
6214
6215
0
    insn = PLT_PIC_ENTRY_WORD4 + (plt_index & 0x7fffff);
6216
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 16);
6217
6218
0
    insn = PLT_PIC_ENTRY_WORD5
6219
0
      + (((unsigned int) ((-(h->plt.offset + 20)) >> 1)) & 0xffffff);
6220
0
    bfd_putb32 (insn, splt->contents + h->plt.offset + 20);
6221
6222
0
    local_plt_offset = 16;
6223
0
  }
6224
6225
      /* Fill in the entry in the global offset table,
6226
   so it will fall through to the next instruction for the first time.  */
6227
0
      bfd_put_32 (output_bfd,
6228
0
      (splt->output_section->vma + splt->output_offset
6229
0
       + h->plt.offset + local_plt_offset),
6230
0
      sgot->contents + got_offset);
6231
6232
      /* Fill in the entry in the .rela.plt section.  */
6233
0
      rela.r_offset = (sgot->output_section->vma
6234
0
           + sgot->output_offset + got_offset);
6235
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_JMP_SLOT);
6236
0
      rela.r_addend = 0;
6237
0
      loc = srela->contents;
6238
0
      loc += plt_index * sizeof (Elf32_External_Rela);
6239
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6240
6241
0
      if (!h->def_regular)
6242
0
  {
6243
    /* Mark the symbol as undefined, rather than as defined in
6244
       the .plt section.  Leave the value alone.  */
6245
0
    sym->st_shndx = SHN_UNDEF;
6246
0
    if (!h->ref_regular_nonweak)
6247
0
      sym->st_value = 0;
6248
0
  }
6249
0
    }
6250
6251
0
  if (h->got.offset != (bfd_vma) - 1
6252
0
      && hent->tls_type == GOT_NORMAL)
6253
0
    {
6254
0
      asection *sgot;
6255
0
      asection *srelagot;
6256
0
      Elf_Internal_Rela rela;
6257
6258
      /* This symbol has an entry in the global offset table.
6259
   Set it up.  */
6260
6261
0
      sgot = ehtab->sgot;
6262
0
      srelagot = ehtab->srelgot;
6263
0
      BFD_ASSERT (sgot != NULL && srelagot != NULL);
6264
6265
0
      rela.r_offset = (sgot->output_section->vma
6266
0
           + sgot->output_offset + (h->got.offset & ~1));
6267
6268
      /* If this is a -Bsymbolic link, and the symbol is defined
6269
   locally, we just want to emit a RELATIVE reloc.  Likewise if
6270
   the symbol was forced to be local because of a version file.
6271
   The entry in the global offset table will already have been
6272
   initialized in the relocate_section function.  */
6273
0
      if ((bfd_link_pic (info)
6274
0
     && (info->symbolic || h->dynindx == -1 || h->forced_local)
6275
0
     && h->def_regular)
6276
0
    || (bfd_link_pie (info) && h->def_regular))
6277
0
  {
6278
0
    rela.r_info = ELF32_R_INFO (0, R_NDS32_RELATIVE);
6279
0
    rela.r_addend = (h->root.u.def.value
6280
0
         + h->root.u.def.section->output_section->vma
6281
0
         + h->root.u.def.section->output_offset);
6282
6283
0
    if ((h->got.offset & 1) == 0)
6284
0
      {
6285
0
        bfd_put_32 (output_bfd, rela.r_addend,
6286
0
        sgot->contents + h->got.offset);
6287
0
      }
6288
0
  }
6289
0
      else
6290
0
  {
6291
0
    BFD_ASSERT ((h->got.offset & 1) == 0);
6292
0
    bfd_put_32 (output_bfd, (bfd_vma) 0,
6293
0
          sgot->contents + h->got.offset);
6294
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_GLOB_DAT);
6295
0
    rela.r_addend = 0;
6296
0
  }
6297
6298
0
      loc = srelagot->contents;
6299
0
      loc += srelagot->reloc_count * sizeof (Elf32_External_Rela);
6300
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6301
0
      ++srelagot->reloc_count;
6302
0
      BFD_ASSERT (loc < (srelagot->contents + srelagot->size));
6303
0
    }
6304
6305
0
  if (h->needs_copy)
6306
0
    {
6307
0
      asection *s;
6308
0
      Elf_Internal_Rela rela;
6309
6310
      /* This symbols needs a copy reloc.  Set it up.  */
6311
6312
0
      BFD_ASSERT (h->dynindx != -1
6313
0
      && (h->root.type == bfd_link_hash_defined
6314
0
          || h->root.type == bfd_link_hash_defweak));
6315
6316
0
      s = bfd_get_section_by_name (h->root.u.def.section->owner, ".rela.bss");
6317
0
      BFD_ASSERT (s != NULL);
6318
6319
0
      rela.r_offset = (h->root.u.def.value
6320
0
           + h->root.u.def.section->output_section->vma
6321
0
           + h->root.u.def.section->output_offset);
6322
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_NDS32_COPY);
6323
0
      rela.r_addend = 0;
6324
0
      loc = s->contents;
6325
0
      loc += s->reloc_count * sizeof (Elf32_External_Rela);
6326
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
6327
0
      ++s->reloc_count;
6328
0
    }
6329
6330
  /* Mark some specially defined symbols as absolute.  */
6331
0
  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
6332
0
      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
6333
0
    sym->st_shndx = SHN_ABS;
6334
6335
0
  return true;
6336
0
}
6337
6338
6339
/* Finish up the dynamic sections.  */
6340
6341
static bool
6342
nds32_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info)
6343
0
{
6344
0
  bfd *dynobj;
6345
0
  asection *sdyn;
6346
0
  asection *sgotplt;
6347
0
  struct elf_link_hash_table *ehtab;
6348
0
  struct elf_nds32_link_hash_table *htab;
6349
6350
0
  ehtab = elf_hash_table (info);
6351
0
  htab = nds32_elf_hash_table (info);
6352
0
  if (htab == NULL)
6353
0
    return false;
6354
6355
0
  dynobj = elf_hash_table (info)->dynobj;
6356
6357
0
  sgotplt = ehtab->sgotplt;
6358
  /* A broken linker script might have discarded the dynamic sections.
6359
     Catch this here so that we do not seg-fault later on.  */
6360
0
  if (sgotplt != NULL && bfd_is_abs_section (sgotplt->output_section))
6361
0
    return false;
6362
0
  sdyn = bfd_get_section_by_name (dynobj, ".dynamic");
6363
6364
0
  if (elf_hash_table (info)->dynamic_sections_created)
6365
0
    {
6366
0
      asection *splt;
6367
0
      Elf32_External_Dyn *dyncon, *dynconend;
6368
6369
0
      BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6370
6371
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
6372
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6373
6374
0
      for (; dyncon < dynconend; dyncon++)
6375
0
  {
6376
0
    Elf_Internal_Dyn dyn;
6377
0
    asection *s;
6378
6379
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
6380
6381
0
    switch (dyn.d_tag)
6382
0
      {
6383
0
      default:
6384
0
        break;
6385
6386
0
      case DT_PLTGOT:
6387
        /* name = ".got";  */
6388
0
        s = ehtab->sgot;
6389
0
        goto get_vma;
6390
0
      case DT_JMPREL:
6391
0
        s = ehtab->srelplt;
6392
0
      get_vma:
6393
0
        BFD_ASSERT (s != NULL);
6394
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6395
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6396
0
        break;
6397
6398
0
      case DT_PLTRELSZ:
6399
0
        s = ehtab->srelplt;
6400
0
        BFD_ASSERT (s != NULL);
6401
0
        dyn.d_un.d_val = s->size;
6402
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6403
0
        break;
6404
6405
0
      case DT_RELASZ:
6406
        /* My reading of the SVR4 ABI indicates that the
6407
     procedure linkage table relocs (DT_JMPREL) should be
6408
     included in the overall relocs (DT_RELA).  This is
6409
     what Solaris does.  However, UnixWare can not handle
6410
     that case.  Therefore, we override the DT_RELASZ entry
6411
     here to make it not include the JMPREL relocs.  Since
6412
     the linker script arranges for .rela.plt to follow all
6413
     other relocation sections, we don't have to worry
6414
     about changing the DT_RELA entry.  */
6415
0
        if (ehtab->srelplt != NULL)
6416
0
    {
6417
0
      s = ehtab->srelplt;
6418
0
      dyn.d_un.d_val -= s->size;
6419
0
    }
6420
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6421
0
        break;
6422
6423
0
      case DT_TLSDESC_PLT:
6424
0
        s = htab->root.splt;
6425
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6426
0
        + htab->root.tlsdesc_plt);
6427
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6428
0
        break;
6429
6430
0
      case DT_TLSDESC_GOT:
6431
0
        s = htab->root.sgot;
6432
0
        dyn.d_un.d_ptr = (s->output_section->vma + s->output_offset
6433
0
        + htab->root.tlsdesc_got);
6434
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6435
0
        break;
6436
0
      }
6437
0
  }
6438
6439
      /* Fill in the first entry in the procedure linkage table.  */
6440
0
      splt = ehtab->splt;
6441
0
      if (splt && splt->size > 0)
6442
0
  {
6443
0
    if (bfd_link_pic (info))
6444
0
      {
6445
0
        unsigned long insn;
6446
0
        long offset;
6447
6448
0
        offset = sgotplt->output_section->vma + sgotplt->output_offset + 4
6449
0
    - elf_gp (output_bfd);
6450
0
        insn = PLT0_PIC_ENTRY_WORD0 | ((offset >> 12) & 0xfffff);
6451
0
        bfd_putb32 (insn, splt->contents);
6452
6453
        /* here has a typo?  */
6454
0
        insn = PLT0_PIC_ENTRY_WORD1 | (offset & 0xfff);
6455
0
        bfd_putb32 (insn, splt->contents + 4);
6456
6457
0
        insn = PLT0_PIC_ENTRY_WORD2;
6458
0
        bfd_putb32 (insn, splt->contents + 8);
6459
6460
0
        insn = PLT0_PIC_ENTRY_WORD3;
6461
0
        bfd_putb32 (insn, splt->contents + 12);
6462
6463
0
        insn = PLT0_PIC_ENTRY_WORD4;
6464
0
        bfd_putb32 (insn, splt->contents + 16);
6465
6466
0
        insn = PLT0_PIC_ENTRY_WORD5;
6467
0
        bfd_putb32 (insn, splt->contents + 20);
6468
0
      }
6469
0
    else
6470
0
      {
6471
0
        unsigned long insn;
6472
0
        unsigned long addr;
6473
6474
        /* addr = .got + 4 */
6475
0
        addr = sgotplt->output_section->vma + sgotplt->output_offset + 4;
6476
0
        insn = PLT0_ENTRY_WORD0 | ((addr >> 12) & 0xfffff);
6477
0
        bfd_putb32 (insn, splt->contents);
6478
6479
0
        insn = PLT0_ENTRY_WORD1 | (addr & 0x0fff);
6480
0
        bfd_putb32 (insn, splt->contents + 4);
6481
6482
0
        insn = PLT0_ENTRY_WORD2;
6483
0
        bfd_putb32 (insn, splt->contents + 8);
6484
6485
0
        insn = PLT0_ENTRY_WORD3;
6486
0
        bfd_putb32 (insn, splt->contents + 12);
6487
6488
0
        insn = PLT0_ENTRY_WORD4;
6489
0
        bfd_putb32 (insn, splt->contents + 16);
6490
0
      }
6491
6492
0
    elf_section_data (splt->output_section)->this_hdr.sh_entsize =
6493
0
      PLT_ENTRY_SIZE;
6494
0
  }
6495
6496
0
      if (htab->root.tlsdesc_plt)
6497
0
  {
6498
    /* Calculate addresses.  */
6499
0
    asection *sgot = sgot = ehtab->sgot;
6500
0
    bfd_vma pltgot = sgotplt->output_section->vma
6501
0
      + sgotplt->output_offset;
6502
0
    bfd_vma tlsdesc_got = sgot->output_section->vma + sgot->output_offset
6503
0
      + htab->root.tlsdesc_got;
6504
6505
    /* Get GP offset.  */
6506
0
    pltgot -= elf_gp (output_bfd) - 4; /* PLTGOT[1]  */
6507
0
    tlsdesc_got -= elf_gp (output_bfd);
6508
6509
    /* Do relocation.  */
6510
0
    dl_tlsdesc_lazy_trampoline[0] += ((1 << 20) - 1) & (tlsdesc_got >> 12);
6511
0
    dl_tlsdesc_lazy_trampoline[1] += 0xfff & tlsdesc_got;
6512
0
    dl_tlsdesc_lazy_trampoline[4] += ((1 << 20) - 1) & (pltgot >> 12);
6513
0
    dl_tlsdesc_lazy_trampoline[5] +=  0xfff & pltgot;
6514
6515
    /* Insert .plt.  */
6516
0
    nds32_put_trampoline (splt->contents + htab->root.tlsdesc_plt,
6517
0
        dl_tlsdesc_lazy_trampoline,
6518
0
        ARRAY_SIZE (dl_tlsdesc_lazy_trampoline));
6519
0
  }
6520
0
    }
6521
6522
  /* Fill in the first three entries in the global offset table.  */
6523
0
  if (sgotplt && sgotplt->size > 0)
6524
0
    {
6525
0
      if (sdyn == NULL)
6526
0
  bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
6527
0
      else
6528
0
  bfd_put_32 (output_bfd,
6529
0
        sdyn->output_section->vma + sdyn->output_offset,
6530
0
        sgotplt->contents);
6531
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6532
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6533
6534
0
      elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6535
0
    }
6536
6537
0
  return true;
6538
0
}
6539

6540
6541
/* Set the right machine number.  */
6542
6543
static bool
6544
nds32_elf_object_p (bfd *abfd)
6545
0
{
6546
0
  static unsigned int cur_arch = 0;
6547
6548
0
  if (E_N1_ARCH != (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH))
6549
0
    {
6550
      /* E_N1_ARCH is a wild card, so it is set only when no others exist.  */
6551
0
      cur_arch = (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH);
6552
0
    }
6553
6554
0
  switch (cur_arch)
6555
0
    {
6556
0
    default:
6557
0
    case E_N1_ARCH:
6558
0
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1);
6559
0
      break;
6560
0
    case E_N1H_ARCH:
6561
0
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h);
6562
0
      break;
6563
0
    case E_NDS_ARCH_STAR_V2_0:
6564
0
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v2);
6565
0
      break;
6566
0
    case E_NDS_ARCH_STAR_V3_0:
6567
0
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3);
6568
0
      break;
6569
0
    case E_NDS_ARCH_STAR_V3_M:
6570
0
      bfd_default_set_arch_mach (abfd, bfd_arch_nds32, bfd_mach_n1h_v3m);
6571
0
      break;
6572
0
    }
6573
6574
0
  return true;
6575
0
}
6576
6577
/* Store the machine number in the flags field.  */
6578
6579
static bool
6580
nds32_elf_final_write_processing (bfd *abfd)
6581
0
{
6582
0
  unsigned long val;
6583
0
  static unsigned int cur_mach = 0;
6584
6585
0
  if (bfd_mach_n1 != bfd_get_mach (abfd))
6586
0
    {
6587
0
      cur_mach = bfd_get_mach (abfd);
6588
0
    }
6589
6590
0
  switch (cur_mach)
6591
0
    {
6592
0
    case bfd_mach_n1:
6593
      /* Only happen when object is empty, since the case is abandon.  */
6594
0
      val = E_N1_ARCH;
6595
0
      val |= E_NDS_ABI_AABI;
6596
0
      val |= E_NDS32_ELF_VER_1_4;
6597
0
      break;
6598
0
    case bfd_mach_n1h:
6599
0
      val = E_N1H_ARCH;
6600
0
      break;
6601
0
    case bfd_mach_n1h_v2:
6602
0
      val = E_NDS_ARCH_STAR_V2_0;
6603
0
      break;
6604
0
    case bfd_mach_n1h_v3:
6605
0
      val = E_NDS_ARCH_STAR_V3_0;
6606
0
      break;
6607
0
    case bfd_mach_n1h_v3m:
6608
0
      val = E_NDS_ARCH_STAR_V3_M;
6609
0
      break;
6610
0
    default:
6611
0
      val = 0;
6612
0
      break;
6613
0
    }
6614
6615
0
  elf_elfheader (abfd)->e_flags &= ~EF_NDS_ARCH;
6616
0
  elf_elfheader (abfd)->e_flags |= val;
6617
0
  return _bfd_elf_final_write_processing (abfd);
6618
0
}
6619
6620
/* Function to keep NDS32 specific file flags.  */
6621
6622
static bool
6623
nds32_elf_set_private_flags (bfd *abfd, flagword flags)
6624
0
{
6625
0
  BFD_ASSERT (!elf_flags_init (abfd)
6626
0
        || elf_elfheader (abfd)->e_flags == flags);
6627
6628
0
  elf_elfheader (abfd)->e_flags = flags;
6629
0
  elf_flags_init (abfd) = true;
6630
0
  return true;
6631
0
}
6632
6633
static unsigned int
6634
convert_e_flags (unsigned int e_flags, unsigned int arch)
6635
0
{
6636
0
  if ((e_flags & EF_NDS_ARCH) == E_NDS_ARCH_STAR_V0_9)
6637
0
    {
6638
      /* From 0.9 to 1.0.  */
6639
0
      e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V1_0;
6640
6641
      /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6642
0
      e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6643
0
      if (arch == E_NDS_ARCH_STAR_V1_0)
6644
0
  {
6645
    /* Done.  */
6646
0
    return e_flags;
6647
0
  }
6648
0
    }
6649
6650
  /* From 1.0 to 2.0.  */
6651
0
  e_flags = (e_flags & (~EF_NDS_ARCH)) | E_NDS_ARCH_STAR_V2_0;
6652
6653
  /* Clear E_NDS32_HAS_MFUSR_PC_INST.  */
6654
0
  e_flags &= ~E_NDS32_HAS_MFUSR_PC_INST;
6655
6656
  /* Invert E_NDS32_HAS_NO_MAC_INST.  */
6657
0
  e_flags ^= E_NDS32_HAS_NO_MAC_INST;
6658
0
  return e_flags;
6659
0
}
6660
6661
static bool
6662
nds32_check_vec_size (bfd *ibfd)
6663
0
{
6664
0
  static unsigned int nds32_vec_size = 0;
6665
6666
0
  asection *sec_t = NULL;
6667
0
  bfd_byte *contents = NULL;
6668
6669
0
  sec_t = bfd_get_section_by_name (ibfd, ".nds32_e_flags");
6670
6671
0
  if (sec_t && sec_t->size >= 4)
6672
0
    {
6673
      /* Get vec_size in file.  */
6674
0
      unsigned int flag_t;
6675
6676
0
      nds32_get_section_contents (ibfd, sec_t, &contents, true);
6677
0
      flag_t = bfd_get_32 (ibfd, contents);
6678
6679
      /* The value could only be 4 or 16.  */
6680
6681
0
      if (!nds32_vec_size)
6682
  /* Set if not set yet.  */
6683
0
  nds32_vec_size = (flag_t & 0x3);
6684
0
      else if (nds32_vec_size != (flag_t & 0x3))
6685
0
  {
6686
0
    _bfd_error_handler
6687
      /* xgettext:c-format */
6688
0
      (_("%pB: ISR vector size mismatch"
6689
0
         " with previous modules, previous %u-byte, current %u-byte"),
6690
0
       ibfd,
6691
0
       nds32_vec_size == 1 ? 4 : nds32_vec_size == 2 ? 16 : 0xffffffff,
6692
0
       (flag_t & 0x3) == 1 ? 4 : (flag_t & 0x3) == 2 ? 16 : 0xffffffff);
6693
0
    return false;
6694
0
  }
6695
0
      else
6696
  /* Only keep the first vec_size section.  */
6697
0
  sec_t->flags |= SEC_EXCLUDE;
6698
0
    }
6699
6700
0
  return true;
6701
0
}
6702
6703
/* Merge backend specific data from an object file to the output
6704
   object file when linking.  */
6705
6706
static bool
6707
nds32_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6708
0
{
6709
0
  bfd *obfd = info->output_bfd;
6710
0
  flagword out_flags;
6711
0
  flagword in_flags;
6712
0
  flagword out_16regs;
6713
0
  flagword in_no_mac;
6714
0
  flagword out_no_mac;
6715
0
  flagword in_16regs;
6716
0
  flagword out_version;
6717
0
  flagword in_version;
6718
0
  flagword out_fpu_config;
6719
0
  flagword in_fpu_config;
6720
6721
  /* FIXME: What should be checked when linking shared libraries?  */
6722
0
  if ((ibfd->flags & DYNAMIC) != 0)
6723
0
    return true;
6724
6725
  /* TODO: Revise to use object-attributes instead.  */
6726
0
  if (!nds32_check_vec_size (ibfd))
6727
0
    return false;
6728
6729
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
6730
0
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
6731
0
    return true;
6732
6733
0
  if (bfd_little_endian (ibfd) != bfd_little_endian (obfd))
6734
0
    {
6735
0
      _bfd_error_handler
6736
0
  (_("%pB: warning: endian mismatch with previous modules"), ibfd);
6737
6738
0
      bfd_set_error (bfd_error_bad_value);
6739
0
      return false;
6740
0
    }
6741
6742
  /* -B option in objcopy cannot work as expected. e_flags = 0 shall be
6743
     treat as generic one without checking and merging.  */
6744
0
  if (elf_elfheader (ibfd)->e_flags)
6745
0
    {
6746
0
      in_version = elf_elfheader (ibfd)->e_flags & EF_NDS32_ELF_VERSION;
6747
0
      if (in_version == E_NDS32_ELF_VER_1_2)
6748
0
  {
6749
0
    _bfd_error_handler
6750
0
      (_("%pB: warning: older version of object file encountered, "
6751
0
         "please recompile with current tool chain"), ibfd);
6752
0
  }
6753
6754
      /* We may need to merge V1 and V2 arch object files to V2.  */
6755
0
      if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6756
0
    != (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6757
0
  {
6758
    /* Need to convert version.  */
6759
0
    if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6760
0
        == E_NDS_ARCH_STAR_RESERVED)
6761
0
      {
6762
0
        elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6763
0
      }
6764
0
    else if ((elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6765
0
       == E_NDS_ARCH_STAR_V3_M
6766
0
       && (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6767
0
       == E_NDS_ARCH_STAR_V3_0)
6768
0
      {
6769
0
        elf_elfheader (ibfd)->e_flags =
6770
0
    (elf_elfheader (ibfd)->e_flags & (~EF_NDS_ARCH))
6771
0
    | E_NDS_ARCH_STAR_V3_0;
6772
0
      }
6773
0
    else if ((elf_elfheader (obfd)->e_flags & EF_NDS_ARCH)
6774
0
       == E_NDS_ARCH_STAR_V0_9
6775
0
       || (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH)
6776
0
       > (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH))
6777
0
      {
6778
0
        elf_elfheader (obfd)->e_flags =
6779
0
    convert_e_flags (elf_elfheader (obfd)->e_flags,
6780
0
         (elf_elfheader (ibfd)->e_flags & EF_NDS_ARCH));
6781
0
      }
6782
0
    else
6783
0
      {
6784
0
        elf_elfheader (ibfd)->e_flags =
6785
0
    convert_e_flags (elf_elfheader (ibfd)->e_flags,
6786
0
         (elf_elfheader (obfd)->e_flags & EF_NDS_ARCH));
6787
0
      }
6788
0
  }
6789
6790
      /* Extract some flags.  */
6791
0
      in_flags = elf_elfheader (ibfd)->e_flags
6792
0
  & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6793
0
       | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6794
6795
      /* The following flags need special treatment.  */
6796
0
      in_16regs = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6797
0
      in_no_mac = elf_elfheader (ibfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6798
0
      in_fpu_config = elf_elfheader (ibfd)->e_flags & E_NDS32_FPU_REG_CONF;
6799
6800
      /* Extract some flags.  */
6801
0
      out_flags = elf_elfheader (obfd)->e_flags
6802
0
  & (~(E_NDS32_HAS_REDUCED_REGS | EF_NDS32_ELF_VERSION
6803
0
       | E_NDS32_HAS_NO_MAC_INST | E_NDS32_FPU_REG_CONF));
6804
6805
      /* The following flags need special treatment.  */
6806
0
      out_16regs = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_REDUCED_REGS;
6807
0
      out_no_mac = elf_elfheader (obfd)->e_flags & E_NDS32_HAS_NO_MAC_INST;
6808
0
      out_fpu_config = elf_elfheader (obfd)->e_flags & E_NDS32_FPU_REG_CONF;
6809
0
      out_version = elf_elfheader (obfd)->e_flags & EF_NDS32_ELF_VERSION;
6810
0
      if (!elf_flags_init (obfd))
6811
0
  {
6812
    /* If the input is the default architecture then do not
6813
       bother setting the flags for the output architecture,
6814
       instead allow future merges to do this.  If no future
6815
       merges ever set these flags then they will retain their
6816
       unitialised values, which surprise surprise, correspond
6817
       to the default values.  */
6818
0
    if (bfd_get_arch_info (ibfd)->the_default)
6819
0
      return true;
6820
6821
0
    elf_flags_init (obfd) = true;
6822
0
    elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
6823
6824
0
    if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
6825
0
        && bfd_get_arch_info (obfd)->the_default)
6826
0
      {
6827
0
        return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
6828
0
          bfd_get_mach (ibfd));
6829
0
      }
6830
6831
0
    return true;
6832
0
  }
6833
6834
      /* Check flag compatibility.  */
6835
0
      if ((in_flags & EF_NDS_ABI) != (out_flags & EF_NDS_ABI))
6836
0
  {
6837
0
    _bfd_error_handler
6838
0
      (_("%pB: error: ABI mismatch with previous modules"), ibfd);
6839
0
    bfd_set_error (bfd_error_bad_value);
6840
0
    return false;
6841
0
  }
6842
6843
0
      if ((in_flags & EF_NDS_ARCH) != (out_flags & EF_NDS_ARCH))
6844
0
  {
6845
0
    if (((in_flags & EF_NDS_ARCH) != E_N1_ARCH))
6846
0
      {
6847
0
        _bfd_error_handler
6848
0
    (_("%pB: error: instruction set mismatch with previous modules"),
6849
0
     ibfd);
6850
6851
0
        bfd_set_error (bfd_error_bad_value);
6852
0
        return false;
6853
0
      }
6854
0
  }
6855
6856
      /* When linking with V1.2 and V1.3 objects together the output is V1.2.
6857
   and perf ext1 and DIV are mergerd to perf ext1.  */
6858
0
      if (in_version == E_NDS32_ELF_VER_1_2 || out_version == E_NDS32_ELF_VER_1_2)
6859
0
  {
6860
0
    elf_elfheader (obfd)->e_flags =
6861
0
      (in_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6862
0
      | (out_flags & (~(E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6863
0
      | (((in_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6864
0
         ?  E_NDS32_HAS_EXT_INST : 0)
6865
0
      | (((out_flags & (E_NDS32_HAS_EXT_INST | E_NDS32_HAS_DIV_INST)))
6866
0
         ?  E_NDS32_HAS_EXT_INST : 0)
6867
0
      | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6868
0
      | ((in_version > out_version) ? out_version : in_version);
6869
0
  }
6870
0
      else
6871
0
  {
6872
0
    if (in_version != out_version)
6873
0
      _bfd_error_handler
6874
        /* xgettext:c-format */
6875
0
        (_("%pB: warning: incompatible elf-versions %s and %s"),
6876
0
         ibfd, nds32_elfver_strtab[out_version],
6877
0
         nds32_elfver_strtab[in_version]);
6878
6879
0
    elf_elfheader (obfd)->e_flags = in_flags | out_flags
6880
0
      | (in_16regs & out_16regs) | (in_no_mac & out_no_mac)
6881
0
      | (in_fpu_config > out_fpu_config ? in_fpu_config : out_fpu_config)
6882
0
      | (in_version > out_version ?  out_version : in_version);
6883
0
  }
6884
0
    }
6885
6886
0
  return true;
6887
0
}
6888
6889
/* Display the flags field.  */
6890
6891
static bool
6892
nds32_elf_print_private_bfd_data (bfd *abfd, void *ptr)
6893
0
{
6894
0
  FILE *file = (FILE *) ptr;
6895
6896
0
  BFD_ASSERT (abfd != NULL && ptr != NULL);
6897
6898
0
  _bfd_elf_print_private_bfd_data (abfd, ptr);
6899
6900
0
  fprintf (file, _("private flags = %lx"), elf_elfheader (abfd)->e_flags);
6901
6902
0
  switch (elf_elfheader (abfd)->e_flags & EF_NDS_ARCH)
6903
0
    {
6904
0
    default:
6905
0
    case E_N1_ARCH:
6906
0
      fprintf (file, _(": n1 instructions"));
6907
0
      break;
6908
0
    case E_N1H_ARCH:
6909
0
      fprintf (file, _(": n1h instructions"));
6910
0
      break;
6911
0
    }
6912
6913
0
  fputc ('\n', file);
6914
6915
0
  return true;
6916
0
}
6917
6918
static unsigned int
6919
nds32_elf_action_discarded (asection *sec)
6920
0
{
6921
6922
0
  if (startswith (sec->name, ".gcc_except_table"))
6923
0
    return 0;
6924
6925
0
  return _bfd_elf_default_action_discarded (sec);
6926
0
}
6927
6928
static asection *
6929
nds32_elf_gc_mark_hook (asection *sec, struct bfd_link_info *info,
6930
      Elf_Internal_Rela *rel, struct elf_link_hash_entry *h,
6931
      Elf_Internal_Sym *sym)
6932
0
{
6933
0
  if (h != NULL)
6934
0
    switch (ELF32_R_TYPE (rel->r_info))
6935
0
      {
6936
0
      case R_NDS32_GNU_VTINHERIT:
6937
0
      case R_NDS32_GNU_VTENTRY:
6938
0
      case R_NDS32_RELA_GNU_VTINHERIT:
6939
0
      case R_NDS32_RELA_GNU_VTENTRY:
6940
0
  return NULL;
6941
0
      }
6942
6943
0
  return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
6944
0
}
6945
6946
static enum elf_nds32_tls_type
6947
get_tls_type (enum elf_nds32_reloc_type r_type,
6948
        struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
6949
0
{
6950
0
  enum elf_nds32_tls_type tls_type;
6951
6952
0
  switch (r_type)
6953
0
    {
6954
0
    case R_NDS32_TLS_LE_HI20:
6955
0
    case R_NDS32_TLS_LE_LO12:
6956
0
      tls_type = GOT_TLS_LE;
6957
0
      break;
6958
0
    case R_NDS32_TLS_IE_HI20:
6959
0
    case R_NDS32_TLS_IE_LO12S2:
6960
0
    case R_NDS32_TLS_IE_LO12:
6961
0
      tls_type = GOT_TLS_IE;
6962
0
      break;
6963
0
    case R_NDS32_TLS_IEGP_HI20:
6964
0
    case R_NDS32_TLS_IEGP_LO12:
6965
0
    case R_NDS32_TLS_IEGP_LO12S2:
6966
0
      tls_type = GOT_TLS_IEGP;
6967
0
      break;
6968
0
    case R_NDS32_TLS_DESC_HI20:
6969
0
    case R_NDS32_TLS_DESC_LO12:
6970
0
    case R_NDS32_TLS_DESC_ADD:
6971
0
    case R_NDS32_TLS_DESC_FUNC:
6972
0
    case R_NDS32_TLS_DESC_CALL:
6973
0
      tls_type = GOT_TLS_DESC;
6974
0
      break;
6975
0
    default:
6976
0
      tls_type = GOT_NORMAL;
6977
0
      break;
6978
0
    }
6979
6980
0
  return tls_type;
6981
0
}
6982
6983
/* Ensure that we have allocated bookkeeping structures for ABFD's local
6984
   symbols.  */
6985
6986
static bool
6987
elf32_nds32_allocate_local_sym_info (bfd *abfd)
6988
0
{
6989
0
  if (elf_local_got_refcounts (abfd) == NULL)
6990
0
    {
6991
0
      bfd_size_type num_syms;
6992
0
      bfd_size_type size;
6993
0
      char *data;
6994
6995
0
      num_syms = elf_tdata (abfd)->symtab_hdr.sh_info;
6996
      /* This space is for got_refcounts, got_tls_type, tlsdesc_gotent, and
6997
   gp_offset.  The details can refer to struct elf_nds32_obj_tdata.  */
6998
0
      size = num_syms * (sizeof (bfd_signed_vma) + sizeof (char)
6999
0
       + sizeof (bfd_vma) + sizeof (int)
7000
0
       + sizeof (bool) + sizeof (bfd_vma));
7001
0
      data = bfd_zalloc (abfd, size);
7002
0
      if (data == NULL)
7003
0
  return false;
7004
7005
0
      elf_local_got_refcounts (abfd) = (bfd_signed_vma *) data;
7006
0
      data += num_syms * sizeof (bfd_signed_vma);
7007
7008
0
      elf32_nds32_local_got_tls_type (abfd) = (char *) data;
7009
0
      data += num_syms * sizeof (char);
7010
7011
0
      elf32_nds32_local_tlsdesc_gotent (abfd) = (bfd_vma *) data;
7012
0
      data += num_syms * sizeof (bfd_vma);
7013
7014
0
      elf32_nds32_local_gp_offset (abfd) = (int *) data;
7015
0
      data += num_syms * sizeof (int);
7016
0
    }
7017
7018
0
  return true;
7019
0
}
7020
7021
/* Look through the relocs for a section during the first phase.
7022
   Since we don't do .gots or .plts, we just need to consider the
7023
   virtual table relocs for gc.  */
7024
7025
static bool
7026
nds32_elf_check_relocs (bfd *abfd, struct bfd_link_info *info,
7027
      asection *sec, const Elf_Internal_Rela *relocs)
7028
0
{
7029
0
  Elf_Internal_Shdr *symtab_hdr;
7030
0
  struct elf_link_hash_entry **sym_hashes;
7031
0
  const Elf_Internal_Rela *rel;
7032
0
  const Elf_Internal_Rela *rel_end;
7033
0
  struct elf_link_hash_table *ehtab;
7034
0
  struct elf_nds32_link_hash_table *htab;
7035
0
  bfd *dynobj;
7036
0
  asection *sreloc = NULL;
7037
7038
  /* No need for relocation if relocatable already.  */
7039
0
  if (bfd_link_relocatable (info))
7040
0
    {
7041
0
      elf32_nds32_check_relax_group (abfd, sec);
7042
0
      return true;
7043
0
    }
7044
7045
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
7046
0
  sym_hashes = elf_sym_hashes (abfd);
7047
7048
0
  ehtab = elf_hash_table (info);
7049
0
  htab = nds32_elf_hash_table (info);
7050
0
  dynobj = htab->root.dynobj;
7051
7052
0
  rel_end = relocs + sec->reloc_count;
7053
0
  for (rel = relocs; rel < rel_end; rel++)
7054
0
    {
7055
0
      enum elf_nds32_reloc_type r_type;
7056
0
      struct elf_link_hash_entry *h;
7057
0
      unsigned long r_symndx;
7058
0
      enum elf_nds32_tls_type tls_type, old_tls_type;
7059
7060
0
      r_symndx = ELF32_R_SYM (rel->r_info);
7061
0
      r_type = ELF32_R_TYPE (rel->r_info);
7062
0
      if (r_symndx < symtab_hdr->sh_info)
7063
0
  h = NULL;
7064
0
      else
7065
0
  {
7066
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
7067
0
    while (h->root.type == bfd_link_hash_indirect
7068
0
     || h->root.type == bfd_link_hash_warning)
7069
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
7070
0
  }
7071
7072
      /* Create .got section if necessary.
7073
   Some relocs require a global offset table.  We create
7074
   got section here, since these relocation need a got section
7075
   and if it is not created yet.  */
7076
0
      if (ehtab->sgot == NULL)
7077
0
  {
7078
0
    switch (r_type)
7079
0
      {
7080
0
      case R_NDS32_GOT_HI20:
7081
0
      case R_NDS32_GOT_LO12:
7082
0
      case R_NDS32_GOT_LO15:
7083
0
      case R_NDS32_GOT_LO19:
7084
0
      case R_NDS32_GOT17S2_RELA:
7085
0
      case R_NDS32_GOT15S2_RELA:
7086
0
      case R_NDS32_GOTOFF:
7087
0
      case R_NDS32_GOTOFF_HI20:
7088
0
      case R_NDS32_GOTOFF_LO12:
7089
0
      case R_NDS32_GOTOFF_LO15:
7090
0
      case R_NDS32_GOTOFF_LO19:
7091
0
      case R_NDS32_GOTPC20:
7092
0
      case R_NDS32_GOTPC_HI20:
7093
0
      case R_NDS32_GOTPC_LO12:
7094
0
      case R_NDS32_GOT20:
7095
0
      case R_NDS32_TLS_IE_HI20:
7096
0
      case R_NDS32_TLS_IE_LO12:
7097
0
      case R_NDS32_TLS_IE_LO12S2:
7098
0
      case R_NDS32_TLS_IEGP_HI20:
7099
0
      case R_NDS32_TLS_IEGP_LO12:
7100
0
      case R_NDS32_TLS_IEGP_LO12S2:
7101
0
      case R_NDS32_TLS_DESC_HI20:
7102
0
      case R_NDS32_TLS_DESC_LO12:
7103
0
        if (dynobj == NULL)
7104
0
    htab->root.dynobj = dynobj = abfd;
7105
0
        if (!create_got_section (dynobj, info))
7106
0
    return false;
7107
0
        break;
7108
7109
0
      default:
7110
0
        break;
7111
0
      }
7112
0
  }
7113
7114
      /* Check relocation type.  */
7115
0
      switch ((int) r_type)
7116
0
  {
7117
0
  case R_NDS32_GOT_HI20:
7118
0
  case R_NDS32_GOT_LO12:
7119
0
  case R_NDS32_GOT_LO15:
7120
0
  case R_NDS32_GOT_LO19:
7121
0
  case R_NDS32_GOT20:
7122
0
  case R_NDS32_TLS_LE_HI20:
7123
0
  case R_NDS32_TLS_LE_LO12:
7124
0
  case R_NDS32_TLS_IE_HI20:
7125
0
  case R_NDS32_TLS_IE_LO12:
7126
0
  case R_NDS32_TLS_IE_LO12S2:
7127
0
  case R_NDS32_TLS_IEGP_HI20:
7128
0
  case R_NDS32_TLS_IEGP_LO12:
7129
0
  case R_NDS32_TLS_IEGP_LO12S2:
7130
0
  case R_NDS32_TLS_DESC_HI20:
7131
0
  case R_NDS32_TLS_DESC_LO12:
7132
0
    tls_type = get_tls_type (r_type, h);
7133
0
    if (h)
7134
0
      {
7135
0
        if (tls_type != GOT_TLS_LE)
7136
0
    h->got.refcount += 1;
7137
0
        old_tls_type = elf32_nds32_hash_entry (h)->tls_type;
7138
0
      }
7139
0
    else
7140
0
      {
7141
        /* This is a global offset table entry for a local symbol.  */
7142
0
        if (!elf32_nds32_allocate_local_sym_info (abfd))
7143
0
    return false;
7144
7145
0
        BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
7146
0
        if (tls_type != GOT_TLS_LE)
7147
0
    elf_local_got_refcounts (abfd)[r_symndx] += 1;
7148
0
        old_tls_type = elf32_nds32_local_got_tls_type (abfd)[r_symndx];
7149
0
      }
7150
7151
    /* We would already have issued an error message if there
7152
       is a TLS/non-TLS mismatch, based on the symbol
7153
       type.  So just combine any TLS types needed.  */
7154
0
    if (old_tls_type != GOT_UNKNOWN && old_tls_type != GOT_NORMAL
7155
0
        && tls_type != GOT_NORMAL)
7156
0
      tls_type |= old_tls_type;
7157
7158
    /* DESC to IE/IEGP if link to executable.  */
7159
0
    if ((tls_type & (GOT_TLS_DESC | GOT_TLS_IEGP))
7160
0
        && (bfd_link_executable (info)))
7161
0
      tls_type |= (bfd_link_pie (info) ? GOT_TLS_IEGP : GOT_TLS_IE);
7162
7163
0
    if (old_tls_type != tls_type)
7164
0
      {
7165
0
        if (h != NULL)
7166
0
    elf32_nds32_hash_entry (h)->tls_type = tls_type;
7167
0
        else
7168
0
    elf32_nds32_local_got_tls_type (abfd)[r_symndx] = tls_type;
7169
0
      }
7170
0
    break;
7171
0
  case R_NDS32_9_PLTREL:
7172
0
  case R_NDS32_25_PLTREL:
7173
0
  case R_NDS32_PLTREL_HI20:
7174
0
  case R_NDS32_PLTREL_LO12:
7175
0
  case R_NDS32_PLT_GOTREL_HI20:
7176
0
  case R_NDS32_PLT_GOTREL_LO12:
7177
0
  case R_NDS32_PLT_GOTREL_LO15:
7178
0
  case R_NDS32_PLT_GOTREL_LO19:
7179
0
  case R_NDS32_PLT_GOTREL_LO20:
7180
7181
    /* This symbol requires a procedure linkage table entry.  We
7182
       actually build the entry in adjust_dynamic_symbol,
7183
       because this might be a case of linking PIC code without
7184
       linking in any dynamic objects, in which case we don't
7185
       need to generate a procedure linkage table after all.  */
7186
7187
    /* If this is a local symbol, we resolve it directly without
7188
       creating a procedure linkage table entry.  */
7189
0
    if (h == NULL)
7190
0
      continue;
7191
7192
0
    if (h->forced_local
7193
0
        || (bfd_link_pie (info) && h->def_regular))
7194
0
      break;
7195
7196
0
    elf32_nds32_hash_entry (h)->tls_type = GOT_NORMAL;
7197
0
    h->needs_plt = 1;
7198
0
    h->plt.refcount += 1;
7199
0
    break;
7200
7201
0
  case R_NDS32_16_RELA:
7202
0
  case R_NDS32_20_RELA:
7203
0
  case R_NDS32_5_RELA:
7204
0
  case R_NDS32_32_RELA:
7205
0
  case R_NDS32_HI20_RELA:
7206
0
  case R_NDS32_LO12S3_RELA:
7207
0
  case R_NDS32_LO12S2_RELA:
7208
0
  case R_NDS32_LO12S2_DP_RELA:
7209
0
  case R_NDS32_LO12S2_SP_RELA:
7210
0
  case R_NDS32_LO12S1_RELA:
7211
0
  case R_NDS32_LO12S0_RELA:
7212
0
  case R_NDS32_LO12S0_ORI_RELA:
7213
0
  case R_NDS32_SDA16S3_RELA:
7214
0
  case R_NDS32_SDA17S2_RELA:
7215
0
  case R_NDS32_SDA18S1_RELA:
7216
0
  case R_NDS32_SDA19S0_RELA:
7217
0
  case R_NDS32_SDA15S3_RELA:
7218
0
  case R_NDS32_SDA15S2_RELA:
7219
0
  case R_NDS32_SDA12S2_DP_RELA:
7220
0
  case R_NDS32_SDA12S2_SP_RELA:
7221
0
  case R_NDS32_SDA15S1_RELA:
7222
0
  case R_NDS32_SDA15S0_RELA:
7223
0
  case R_NDS32_SDA_FP7U2_RELA:
7224
0
  case R_NDS32_15_PCREL_RELA:
7225
0
  case R_NDS32_17_PCREL_RELA:
7226
0
  case R_NDS32_25_PCREL_RELA:
7227
7228
0
    if (h != NULL && !bfd_link_pic (info))
7229
0
      {
7230
0
        h->non_got_ref = 1;
7231
0
        h->plt.refcount += 1;
7232
0
      }
7233
7234
    /* If we are creating a shared library, and this is a reloc against
7235
       a global symbol, or a non PC relative reloc against a local
7236
       symbol, then we need to copy the reloc into the shared library.
7237
       However, if we are linking with -Bsymbolic, we do not need to
7238
       copy a reloc against a global symbol which is defined in an
7239
       object we are including in the link (i.e., DEF_REGULAR is set).
7240
       At this point we have not seen all the input files, so it is
7241
       possible that DEF_REGULAR is not set now but will be set later
7242
       (it is never cleared).  We account for that possibility below by
7243
       storing information in the dyn_relocs field of the hash table
7244
       entry.  A similar situation occurs when creating shared libraries
7245
       and symbol visibility changes render the symbol local.
7246
7247
       If on the other hand, we are creating an executable, we may need
7248
       to keep relocations for symbols satisfied by a dynamic library
7249
       if we manage to avoid copy relocs for the symbol.  */
7250
0
    if ((bfd_link_pic (info)
7251
0
         && (sec->flags & SEC_ALLOC) != 0
7252
0
         && ((r_type != R_NDS32_25_PCREL_RELA
7253
0
        && r_type != R_NDS32_15_PCREL_RELA
7254
0
        && r_type != R_NDS32_17_PCREL_RELA
7255
0
        && !(r_type == R_NDS32_32_RELA
7256
0
       && strcmp (sec->name, ".eh_frame") == 0))
7257
0
       || (h != NULL
7258
0
           && (!info->symbolic
7259
0
         || h->root.type == bfd_link_hash_defweak
7260
0
         || !h->def_regular))))
7261
0
        || (!bfd_link_pic (info)
7262
0
      && (sec->flags & SEC_ALLOC) != 0
7263
0
      && h != NULL
7264
0
      && (h->root.type == bfd_link_hash_defweak
7265
0
          || !h->def_regular)))
7266
0
      {
7267
0
        struct elf_dyn_relocs *p;
7268
0
        struct elf_dyn_relocs **head;
7269
7270
0
        if (dynobj == NULL)
7271
0
    htab->root.dynobj = dynobj = abfd;
7272
7273
        /* When creating a shared object, we must copy these
7274
     relocs into the output file.  We create a reloc
7275
     section in dynobj and make room for the reloc.  */
7276
0
        if (sreloc == NULL)
7277
0
    {
7278
0
      const char *name;
7279
7280
0
      name = bfd_elf_string_from_elf_section
7281
0
        (abfd, elf_elfheader (abfd)->e_shstrndx,
7282
0
         elf_section_data (sec)->rela.hdr->sh_name);
7283
0
      if (name == NULL)
7284
0
        return false;
7285
7286
0
      BFD_ASSERT (startswith (name, ".rela")
7287
0
            && strcmp (bfd_section_name (sec),
7288
0
           name + 5) == 0);
7289
7290
0
      sreloc = bfd_get_section_by_name (dynobj, name);
7291
0
      if (sreloc == NULL)
7292
0
        {
7293
0
          flagword flags;
7294
7295
0
          sreloc = bfd_make_section (dynobj, name);
7296
0
          flags = (SEC_HAS_CONTENTS | SEC_READONLY
7297
0
             | SEC_IN_MEMORY | SEC_LINKER_CREATED);
7298
0
          if ((sec->flags & SEC_ALLOC) != 0)
7299
0
      flags |= SEC_ALLOC | SEC_LOAD;
7300
0
          if (sreloc == NULL
7301
0
        || !bfd_set_section_flags (sreloc, flags)
7302
0
        || !bfd_set_section_alignment (sreloc, 2))
7303
0
      return false;
7304
7305
0
          elf_section_type (sreloc) = SHT_RELA;
7306
0
        }
7307
0
      elf_section_data (sec)->sreloc = sreloc;
7308
0
    }
7309
7310
        /* If this is a global symbol, we count the number of
7311
     relocations we need for this symbol.  */
7312
0
        if (h != NULL)
7313
0
    head = &h->dyn_relocs;
7314
0
        else
7315
0
    {
7316
0
      asection *s;
7317
0
      void *vpp;
7318
7319
0
      Elf_Internal_Sym *isym;
7320
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
7321
0
            abfd, r_symndx);
7322
0
      if (isym == NULL)
7323
0
        return false;
7324
7325
      /* Track dynamic relocs needed for local syms too.  */
7326
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
7327
0
      if (s == NULL)
7328
0
        return false;
7329
7330
0
      vpp = &elf_section_data (s)->local_dynrel;
7331
0
      head = (struct elf_dyn_relocs **) vpp;
7332
0
    }
7333
7334
0
        p = *head;
7335
0
        if (p == NULL || p->sec != sec)
7336
0
    {
7337
0
      size_t amt = sizeof (*p);
7338
0
      p = (struct elf_dyn_relocs *) bfd_alloc (dynobj, amt);
7339
0
      if (p == NULL)
7340
0
        return false;
7341
0
      p->next = *head;
7342
0
      *head = p;
7343
0
      p->sec = sec;
7344
0
      p->count = 0;
7345
0
      p->pc_count = 0;
7346
0
    }
7347
7348
0
        p->count += 1;
7349
7350
        /* Since eh_frame is readonly, R_NDS32_32_RELA
7351
     reloc for eh_frame will cause shared library has
7352
     TEXTREL entry in the dynamic section. This lead glibc
7353
     testsuites to failure (bug-13092) and cause kernel fail
7354
     (bug-11819).  I think the best solution is to replace
7355
     absolute reloc with pc relative reloc in the eh_frame.
7356
     To do that, we need to support the following issues:
7357
7358
     === For GCC ===
7359
     * gcc/config/nds32/nds32.h: Define
7360
     ASM_PREFERRED_EH_DATA_FORMAT to encode DW_EH_PE_pcrel
7361
     and DW_EH_PE_sdata4 into DWARF exception header when
7362
     option have '-fpic'.
7363
7364
     === For binutils ===
7365
     * bfd/: Define new reloc R_NDS32_32_PCREL_RELA.
7366
     * gas/config/tc-nds32.h: Define DIFF_EXPR_OK. This
7367
     may break our nds DIFF mechanism, therefore, we
7368
     must disable all linker relaxations to ensure
7369
     correctness.
7370
     * gas/config/tc-nds32.c (nds32_apply_fix): Replace
7371
     R_NDS32_32_RELA with R_NDS32_32_PCREL_RELA, and
7372
     do the necessary modification.
7373
7374
     Unfortunately, it still have some problems for nds32
7375
     to support pc relative reloc in the eh_frame. So I use
7376
     another solution to fix this issue.
7377
7378
     However, I find that ld always emit TEXTREL marker for
7379
     R_NDS32_NONE relocs in rel.dyn. These none relocs are
7380
     correspond to R_NDS32_32_RELA for .eh_frame section.
7381
     It means that we always reserve redundant entries of rel.dyn
7382
     for these relocs which actually do nothing in dynamic linker.
7383
7384
     Therefore, we regard these relocs as pc relative relocs
7385
     here and increase the pc_count.  */
7386
0
        if (ELF32_R_TYPE (rel->r_info) == R_NDS32_25_PCREL_RELA
7387
0
      || ELF32_R_TYPE (rel->r_info) == R_NDS32_15_PCREL_RELA
7388
0
      || ELF32_R_TYPE (rel->r_info) == R_NDS32_17_PCREL_RELA
7389
0
      || (r_type == R_NDS32_32_RELA
7390
0
          && strcmp (sec->name, ".eh_frame") == 0))
7391
0
    p->pc_count += 1;
7392
0
      }
7393
0
    break;
7394
7395
    /* This relocation describes the C++ object vtable hierarchy.
7396
       Reconstruct it for later use during GC.  */
7397
0
  case R_NDS32_RELA_GNU_VTINHERIT:
7398
0
  case R_NDS32_GNU_VTINHERIT:
7399
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
7400
0
      return false;
7401
0
    break;
7402
7403
    /* This relocation describes which C++ vtable entries are actually
7404
       used.  Record for later use during GC.  */
7405
0
  case R_NDS32_GNU_VTENTRY:
7406
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
7407
0
      return false;
7408
0
    break;
7409
0
  case R_NDS32_RELA_GNU_VTENTRY:
7410
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
7411
0
      return false;
7412
0
    break;
7413
0
  }
7414
0
    }
7415
7416
0
  return true;
7417
0
}
7418
7419
/* Write VAL in uleb128 format to P, returning a pointer to the
7420
   following byte.
7421
   This code is copied from elf-attr.c.  */
7422
7423
static bfd_byte *
7424
write_uleb128 (bfd_byte *p, unsigned int val)
7425
0
{
7426
0
  bfd_byte c;
7427
0
  do
7428
0
    {
7429
0
      c = val & 0x7f;
7430
0
      val >>= 7;
7431
0
      if (val)
7432
0
  c |= 0x80;
7433
0
      *(p++) = c;
7434
0
    }
7435
0
  while (val);
7436
0
  return p;
7437
0
}
7438
7439
static bfd_signed_vma
7440
calculate_offset (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
7441
      Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
7442
0
{
7443
0
  bfd_signed_vma foff;
7444
0
  bfd_vma symval, addend;
7445
0
  asection *sym_sec;
7446
7447
  /* Get the value of the symbol referred to by the reloc.  */
7448
0
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
7449
0
    {
7450
0
      Elf_Internal_Sym *isym;
7451
7452
      /* A local symbol.  */
7453
0
      isym = isymbuf + ELF32_R_SYM (irel->r_info);
7454
7455
0
      if (isym->st_shndx == SHN_UNDEF)
7456
0
  sym_sec = bfd_und_section_ptr;
7457
0
      else if (isym->st_shndx == SHN_ABS)
7458
0
  sym_sec = bfd_abs_section_ptr;
7459
0
      else if (isym->st_shndx == SHN_COMMON)
7460
0
  sym_sec = bfd_com_section_ptr;
7461
0
      else
7462
0
  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
7463
0
      symval = isym->st_value + sym_sec->output_section->vma
7464
0
         + sym_sec->output_offset;
7465
0
    }
7466
0
  else
7467
0
    {
7468
0
      unsigned long indx;
7469
0
      struct elf_link_hash_entry *h;
7470
7471
      /* An external symbol.  */
7472
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
7473
0
      h = elf_sym_hashes (abfd)[indx];
7474
0
      BFD_ASSERT (h != NULL);
7475
7476
0
      if (h->root.type != bfd_link_hash_defined
7477
0
    && h->root.type != bfd_link_hash_defweak)
7478
  /* This appears to be a reference to an undefined
7479
     symbol.  Just ignore it--it will be caught by the
7480
     regular reloc processing.  */
7481
0
  return 0;
7482
7483
0
      if (h->root.u.def.section->flags & SEC_MERGE)
7484
0
  {
7485
0
    sym_sec = h->root.u.def.section;
7486
0
    symval = _bfd_merged_section_offset (abfd, &sym_sec,
7487
0
                 elf_section_data (sym_sec)->sec_info,
7488
0
                 h->root.u.def.value);
7489
0
    symval = symval + sym_sec->output_section->vma
7490
0
       + sym_sec->output_offset;
7491
0
  }
7492
0
      else
7493
0
  symval = (h->root.u.def.value
7494
0
      + h->root.u.def.section->output_section->vma
7495
0
      + h->root.u.def.section->output_offset);
7496
0
    }
7497
7498
0
  addend = irel->r_addend;
7499
7500
0
  foff = (symval + addend
7501
0
    - (irel->r_offset + sec->output_section->vma + sec->output_offset));
7502
0
  return foff;
7503
0
}
7504

7505
7506
/* Convert a 32-bit instruction to 16-bit one.
7507
   INSN is the input 32-bit instruction, INSN16 is the output 16-bit
7508
   instruction.  If INSN_TYPE is not NULL, it the CGEN instruction
7509
   type of INSN16.  Return 1 if successful.  */
7510
7511
static int
7512
nds32_convert_32_to_16_alu1 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7513
           int *pinsn_type)
7514
0
{
7515
0
  uint16_t insn16 = 0;
7516
0
  int insn_type = 0;
7517
0
  unsigned long mach = bfd_get_mach (abfd);
7518
7519
0
  if (N32_SH5 (insn) != 0)
7520
0
    return 0;
7521
7522
0
  switch (N32_SUB5 (insn))
7523
0
    {
7524
0
    case N32_ALU1_ADD_SLLI:
7525
0
    case N32_ALU1_ADD_SRLI:
7526
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7527
0
  {
7528
0
    insn16 = N16_TYPE333 (ADD333, N32_RT5 (insn), N32_RA5 (insn),
7529
0
        N32_RB5 (insn));
7530
0
    insn_type = NDS32_INSN_ADD333;
7531
0
  }
7532
0
      else if (N32_IS_RT4 (insn))
7533
0
  {
7534
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7535
0
      insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RB5 (insn));
7536
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7537
0
      insn16 = N16_TYPE45 (ADD45, N32_RT54 (insn), N32_RA5 (insn));
7538
0
    insn_type = NDS32_INSN_ADD45;
7539
0
  }
7540
0
      break;
7541
7542
0
    case N32_ALU1_SUB_SLLI:
7543
0
    case N32_ALU1_SUB_SRLI:
7544
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IS_RB3 (insn))
7545
0
  {
7546
0
    insn16 = N16_TYPE333 (SUB333, N32_RT5 (insn), N32_RA5 (insn),
7547
0
        N32_RB5 (insn));
7548
0
    insn_type = NDS32_INSN_SUB333;
7549
0
  }
7550
0
      else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7551
0
  {
7552
0
    insn16 = N16_TYPE45 (SUB45, N32_RT54 (insn), N32_RB5 (insn));
7553
0
    insn_type = NDS32_INSN_SUB45;
7554
0
  }
7555
0
      break;
7556
7557
0
    case N32_ALU1_AND_SLLI:
7558
0
    case N32_ALU1_AND_SRLI:
7559
      /* and $rt, $rt, $rb -> and33 for v3, v3m.  */
7560
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7561
0
    && N32_IS_RB3 (insn))
7562
0
  {
7563
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7564
0
      insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RB5 (insn));
7565
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7566
0
      insn16 = N16_MISC33 (AND33, N32_RT5 (insn), N32_RA5 (insn));
7567
0
    if (insn16)
7568
0
      insn_type = NDS32_INSN_AND33;
7569
0
  }
7570
0
      break;
7571
7572
0
    case N32_ALU1_XOR_SLLI:
7573
0
    case N32_ALU1_XOR_SRLI:
7574
      /* xor $rt, $rt, $rb -> xor33 for v3, v3m.  */
7575
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7576
0
    && N32_IS_RB3 (insn))
7577
0
  {
7578
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7579
0
      insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RB5 (insn));
7580
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7581
0
      insn16 = N16_MISC33 (XOR33, N32_RT5 (insn), N32_RA5 (insn));
7582
0
    if (insn16)
7583
0
      insn_type = NDS32_INSN_XOR33;
7584
0
  }
7585
0
      break;
7586
7587
0
    case N32_ALU1_OR_SLLI:
7588
0
    case N32_ALU1_OR_SRLI:
7589
      /* or $rt, $rt, $rb -> or33 for v3, v3m.  */
7590
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7591
0
    && N32_IS_RB3 (insn))
7592
0
  {
7593
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7594
0
      insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RB5 (insn));
7595
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7596
0
      insn16 = N16_MISC33 (OR33, N32_RT5 (insn), N32_RA5 (insn));
7597
0
    if (insn16)
7598
0
      insn_type = NDS32_INSN_OR33;
7599
0
  }
7600
0
      break;
7601
0
    case N32_ALU1_NOR:
7602
      /* nor $rt, $ra, $ra -> not33 for v3, v3m.  */
7603
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RB3 (insn)
7604
0
    && N32_RA5 (insn) == N32_RB5 (insn))
7605
0
  {
7606
0
    insn16 = N16_MISC33 (NOT33, N32_RT5 (insn), N32_RA5 (insn));
7607
0
    insn_type = NDS32_INSN_NOT33;
7608
0
  }
7609
0
      break;
7610
0
    case N32_ALU1_SRAI:
7611
0
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7612
0
  {
7613
0
    insn16 = N16_TYPE45 (SRAI45, N32_RT54 (insn), N32_UB5 (insn));
7614
0
    insn_type = NDS32_INSN_SRAI45;
7615
0
  }
7616
0
      break;
7617
7618
0
    case N32_ALU1_SRLI:
7619
0
      if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn))
7620
0
  {
7621
0
    insn16 = N16_TYPE45 (SRLI45, N32_RT54 (insn), N32_UB5 (insn));
7622
0
    insn_type = NDS32_INSN_SRLI45;
7623
0
  }
7624
0
      break;
7625
7626
0
    case N32_ALU1_SLLI:
7627
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_UB5 (insn) < 8)
7628
0
  {
7629
0
    insn16 = N16_TYPE333 (SLLI333, N32_RT5 (insn), N32_RA5 (insn),
7630
0
        N32_UB5 (insn));
7631
0
    insn_type = NDS32_INSN_SLLI333;
7632
0
  }
7633
0
      break;
7634
7635
0
    case N32_ALU1_ZEH:
7636
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7637
0
  {
7638
0
    insn16 = N16_BFMI333 (ZEH33, N32_RT5 (insn), N32_RA5 (insn));
7639
0
    insn_type = NDS32_INSN_ZEH33;
7640
0
  }
7641
0
      break;
7642
7643
0
    case N32_ALU1_SEB:
7644
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7645
0
  {
7646
0
    insn16 = N16_BFMI333 (SEB33, N32_RT5 (insn), N32_RA5 (insn));
7647
0
    insn_type = NDS32_INSN_SEB33;
7648
0
  }
7649
0
      break;
7650
7651
0
    case N32_ALU1_SEH:
7652
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7653
0
  {
7654
0
    insn16 = N16_BFMI333 (SEH33, N32_RT5 (insn), N32_RA5 (insn));
7655
0
    insn_type = NDS32_INSN_SEH33;
7656
0
  }
7657
0
      break;
7658
7659
0
    case N32_ALU1_SLT:
7660
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7661
0
  {
7662
    /* Implicit r15.  */
7663
0
    insn16 = N16_TYPE45 (SLT45, N32_RA54 (insn), N32_RB5 (insn));
7664
0
    insn_type = NDS32_INSN_SLT45;
7665
0
  }
7666
0
      break;
7667
7668
0
    case N32_ALU1_SLTS:
7669
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn))
7670
0
  {
7671
    /* Implicit r15.  */
7672
0
    insn16 = N16_TYPE45 (SLTS45, N32_RA54 (insn), N32_RB5 (insn));
7673
0
    insn_type = NDS32_INSN_SLTS45;
7674
0
  }
7675
0
      break;
7676
0
    }
7677
7678
0
  if ((insn16 & 0x8000) == 0)
7679
0
    return 0;
7680
7681
0
  if (pinsn16)
7682
0
    *pinsn16 = insn16;
7683
0
  if (pinsn_type)
7684
0
    *pinsn_type = insn_type;
7685
0
  return 1;
7686
0
}
7687
7688
static int
7689
nds32_convert_32_to_16_alu2 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7690
           int *pinsn_type)
7691
0
{
7692
0
  uint16_t insn16 = 0;
7693
0
  int insn_type;
7694
0
  unsigned long mach = bfd_get_mach (abfd);
7695
7696
  /* TODO: bset, bclr, btgl, btst.  */
7697
0
  if (__GF (insn, 6, 4) != 0)
7698
0
    return 0;
7699
7700
0
  switch (N32_IMMU (insn, 6))
7701
0
    {
7702
0
    case N32_ALU2_MUL:
7703
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7704
0
    && N32_IS_RB3 (insn))
7705
0
  {
7706
0
    if (N32_RT5 (insn) == N32_RA5 (insn))
7707
0
      insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RB5 (insn));
7708
0
    else if (N32_RT5 (insn) == N32_RB5 (insn))
7709
0
      insn16 = N16_MISC33 (MUL33, N32_RT5 (insn), N32_RA5 (insn));
7710
0
    if (insn16)
7711
0
      insn_type = NDS32_INSN_MUL33;
7712
0
  }
7713
0
    }
7714
7715
0
  if ((insn16 & 0x8000) == 0)
7716
0
    return 0;
7717
7718
0
  if (pinsn16)
7719
0
    *pinsn16 = insn16;
7720
0
  if (pinsn_type)
7721
0
    *pinsn_type = insn_type;
7722
0
  return 1;
7723
0
}
7724
7725
int
7726
nds32_convert_32_to_16 (bfd *abfd, uint32_t insn, uint16_t *pinsn16,
7727
      int *pinsn_type)
7728
0
{
7729
0
  int op6;
7730
0
  uint16_t insn16 = 0;
7731
0
  int insn_type = 0;
7732
0
  unsigned long mach = bfd_get_mach (abfd);
7733
7734
  /* Decode 32-bit instruction.  */
7735
0
  if (insn & 0x80000000)
7736
0
    {
7737
      /* Not 32-bit insn.  */
7738
0
      return 0;
7739
0
    }
7740
7741
0
  op6 = N32_OP6 (insn);
7742
7743
  /* Convert it to 16-bit instruction.  */
7744
0
  switch (op6)
7745
0
    {
7746
0
    case N32_OP6_MOVI:
7747
0
      if (IS_WITHIN_S (N32_IMM20S (insn), 5))
7748
0
  {
7749
0
    insn16 = N16_TYPE55 (MOVI55, N32_RT5 (insn), N32_IMM20S (insn));
7750
0
    insn_type = NDS32_INSN_MOVI55;
7751
0
  }
7752
0
      else if (mach >= MACH_V3 && N32_IMM20S (insn) >= 16
7753
0
         && N32_IMM20S (insn) < 48 && N32_IS_RT4 (insn))
7754
0
  {
7755
0
    insn16 = N16_TYPE45 (MOVPI45, N32_RT54 (insn),
7756
0
             N32_IMM20S (insn) - 16);
7757
0
    insn_type = NDS32_INSN_MOVPI45;
7758
0
  }
7759
0
      break;
7760
7761
0
    case N32_OP6_ADDI:
7762
0
      if (N32_IMM15S (insn) == 0)
7763
0
  {
7764
    /* Do not convert `addi $sp, $sp, 0' to `mov55 $sp, $sp',
7765
       because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7766
0
    if (mach <= MACH_V2
7767
0
        || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7768
0
      {
7769
0
        insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7770
0
        insn_type = NDS32_INSN_MOV55;
7771
0
      }
7772
0
  }
7773
0
      else if (N32_IMM15S (insn) > 0)
7774
0
  {
7775
0
    if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) < 8)
7776
0
      {
7777
0
        insn16 = N16_TYPE333 (ADDI333, N32_RT5 (insn), N32_RA5 (insn),
7778
0
            N32_IMM15S (insn));
7779
0
        insn_type = NDS32_INSN_ADDI333;
7780
0
      }
7781
0
    else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7782
0
       && N32_IMM15S (insn) < 32)
7783
0
      {
7784
0
        insn16 = N16_TYPE45 (ADDI45, N32_RT54 (insn), N32_IMM15S (insn));
7785
0
        insn_type = NDS32_INSN_ADDI45;
7786
0
      }
7787
0
    else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7788
0
       && N32_RT5 (insn) == N32_RA5 (insn)
7789
0
       && N32_IMM15S (insn) < 512)
7790
0
      {
7791
0
        insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7792
0
        insn_type = NDS32_INSN_ADDI10_SP;
7793
0
      }
7794
0
    else if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7795
0
       && N32_RA5 (insn) == REG_SP && N32_IMM15S (insn) < 256
7796
0
       && (N32_IMM15S (insn) % 4 == 0))
7797
0
      {
7798
0
        insn16 = N16_TYPE36 (ADDRI36_SP, N32_RT5 (insn),
7799
0
           N32_IMM15S (insn) >> 2);
7800
0
        insn_type = NDS32_INSN_ADDRI36_SP;
7801
0
      }
7802
0
  }
7803
0
      else
7804
0
  {
7805
    /* Less than 0.  */
7806
0
    if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn) && N32_IMM15S (insn) > -8)
7807
0
      {
7808
0
        insn16 = N16_TYPE333 (SUBI333, N32_RT5 (insn), N32_RA5 (insn),
7809
0
            0 - N32_IMM15S (insn));
7810
0
        insn_type = NDS32_INSN_SUBI333;
7811
0
      }
7812
0
    else if (N32_IS_RT4 (insn) && N32_RT5 (insn) == N32_RA5 (insn)
7813
0
       && N32_IMM15S (insn) > -32)
7814
0
      {
7815
0
        insn16 = N16_TYPE45 (SUBI45, N32_RT54 (insn),
7816
0
           0 - N32_IMM15S (insn));
7817
0
        insn_type = NDS32_INSN_SUBI45;
7818
0
      }
7819
0
    else if (mach >= MACH_V2 && N32_RT5 (insn) == REG_SP
7820
0
       && N32_RT5 (insn) == N32_RA5 (insn)
7821
0
       && N32_IMM15S (insn) >= -512)
7822
0
      {
7823
0
        insn16 = N16_TYPE10 (ADDI10S, N32_IMM15S (insn));
7824
0
        insn_type = NDS32_INSN_ADDI10_SP;
7825
0
      }
7826
0
  }
7827
0
      break;
7828
7829
0
    case N32_OP6_ORI:
7830
0
      if (N32_IMM15S (insn) == 0)
7831
0
  {
7832
    /* Do not convert `ori $sp, $sp, 0' to `mov55 $sp, $sp',
7833
       because `mov55 $sp, $sp' is ifret16 in V3 ISA.  */
7834
0
    if (mach <= MACH_V2
7835
0
        || N32_RT5 (insn) != REG_SP || N32_RA5 (insn) != REG_SP)
7836
0
      {
7837
0
        insn16 = N16_TYPE55 (MOV55, N32_RT5 (insn), N32_RA5 (insn));
7838
0
        insn_type = NDS32_INSN_MOV55;
7839
0
      }
7840
0
  }
7841
0
      break;
7842
7843
0
    case N32_OP6_SUBRI:
7844
0
      if (mach >= MACH_V3 && N32_IS_RT3 (insn)
7845
0
    && N32_IS_RA3 (insn) && N32_IMM15S (insn) == 0)
7846
0
  {
7847
0
    insn16 = N16_MISC33 (NEG33, N32_RT5 (insn), N32_RA5 (insn));
7848
0
    insn_type = NDS32_INSN_NEG33;
7849
0
  }
7850
0
      break;
7851
7852
0
    case N32_OP6_ANDI:
7853
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn))
7854
0
  {
7855
0
    if (N32_IMM15U (insn) == 1)
7856
0
      {
7857
0
        insn16 = N16_BFMI333 (XLSB33, N32_RT5 (insn), N32_RA5 (insn));
7858
0
        insn_type = NDS32_INSN_XLSB33;
7859
0
      }
7860
0
    else if (N32_IMM15U (insn) == 0x7ff)
7861
0
      {
7862
0
        insn16 = N16_BFMI333 (X11B33, N32_RT5 (insn), N32_RA5 (insn));
7863
0
        insn_type = NDS32_INSN_X11B33;
7864
0
      }
7865
0
    else if (N32_IMM15U (insn) == 0xff)
7866
0
      {
7867
0
        insn16 = N16_BFMI333 (ZEB33, N32_RT5 (insn), N32_RA5 (insn));
7868
0
        insn_type = NDS32_INSN_ZEB33;
7869
0
      }
7870
0
    else if (mach >= MACH_V3 && N32_RT5 (insn) == N32_RA5 (insn)
7871
0
       && N32_IMM15U (insn) < 256)
7872
0
      {
7873
0
        int imm15u = N32_IMM15U (insn);
7874
7875
0
        if (__builtin_popcount (imm15u) == 1)
7876
0
    {
7877
      /* BMSKI33 */
7878
0
      int imm3u = __builtin_ctz (imm15u);
7879
7880
0
      insn16 = N16_BFMI333 (BMSKI33, N32_RT5 (insn), imm3u);
7881
0
      insn_type = NDS32_INSN_BMSKI33;
7882
0
    }
7883
0
        else if (imm15u != 0 && __builtin_popcount (imm15u + 1) == 1)
7884
0
    {
7885
      /* FEXTI33 */
7886
0
      int imm3u = __builtin_ctz (imm15u + 1) - 1;
7887
7888
0
      insn16 = N16_BFMI333 (FEXTI33, N32_RT5 (insn), imm3u);
7889
0
      insn_type = NDS32_INSN_FEXTI33;
7890
0
    }
7891
0
      }
7892
0
  }
7893
0
      break;
7894
7895
0
    case N32_OP6_SLTI:
7896
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7897
0
    && IS_WITHIN_U (N32_IMM15S (insn), 5))
7898
0
  {
7899
0
    insn16 = N16_TYPE45 (SLTI45, N32_RA54 (insn), N32_IMM15S (insn));
7900
0
    insn_type = NDS32_INSN_SLTI45;
7901
0
  }
7902
0
      break;
7903
7904
0
    case N32_OP6_SLTSI:
7905
0
      if (N32_RT5 (insn) == REG_R15 && N32_IS_RA4 (insn)
7906
0
    && IS_WITHIN_U (N32_IMM15S (insn), 5))
7907
0
  {
7908
0
    insn16 = N16_TYPE45 (SLTSI45, N32_RA54 (insn), N32_IMM15S (insn));
7909
0
    insn_type = NDS32_INSN_SLTSI45;
7910
0
  }
7911
0
      break;
7912
7913
0
    case N32_OP6_LWI:
7914
0
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7915
0
  {
7916
0
    insn16 = N16_TYPE45 (LWI450, N32_RT54 (insn), N32_RA5 (insn));
7917
0
    insn_type = NDS32_INSN_LWI450;
7918
0
  }
7919
0
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7920
0
         && IS_WITHIN_U (N32_IMM15S (insn), 3))
7921
0
  {
7922
0
    insn16 = N16_TYPE333 (LWI333, N32_RT5 (insn), N32_RA5 (insn),
7923
0
        N32_IMM15S (insn));
7924
0
    insn_type = NDS32_INSN_LWI333;
7925
0
  }
7926
0
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7927
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
7928
0
  {
7929
0
    insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
7930
0
    insn_type = NDS32_INSN_LWI37;
7931
0
  }
7932
0
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7933
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
7934
0
  {
7935
0
    insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 0, N32_IMM15S (insn));
7936
0
    insn_type = NDS32_INSN_LWI37_SP;
7937
0
  }
7938
0
      else if (mach >= MACH_V2 && N32_IS_RT4 (insn) && N32_RA5 (insn) == REG_R8
7939
0
         && -32 <= N32_IMM15S (insn) && N32_IMM15S (insn) < 0)
7940
0
  {
7941
0
    insn16 = N16_TYPE45 (LWI45_FE, N32_RT54 (insn),
7942
0
             N32_IMM15S (insn) + 32);
7943
0
    insn_type = NDS32_INSN_LWI45_FE;
7944
0
  }
7945
0
      break;
7946
7947
0
    case N32_OP6_SWI:
7948
0
      if (N32_IS_RT4 (insn) && N32_IMM15S (insn) == 0)
7949
0
  {
7950
0
    insn16 = N16_TYPE45 (SWI450, N32_RT54 (insn), N32_RA5 (insn));
7951
0
    insn_type = NDS32_INSN_SWI450;
7952
0
  }
7953
0
      else if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7954
0
         && IS_WITHIN_U (N32_IMM15S (insn), 3))
7955
0
  {
7956
0
    insn16 = N16_TYPE333 (SWI333, N32_RT5 (insn), N32_RA5 (insn),
7957
0
        N32_IMM15S (insn));
7958
0
    insn_type = NDS32_INSN_SWI333;
7959
0
  }
7960
0
      else if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_FP
7961
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
7962
0
  {
7963
0
    insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
7964
0
    insn_type = NDS32_INSN_SWI37;
7965
0
  }
7966
0
      else if (mach >= MACH_V2 && N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_SP
7967
0
         && IS_WITHIN_U (N32_IMM15S (insn), 7))
7968
0
  {
7969
0
    insn16 = N16_TYPE37 (XWI37SP, N32_RT5 (insn), 1, N32_IMM15S (insn));
7970
0
    insn_type = NDS32_INSN_SWI37_SP;
7971
0
  }
7972
0
      break;
7973
7974
0
    case N32_OP6_LWI_BI:
7975
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7976
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
7977
0
  {
7978
0
    insn16 = N16_TYPE333 (LWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7979
0
        N32_IMM15S (insn));
7980
0
    insn_type = NDS32_INSN_LWI333_BI;
7981
0
  }
7982
0
      break;
7983
7984
0
    case N32_OP6_SWI_BI:
7985
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7986
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
7987
0
  {
7988
0
    insn16 = N16_TYPE333 (SWI333_BI, N32_RT5 (insn), N32_RA5 (insn),
7989
0
        N32_IMM15S (insn));
7990
0
    insn_type = NDS32_INSN_SWI333_BI;
7991
0
  }
7992
0
      break;
7993
7994
0
    case N32_OP6_LHI:
7995
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
7996
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
7997
0
  {
7998
0
    insn16 = N16_TYPE333 (LHI333, N32_RT5 (insn), N32_RA5 (insn),
7999
0
        N32_IMM15S (insn));
8000
0
    insn_type = NDS32_INSN_LHI333;
8001
0
  }
8002
0
      break;
8003
8004
0
    case N32_OP6_SHI:
8005
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8006
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8007
0
  {
8008
0
    insn16 = N16_TYPE333 (SHI333, N32_RT5 (insn), N32_RA5 (insn),
8009
0
        N32_IMM15S (insn));
8010
0
    insn_type = NDS32_INSN_SHI333;
8011
0
  }
8012
0
      break;
8013
8014
0
    case N32_OP6_LBI:
8015
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8016
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8017
0
  {
8018
0
    insn16 = N16_TYPE333 (LBI333, N32_RT5 (insn), N32_RA5 (insn),
8019
0
        N32_IMM15S (insn));
8020
0
    insn_type = NDS32_INSN_LBI333;
8021
0
  }
8022
0
      break;
8023
8024
0
    case N32_OP6_SBI:
8025
0
      if (N32_IS_RT3 (insn) && N32_IS_RA3 (insn)
8026
0
    && IS_WITHIN_U (N32_IMM15S (insn), 3))
8027
0
  {
8028
0
    insn16 = N16_TYPE333 (SBI333, N32_RT5 (insn), N32_RA5 (insn),
8029
0
        N32_IMM15S (insn));
8030
0
    insn_type = NDS32_INSN_SBI333;
8031
0
  }
8032
0
      break;
8033
8034
0
    case N32_OP6_ALU1:
8035
0
      return nds32_convert_32_to_16_alu1 (abfd, insn, pinsn16, pinsn_type);
8036
8037
0
    case N32_OP6_ALU2:
8038
0
      return nds32_convert_32_to_16_alu2 (abfd, insn, pinsn16, pinsn_type);
8039
8040
0
    case N32_OP6_BR1:
8041
0
      if (!IS_WITHIN_S (N32_IMM14S (insn), 8))
8042
0
  goto done;
8043
8044
0
      if ((insn & N32_BIT (14)) == 0)
8045
0
  {
8046
    /* N32_BR1_BEQ */
8047
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8048
0
        && N32_RT5 (insn) != REG_R5)
8049
0
      insn16 = N16_TYPE38 (BEQS38, N32_RT5 (insn), N32_IMM14S (insn));
8050
0
    else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8051
0
       && N32_RA5 (insn) != REG_R5)
8052
0
      insn16 = N16_TYPE38 (BEQS38, N32_RA5 (insn), N32_IMM14S (insn));
8053
0
    insn_type = NDS32_INSN_BEQS38;
8054
0
    break;
8055
0
  }
8056
0
      else
8057
0
  {
8058
    /* N32_BR1_BNE */
8059
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5
8060
0
        && N32_RT5 (insn) != REG_R5)
8061
0
      insn16 = N16_TYPE38 (BNES38, N32_RT5 (insn), N32_IMM14S (insn));
8062
0
    else if (N32_IS_RA3 (insn) && N32_RT5 (insn) == REG_R5
8063
0
       && N32_RA5 (insn) != REG_R5)
8064
0
      insn16 = N16_TYPE38 (BNES38, N32_RA5 (insn), N32_IMM14S (insn));
8065
0
    insn_type = NDS32_INSN_BNES38;
8066
0
    break;
8067
0
  }
8068
0
      break;
8069
8070
0
    case N32_OP6_BR2:
8071
0
      switch (N32_BR2_SUB (insn))
8072
0
  {
8073
0
  case N32_BR2_BEQZ:
8074
0
    if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8075
0
      {
8076
0
        insn16 = N16_TYPE38 (BEQZ38, N32_RT5 (insn), N32_IMM16S (insn));
8077
0
        insn_type = NDS32_INSN_BEQZ38;
8078
0
      }
8079
0
    else if (N32_RT5 (insn) == REG_R15
8080
0
       && IS_WITHIN_S (N32_IMM16S (insn), 8))
8081
0
      {
8082
0
        insn16 = N16_TYPE8 (BEQZS8, N32_IMM16S (insn));
8083
0
        insn_type = NDS32_INSN_BEQZS8;
8084
0
      }
8085
0
    break;
8086
8087
0
  case N32_BR2_BNEZ:
8088
0
    if (N32_IS_RT3 (insn) && IS_WITHIN_S (N32_IMM16S (insn), 8))
8089
0
      {
8090
0
        insn16 = N16_TYPE38 (BNEZ38, N32_RT5 (insn), N32_IMM16S (insn));
8091
0
        insn_type = NDS32_INSN_BNEZ38;
8092
0
      }
8093
0
    else if (N32_RT5 (insn) == REG_R15
8094
0
       && IS_WITHIN_S (N32_IMM16S (insn), 8))
8095
0
      {
8096
0
        insn16 = N16_TYPE8 (BNEZS8, N32_IMM16S (insn));
8097
0
        insn_type = NDS32_INSN_BNEZS8;
8098
0
      }
8099
0
    break;
8100
8101
0
  case N32_BR2_SOP0:
8102
0
    if (__GF (insn, 20, 5) == 0 && IS_WITHIN_U (N32_IMM16S (insn), 9))
8103
0
      {
8104
0
        insn16 = N16_TYPE9 (IFCALL9, N32_IMM16S (insn));
8105
0
        insn_type = NDS32_INSN_IFCALL9;
8106
0
      }
8107
0
    break;
8108
0
  }
8109
0
      break;
8110
8111
0
    case N32_OP6_JI:
8112
0
      if ((insn & N32_BIT (24)) == 0)
8113
0
  {
8114
    /* N32_JI_J */
8115
0
    if (IS_WITHIN_S (N32_IMM24S (insn), 8))
8116
0
      {
8117
0
        insn16 = N16_TYPE8 (J8, N32_IMM24S (insn));
8118
0
        insn_type = NDS32_INSN_J8;
8119
0
      }
8120
0
  }
8121
0
      break;
8122
8123
0
    case N32_OP6_JREG:
8124
0
      if (__GF (insn, 8, 2) != 0)
8125
0
  goto done;
8126
8127
0
      switch (N32_IMMU (insn, 5))
8128
0
  {
8129
0
  case N32_JREG_JR:
8130
0
    if (N32_JREG_HINT (insn) == 0)
8131
0
      {
8132
        /* jr */
8133
0
        insn16 = N16_TYPE5 (JR5, N32_RB5 (insn));
8134
0
        insn_type = NDS32_INSN_JR5;
8135
0
      }
8136
0
    else if (N32_JREG_HINT (insn) == 1)
8137
0
      {
8138
        /* ret */
8139
0
        insn16 = N16_TYPE5 (RET5, N32_RB5 (insn));
8140
0
        insn_type = NDS32_INSN_RET5;
8141
0
      }
8142
0
    else if (N32_JREG_HINT (insn) == 3)
8143
0
      {
8144
        /* ifret = mov55 $sp, $sp */
8145
0
        insn16 = N16_TYPE55 (MOV55, REG_SP, REG_SP);
8146
0
        insn_type = NDS32_INSN_IFRET;
8147
0
      }
8148
0
    break;
8149
8150
0
  case N32_JREG_JRAL:
8151
    /* It's convertible when return rt5 is $lp and address
8152
       translation is kept.  */
8153
0
    if (N32_RT5 (insn) == REG_LP && N32_JREG_HINT (insn) == 0)
8154
0
      {
8155
0
        insn16 = N16_TYPE5 (JRAL5, N32_RB5 (insn));
8156
0
        insn_type = NDS32_INSN_JRAL5;
8157
0
      }
8158
0
    break;
8159
0
  }
8160
0
      break;
8161
8162
0
    case N32_OP6_MISC:
8163
0
      if (N32_SUB5 (insn) == N32_MISC_BREAK && N32_SWID (insn) < 32)
8164
0
  {
8165
    /* For v3, swid above 31 are used for ex9.it.  */
8166
0
    insn16 = N16_TYPE5 (BREAK16, N32_SWID (insn));
8167
0
    insn_type = NDS32_INSN_BREAK16;
8168
0
  }
8169
0
      break;
8170
8171
0
    default:
8172
      /* This instruction has no 16-bit variant.  */
8173
0
      goto done;
8174
0
    }
8175
8176
0
 done:
8177
  /* Bit-15 of insn16 should be set for a valid instruction.  */
8178
0
  if ((insn16 & 0x8000) == 0)
8179
0
    return 0;
8180
8181
0
  if (pinsn16)
8182
0
    *pinsn16 = insn16;
8183
0
  if (pinsn_type)
8184
0
    *pinsn_type = insn_type;
8185
0
  return 1;
8186
0
}
8187
8188
static int
8189
special_convert_32_to_16 (unsigned long insn, uint16_t *pinsn16,
8190
        Elf_Internal_Rela *reloc)
8191
0
{
8192
0
  uint16_t insn16 = 0;
8193
8194
0
  if ((reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG) == 0
8195
0
      || (ELF32_R_TYPE (reloc->r_info) != R_NDS32_INSN16))
8196
0
    return 0;
8197
8198
0
  if (!N32_IS_RT3 (insn))
8199
0
    return 0;
8200
8201
0
  switch (N32_OP6 (insn))
8202
0
    {
8203
0
    case N32_OP6_LWI:
8204
0
      if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8205
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM15S (insn));
8206
0
      break;
8207
0
    case N32_OP6_SWI:
8208
0
      if (N32_RA5 (insn) == REG_GP && IS_WITHIN_U (N32_IMM15S (insn), 7))
8209
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM15S (insn));
8210
0
      break;
8211
0
    case N32_OP6_HWGP:
8212
0
      if (!IS_WITHIN_U (N32_IMM17S (insn), 7))
8213
0
  break;
8214
8215
0
      if (__GF (insn, 17, 3) == 6)
8216
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 0, N32_IMM17S (insn));
8217
0
      else if (__GF (insn, 17, 3) == 7)
8218
0
  insn16 = N16_TYPE37 (XWI37, N32_RT5 (insn), 1, N32_IMM17S (insn));
8219
0
      break;
8220
0
    }
8221
8222
0
  if ((insn16 & 0x8000) == 0)
8223
0
    return 0;
8224
8225
0
  *pinsn16 = insn16;
8226
0
  return 1;
8227
0
}
8228
8229
/* Convert a 16-bit instruction to 32-bit one.
8230
   INSN16 it the input and PINSN it the point to output.
8231
   Return non-zero on successful.  Otherwise 0 is returned.  */
8232
8233
int
8234
nds32_convert_16_to_32 (bfd *abfd, uint16_t insn16, uint32_t *pinsn)
8235
0
{
8236
0
  uint32_t insn = 0xffffffff;
8237
0
  unsigned long mach = bfd_get_mach (abfd);
8238
8239
  /* NOTE: push25, pop25 and movd44 do not have 32-bit variants.  */
8240
8241
0
  switch (__GF (insn16, 9, 6))
8242
0
    {
8243
0
    case 0x4:     /* add45 */
8244
0
      insn = N32_ALU1 (ADD, N16_RT4 (insn16), N16_RT4 (insn16),
8245
0
           N16_RA5 (insn16));
8246
0
      goto done;
8247
0
    case 0x5:     /* sub45 */
8248
0
      insn = N32_ALU1 (SUB, N16_RT4 (insn16), N16_RT4 (insn16),
8249
0
           N16_RA5 (insn16));
8250
0
      goto done;
8251
0
    case 0x6:     /* addi45 */
8252
0
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8253
0
      N16_IMM5U (insn16));
8254
0
      goto done;
8255
0
    case 0x7:     /* subi45 */
8256
0
      insn = N32_TYPE2 (ADDI, N16_RT4 (insn16), N16_RT4 (insn16),
8257
0
      -N16_IMM5U (insn16));
8258
0
      goto done;
8259
0
    case 0x8:     /* srai45 */
8260
0
      insn = N32_ALU1 (SRAI, N16_RT4 (insn16), N16_RT4 (insn16),
8261
0
           N16_IMM5U (insn16));
8262
0
      goto done;
8263
0
    case 0x9:     /* srli45 */
8264
0
      insn = N32_ALU1 (SRLI, N16_RT4 (insn16), N16_RT4 (insn16),
8265
0
           N16_IMM5U (insn16));
8266
0
      goto done;
8267
0
    case 0xa:     /* slli333 */
8268
0
      insn = N32_ALU1 (SLLI, N16_RT3 (insn16), N16_RA3 (insn16),
8269
0
           N16_IMM3U (insn16));
8270
0
      goto done;
8271
0
    case 0xc:     /* add333 */
8272
0
      insn = N32_ALU1 (ADD, N16_RT3 (insn16), N16_RA3 (insn16),
8273
0
           N16_RB3 (insn16));
8274
0
      goto done;
8275
0
    case 0xd:     /* sub333 */
8276
0
      insn = N32_ALU1 (SUB, N16_RT3 (insn16), N16_RA3 (insn16),
8277
0
           N16_RB3 (insn16));
8278
0
      goto done;
8279
0
    case 0xe:     /* addi333 */
8280
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8281
0
      N16_IMM3U (insn16));
8282
0
      goto done;
8283
0
    case 0xf:     /* subi333 */
8284
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), N16_RA3 (insn16),
8285
0
      -N16_IMM3U (insn16));
8286
0
      goto done;
8287
0
    case 0x10:      /* lwi333 */
8288
0
      insn = N32_TYPE2 (LWI, N16_RT3 (insn16), N16_RA3 (insn16),
8289
0
      N16_IMM3U (insn16));
8290
0
      goto done;
8291
0
    case 0x12:      /* lhi333 */
8292
0
      insn = N32_TYPE2 (LHI, N16_RT3 (insn16), N16_RA3 (insn16),
8293
0
      N16_IMM3U (insn16));
8294
0
      goto done;
8295
0
    case 0x13:      /* lbi333 */
8296
0
      insn = N32_TYPE2 (LBI, N16_RT3 (insn16), N16_RA3 (insn16),
8297
0
      N16_IMM3U (insn16));
8298
0
      goto done;
8299
0
    case 0x11:      /* lwi333.bi */
8300
0
      insn = N32_TYPE2 (LWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8301
0
      N16_IMM3U (insn16));
8302
0
      goto done;
8303
0
    case 0x14:      /* swi333 */
8304
0
      insn = N32_TYPE2 (SWI, N16_RT3 (insn16), N16_RA3 (insn16),
8305
0
      N16_IMM3U (insn16));
8306
0
      goto done;
8307
0
    case 0x16:      /* shi333 */
8308
0
      insn = N32_TYPE2 (SHI, N16_RT3 (insn16), N16_RA3 (insn16),
8309
0
      N16_IMM3U (insn16));
8310
0
      goto done;
8311
0
    case 0x17:      /* sbi333 */
8312
0
      insn = N32_TYPE2 (SBI, N16_RT3 (insn16), N16_RA3 (insn16),
8313
0
      N16_IMM3U (insn16));
8314
0
      goto done;
8315
0
    case 0x15:      /* swi333.bi */
8316
0
      insn = N32_TYPE2 (SWI_BI, N16_RT3 (insn16), N16_RA3 (insn16),
8317
0
      N16_IMM3U (insn16));
8318
0
      goto done;
8319
0
    case 0x18:      /* addri36.sp */
8320
0
      insn = N32_TYPE2 (ADDI, N16_RT3 (insn16), REG_SP,
8321
0
      N16_IMM6U (insn16) << 2);
8322
0
      goto done;
8323
0
    case 0x19:      /* lwi45.fe */
8324
0
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), REG_R8,
8325
0
      (N16_IMM5U (insn16) - 32));
8326
0
      goto done;
8327
0
    case 0x1a:      /* lwi450 */
8328
0
      insn = N32_TYPE2 (LWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8329
0
      goto done;
8330
0
    case 0x1b:      /* swi450 */
8331
0
      insn = N32_TYPE2 (SWI, N16_RT4 (insn16), N16_RA5 (insn16), 0);
8332
0
      goto done;
8333
8334
      /* These are r15 implied instructions.  */
8335
0
    case 0x30:      /* slts45 */
8336
0
      insn = N32_ALU1 (SLTS, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8337
0
      goto done;
8338
0
    case 0x31:      /* slt45 */
8339
0
      insn = N32_ALU1 (SLT, REG_TA, N16_RT4 (insn16), N16_RA5 (insn16));
8340
0
      goto done;
8341
0
    case 0x32:      /* sltsi45 */
8342
0
      insn = N32_TYPE2 (SLTSI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8343
0
      goto done;
8344
0
    case 0x33:      /* slti45 */
8345
0
      insn = N32_TYPE2 (SLTI, REG_TA, N16_RT4 (insn16), N16_IMM5U (insn16));
8346
0
      goto done;
8347
0
    case 0x34:      /* beqzs8, bnezs8 */
8348
0
      if (insn16 & N32_BIT (8))
8349
0
  insn = N32_BR2 (BNEZ, REG_TA, N16_IMM8S (insn16));
8350
0
      else
8351
0
  insn = N32_BR2 (BEQZ, REG_TA, N16_IMM8S (insn16));
8352
0
      goto done;
8353
8354
0
    case 0x35:      /* break16, ex9.it */
8355
      /* Only consider range of v3 break16.  */
8356
0
      insn = N32_TYPE0 (MISC, (N16_IMM5U (insn16) << 5) | N32_MISC_BREAK);
8357
0
      goto done;
8358
8359
0
    case 0x3c:      /* ifcall9 */
8360
0
      insn = N32_BR2 (SOP0, 0, N16_IMM9U (insn16));
8361
0
      goto done;
8362
0
    case 0x3d:      /* movpi45 */
8363
0
      insn = N32_TYPE1 (MOVI, N16_RT4 (insn16), N16_IMM5U (insn16) + 16);
8364
0
      goto done;
8365
8366
0
    case 0x3f:      /* MISC33 */
8367
0
      switch (insn16 & 0x7)
8368
0
  {
8369
0
  case 2:     /* neg33 */
8370
0
    insn = N32_TYPE2 (SUBRI, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8371
0
    break;
8372
0
  case 3:     /* not33 */
8373
0
    insn = N32_ALU1 (NOR, N16_RT3 (insn16), N16_RA3 (insn16),
8374
0
         N16_RA3 (insn16));
8375
0
    break;
8376
0
  case 4:     /* mul33 */
8377
0
    insn = N32_ALU2 (MUL, N16_RT3 (insn16), N16_RT3 (insn16),
8378
0
         N16_RA3 (insn16));
8379
0
    break;
8380
0
  case 5:     /* xor33 */
8381
0
    insn = N32_ALU1 (XOR, N16_RT3 (insn16), N16_RT3 (insn16),
8382
0
         N16_RA3 (insn16));
8383
0
    break;
8384
0
  case 6:     /* and33 */
8385
0
    insn = N32_ALU1 (AND, N16_RT3 (insn16), N16_RT3 (insn16),
8386
0
         N16_RA3 (insn16));
8387
0
    break;
8388
0
  case 7:     /* or33 */
8389
0
    insn = N32_ALU1 (OR, N16_RT3 (insn16), N16_RT3 (insn16),
8390
0
         N16_RA3 (insn16));
8391
0
    break;
8392
0
  }
8393
0
      goto done;
8394
8395
0
    case 0xb:
8396
0
      switch (insn16 & 0x7)
8397
0
  {
8398
0
  case 0:     /* zeb33 */
8399
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0xff);
8400
0
    break;
8401
0
  case 1:     /* zeh33 */
8402
0
    insn = N32_ALU1 (ZEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8403
0
    break;
8404
0
  case 2:     /* seb33 */
8405
0
    insn = N32_ALU1 (SEB, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8406
0
    break;
8407
0
  case 3:     /* seh33 */
8408
0
    insn = N32_ALU1 (SEH, N16_RT3 (insn16), N16_RA3 (insn16), 0);
8409
0
    break;
8410
0
  case 4:     /* xlsb33 */
8411
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 1);
8412
0
    break;
8413
0
  case 5:     /* x11b33 */
8414
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RA3 (insn16), 0x7ff);
8415
0
    break;
8416
0
  case 6:     /* bmski33 */
8417
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8418
0
          1 << __GF (insn16, 3, 3));
8419
0
    break;
8420
0
  case 7:     /* fexti33 */
8421
0
    insn = N32_TYPE2 (ANDI, N16_RT3 (insn16), N16_RT3 (insn16),
8422
0
          (1 << (__GF (insn16, 3, 3) + 1)) - 1);
8423
0
    break;
8424
0
  }
8425
0
      goto done;
8426
0
    }
8427
8428
0
  switch (__GF (insn16, 10, 5))
8429
0
    {
8430
0
    case 0x0:     /* mov55 or ifret16 */
8431
0
      if (mach >= MACH_V3 && N16_RT5 (insn16) == REG_SP
8432
0
    && N16_RT5 (insn16) == N16_RA5 (insn16))
8433
0
  insn = N32_JREG (JR, 0, 0, 0, 3);
8434
0
      else
8435
0
  insn = N32_TYPE2 (ADDI, N16_RT5 (insn16), N16_RA5 (insn16), 0);
8436
0
      goto done;
8437
0
    case 0x1:     /* movi55 */
8438
0
      insn = N32_TYPE1 (MOVI, N16_RT5 (insn16), N16_IMM5S (insn16));
8439
0
      goto done;
8440
0
    case 0x1b:      /* addi10s (V2) */
8441
0
      insn = N32_TYPE2 (ADDI, REG_SP, REG_SP, N16_IMM10S (insn16));
8442
0
      goto done;
8443
0
    }
8444
8445
0
  switch (__GF (insn16, 11, 4))
8446
0
    {
8447
0
    case 0x7:     /* lwi37.fp/swi37.fp */
8448
0
      if (insn16 & N32_BIT (7)) /* swi37.fp */
8449
0
  insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8450
0
      else      /* lwi37.fp */
8451
0
  insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_FP, N16_IMM7U (insn16));
8452
0
      goto done;
8453
0
    case 0x8:     /* beqz38 */
8454
0
      insn = N32_BR2 (BEQZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8455
0
      goto done;
8456
0
    case 0x9:     /* bnez38 */
8457
0
      insn = N32_BR2 (BNEZ, N16_RT38 (insn16), N16_IMM8S (insn16));
8458
0
      goto done;
8459
0
    case 0xa:     /* beqs38/j8, implied r5 */
8460
0
      if (N16_RT38 (insn16) == 5)
8461
0
  insn = N32_JI (J, N16_IMM8S (insn16));
8462
0
      else
8463
0
  insn = N32_BR1 (BEQ, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8464
0
      goto done;
8465
0
    case 0xb:     /* bnes38 and others.  */
8466
0
      if (N16_RT38 (insn16) == 5)
8467
0
  {
8468
0
    switch (__GF (insn16, 5, 3))
8469
0
      {
8470
0
      case 0:   /* jr5 */
8471
0
        insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 0);
8472
0
        break;
8473
0
      case 4:   /* ret5 */
8474
0
        insn = N32_JREG (JR, 0, N16_RA5 (insn16), 0, 1);
8475
0
        break;
8476
0
      case 1:   /* jral5 */
8477
0
        insn = N32_JREG (JRAL, REG_LP, N16_RA5 (insn16), 0, 0);
8478
0
        break;
8479
0
      case 2:   /* ex9.it imm5 */
8480
        /* ex9.it had no 32-bit variantl.  */
8481
0
        break;
8482
0
      case 5:   /* add5.pc */
8483
        /* add5.pc had no 32-bit variantl.  */
8484
0
        break;
8485
0
      }
8486
0
  }
8487
0
      else      /* bnes38 */
8488
0
  insn = N32_BR1 (BNE, N16_RT38 (insn16), REG_R5, N16_IMM8S (insn16));
8489
0
      goto done;
8490
0
    case 0xe:     /* lwi37/swi37 */
8491
0
      if (insn16 & (1 << 7)) /* swi37.sp */
8492
0
  insn = N32_TYPE2 (SWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8493
0
      else      /* lwi37.sp */
8494
0
  insn = N32_TYPE2 (LWI, N16_RT38 (insn16), REG_SP, N16_IMM7U (insn16));
8495
0
      goto done;
8496
0
    }
8497
8498
0
 done:
8499
0
  if (insn & 0x80000000)
8500
0
    return 0;
8501
8502
0
  if (pinsn)
8503
0
    *pinsn = insn;
8504
0
  return 1;
8505
0
}
8506

8507
8508
static bool
8509
is_sda_access_insn (unsigned long insn)
8510
0
{
8511
0
  switch (N32_OP6 (insn))
8512
0
    {
8513
0
    case N32_OP6_LWI:
8514
0
    case N32_OP6_LHI:
8515
0
    case N32_OP6_LHSI:
8516
0
    case N32_OP6_LBI:
8517
0
    case N32_OP6_LBSI:
8518
0
    case N32_OP6_SWI:
8519
0
    case N32_OP6_SHI:
8520
0
    case N32_OP6_SBI:
8521
0
    case N32_OP6_LWC:
8522
0
    case N32_OP6_LDC:
8523
0
    case N32_OP6_SWC:
8524
0
    case N32_OP6_SDC:
8525
0
      return true;
8526
0
    default:
8527
0
      ;
8528
0
    }
8529
0
  return false;
8530
0
}
8531
8532
static unsigned long
8533
turn_insn_to_sda_access (uint32_t insn, bfd_signed_vma type, uint32_t *pinsn)
8534
0
{
8535
0
  uint32_t oinsn = 0;
8536
8537
0
  switch (type)
8538
0
    {
8539
0
    case R_NDS32_GOT_LO12:
8540
0
    case R_NDS32_GOTOFF_LO12:
8541
0
    case R_NDS32_PLTREL_LO12:
8542
0
    case R_NDS32_PLT_GOTREL_LO12:
8543
0
    case R_NDS32_LO12S0_RELA:
8544
0
      switch (N32_OP6 (insn))
8545
0
  {
8546
0
  case N32_OP6_LBI:
8547
    /* lbi.gp */
8548
0
    oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), 0);
8549
0
    break;
8550
0
  case N32_OP6_LBSI:
8551
    /* lbsi.gp */
8552
0
    oinsn = N32_TYPE1 (LBGP, N32_RT5 (insn), N32_BIT (19));
8553
0
    break;
8554
0
  case N32_OP6_SBI:
8555
    /* sbi.gp */
8556
0
    oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), 0);
8557
0
    break;
8558
0
  case N32_OP6_ORI:
8559
    /* addi.gp */
8560
0
    oinsn = N32_TYPE1 (SBGP, N32_RT5 (insn), N32_BIT (19));
8561
0
    break;
8562
0
  }
8563
0
      break;
8564
8565
0
    case R_NDS32_LO12S1_RELA:
8566
0
      switch (N32_OP6 (insn))
8567
0
  {
8568
0
  case N32_OP6_LHI:
8569
    /* lhi.gp */
8570
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), 0);
8571
0
    break;
8572
0
  case N32_OP6_LHSI:
8573
    /* lhsi.gp */
8574
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (18));
8575
0
    break;
8576
0
  case N32_OP6_SHI:
8577
    /* shi.gp */
8578
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), N32_BIT (19));
8579
0
    break;
8580
0
  }
8581
0
      break;
8582
8583
0
    case R_NDS32_LO12S2_RELA:
8584
0
      switch (N32_OP6 (insn))
8585
0
  {
8586
0
  case N32_OP6_LWI:
8587
    /* lwi.gp */
8588
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (6, 17, 3));
8589
0
    break;
8590
0
  case N32_OP6_SWI:
8591
    /* swi.gp */
8592
0
    oinsn = N32_TYPE1 (HWGP, N32_RT5 (insn), __MF (7, 17, 3));
8593
0
    break;
8594
0
  }
8595
0
      break;
8596
8597
0
    case R_NDS32_LO12S2_DP_RELA:
8598
0
    case R_NDS32_LO12S2_SP_RELA:
8599
0
      oinsn = (insn & 0x7ff07000) | (REG_GP << 15);
8600
0
      break;
8601
0
    }
8602
8603
0
  if (oinsn)
8604
0
    *pinsn = oinsn;
8605
8606
0
  return oinsn != 0;
8607
0
}
8608
8609
/* Linker hasn't found the correct merge section for non-section symbol
8610
   in relax time, this work is left to the function elf_link_input_bfd().
8611
   So for non-section symbol, _bfd_merged_section_offset is also needed
8612
   to find the correct symbol address.  */
8613
8614
static bfd_vma
8615
nds32_elf_rela_local_sym (bfd *abfd, Elf_Internal_Sym *sym,
8616
        asection **psec, Elf_Internal_Rela *rel)
8617
0
{
8618
0
  asection *sec = *psec;
8619
0
  bfd_vma relocation;
8620
8621
0
  relocation = (sec->output_section->vma
8622
0
    + sec->output_offset + sym->st_value);
8623
0
  if ((sec->flags & SEC_MERGE) && sec->sec_info_type == SEC_INFO_TYPE_MERGE)
8624
0
    {
8625
0
      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
8626
0
  rel->r_addend =
8627
0
    _bfd_merged_section_offset (abfd, psec,
8628
0
              elf_section_data (sec)->sec_info,
8629
0
              sym->st_value + rel->r_addend);
8630
0
      else
8631
0
  rel->r_addend =
8632
0
    _bfd_merged_section_offset (abfd, psec,
8633
0
              elf_section_data (sec)->sec_info,
8634
0
              sym->st_value) + rel->r_addend;
8635
8636
0
      if (sec != *psec)
8637
0
  {
8638
    /* If we have changed the section, and our original section is
8639
       marked with SEC_EXCLUDE, it means that the original
8640
       SEC_MERGE section has been completely subsumed in some
8641
       other SEC_MERGE section.  In this case, we need to leave
8642
       some info around for --emit-relocs.  */
8643
0
    if ((sec->flags & SEC_EXCLUDE) != 0)
8644
0
      sec->kept_section = *psec;
8645
0
    sec = *psec;
8646
0
  }
8647
0
      rel->r_addend -= relocation;
8648
0
      rel->r_addend += sec->output_section->vma + sec->output_offset;
8649
0
    }
8650
0
  return relocation;
8651
0
}
8652
8653
static bfd_vma
8654
calculate_memory_address (bfd *abfd, Elf_Internal_Rela *irel,
8655
        Elf_Internal_Sym *isymbuf,
8656
        Elf_Internal_Shdr *symtab_hdr)
8657
0
{
8658
0
  bfd_signed_vma foff;
8659
0
  bfd_vma symval, addend;
8660
0
  Elf_Internal_Rela irel_fn;
8661
0
  Elf_Internal_Sym *isym;
8662
0
  asection *sym_sec;
8663
8664
  /* Get the value of the symbol referred to by the reloc.  */
8665
0
  if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
8666
0
    {
8667
      /* A local symbol.  */
8668
0
      isym = isymbuf + ELF32_R_SYM (irel->r_info);
8669
8670
0
      if (isym->st_shndx == SHN_UNDEF)
8671
0
  sym_sec = bfd_und_section_ptr;
8672
0
      else if (isym->st_shndx == SHN_ABS)
8673
0
  sym_sec = bfd_abs_section_ptr;
8674
0
      else if (isym->st_shndx == SHN_COMMON)
8675
0
  sym_sec = bfd_com_section_ptr;
8676
0
      else
8677
0
  sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
8678
0
      memcpy (&irel_fn, irel, sizeof (Elf_Internal_Rela));
8679
0
      symval = nds32_elf_rela_local_sym (abfd, isym, &sym_sec, &irel_fn);
8680
0
      addend = irel_fn.r_addend;
8681
0
    }
8682
0
  else
8683
0
    {
8684
0
      unsigned long indx;
8685
0
      struct elf_link_hash_entry *h;
8686
8687
      /* An external symbol.  */
8688
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
8689
0
      h = elf_sym_hashes (abfd)[indx];
8690
0
      BFD_ASSERT (h != NULL);
8691
8692
0
      while (h->root.type == bfd_link_hash_indirect
8693
0
       || h->root.type == bfd_link_hash_warning)
8694
0
  h = (struct elf_link_hash_entry *) h->root.u.i.link;
8695
8696
0
      if (h->root.type != bfd_link_hash_defined
8697
0
    && h->root.type != bfd_link_hash_defweak)
8698
  /* This appears to be a reference to an undefined
8699
     symbol.  Just ignore it--it will be caught by the
8700
     regular reloc processing.  */
8701
0
  return 0;
8702
8703
0
      if (h->root.u.def.section->flags & SEC_MERGE)
8704
0
  {
8705
0
    sym_sec = h->root.u.def.section;
8706
0
    symval = _bfd_merged_section_offset (abfd, &sym_sec, elf_section_data
8707
0
                 (sym_sec)->sec_info, h->root.u.def.value);
8708
0
    symval = symval + sym_sec->output_section->vma
8709
0
       + sym_sec->output_offset;
8710
0
  }
8711
0
      else
8712
0
  symval = (h->root.u.def.value
8713
0
      + h->root.u.def.section->output_section->vma
8714
0
      + h->root.u.def.section->output_offset);
8715
0
      addend = irel->r_addend;
8716
0
    }
8717
8718
0
  foff = symval + addend;
8719
8720
0
  return foff;
8721
0
}
8722
8723
static int
8724
is_16bit_NOP (bfd *abfd ATTRIBUTE_UNUSED,
8725
        asection *sec, Elf_Internal_Rela *rel)
8726
0
{
8727
0
  bfd_byte *contents;
8728
0
  unsigned short insn16;
8729
8730
0
  if (!(rel->r_addend & R_NDS32_INSN16_CONVERT_FLAG))
8731
0
    return false;
8732
0
  contents = elf_section_data (sec)->this_hdr.contents;
8733
0
  insn16 = bfd_getb16 (contents + rel->r_offset);
8734
0
  if (insn16 == NDS32_NOP16)
8735
0
    return true;
8736
0
  return false;
8737
0
}
8738
8739
/* It checks whether the instruction could be converted to
8740
   16-bit form and returns the converted one.
8741
8742
   `internal_relocs' is supposed to be sorted.  */
8743
8744
static int
8745
is_convert_32_to_16 (bfd *abfd, asection *sec,
8746
         Elf_Internal_Rela *reloc,
8747
         Elf_Internal_Rela *internal_relocs,
8748
         Elf_Internal_Rela *irelend,
8749
         uint16_t *insn16)
8750
0
{
8751
0
#define NORMAL_32_TO_16 (1 << 0)
8752
0
#define SPECIAL_32_TO_16 (1 << 1)
8753
0
  bfd_byte *contents = NULL;
8754
0
  bfd_signed_vma off;
8755
0
  bfd_vma mem_addr;
8756
0
  uint32_t insn = 0;
8757
0
  Elf_Internal_Rela *pc_rel;
8758
0
  Elf_Internal_Shdr *symtab_hdr;
8759
0
  Elf_Internal_Sym *isymbuf = NULL;
8760
0
  int convert_type;
8761
0
  bfd_vma offset;
8762
8763
0
  if (reloc->r_offset + 4 > sec->size)
8764
0
    return false;
8765
8766
0
  offset = reloc->r_offset;
8767
8768
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true))
8769
0
    return false;
8770
0
  insn = bfd_getb32 (contents + offset);
8771
8772
0
  if (nds32_convert_32_to_16 (abfd, insn, insn16, NULL))
8773
0
    convert_type = NORMAL_32_TO_16;
8774
0
  else if (special_convert_32_to_16 (insn, insn16, reloc))
8775
0
    convert_type = SPECIAL_32_TO_16;
8776
0
  else
8777
0
    return false;
8778
8779
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
8780
0
  if (!nds32_get_local_syms (abfd, sec, &isymbuf))
8781
0
    return false;
8782
8783
  /* Find the first relocation of the same relocation-type,
8784
     so we iteratie them forward.  */
8785
0
  pc_rel = reloc;
8786
0
  while ((pc_rel - 1) >= internal_relocs && pc_rel[-1].r_offset == offset)
8787
0
    pc_rel--;
8788
8789
0
  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8790
0
    {
8791
0
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8792
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8793
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA
8794
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8795
0
  {
8796
0
    off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8797
0
    if (off >= ACCURATE_8BIT_S1 || off < -ACCURATE_8BIT_S1
8798
0
        || off == 0)
8799
0
      return false;
8800
0
    break;
8801
0
  }
8802
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8803
0
  {
8804
    /* movi => movi55  */
8805
0
    mem_addr = calculate_memory_address (abfd, pc_rel, isymbuf,
8806
0
                 symtab_hdr);
8807
    /* mem_addr is unsigned, but the value should
8808
       be between [-16, 15].  */
8809
0
    if ((mem_addr + 0x10) >> 5)
8810
0
      return false;
8811
0
    break;
8812
0
  }
8813
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_20)
8814
0
         || (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_TLS_LE_LO12))
8815
0
  {
8816
    /* It never happen movi to movi55 for R_NDS32_TLS_LE_20,
8817
       because it can be relaxed to addi for TLS_LE_ADD.  */
8818
0
    return false;
8819
0
  }
8820
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8821
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8822
0
         && (reloc->r_addend & R_NDS32_INSN16_FP7U2_FLAG)
8823
0
         && convert_type == SPECIAL_32_TO_16)
8824
0
  {
8825
    /* fp-as-gp
8826
       We've selected a best fp-base for this access, so we can
8827
       always resolve it anyway.  Do nothing.  */
8828
0
    break;
8829
0
  }
8830
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_NONE
8831
0
    && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_RELA_GNU_VTINHERIT))
8832
0
         || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_RELA_GNU_VTENTRY)
8833
0
       && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_INSN16))
8834
0
         || ((ELF32_R_TYPE (pc_rel->r_info) > R_NDS32_LOADSTORE)
8835
0
       && (ELF32_R_TYPE (pc_rel->r_info) < R_NDS32_DWARF2_OP1_RELA)))
8836
0
  {
8837
    /* Prevent unresolved addi instruction translate
8838
       to addi45 or addi333.  */
8839
0
    return false;
8840
0
  }
8841
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8842
0
  {
8843
0
    off = calculate_offset (abfd, sec, pc_rel, isymbuf, symtab_hdr);
8844
0
    if (off >= ACCURATE_U9BIT_S1 || off <= 0)
8845
0
      return false;
8846
0
    break;
8847
0
  }
8848
0
    }
8849
8850
0
  return true;
8851
0
}
8852
8853
static void
8854
nds32_elf_write_16 (bfd *abfd ATTRIBUTE_UNUSED, bfd_byte *contents,
8855
        Elf_Internal_Rela *reloc,
8856
        Elf_Internal_Rela *internal_relocs,
8857
        Elf_Internal_Rela *irelend,
8858
        unsigned short insn16)
8859
0
{
8860
0
  Elf_Internal_Rela *pc_rel;
8861
0
  bfd_vma offset;
8862
8863
0
  offset = reloc->r_offset;
8864
0
  bfd_putb16 (insn16, contents + offset);
8865
  /* Find the first relocation of the same relocation-type,
8866
     so we iteratie them forward.  */
8867
0
  pc_rel = reloc;
8868
0
  while ((pc_rel - 1) > internal_relocs && pc_rel[-1].r_offset == offset)
8869
0
    pc_rel--;
8870
8871
0
  for (; pc_rel < irelend && pc_rel->r_offset == offset; pc_rel++)
8872
0
    {
8873
0
      if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_15_PCREL_RELA
8874
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17_PCREL_RELA
8875
0
    || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PCREL_RELA)
8876
0
  {
8877
0
    pc_rel->r_info =
8878
0
      ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PCREL_RELA);
8879
0
  }
8880
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_25_PLTREL)
8881
0
  pc_rel->r_info =
8882
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_9_PLTREL);
8883
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_20_RELA)
8884
0
  pc_rel->r_info =
8885
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_5_RELA);
8886
0
      else if (ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA15S2_RELA
8887
0
         || ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_SDA17S2_RELA)
8888
0
  pc_rel->r_info =
8889
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_SDA_FP7U2_RELA);
8890
0
      else if ((ELF32_R_TYPE (pc_rel->r_info) == R_NDS32_17IFC_PCREL_RELA))
8891
0
  pc_rel->r_info =
8892
0
    ELF32_R_INFO (ELF32_R_SYM (pc_rel->r_info), R_NDS32_10IFCU_PCREL_RELA);
8893
0
    }
8894
0
}
8895
8896
/* Find a relocation of type specified by `reloc_type'
8897
   of the same r_offset with reloc.
8898
   If not found, return irelend.
8899
8900
   Assuming relocations are sorted by r_offset,
8901
   we find the relocation from `reloc' backward untill relocs,
8902
   or find it from `reloc' forward untill irelend.  */
8903
8904
static Elf_Internal_Rela *
8905
find_relocs_at_address (Elf_Internal_Rela *reloc,
8906
      Elf_Internal_Rela *relocs,
8907
      Elf_Internal_Rela *irelend,
8908
      enum elf_nds32_reloc_type reloc_type)
8909
0
{
8910
0
  Elf_Internal_Rela *rel_t;
8911
8912
  /* Find backward.  */
8913
0
  for (rel_t = reloc;
8914
0
       rel_t >= relocs && rel_t->r_offset == reloc->r_offset;
8915
0
       rel_t--)
8916
0
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8917
0
      return rel_t;
8918
8919
  /* We didn't find it backward.  Try find it forward.  */
8920
0
  for (rel_t = reloc;
8921
0
       rel_t < irelend && rel_t->r_offset == reloc->r_offset;
8922
0
       rel_t++)
8923
0
    if (ELF32_R_TYPE (rel_t->r_info) == reloc_type)
8924
0
      return rel_t;
8925
8926
0
  return irelend;
8927
0
}
8928
8929
/* Find a relocation of specified type and offset.
8930
   `reloc' is just a refence point to find a relocation at specified offset.
8931
   If not found, return irelend.
8932
8933
   Assuming relocations are sorted by r_offset,
8934
   we find the relocation from `reloc' backward untill relocs,
8935
   or find it from `reloc' forward untill irelend.  */
8936
8937
static Elf_Internal_Rela *
8938
find_relocs_at_address_addr (Elf_Internal_Rela *reloc,
8939
           Elf_Internal_Rela *relocs,
8940
           Elf_Internal_Rela *irelend,
8941
           enum elf_nds32_reloc_type reloc_type,
8942
           bfd_vma offset_p)
8943
0
{
8944
0
  Elf_Internal_Rela *rel_t = NULL;
8945
8946
  /* First, we try to find a relocation of offset `offset_p',
8947
     and then we use find_relocs_at_address to find specific type.  */
8948
8949
0
  if (reloc->r_offset > offset_p)
8950
0
    {
8951
      /* Find backward.  */
8952
0
      for (rel_t = reloc;
8953
0
     rel_t >= relocs && rel_t->r_offset > offset_p; rel_t--)
8954
0
  /* Do nothing.  */;
8955
0
    }
8956
0
  else if (reloc->r_offset < offset_p)
8957
0
    {
8958
      /* Find forward.  */
8959
0
      for (rel_t = reloc;
8960
0
     rel_t < irelend && rel_t->r_offset < offset_p; rel_t++)
8961
0
  /* Do nothing.  */;
8962
0
    }
8963
0
  else
8964
0
    rel_t = reloc;
8965
8966
  /* Not found?  */
8967
0
  if (rel_t < relocs || rel_t == irelend || rel_t->r_offset != offset_p)
8968
0
    return irelend;
8969
8970
0
  return find_relocs_at_address (rel_t, relocs, irelend, reloc_type);
8971
0
}
8972
8973
typedef struct nds32_elf_blank nds32_elf_blank_t;
8974
struct nds32_elf_blank
8975
{
8976
  /* Where the blank begins.  */
8977
  bfd_vma offset;
8978
  /* The size of the blank.  */
8979
  bfd_vma size;
8980
  /* The accumulative size before this blank.  */
8981
  bfd_vma total_size;
8982
  nds32_elf_blank_t *next;
8983
  nds32_elf_blank_t *prev;
8984
};
8985
8986
static nds32_elf_blank_t *blank_free_list = NULL;
8987
8988
static nds32_elf_blank_t *
8989
create_nds32_elf_blank (bfd_vma offset_p, bfd_vma size_p)
8990
0
{
8991
0
  nds32_elf_blank_t *blank_t;
8992
8993
0
  if (blank_free_list)
8994
0
    {
8995
0
      blank_t = blank_free_list;
8996
0
      blank_free_list = blank_free_list->next;
8997
0
    }
8998
0
  else
8999
0
    blank_t = bfd_malloc (sizeof (nds32_elf_blank_t));
9000
9001
0
  if (blank_t == NULL)
9002
0
    return NULL;
9003
9004
0
  blank_t->offset = offset_p;
9005
0
  blank_t->size = size_p;
9006
0
  blank_t->total_size = 0;
9007
0
  blank_t->next = NULL;
9008
0
  blank_t->prev = NULL;
9009
9010
0
  return blank_t;
9011
0
}
9012
9013
static void
9014
remove_nds32_elf_blank (nds32_elf_blank_t *blank_p)
9015
0
{
9016
0
  if (blank_free_list)
9017
0
    {
9018
0
      blank_free_list->prev = blank_p;
9019
0
      blank_p->next = blank_free_list;
9020
0
    }
9021
0
  else
9022
0
    blank_p->next = NULL;
9023
9024
0
  blank_p->prev = NULL;
9025
0
  blank_free_list = blank_p;
9026
0
}
9027
9028
static void
9029
clean_nds32_elf_blank (void)
9030
0
{
9031
0
  nds32_elf_blank_t *blank_t;
9032
9033
0
  while (blank_free_list)
9034
0
    {
9035
0
      blank_t = blank_free_list;
9036
0
      blank_free_list = blank_free_list->next;
9037
0
      free (blank_t);
9038
0
    }
9039
0
}
9040
9041
static nds32_elf_blank_t *
9042
search_nds32_elf_blank (nds32_elf_blank_t *blank_p, bfd_vma addr)
9043
0
{
9044
0
  nds32_elf_blank_t *blank_t;
9045
9046
0
  if (!blank_p)
9047
0
    return NULL;
9048
0
  blank_t = blank_p;
9049
9050
0
  while (blank_t && addr < blank_t->offset)
9051
0
    blank_t = blank_t->prev;
9052
0
  while (blank_t && blank_t->next && addr >= blank_t->next->offset)
9053
0
    blank_t = blank_t->next;
9054
9055
0
  return blank_t;
9056
0
}
9057
9058
static bfd_vma
9059
get_nds32_elf_blank_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9060
         int overwrite)
9061
0
{
9062
0
  nds32_elf_blank_t *blank_t;
9063
9064
0
  blank_t = search_nds32_elf_blank (*blank_p, addr);
9065
0
  if (!blank_t)
9066
0
    return 0;
9067
9068
0
  if (overwrite)
9069
0
    *blank_p = blank_t;
9070
9071
0
  if (addr < blank_t->offset + blank_t->size)
9072
0
    return blank_t->total_size + (addr - blank_t->offset);
9073
0
  else
9074
0
    return blank_t->total_size + blank_t->size;
9075
0
}
9076
9077
static bool
9078
insert_nds32_elf_blank (nds32_elf_blank_t **blank_p, bfd_vma addr, bfd_vma len)
9079
0
{
9080
0
  nds32_elf_blank_t *blank_t, *blank_t2;
9081
9082
0
  if (!*blank_p)
9083
0
    {
9084
0
      *blank_p = create_nds32_elf_blank (addr, len);
9085
0
      return *blank_p != NULL;
9086
0
    }
9087
9088
0
  blank_t = search_nds32_elf_blank (*blank_p, addr);
9089
9090
0
  if (blank_t == NULL)
9091
0
    {
9092
0
      blank_t = create_nds32_elf_blank (addr, len);
9093
0
      if (!blank_t)
9094
0
  return false;
9095
0
      while ((*blank_p)->prev != NULL)
9096
0
  *blank_p = (*blank_p)->prev;
9097
0
      blank_t->next = *blank_p;
9098
0
      (*blank_p)->prev = blank_t;
9099
0
      (*blank_p) = blank_t;
9100
0
      return true;
9101
0
    }
9102
9103
0
  if (addr < blank_t->offset + blank_t->size)
9104
0
    {
9105
      /* Extend the origin blank.  */
9106
0
      if (addr + len > blank_t->offset + blank_t->size)
9107
0
  blank_t->size = addr + len - blank_t->offset;
9108
0
    }
9109
0
  else
9110
0
    {
9111
0
      blank_t2 = create_nds32_elf_blank (addr, len);
9112
0
      if (!blank_t2)
9113
0
  return false;
9114
0
      if (blank_t->next)
9115
0
  {
9116
0
    blank_t->next->prev = blank_t2;
9117
0
    blank_t2->next = blank_t->next;
9118
0
  }
9119
0
      blank_t2->prev = blank_t;
9120
0
      blank_t->next = blank_t2;
9121
0
      *blank_p = blank_t2;
9122
0
    }
9123
9124
0
  return true;
9125
0
}
9126
9127
static bool
9128
insert_nds32_elf_blank_recalc_total (nds32_elf_blank_t **blank_p, bfd_vma addr,
9129
             bfd_vma len)
9130
0
{
9131
0
  nds32_elf_blank_t *blank_t;
9132
9133
0
  if (!insert_nds32_elf_blank (blank_p, addr, len))
9134
0
    return false;
9135
9136
0
  blank_t = *blank_p;
9137
9138
0
  if (!blank_t->prev)
9139
0
    {
9140
0
      blank_t->total_size = 0;
9141
0
      blank_t = blank_t->next;
9142
0
    }
9143
9144
0
  while (blank_t)
9145
0
    {
9146
0
      blank_t->total_size = blank_t->prev->total_size + blank_t->prev->size;
9147
0
      blank_t = blank_t->next;
9148
0
    }
9149
9150
0
  return true;
9151
0
}
9152
9153
static void
9154
calc_nds32_blank_total (nds32_elf_blank_t *blank_p)
9155
0
{
9156
0
  nds32_elf_blank_t *blank_t;
9157
0
  bfd_vma total_size = 0;
9158
9159
0
  if (!blank_p)
9160
0
    return;
9161
9162
0
  blank_t = blank_p;
9163
0
  while (blank_t->prev)
9164
0
    blank_t = blank_t->prev;
9165
0
  while (blank_t)
9166
0
    {
9167
0
      blank_t->total_size = total_size;
9168
0
      total_size += blank_t->size;
9169
0
      blank_t = blank_t->next;
9170
0
    }
9171
0
}
9172
9173
static bool
9174
nds32_elf_relax_delete_blanks (bfd *abfd, asection *sec,
9175
             nds32_elf_blank_t *blank_p)
9176
0
{
9177
0
  Elf_Internal_Shdr *symtab_hdr;  /* Symbol table header of this bfd.  */
9178
0
  Elf_Internal_Sym *isym = NULL;  /* Symbol table of this bfd.  */
9179
0
  Elf_Internal_Sym *isymend;    /* Symbol entry iterator.  */
9180
0
  unsigned int sec_shndx;   /* The section the be relaxed.  */
9181
0
  bfd_byte *contents;     /* Contents data of iterating section.  */
9182
0
  Elf_Internal_Rela *internal_relocs;
9183
0
  Elf_Internal_Rela *irel;
9184
0
  Elf_Internal_Rela *irelend;
9185
0
  struct elf_link_hash_entry **sym_hashes;
9186
0
  struct elf_link_hash_entry **end_hashes;
9187
0
  unsigned int symcount;
9188
0
  asection *sect;
9189
0
  nds32_elf_blank_t *blank_t;
9190
0
  nds32_elf_blank_t *blank_t2;
9191
0
  nds32_elf_blank_t *blank_head;
9192
9193
0
  blank_head = blank_t = blank_p;
9194
0
  while (blank_head->prev != NULL)
9195
0
    blank_head = blank_head->prev;
9196
0
  while (blank_t->next != NULL)
9197
0
    blank_t = blank_t->next;
9198
9199
0
  if (blank_t->offset + blank_t->size <= sec->size)
9200
0
    {
9201
0
      blank_t->next = create_nds32_elf_blank (sec->size + 4, 0);
9202
0
      blank_t->next->prev = blank_t;
9203
0
    }
9204
0
  if (blank_head->offset > 0)
9205
0
    {
9206
0
      blank_head->prev = create_nds32_elf_blank (0, 0);
9207
0
      blank_head->prev->next = blank_head;
9208
0
      blank_head = blank_head->prev;
9209
0
    }
9210
9211
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
9212
9213
  /* The deletion must stop at the next ALIGN reloc for an alignment
9214
     power larger than the number of bytes we are deleting.  */
9215
9216
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9217
0
  if (!nds32_get_local_syms (abfd, sec, &isym))
9218
0
    return false;
9219
9220
0
  if (isym == NULL)
9221
0
    {
9222
0
      isym = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9223
0
           symtab_hdr->sh_info, 0, NULL, NULL, NULL);
9224
0
      symtab_hdr->contents = (bfd_byte *) isym;
9225
0
    }
9226
9227
0
  if (isym == NULL || symtab_hdr->sh_info == 0)
9228
0
    return false;
9229
9230
0
  blank_t = blank_head;
9231
0
  calc_nds32_blank_total (blank_head);
9232
9233
0
  for (sect = abfd->sections; sect != NULL; sect = sect->next)
9234
0
    {
9235
      /* Adjust all the relocs.  */
9236
9237
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
9238
0
      internal_relocs = _bfd_elf_link_read_relocs (abfd, sect, NULL, NULL,
9239
0
               true /* keep_memory */);
9240
0
      irelend = internal_relocs + sect->reloc_count;
9241
9242
0
      blank_t = blank_head;
9243
0
      blank_t2 = blank_head;
9244
9245
0
      if (!(sect->flags & SEC_RELOC))
9246
0
  continue;
9247
9248
0
      contents = NULL;
9249
0
      nds32_get_section_contents (abfd, sect, &contents, true);
9250
9251
0
      for (irel = internal_relocs; irel < irelend; irel++)
9252
0
  {
9253
0
    bfd_vma raddr;
9254
9255
0
    if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_DIFF8
9256
0
        && ELF32_R_TYPE (irel->r_info) <= R_NDS32_DIFF32
9257
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9258
0
      {
9259
0
        unsigned long val = 0;
9260
0
        unsigned long mask;
9261
0
        long before, between;
9262
0
        long offset = 0;
9263
9264
0
        switch (ELF32_R_TYPE (irel->r_info))
9265
0
    {
9266
0
    case R_NDS32_DIFF8:
9267
0
      offset = bfd_get_8 (abfd, contents + irel->r_offset);
9268
0
      break;
9269
0
    case R_NDS32_DIFF16:
9270
0
      offset = bfd_get_16 (abfd, contents + irel->r_offset);
9271
0
      break;
9272
0
    case R_NDS32_DIFF32:
9273
0
      val = bfd_get_32 (abfd, contents + irel->r_offset);
9274
      /* Get the signed bit and mask for the high part.  The
9275
         gcc will alarm when right shift 32-bit since the
9276
         type size of long may be 32-bit.  */
9277
0
      mask = 0 - (val >> 31);
9278
0
      if (mask)
9279
0
        offset = (val | (mask - 0xffffffff));
9280
0
      else
9281
0
        offset = val;
9282
0
      break;
9283
0
    default:
9284
0
      BFD_ASSERT (0);
9285
0
    }
9286
9287
        /*      DIFF value
9288
    0      |encoded in location|
9289
    |------------|-------------------|---------
9290
          sym+off(addend)
9291
    -- before ---| *****************
9292
    --------------------- between ---|
9293
9294
    We only care how much data are relax between DIFF,
9295
    marked as ***.  */
9296
9297
0
        before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9298
0
        between = get_nds32_elf_blank_total (&blank_t,
9299
0
               irel->r_addend + offset, 0);
9300
0
        if (between == before)
9301
0
    goto done_adjust_diff;
9302
9303
0
        switch (ELF32_R_TYPE (irel->r_info))
9304
0
    {
9305
0
    case R_NDS32_DIFF8:
9306
0
      bfd_put_8 (abfd, offset - (between - before),
9307
0
           contents + irel->r_offset);
9308
0
      break;
9309
0
    case R_NDS32_DIFF16:
9310
0
      bfd_put_16 (abfd, offset - (between - before),
9311
0
            contents + irel->r_offset);
9312
0
      break;
9313
0
    case R_NDS32_DIFF32:
9314
0
      bfd_put_32 (abfd, offset - (between - before),
9315
0
            contents + irel->r_offset);
9316
0
      break;
9317
0
    }
9318
0
      }
9319
0
    else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_DIFF_ULEB128
9320
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx)
9321
0
      {
9322
0
        bfd_vma val = 0;
9323
0
        unsigned int len = 0;
9324
0
        unsigned long before, between;
9325
0
        bfd_byte *endp, *p;
9326
9327
0
        val = _bfd_read_unsigned_leb128 (abfd, contents + irel->r_offset,
9328
0
                 &len);
9329
9330
0
        before = get_nds32_elf_blank_total (&blank_t, irel->r_addend, 0);
9331
0
        between = get_nds32_elf_blank_total (&blank_t,
9332
0
               irel->r_addend + val, 0);
9333
0
        if (between == before)
9334
0
    goto done_adjust_diff;
9335
9336
0
        p = contents + irel->r_offset;
9337
0
        endp = p + len -1;
9338
0
        memset (p, 0x80, len);
9339
0
        *(endp) = 0;
9340
0
        p = write_uleb128 (p, val - (between - before)) - 1;
9341
0
        if (p < endp)
9342
0
    *p |= 0x80;
9343
0
      }
9344
0
  done_adjust_diff:
9345
9346
0
    if (sec == sect)
9347
0
      {
9348
0
        raddr = irel->r_offset;
9349
0
        irel->r_offset -= get_nds32_elf_blank_total (&blank_t2,
9350
0
                 irel->r_offset, 1);
9351
9352
0
        if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE)
9353
0
    continue;
9354
0
        if (blank_t2 && blank_t2->next
9355
0
      && (blank_t2->offset > raddr
9356
0
          || blank_t2->next->offset <= raddr))
9357
0
    _bfd_error_handler
9358
0
      (_("%pB: error: search_nds32_elf_blank reports wrong node"),
9359
0
       abfd);
9360
9361
        /* Mark reloc in deleted portion as NONE.
9362
     For some relocs like R_NDS32_LABEL that doesn't modify the
9363
     content in the section.  R_NDS32_LABEL doesn't belong to the
9364
     instruction in the section, so we should preserve it.  */
9365
0
        if (raddr >= blank_t2->offset
9366
0
      && raddr < blank_t2->offset + blank_t2->size
9367
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL
9368
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_BEGIN
9369
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_REGION_END
9370
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_RELAX_ENTRY
9371
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_SUBTRAHEND
9372
0
      && ELF32_R_TYPE (irel->r_info) != R_NDS32_MINUEND)
9373
0
    {
9374
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
9375
0
                 R_NDS32_NONE);
9376
0
      continue;
9377
0
    }
9378
0
      }
9379
9380
0
    if (ELF32_R_TYPE (irel->r_info) == R_NDS32_NONE
9381
0
        || ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
9382
0
        || ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
9383
0
      continue;
9384
9385
0
    if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info
9386
0
        && isym[ELF32_R_SYM (irel->r_info)].st_shndx == sec_shndx
9387
0
        && ELF_ST_TYPE (isym[ELF32_R_SYM (irel->r_info)].st_info) == STT_SECTION)
9388
0
      {
9389
0
        if (irel->r_addend <= sec->size)
9390
0
    irel->r_addend -=
9391
0
      get_nds32_elf_blank_total (&blank_t, irel->r_addend, 1);
9392
0
      }
9393
0
  }
9394
0
    }
9395
9396
  /* Adjust the local symbols defined in this section.  */
9397
0
  blank_t = blank_head;
9398
0
  for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
9399
0
    {
9400
0
      if (isym->st_shndx == sec_shndx)
9401
0
  {
9402
0
    if (isym->st_value <= sec->size)
9403
0
      {
9404
0
        bfd_vma ahead;
9405
0
        bfd_vma orig_addr = isym->st_value;
9406
9407
0
        ahead = get_nds32_elf_blank_total (&blank_t, isym->st_value, 1);
9408
0
        isym->st_value -= ahead;
9409
9410
        /* Adjust function size.  */
9411
0
        if (ELF32_ST_TYPE (isym->st_info) == STT_FUNC
9412
0
      && isym->st_size > 0)
9413
0
    isym->st_size -=
9414
0
      get_nds32_elf_blank_total
9415
0
      (&blank_t, orig_addr + isym->st_size, 0) - ahead;
9416
0
      }
9417
0
  }
9418
0
    }
9419
9420
  /* Now adjust the global symbols defined in this section.  */
9421
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
9422
0
        - symtab_hdr->sh_info);
9423
0
  sym_hashes = elf_sym_hashes (abfd);
9424
0
  end_hashes = sym_hashes + symcount;
9425
0
  blank_t = blank_head;
9426
0
  for (; sym_hashes < end_hashes; sym_hashes++)
9427
0
    {
9428
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
9429
9430
0
      if ((sym_hash->root.type == bfd_link_hash_defined
9431
0
     || sym_hash->root.type == bfd_link_hash_defweak)
9432
0
    && sym_hash->root.u.def.section == sec)
9433
0
  {
9434
0
    if (sym_hash->root.u.def.value <= sec->size)
9435
0
      {
9436
0
        bfd_vma ahead;
9437
0
        bfd_vma orig_addr = sym_hash->root.u.def.value;
9438
9439
0
        ahead = get_nds32_elf_blank_total (&blank_t, sym_hash->root.u.def.value, 1);
9440
0
        sym_hash->root.u.def.value -= ahead;
9441
9442
        /* Adjust function size.  */
9443
0
        if (sym_hash->type == STT_FUNC)
9444
0
    sym_hash->size -=
9445
0
      get_nds32_elf_blank_total
9446
0
      (&blank_t, orig_addr + sym_hash->size, 0) - ahead;
9447
9448
0
      }
9449
0
  }
9450
0
    }
9451
9452
0
  contents = elf_section_data (sec)->this_hdr.contents;
9453
0
  blank_t = blank_head;
9454
0
  while (blank_t->next)
9455
0
    {
9456
      /* Actually delete the bytes.  */
9457
9458
      /* If current blank is the last blank overlap with current section,
9459
   go to finish process.  */
9460
0
      if (sec->size <= (blank_t->next->offset))
9461
0
  break;
9462
9463
0
      memmove (contents + blank_t->offset - blank_t->total_size,
9464
0
         contents + blank_t->offset + blank_t->size,
9465
0
         blank_t->next->offset - (blank_t->offset + blank_t->size));
9466
9467
0
      blank_t = blank_t->next;
9468
0
    }
9469
9470
0
  if (sec->size > (blank_t->offset + blank_t->size))
9471
0
    {
9472
      /* There are remaining code between blank and section boundary.
9473
   Move the remaining code to appropriate location.  */
9474
0
      memmove (contents + blank_t->offset - blank_t->total_size,
9475
0
         contents + blank_t->offset + blank_t->size,
9476
0
         sec->size - (blank_t->offset + blank_t->size));
9477
0
      sec->size -= blank_t->total_size + blank_t->size;
9478
0
    }
9479
0
  else
9480
    /* This blank is not entirely included in the section,
9481
       reduce the section size by only part of the blank size.  */
9482
0
    sec->size -= blank_t->total_size + (sec->size - blank_t->offset);
9483
9484
0
  while (blank_head)
9485
0
    {
9486
0
      blank_t = blank_head;
9487
0
      blank_head = blank_head->next;
9488
0
      remove_nds32_elf_blank (blank_t);
9489
0
    }
9490
9491
0
  return true;
9492
0
}
9493
9494
/* Get the contents of a section.  */
9495
9496
static int
9497
nds32_get_section_contents (bfd *abfd, asection *sec,
9498
          bfd_byte **contents_p, bool cache)
9499
0
{
9500
  /* Get the section contents.  */
9501
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
9502
0
    *contents_p = elf_section_data (sec)->this_hdr.contents;
9503
0
  else
9504
0
    {
9505
0
      if (!bfd_get_full_section_contents (abfd, sec, contents_p))
9506
0
  return false;
9507
0
      if (cache)
9508
0
  elf_section_data (sec)->this_hdr.contents = *contents_p;
9509
0
    }
9510
9511
0
  return true;
9512
0
}
9513
9514
/* Get the contents of the internal symbol of abfd.  */
9515
9516
static int
9517
nds32_get_local_syms (bfd *abfd, asection *sec ATTRIBUTE_UNUSED,
9518
          Elf_Internal_Sym **isymbuf_p)
9519
0
{
9520
0
  Elf_Internal_Shdr *symtab_hdr;
9521
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
9522
9523
  /* Read this BFD's local symbols if we haven't done so already.  */
9524
0
  if (*isymbuf_p == NULL && symtab_hdr->sh_info != 0)
9525
0
    {
9526
0
      *isymbuf_p = (Elf_Internal_Sym *) symtab_hdr->contents;
9527
0
      if (*isymbuf_p == NULL)
9528
0
  {
9529
0
    *isymbuf_p = bfd_elf_get_elf_syms (abfd, symtab_hdr,
9530
0
               symtab_hdr->sh_info, 0,
9531
0
               NULL, NULL, NULL);
9532
0
    if (*isymbuf_p == NULL)
9533
0
      return false;
9534
0
  }
9535
0
    }
9536
0
  symtab_hdr->contents = (bfd_byte *) (*isymbuf_p);
9537
9538
0
  return true;
9539
0
}
9540
9541
/* Range of small data.  */
9542
static bfd_vma sdata_range[2][2];
9543
static bfd_vma const sdata_init_range[2] =
9544
{ ACCURATE_12BIT_S1, ACCURATE_19BIT };
9545
9546
static int
9547
nds32_elf_insn_size (bfd *abfd ATTRIBUTE_UNUSED,
9548
         bfd_byte *contents, bfd_vma addr)
9549
0
{
9550
0
  unsigned long insn = bfd_getb32 (contents + addr);
9551
9552
0
  if (insn & 0x80000000)
9553
0
    return 2;
9554
9555
0
  return 4;
9556
0
}
9557
9558
/* Set the gp relax range.  We have to measure the safe range
9559
   to do gp relaxation.  */
9560
9561
static void
9562
relax_range_measurement (bfd *abfd, struct bfd_link_info *link_info)
9563
0
{
9564
0
  asection *sec_f, *sec_b;
9565
  /* For upper bound.   */
9566
0
  bfd_vma maxpgsz;
9567
0
  bfd_vma align;
9568
0
  static int decide_relax_range = 0;
9569
0
  int i;
9570
0
  int range_number = ARRAY_SIZE (sdata_init_range);
9571
9572
0
  if (decide_relax_range)
9573
0
    return;
9574
0
  decide_relax_range = 1;
9575
9576
0
  if (sda_rela_sec == NULL)
9577
0
    {
9578
      /* Since there is no data sections, we assume the range is page size.  */
9579
0
      for (i = 0; i < range_number; i++)
9580
0
  {
9581
0
    sdata_range[i][0] = sdata_init_range[i] - 0x1000;
9582
0
    sdata_range[i][1] = sdata_init_range[i] - 0x1000;
9583
0
  }
9584
0
      return;
9585
0
    }
9586
9587
  /* Get the biggest alignment power after the gp located section.  */
9588
0
  sec_f = sda_rela_sec->output_section;
9589
0
  sec_b = sec_f->next;
9590
0
  align = 0;
9591
0
  while (sec_b != NULL)
9592
0
    {
9593
0
      if ((unsigned)(1 << sec_b->alignment_power) > align)
9594
0
  align = (1 << sec_b->alignment_power);
9595
0
      sec_b = sec_b->next;
9596
0
    }
9597
9598
0
  if (link_info != NULL)
9599
0
    maxpgsz = link_info->maxpagesize;
9600
0
  else
9601
0
    maxpgsz = get_elf_backend_data (abfd)->maxpagesize;
9602
  /* I guess we can not determine the section before
9603
     gp located section, so we assume the align is max page size.  */
9604
0
  for (i = 0; i < range_number; i++)
9605
0
    {
9606
0
      sdata_range[i][1] = sdata_init_range[i] - align;
9607
0
      BFD_ASSERT (sdata_range[i][1] <= sdata_init_range[i]);
9608
0
      sdata_range[i][0] = sdata_init_range[i] - maxpgsz;
9609
0
      BFD_ASSERT (sdata_range[i][0] <= sdata_init_range[i]);
9610
0
    }
9611
0
}
9612
9613
/* These are macros used to check flags encoded in r_addend.
9614
   They are only used by nds32_elf_relax_section ().  */
9615
0
#define GET_SEQ_LEN(addend)     ((addend) & 0x000000ff)
9616
0
#define IS_1ST_CONVERT(addend)  ((addend) & 0x80000000)
9617
#define IS_OPTIMIZE(addend)     ((addend) & 0x40000000)
9618
0
#define IS_16BIT_ON(addend)     ((addend) & 0x20000000)
9619
9620
static const char * unrecognized_reloc_msg =
9621
  /* xgettext:c-format */
9622
  N_("%pB: warning: %s points to unrecognized reloc at %#" PRIx64);
9623
9624
/* Relax LONGCALL1 relocation for nds32_elf_relax_section.  */
9625
9626
static bool
9627
nds32_elf_relax_longcall1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9628
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9629
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9630
         Elf_Internal_Shdr *symtab_hdr)
9631
0
{
9632
  /* There are 3 variations for LONGCALL1
9633
     case 4-4-2; 16-bit on, optimize off or optimize for space
9634
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9635
     ori   ta, ta, lo12(symbol) ; LO12S0
9636
     jral5 ta     ;
9637
9638
     case 4-4-4; 16-bit off, optimize don't care
9639
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9640
     ori   ta, ta, lo12(symbol) ; LO12S0
9641
     jral  ta     ;
9642
9643
     case 4-4-4; 16-bit on, optimize for speed
9644
     sethi ta, hi20(symbol) ; LONGCALL1/HI20
9645
     ori   ta, ta, lo12(symbol) ; LO12S0
9646
     jral  ta     ;
9647
     Check code for -mlong-calls output.  */
9648
9649
  /* Get the reloc for the address from which the register is
9650
     being loaded.  This reloc will tell us which function is
9651
     actually being called.  */
9652
9653
0
  bfd_vma laddr;
9654
0
  int seq_len;  /* Original length of instruction sequence.  */
9655
0
  uint32_t insn;
9656
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9657
0
  bfd_signed_vma foff;
9658
0
  uint16_t insn16;
9659
9660
0
  irelend = internal_relocs + sec->reloc_count;
9661
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9662
0
  laddr = irel->r_offset;
9663
0
  *insn_len = seq_len;
9664
9665
0
  hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9666
0
             R_NDS32_HI20_RELA, laddr);
9667
0
  lo_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
9668
0
             R_NDS32_LO12S0_ORI_RELA,
9669
0
             laddr + 4);
9670
9671
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9672
0
    {
9673
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL1",
9674
0
        (uint64_t) irel->r_offset);
9675
0
      return false;
9676
0
    }
9677
9678
  /* Get the value of the symbol referred to by the reloc.  */
9679
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9680
9681
  /* This condition only happened when symbol is undefined.  */
9682
0
  if (foff == 0
9683
0
      || foff < -CONSERVATIVE_24BIT_S1
9684
0
      || foff >= CONSERVATIVE_24BIT_S1)
9685
0
    return false;
9686
9687
  /* Relax to: jal symbol; 25_PCREL.  */
9688
  /* For simplicity of coding, we are going to modify the section
9689
     contents, the section relocs, and the BFD symbol table.  We
9690
     must tell the rest of the code not to free up this
9691
     information.  It would be possible to instead create a table
9692
     of changes which have to be made, as is done in coff-mips.c;
9693
     that would be more work, but would require less memory when
9694
     the linker is run.  */
9695
9696
  /* Replace the long call with a jal.  */
9697
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9698
0
             R_NDS32_25_PCREL_RELA);
9699
0
  irel->r_addend = hi_irelfn->r_addend;
9700
9701
  /* We don't resolve this here but resolve it in relocate_section.  */
9702
0
  insn = INSN_JAL;
9703
0
  bfd_putb32 (insn, contents + irel->r_offset);
9704
9705
0
  hi_irelfn->r_info =
9706
0
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9707
0
  lo_irelfn->r_info =
9708
0
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9709
0
  *insn_len = 4;
9710
9711
0
  if (seq_len & 0x2)
9712
0
    {
9713
0
      insn16 = NDS32_NOP16;
9714
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9715
0
      lo_irelfn->r_info =
9716
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9717
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9718
0
      *insn_len += 2;
9719
0
    }
9720
0
  return true;
9721
0
}
9722
9723
0
#define CONVERT_CONDITION_CALL(insn) (((insn) & 0xffff0000) ^ 0x90000)
9724
/* Relax LONGCALL2 relocation for nds32_elf_relax_section.  */
9725
9726
static bool
9727
nds32_elf_relax_longcall2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9728
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9729
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9730
         Elf_Internal_Shdr *symtab_hdr)
9731
0
{
9732
  /* bltz  rt, .L1   ; LONGCALL2
9733
     jal   symbol   ; 25_PCREL
9734
     .L1: */
9735
9736
  /* Get the reloc for the address from which the register is
9737
     being loaded.  This reloc will tell us which function is
9738
     actually being called.  */
9739
9740
0
  bfd_vma laddr;
9741
0
  uint32_t insn;
9742
0
  Elf_Internal_Rela *i1_irelfn, *cond_irelfn, *irelend;
9743
0
  bfd_signed_vma foff;
9744
9745
0
  irelend = internal_relocs + sec->reloc_count;
9746
0
  laddr = irel->r_offset;
9747
0
  i1_irelfn =
9748
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9749
0
         R_NDS32_25_PCREL_RELA, laddr + 4);
9750
9751
0
  if (i1_irelfn == irelend)
9752
0
    {
9753
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL2",
9754
0
        (uint64_t) irel->r_offset);
9755
0
      return false;
9756
0
    }
9757
9758
0
  insn = bfd_getb32 (contents + laddr);
9759
9760
  /* Get the value of the symbol referred to by the reloc.  */
9761
0
  foff = calculate_offset (abfd, sec, i1_irelfn, isymbuf, symtab_hdr);
9762
9763
0
  if (foff == 0
9764
0
      || foff < -CONSERVATIVE_16BIT_S1
9765
0
      || foff >= CONSERVATIVE_16BIT_S1)
9766
0
    return false;
9767
9768
  /* Relax to bgezal   rt, label ; 17_PCREL
9769
     or   bltzal   rt, label ; 17_PCREL */
9770
9771
  /* Convert to complimentary conditional call.  */
9772
0
  insn = CONVERT_CONDITION_CALL (insn);
9773
9774
  /* For simplicity of coding, we are going to modify the section
9775
     contents, the section relocs, and the BFD symbol table.  We
9776
     must tell the rest of the code not to free up this
9777
     information.  It would be possible to instead create a table
9778
     of changes which have to be made, as is done in coff-mips.c;
9779
     that would be more work, but would require less memory when
9780
     the linker is run.  */
9781
9782
  /* Clean unnessary relocations.  */
9783
0
  i1_irelfn->r_info =
9784
0
    ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info), R_NDS32_NONE);
9785
0
  cond_irelfn =
9786
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9787
0
         R_NDS32_17_PCREL_RELA, laddr);
9788
0
  if (cond_irelfn != irelend)
9789
0
    cond_irelfn->r_info =
9790
0
      ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info), R_NDS32_NONE);
9791
9792
  /* Replace the long call with a bgezal.  */
9793
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i1_irelfn->r_info),
9794
0
             R_NDS32_17_PCREL_RELA);
9795
0
  irel->r_addend = i1_irelfn->r_addend;
9796
9797
0
  bfd_putb32 (insn, contents + irel->r_offset);
9798
9799
0
  *insn_len = 4;
9800
0
  return true;
9801
0
}
9802
9803
/* Relax LONGCALL3 relocation for nds32_elf_relax_section.  */
9804
9805
static bool
9806
nds32_elf_relax_longcall3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9807
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9808
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9809
         Elf_Internal_Shdr *symtab_hdr)
9810
0
{
9811
  /* There are 3 variations for LONGCALL3
9812
     case 4-4-4-2; 16-bit on, optimize off or optimize for space
9813
     bltz  rt,   $1      ; LONGCALL3
9814
     sethi ta,   hi20(symbol)    ; HI20
9815
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9816
     jral5 ta        ;
9817
     $1
9818
9819
     case 4-4-4-4; 16-bit off, optimize don't care
9820
     bltz  rt,   $1      ; LONGCALL3
9821
     sethi ta,   hi20(symbol)    ; HI20
9822
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9823
     jral  ta        ;
9824
     $1
9825
9826
     case 4-4-4-4; 16-bit on, optimize for speed
9827
     bltz  rt,   $1      ; LONGCALL3
9828
     sethi ta,   hi20(symbol)    ; HI20
9829
     ori   ta, ta,  lo12(symbol)   ; LO12S0
9830
     jral  ta        ;
9831
     $1 */
9832
9833
  /* Get the reloc for the address from which the register is
9834
     being loaded.  This reloc will tell us which function is
9835
     actually being called.  */
9836
9837
0
  bfd_vma laddr;
9838
0
  int seq_len;  /* Original length of instruction sequence.  */
9839
0
  uint32_t insn;
9840
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
9841
0
  bfd_signed_vma foff;
9842
0
  uint16_t insn16;
9843
9844
0
  irelend = internal_relocs + sec->reloc_count;
9845
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9846
0
  laddr = irel->r_offset;
9847
0
  *insn_len = seq_len;
9848
9849
0
  hi_irelfn =
9850
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9851
0
         R_NDS32_HI20_RELA, laddr + 4);
9852
0
  lo_irelfn =
9853
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9854
0
         R_NDS32_LO12S0_ORI_RELA, laddr + 8);
9855
9856
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9857
0
    {
9858
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL3",
9859
0
        (uint64_t) irel->r_offset);
9860
0
      return false;
9861
0
    }
9862
9863
  /* Get the value of the symbol referred to by the reloc.  */
9864
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9865
9866
0
  if (foff == 0
9867
0
      || foff < -CONSERVATIVE_24BIT_S1
9868
0
      || foff >= CONSERVATIVE_24BIT_S1)
9869
0
    return false;
9870
9871
0
  insn = bfd_getb32 (contents + laddr);
9872
0
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
9873
0
    {
9874
      /* Relax to  bgezal   rt, label ; 17_PCREL
9875
   or    bltzal   rt, label ; 17_PCREL */
9876
9877
      /* Convert to complimentary conditional call.  */
9878
0
      insn = CONVERT_CONDITION_CALL (insn);
9879
0
      bfd_putb32 (insn, contents + irel->r_offset);
9880
9881
0
      *insn_len = 4;
9882
0
      irel->r_info =
9883
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9884
0
      hi_irelfn->r_info =
9885
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
9886
0
      lo_irelfn->r_info =
9887
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9888
9889
0
      cond_irelfn =
9890
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
9891
0
             R_NDS32_17_PCREL_RELA, laddr);
9892
0
      if (cond_irelfn != irelend)
9893
0
  {
9894
0
    cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9895
0
                R_NDS32_17_PCREL_RELA);
9896
0
    cond_irelfn->r_addend = hi_irelfn->r_addend;
9897
0
  }
9898
9899
0
      if (seq_len & 0x2)
9900
0
  {
9901
0
    insn16 = NDS32_NOP16;
9902
0
    bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9903
0
    hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9904
0
              R_NDS32_INSN16);
9905
0
    hi_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9906
0
    insn_len += 2;
9907
0
  }
9908
0
    }
9909
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
9910
0
    {
9911
      /* Relax to the following instruction sequence
9912
   bltz  rt,   $1 ; LONGCALL2
9913
   jal   symbol   ; 25_PCREL
9914
   $1 */
9915
0
      *insn_len = 8;
9916
0
      insn = INSN_JAL;
9917
0
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);
9918
9919
0
      hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
9920
0
          R_NDS32_25_PCREL_RELA);
9921
0
      irel->r_info =
9922
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL2);
9923
9924
0
      lo_irelfn->r_info =
9925
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
9926
9927
0
      if (seq_len & 0x2)
9928
0
  {
9929
0
    insn16 = NDS32_NOP16;
9930
0
    bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
9931
0
    lo_irelfn->r_info =
9932
0
      ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_INSN16);
9933
0
    lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
9934
0
    insn_len += 2;
9935
0
  }
9936
0
    }
9937
0
  return true;
9938
0
}
9939
9940
/* Relax LONGJUMP1 relocation for nds32_elf_relax_section.  */
9941
9942
static bool
9943
nds32_elf_relax_longjump1 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
9944
         Elf_Internal_Rela *internal_relocs, int *insn_len,
9945
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
9946
         Elf_Internal_Shdr *symtab_hdr)
9947
0
{
9948
  /* There are 3 variations for LONGJUMP1
9949
     case 4-4-2; 16-bit bit on, optimize off or optimize for space
9950
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
9951
     ori   ta, ta, lo12(symbol)  ; LO12S0
9952
     jr5   ta      ;
9953
9954
     case 4-4-4; 16-bit off, optimize don't care
9955
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
9956
     ori   ta, ta, lo12(symbol)  ; LO12S0
9957
     jr    ta      ;
9958
9959
     case 4-4-4; 16-bit on, optimize for speed
9960
     sethi ta, hi20(symbol)  ; LONGJUMP1/HI20
9961
     ori   ta, ta, lo12(symbol)  ; LO12S0
9962
     jr    ta      ;  */
9963
9964
  /* Get the reloc for the address from which the register is
9965
     being loaded.  This reloc will tell us which function is
9966
     actually being called.  */
9967
9968
0
  bfd_vma laddr;
9969
0
  int seq_len;  /* Original length of instruction sequence.  */
9970
0
  int insn16_on;  /* 16-bit on/off.  */
9971
0
  uint32_t insn;
9972
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *irelend;
9973
0
  bfd_signed_vma foff;
9974
0
  uint16_t insn16;
9975
0
  unsigned long reloc;
9976
9977
0
  irelend = internal_relocs + sec->reloc_count;
9978
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
9979
0
  laddr = irel->r_offset;
9980
0
  *insn_len = seq_len;
9981
0
  insn16_on = IS_16BIT_ON (irel->r_addend);
9982
9983
0
  hi_irelfn =
9984
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9985
0
         R_NDS32_HI20_RELA, laddr);
9986
0
  lo_irelfn =
9987
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
9988
0
         R_NDS32_LO12S0_ORI_RELA, laddr + 4);
9989
0
  if (hi_irelfn == irelend || lo_irelfn == irelend)
9990
0
    {
9991
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP1",
9992
0
        (uint64_t) irel->r_offset);
9993
0
      return false;
9994
0
    }
9995
9996
  /* Get the value of the symbol referred to by the reloc.  */
9997
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
9998
9999
0
  if (foff == 0
10000
0
      || foff >= CONSERVATIVE_24BIT_S1
10001
0
      || foff < -CONSERVATIVE_24BIT_S1)
10002
0
    return false;
10003
10004
0
  if (insn16_on
10005
0
      && foff >= -ACCURATE_8BIT_S1
10006
0
      && foff < ACCURATE_8BIT_S1
10007
0
      && (seq_len & 0x2))
10008
0
    {
10009
      /* j8 label */
10010
      /* 16-bit on, but not optimized for speed.  */
10011
0
      reloc = R_NDS32_9_PCREL_RELA;
10012
0
      insn16 = INSN_J8;
10013
0
      bfd_putb16 (insn16, contents + irel->r_offset);
10014
0
      *insn_len = 2;
10015
0
      irel->r_info =
10016
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10017
0
    }
10018
0
  else
10019
0
    {
10020
      /* j     label */
10021
0
      reloc = R_NDS32_25_PCREL_RELA;
10022
0
      insn = INSN_J;
10023
0
      bfd_putb32 (insn, contents + irel->r_offset);
10024
0
      *insn_len = 4;
10025
0
      irel->r_info =
10026
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_INSN16);
10027
0
      irel->r_addend = 0;
10028
0
    }
10029
10030
0
  hi_irelfn->r_info =
10031
0
    ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10032
0
  lo_irelfn->r_info =
10033
0
    ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info), R_NDS32_NONE);
10034
10035
0
  if ((seq_len & 0x2) && ((*insn_len & 2) == 0))
10036
0
    {
10037
0
      insn16 = NDS32_NOP16;
10038
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10039
0
      lo_irelfn->r_info =
10040
0
  ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10041
0
          R_NDS32_INSN16);
10042
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10043
0
      *insn_len += 2;
10044
0
    }
10045
0
  return true;
10046
0
}
10047
10048
/* Revert condition branch.  This function does not check if the input
10049
   instruction is condition branch or not.  */
10050
10051
static void
10052
nds32_elf_convert_branch (uint16_t insn16, uint32_t insn,
10053
         uint16_t *re_insn16, uint32_t *re_insn)
10054
0
{
10055
0
  uint32_t comp_insn = 0;
10056
0
  uint16_t comp_insn16 = 0;
10057
10058
0
  if (insn)
10059
0
    {
10060
0
      if (N32_OP6 (insn) == N32_OP6_BR1)
10061
0
  {
10062
    /* beqs label.  */
10063
0
    comp_insn = (insn ^ 0x4000) & 0xffffc000;
10064
0
    if (N32_IS_RT3 (insn) && N32_RA5 (insn) == REG_R5)
10065
0
      {
10066
        /* Insn can be contracted to 16-bit implied r5.  */
10067
0
        comp_insn16 =
10068
0
    (comp_insn & 0x4000) ? INSN_BNES38 : INSN_BEQS38;
10069
0
        comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10070
0
      }
10071
0
  }
10072
0
      else if (N32_OP6 (insn) == N32_OP6_BR3)
10073
0
  {
10074
    /* bnec $ta, imm11, label.  */
10075
0
    comp_insn = (insn ^ 0x80000) & 0xffffff00;
10076
0
  }
10077
0
      else
10078
0
  {
10079
0
    comp_insn = (insn ^ 0x10000) & 0xffffc000;
10080
0
    if (N32_BR2_SUB (insn) == N32_BR2_BEQZ
10081
0
        || N32_BR2_SUB (insn) == N32_BR2_BNEZ)
10082
0
      {
10083
0
        if (N32_IS_RT3 (insn))
10084
0
    {
10085
      /* Insn can be contracted to 16-bit.  */
10086
0
      comp_insn16 =
10087
0
        (comp_insn & 0x10000) ? INSN_BNEZ38 : INSN_BEQZ38;
10088
0
      comp_insn16 |= (N32_RT5 (insn) & 0x7) << 8;
10089
0
    }
10090
0
        else if (N32_RT5 (insn) == REG_R15)
10091
0
    {
10092
      /* Insn can be contracted to 16-bit.  */
10093
0
      comp_insn16 =
10094
0
        (comp_insn & 0x10000) ? INSN_BNES38 : INSN_BEQS38;
10095
0
    }
10096
0
      }
10097
0
  }
10098
0
    }
10099
0
  else
10100
0
    {
10101
0
      switch ((insn16 & 0xf000) >> 12)
10102
0
  {
10103
0
  case 0xc:
10104
    /* beqz38 or bnez38 */
10105
0
    comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10106
0
    comp_insn = (comp_insn16 & 0x0800) ? INSN_BNEZ : INSN_BEQZ;
10107
0
    comp_insn |= ((comp_insn16 & 0x0700) >> 8) << 20;
10108
0
    break;
10109
10110
0
  case 0xd:
10111
    /* beqs38 or bnes38 */
10112
0
    comp_insn16 = (insn16 ^ 0x0800) & 0xff00;
10113
0
    comp_insn = (comp_insn16 & 0x0800) ? INSN_BNE : INSN_BEQ;
10114
0
    comp_insn |= (((comp_insn16 & 0x0700) >> 8) << 20)
10115
0
      | (REG_R5 << 15);
10116
0
    break;
10117
10118
0
  case 0xe:
10119
    /* beqzS8 or bnezS8 */
10120
0
    comp_insn16 = (insn16 ^ 0x0100) & 0xff00;
10121
0
    comp_insn = (comp_insn16 & 0x0100) ? INSN_BNEZ : INSN_BEQZ;
10122
0
    comp_insn |= REG_R15 << 20;
10123
0
    break;
10124
10125
0
  default:
10126
0
    break;
10127
0
  }
10128
0
    }
10129
0
  if (comp_insn && re_insn)
10130
0
    *re_insn = comp_insn;
10131
0
  if (comp_insn16 && re_insn16)
10132
0
    *re_insn16 = comp_insn16;
10133
0
}
10134
10135
/* Relax LONGJUMP2 relocation for nds32_elf_relax_section.  */
10136
10137
static bool
10138
nds32_elf_relax_longjump2 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10139
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10140
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10141
         Elf_Internal_Shdr *symtab_hdr)
10142
0
{
10143
  /* There are 3 variations for LONGJUMP2
10144
     case 2-4;  1st insn convertible, 16-bit on,
10145
     optimize off or optimize for space
10146
     bnes38  rt, ra, $1 ; LONGJUMP2
10147
     j       label      ; 25_PCREL
10148
     $1:
10149
10150
     case 4-4; 1st insn not convertible
10151
     bne  rt, ra, $1 ; LONGJUMP2
10152
     j    label      ; 25_PCREL
10153
     $1:
10154
10155
     case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10156
     bne  rt, ra, $1 ; LONGJUMP2
10157
     j    label      ; 25_PCREL
10158
     $1: */
10159
10160
  /* Get the reloc for the address from which the register is
10161
     being loaded.  This reloc will tell us which function is
10162
     actually being called.  */
10163
10164
0
  bfd_vma laddr;
10165
0
  int seq_len;  /* Original length of instruction sequence.  */
10166
0
  Elf_Internal_Rela *i2_irelfn, *cond_irelfn, *irelend;
10167
0
  int first_size;
10168
0
  unsigned int i;
10169
0
  bfd_signed_vma foff;
10170
0
  uint32_t insn, re_insn = 0;
10171
0
  uint16_t insn16, re_insn16 = 0;
10172
0
  unsigned long reloc, cond_reloc;
10173
10174
0
  enum elf_nds32_reloc_type checked_types[] =
10175
0
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10176
10177
0
  irelend = internal_relocs + sec->reloc_count;
10178
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10179
0
  laddr = irel->r_offset;
10180
0
  *insn_len = seq_len;
10181
0
  first_size = (seq_len == 6) ? 2 : 4;
10182
10183
0
  i2_irelfn =
10184
0
    find_relocs_at_address_addr (irel, internal_relocs,
10185
0
         irelend, R_NDS32_25_PCREL_RELA,
10186
0
         laddr + first_size);
10187
10188
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10189
0
    {
10190
0
      cond_irelfn =
10191
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10192
0
             checked_types[i], laddr);
10193
0
      if (cond_irelfn != irelend)
10194
0
  break;
10195
0
    }
10196
10197
0
  if (i2_irelfn == irelend || cond_irelfn == irelend)
10198
0
    {
10199
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP2",
10200
0
        (uint64_t) irel->r_offset);
10201
0
      return false;
10202
0
    }
10203
10204
  /* Get the value of the symbol referred to by the reloc.  */
10205
0
  foff = calculate_offset (abfd, sec, i2_irelfn, isymbuf, symtab_hdr);
10206
0
  if (foff == 0
10207
0
      || foff < -CONSERVATIVE_16BIT_S1
10208
0
      || foff >= CONSERVATIVE_16BIT_S1)
10209
0
    return false;
10210
10211
  /* Get the all corresponding instructions.  */
10212
0
  if (first_size == 4)
10213
0
    {
10214
0
      insn = bfd_getb32 (contents + laddr);
10215
0
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10216
0
    }
10217
0
  else
10218
0
    {
10219
0
      insn16 = bfd_getb16 (contents + laddr);
10220
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10221
0
    }
10222
10223
0
  if (re_insn16 && foff >= -(ACCURATE_8BIT_S1 - first_size)
10224
0
      && foff < ACCURATE_8BIT_S1 - first_size)
10225
0
    {
10226
0
      if (first_size == 4)
10227
0
  {
10228
    /* Don't convert it to 16-bit now, keep this as relaxable for
10229
       ``label reloc; INSN16''.  */
10230
10231
    /* Save comp_insn32 to buffer.  */
10232
0
    bfd_putb32 (re_insn, contents + irel->r_offset);
10233
0
    *insn_len = 4;
10234
0
    reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10235
0
      R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10236
0
    cond_reloc = R_NDS32_INSN16;
10237
0
  }
10238
0
      else
10239
0
  {
10240
0
    bfd_putb16 (re_insn16, contents + irel->r_offset);
10241
0
    *insn_len = 2;
10242
0
    reloc = R_NDS32_9_PCREL_RELA;
10243
0
    cond_reloc = R_NDS32_NONE;
10244
0
  }
10245
0
    }
10246
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
10247
0
     && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10248
0
         && foff < ACCURATE_14BIT_S1 - first_size))
10249
0
    {
10250
      /* beqs     label    ; 15_PCREL */
10251
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10252
0
      *insn_len = 4;
10253
0
      reloc = R_NDS32_15_PCREL_RELA;
10254
0
      cond_reloc = R_NDS32_NONE;
10255
0
    }
10256
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
10257
0
     && foff >= -CONSERVATIVE_16BIT_S1
10258
0
     && foff < CONSERVATIVE_16BIT_S1)
10259
0
    {
10260
      /* beqz     label ; 17_PCREL */
10261
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10262
0
      *insn_len = 4;
10263
0
      reloc = R_NDS32_17_PCREL_RELA;
10264
0
      cond_reloc = R_NDS32_NONE;
10265
0
    }
10266
0
  else
10267
0
    return false;
10268
10269
  /* Set all relocations.  */
10270
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info), reloc);
10271
0
  irel->r_addend = i2_irelfn->r_addend;
10272
10273
0
  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10274
0
              cond_reloc);
10275
0
  cond_irelfn->r_addend = 0;
10276
10277
0
  if ((seq_len ^ *insn_len ) & 0x2)
10278
0
    {
10279
0
      insn16 = NDS32_NOP16;
10280
0
      bfd_putb16 (insn16, contents + irel->r_offset + 4);
10281
0
      i2_irelfn->r_offset = 4;
10282
0
      i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10283
0
          R_NDS32_INSN16);
10284
0
      i2_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10285
0
      *insn_len += 2;
10286
0
    }
10287
0
  else
10288
0
    i2_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (i2_irelfn->r_info),
10289
0
              R_NDS32_NONE);
10290
0
  return true;
10291
0
}
10292
10293
/* Relax LONGJUMP3 relocation for nds32_elf_relax_section.  */
10294
10295
static bool
10296
nds32_elf_relax_longjump3 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10297
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10298
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10299
         Elf_Internal_Shdr *symtab_hdr)
10300
0
{
10301
  /* There are 5 variations for LONGJUMP3
10302
     case 1: 2-4-4-2; 1st insn convertible, 16-bit on,
10303
     optimize off or optimize for space
10304
     bnes38   rt, ra, $1      ; LONGJUMP3
10305
     sethi    ta, hi20(symbol)      ; HI20
10306
     ori      ta, ta, lo12(symbol)  ; LO12S0
10307
     jr5      ta        ;
10308
     $1:          ;
10309
10310
     case 2: 2-4-4-2; 1st insn convertible, 16-bit on, optimize for speed
10311
     bnes38   rt, ra, $1     ; LONGJUMP3
10312
     sethi    ta, hi20(symbol)     ; HI20
10313
     ori      ta, ta, lo12(symbol) ; LO12S0
10314
     jr5      ta       ;
10315
     $1:         ; LABEL
10316
10317
     case 3: 4-4-4-2; 1st insn not convertible, 16-bit on,
10318
     optimize off or optimize for space
10319
     bne   rt, ra, $1   ; LONGJUMP3
10320
     sethi ta, hi20(symbol) ; HI20
10321
     ori   ta, ta, lo12(symbol) ; LO12S0
10322
     jr5   ta     ;
10323
     $1:      ;
10324
10325
     case 4: 4-4-4-4; 1st insn don't care, 16-bit off, optimize don't care
10326
     16-bit off if no INSN16
10327
     bne   rt, ra, $1   ; LONGJUMP3
10328
     sethi ta, hi20(symbol) ; HI20
10329
     ori   ta, ta, lo12(symbol) ; LO12S0
10330
     jr    ta     ;
10331
     $1:      ;
10332
10333
     case 5: 4-4-4-4; 1st insn not convertible, 16-bit on, optimize for speed
10334
     16-bit off if no INSN16
10335
     bne   rt, ra, $1   ; LONGJUMP3
10336
     sethi ta, hi20(symbol) ; HI20
10337
     ori   ta, ta, lo12(symbol) ; LO12S0
10338
     jr    ta     ;
10339
     $1:      ; LABEL */
10340
10341
  /* Get the reloc for the address from which the register is
10342
     being loaded.  This reloc will tell us which function is
10343
     actually being called.  */
10344
0
  enum elf_nds32_reloc_type checked_types[] =
10345
0
    { R_NDS32_15_PCREL_RELA, R_NDS32_9_PCREL_RELA };
10346
10347
0
  int reloc_off = 0, cond_removed = 0, convertible;
10348
0
  bfd_vma laddr;
10349
0
  int seq_len;  /* Original length of instruction sequence.  */
10350
0
  Elf_Internal_Rela *hi_irelfn, *lo_irelfn, *cond_irelfn, *irelend;
10351
0
  int first_size;
10352
0
  unsigned int i;
10353
0
  bfd_signed_vma foff;
10354
0
  uint32_t insn, re_insn = 0;
10355
0
  uint16_t insn16, re_insn16 = 0;
10356
0
  unsigned long reloc, cond_reloc;
10357
10358
0
  irelend = internal_relocs + sec->reloc_count;
10359
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10360
0
  laddr = irel->r_offset;
10361
0
  *insn_len = seq_len;
10362
10363
0
  convertible = IS_1ST_CONVERT (irel->r_addend);
10364
10365
0
  if (convertible)
10366
0
    first_size = 2;
10367
0
  else
10368
0
    first_size = 4;
10369
10370
  /* Get all needed relocations.  */
10371
0
  hi_irelfn =
10372
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10373
0
         R_NDS32_HI20_RELA, laddr + first_size);
10374
0
  lo_irelfn =
10375
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10376
0
         R_NDS32_LO12S0_ORI_RELA,
10377
0
         laddr + first_size + 4);
10378
10379
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
10380
0
    {
10381
0
      cond_irelfn =
10382
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10383
0
             checked_types[i], laddr);
10384
0
      if (cond_irelfn != irelend)
10385
0
  break;
10386
0
    }
10387
10388
0
  if (hi_irelfn == irelend
10389
0
      || lo_irelfn == irelend
10390
0
      || cond_irelfn == irelend)
10391
0
    {
10392
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP3",
10393
0
        (uint64_t) irel->r_offset);
10394
0
      return false;
10395
0
    }
10396
10397
  /* Get the value of the symbol referred to by the reloc.  */
10398
0
  foff = calculate_offset (abfd, sec, hi_irelfn, isymbuf, symtab_hdr);
10399
10400
0
  if (foff == 0
10401
0
      || foff < -CONSERVATIVE_24BIT_S1
10402
0
      || foff >= CONSERVATIVE_24BIT_S1)
10403
0
    return false;
10404
10405
  /* Get the all corresponding instructions.  */
10406
0
  if (first_size == 4)
10407
0
    {
10408
0
      insn = bfd_getb32 (contents + laddr);
10409
0
      nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
10410
0
    }
10411
0
  else
10412
0
    {
10413
0
      insn16 = bfd_getb16 (contents + laddr);
10414
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
10415
0
    }
10416
10417
  /* For simplicity of coding, we are going to modify the section
10418
     contents, the section relocs, and the BFD symbol table.  We
10419
     must tell the rest of the code not to free up this
10420
     information.  It would be possible to instead create a table
10421
     of changes which have to be made, as is done in coff-mips.c;
10422
     that would be more work, but would require less memory when
10423
     the linker is run.  */
10424
10425
0
  if (re_insn16
10426
0
      && foff >= -ACCURATE_8BIT_S1 - first_size
10427
0
      && foff < ACCURATE_8BIT_S1 - first_size)
10428
0
    {
10429
0
      if (!(seq_len & 0x2))
10430
0
  {
10431
    /* Don't convert it to 16-bit now, keep this as relaxable
10432
       for ``label reloc; INSN1a''6.  */
10433
    /* Save comp_insn32 to buffer.  */
10434
0
    bfd_putb32 (re_insn, contents + irel->r_offset);
10435
0
    *insn_len = 4;
10436
0
    reloc = (N32_OP6 (re_insn) == N32_OP6_BR1) ?
10437
0
      R_NDS32_15_PCREL_RELA : R_NDS32_17_PCREL_RELA;
10438
0
    cond_reloc = R_NDS32_INSN16;
10439
0
  }
10440
0
      else
10441
0
  {
10442
    /* Not optimize for speed; convert sequence to 16-bit.  */
10443
    /* Save comp_insn16 to buffer.  */
10444
0
    bfd_putb16 (re_insn16, contents + irel->r_offset);
10445
0
    *insn_len = 2;
10446
0
    reloc = R_NDS32_9_PCREL_RELA;
10447
0
    cond_reloc = R_NDS32_NONE;
10448
0
  }
10449
0
      cond_removed = 1;
10450
0
    }
10451
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR1
10452
0
     && (foff >= -(ACCURATE_14BIT_S1 - first_size)
10453
0
         && foff < ACCURATE_14BIT_S1 - first_size))
10454
0
    {
10455
      /* beqs     label    ; 15_PCREL */
10456
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10457
0
      *insn_len = 4;
10458
0
      reloc = R_NDS32_15_PCREL_RELA;
10459
0
      cond_reloc = R_NDS32_NONE;
10460
0
      cond_removed = 1;
10461
0
    }
10462
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
10463
0
     && foff >= -CONSERVATIVE_16BIT_S1
10464
0
     && foff < CONSERVATIVE_16BIT_S1)
10465
0
    {
10466
      /* beqz     label ; 17_PCREL */
10467
0
      bfd_putb32 (re_insn, contents + irel->r_offset);
10468
0
      *insn_len = 4;
10469
0
      reloc = R_NDS32_17_PCREL_RELA;
10470
0
      cond_reloc = R_NDS32_NONE;
10471
0
      cond_removed = 1;
10472
0
    }
10473
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
10474
0
     && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
10475
0
    {
10476
      /* Relax to one of the following 3 variations
10477
10478
   case 2-4; 1st insn convertible, 16-bit on, optimize off or optimize
10479
   for space
10480
   bnes38  rt, $1 ; LONGJUMP2
10481
   j       label  ; 25_PCREL
10482
   $1
10483
10484
   case 4-4; 1st insn not convertible, others don't care
10485
   bne   rt, ra, $1 ; LONGJUMP2
10486
   j     label      ; 25_PCREL
10487
   $1
10488
10489
   case 4-4; 1st insn convertible, 16-bit on, optimize for speed
10490
   bne   rt, ra, $1 ; LONGJUMP2
10491
   j     label      ; 25_PCREL
10492
   $1 */
10493
10494
      /* Offset for first instruction.  */
10495
10496
      /* Use j label as second instruction.  */
10497
0
      *insn_len = 4 + first_size;
10498
0
      insn = INSN_J;
10499
0
      bfd_putb32 (insn, contents + hi_irelfn->r_offset);
10500
0
      reloc = R_NDS32_LONGJUMP2;
10501
0
      cond_reloc = R_NDS32_25_PLTREL;
10502
0
    }
10503
0
    else
10504
0
      return false;
10505
10506
0
    if (cond_removed == 1)
10507
0
      {
10508
  /* Set all relocations.  */
10509
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), reloc);
10510
0
  irel->r_addend = hi_irelfn->r_addend;
10511
10512
0
  cond_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irelfn->r_info),
10513
0
              cond_reloc);
10514
0
  cond_irelfn->r_addend = 0;
10515
0
  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10516
0
            R_NDS32_NONE);
10517
0
      }
10518
0
    else
10519
0
      {
10520
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
10521
0
  irel->r_addend = irel->r_addend;
10522
0
  hi_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info),
10523
0
            cond_reloc);
10524
0
      }
10525
10526
0
  if ((seq_len ^ *insn_len ) & 0x2)
10527
0
    {
10528
0
      insn16 = NDS32_NOP16;
10529
0
      bfd_putb16 (insn16, contents + irel->r_offset + *insn_len);
10530
0
      lo_irelfn->r_offset = *insn_len;
10531
0
      lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10532
0
          R_NDS32_INSN16);
10533
0
      lo_irelfn->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
10534
0
      *insn_len += 2;
10535
0
    }
10536
0
  else
10537
0
    lo_irelfn->r_info = ELF32_R_INFO (ELF32_R_SYM (lo_irelfn->r_info),
10538
0
              R_NDS32_NONE);
10539
0
  return true;
10540
0
}
10541
10542
/* Relax LONGCALL4 relocation for nds32_elf_relax_section.  */
10543
10544
static bool
10545
nds32_elf_relax_longcall4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10546
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10547
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10548
         Elf_Internal_Shdr *symtab_hdr)
10549
0
{
10550
  /* The pattern for LONGCALL4.  Support for function cse.
10551
     sethi ta, hi20(symbol) ; LONGCALL4/HI20
10552
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10553
     jral  ta     ; PTR_RES/EMPTY/INSN16  */
10554
10555
0
  bfd_vma laddr;
10556
0
  uint32_t insn;
10557
0
  Elf_Internal_Rela *hi_irel, *ptr_irel, *insn_irel, *em_irel, *call_irel;
10558
0
  Elf_Internal_Rela *irelend;
10559
0
  bfd_signed_vma foff;
10560
10561
0
  irelend = internal_relocs + sec->reloc_count;
10562
0
  laddr = irel->r_offset;
10563
10564
  /* Get the reloc for the address from which the register is
10565
     being loaded.  This reloc will tell us which function is
10566
     actually being called.  */
10567
0
  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10568
0
           R_NDS32_HI20_RELA, laddr);
10569
10570
0
  if (hi_irel == irelend)
10571
0
    {
10572
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10573
0
        (uint64_t) irel->r_offset);
10574
0
      return false;
10575
0
    }
10576
10577
  /* Get the value of the symbol referred to by the reloc.  */
10578
0
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10579
10580
  /* This condition only happened when symbol is undefined.  */
10581
0
  if (foff == 0
10582
0
      || foff < -CONSERVATIVE_24BIT_S1
10583
0
      || foff >= CONSERVATIVE_24BIT_S1)
10584
0
    return false;
10585
10586
  /* Relax to: jal symbol; 25_PCREL.  */
10587
  /* For simplicity of coding, we are going to modify the section
10588
     contents, the section relocs, and the BFD symbol table.  We
10589
     must tell the rest of the code not to free up this
10590
     information.  It would be possible to instead create a table
10591
     of changes which have to be made, as is done in coff-mips.c;
10592
     that would be more work, but would require less memory when
10593
     the linker is run.  */
10594
10595
0
  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10596
0
            R_NDS32_PTR_RESOLVED, irel->r_addend);
10597
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10598
0
            R_NDS32_EMPTY, irel->r_addend);
10599
10600
0
  if (ptr_irel == irelend || em_irel == irelend)
10601
0
    {
10602
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL4",
10603
0
        (uint64_t) irel->r_offset);
10604
0
      return false;
10605
0
    }
10606
  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10607
0
  insn = bfd_getb32 (contents + irel->r_addend);
10608
0
  if (insn & 0x80000000)
10609
0
    return false;
10610
10611
  /* Replace the long call with a jal.  */
10612
0
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10613
0
          R_NDS32_25_PCREL_RELA);
10614
0
  ptr_irel->r_addend = 1;
10615
10616
  /* We don't resolve this here but resolve it in relocate_section.  */
10617
0
  insn = INSN_JAL;
10618
0
  bfd_putb32 (insn, contents + em_irel->r_offset);
10619
10620
0
  irel->r_info =
10621
0
    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10622
10623
  /* If there is function cse, HI20 can not remove now.  */
10624
0
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10625
0
             R_NDS32_LONGCALL4, laddr);
10626
0
  if (call_irel == irelend)
10627
0
    {
10628
0
      *insn_len = 0;
10629
0
      hi_irel->r_info =
10630
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10631
0
    }
10632
10633
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10634
0
            R_NDS32_INSN16, irel->r_addend);
10635
0
  if (insn_irel != irelend)
10636
0
    insn_irel->r_info =
10637
0
      ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10638
10639
0
  return true;
10640
0
}
10641
10642
/* Relax LONGCALL5 relocation for nds32_elf_relax_section.  */
10643
10644
static bool
10645
nds32_elf_relax_longcall5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10646
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10647
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10648
         Elf_Internal_Shdr *symtab_hdr)
10649
0
{
10650
  /* The pattern for LONGCALL5.
10651
     bltz  rt, .L1  ; LONGCALL5/17_PCREL
10652
     jal   symbol ; 25_PCREL
10653
     .L1:  */
10654
10655
0
  bfd_vma laddr;
10656
0
  uint32_t insn;
10657
0
  Elf_Internal_Rela *cond_irel, *irelend;
10658
0
  bfd_signed_vma foff;
10659
10660
0
  irelend = internal_relocs + sec->reloc_count;
10661
0
  laddr = irel->r_offset;
10662
0
  insn = bfd_getb32 (contents + laddr);
10663
10664
  /* Get the reloc for the address from which the register is
10665
     being loaded.  This reloc will tell us which function is
10666
     actually being called.  */
10667
0
  cond_irel =
10668
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10669
0
         R_NDS32_25_PCREL_RELA, irel->r_addend);
10670
0
  if (cond_irel == irelend)
10671
0
    {
10672
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL5",
10673
0
        (uint64_t) irel->r_offset);
10674
0
      return false;
10675
0
    }
10676
10677
  /* Get the value of the symbol referred to by the reloc.  */
10678
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10679
10680
0
  if (foff == 0
10681
0
      || foff < -CONSERVATIVE_16BIT_S1
10682
0
      || foff >= CONSERVATIVE_16BIT_S1)
10683
0
    return false;
10684
10685
  /* Relax to bgezal   rt, label ; 17_PCREL
10686
     or   bltzal   rt, label ; 17_PCREL.  */
10687
10688
  /* Convert to complimentary conditional call.  */
10689
0
  insn = CONVERT_CONDITION_CALL (insn);
10690
10691
  /* For simplicity of coding, we are going to modify the section
10692
     contents, the section relocs, and the BFD symbol table.  We
10693
     must tell the rest of the code not to free up this
10694
     information.  It would be possible to instead create a table
10695
     of changes which have to be made, as is done in coff-mips.c;
10696
     that would be more work, but would require less memory when
10697
     the linker is run.  */
10698
10699
  /* Modify relocation and contents.  */
10700
0
  cond_irel->r_info =
10701
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_17_PCREL_RELA);
10702
10703
  /* Replace the long call with a bgezal.  */
10704
0
  bfd_putb32 (insn, contents + cond_irel->r_offset);
10705
0
  *insn_len = 0;
10706
10707
  /* Clean unnessary relocations.  */
10708
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10709
10710
0
  cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10711
0
             R_NDS32_17_PCREL_RELA, laddr);
10712
0
  cond_irel->r_info =
10713
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10714
10715
0
  return true;
10716
0
}
10717
10718
/* Relax LONGCALL6 relocation for nds32_elf_relax_section.  */
10719
10720
static bool
10721
nds32_elf_relax_longcall6 (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 LONGCALL6.
10727
     bltz  rt,   .L1      ; LONGCALL6/17_PCREL
10728
     sethi ta,   hi20(symbol)   ; HI20/PTR
10729
     ori   ta, ta,  lo12(symbol)  ; LO12S0_ORI/PTR
10730
     jral  ta       ; PTR_RES/EMPTY/INSN16
10731
     .L1  */
10732
10733
0
  bfd_vma laddr;
10734
0
  uint32_t insn;
10735
0
  Elf_Internal_Rela *em_irel, *cond_irel, *irelend;
10736
0
  bfd_signed_vma foff;
10737
10738
0
  irelend = internal_relocs + sec->reloc_count;
10739
0
  laddr = irel->r_offset;
10740
10741
  /* Get the reloc for the address from which the register is
10742
     being loaded.  This reloc will tell us which function is
10743
     actually being called.  */
10744
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10745
0
           R_NDS32_EMPTY, irel->r_addend);
10746
10747
0
  if (em_irel == irelend)
10748
0
    {
10749
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGCALL6",
10750
0
        (uint64_t) irel->r_offset);
10751
0
      return false;
10752
0
    }
10753
10754
  /* Get the value of the symbol referred to by the reloc.  */
10755
0
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
10756
10757
0
  if (foff == 0
10758
0
      || foff < -CONSERVATIVE_24BIT_S1
10759
0
      || foff >= CONSERVATIVE_24BIT_S1)
10760
0
    return false;
10761
10762
  /* Check these is enough space to insert jal in R_NDS32_EMPTY.  */
10763
0
  insn = bfd_getb32 (contents + irel->r_addend);
10764
0
  if (insn & 0x80000000)
10765
0
    return false;
10766
10767
0
  insn = bfd_getb32 (contents + laddr);
10768
0
  if (foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
10769
0
    {
10770
      /* Relax to  bgezal   rt, label ; 17_PCREL
10771
   or    bltzal   rt, label ; 17_PCREL.  */
10772
10773
      /* Convert to complimentary conditional call.  */
10774
0
      *insn_len = 0;
10775
0
      insn = CONVERT_CONDITION_CALL (insn);
10776
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
10777
10778
0
      em_irel->r_info =
10779
0
  ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_17_PCREL_RELA);
10780
10781
      /* Set resolved relocation.  */
10782
0
      cond_irel =
10783
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10784
0
             R_NDS32_PTR_RESOLVED, irel->r_addend);
10785
0
      if (cond_irel == irelend)
10786
0
  {
10787
0
    _bfd_error_handler (unrecognized_reloc_msg, abfd,
10788
0
            "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10789
0
    return false;
10790
0
  }
10791
0
      cond_irel->r_addend = 1;
10792
10793
      /* Clear relocations.  */
10794
10795
0
      irel->r_info =
10796
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10797
10798
0
      cond_irel =
10799
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10800
0
             R_NDS32_17_PCREL_RELA, laddr);
10801
0
      if (cond_irel != irelend)
10802
0
  cond_irel->r_info =
10803
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10804
10805
0
      cond_irel =
10806
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10807
0
             R_NDS32_INSN16, irel->r_addend);
10808
0
      if (cond_irel != irelend)
10809
0
  cond_irel->r_info =
10810
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10811
10812
0
    }
10813
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 && foff < CONSERVATIVE_24BIT_S1)
10814
0
    {
10815
      /* Relax to the following instruction sequence
10816
   bltz  rt, .L1  ; LONGCALL2/17_PCREL
10817
   jal   symbol ; 25_PCREL/PTR_RES
10818
   .L1  */
10819
0
      *insn_len = 4;
10820
      /* Convert instruction.  */
10821
0
      insn = INSN_JAL;
10822
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
10823
10824
      /* Convert relocations.  */
10825
0
      em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info),
10826
0
              R_NDS32_25_PCREL_RELA);
10827
0
      irel->r_info =
10828
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_LONGCALL5);
10829
10830
      /* Set resolved relocation.  */
10831
0
      cond_irel =
10832
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10833
0
             R_NDS32_PTR_RESOLVED, irel->r_addend);
10834
0
      if (cond_irel == irelend)
10835
0
  {
10836
0
    _bfd_error_handler (unrecognized_reloc_msg, abfd,
10837
0
            "R_NDS32_LONGCALL6", (uint64_t) irel->r_offset);
10838
0
    return false;
10839
0
  }
10840
0
      cond_irel->r_addend = 1;
10841
10842
0
      cond_irel =
10843
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
10844
0
             R_NDS32_INSN16, irel->r_addend);
10845
0
      if (cond_irel != irelend)
10846
0
  cond_irel->r_info =
10847
0
    ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
10848
0
    }
10849
0
  return true;
10850
0
}
10851
10852
/* Relax LONGJUMP4 relocation for nds32_elf_relax_section.  */
10853
10854
static bool
10855
nds32_elf_relax_longjump4 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10856
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10857
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
10858
         Elf_Internal_Shdr *symtab_hdr)
10859
0
{
10860
  /* The pattern for LONGJUMP4.
10861
     sethi ta, hi20(symbol) ; LONGJUMP4/HI20
10862
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
10863
     jr    ta     ; PTR_RES/INSN16/EMPTY  */
10864
10865
0
  bfd_vma laddr;
10866
0
  int seq_len;  /* Original length of instruction sequence.  */
10867
0
  uint32_t insn;
10868
0
  Elf_Internal_Rela *hi_irel, *ptr_irel, *em_irel, *call_irel, *irelend;
10869
0
  bfd_signed_vma foff;
10870
10871
0
  irelend = internal_relocs + sec->reloc_count;
10872
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
10873
0
  laddr = irel->r_offset;
10874
0
  *insn_len = seq_len;
10875
10876
  /* Get the reloc for the address from which the register is
10877
     being loaded.  This reloc will tell us which function is
10878
     actually being called.  */
10879
10880
0
  hi_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10881
0
           R_NDS32_HI20_RELA, laddr);
10882
10883
0
  if (hi_irel == irelend)
10884
0
    {
10885
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10886
0
        (uint64_t) irel->r_offset);
10887
0
      return false;
10888
0
    }
10889
10890
  /* Get the value of the symbol referred to by the reloc.  */
10891
0
  foff = calculate_offset (abfd, sec, hi_irel, isymbuf, symtab_hdr);
10892
10893
0
  if (foff == 0
10894
0
      || foff >= CONSERVATIVE_24BIT_S1
10895
0
      || foff < -CONSERVATIVE_24BIT_S1)
10896
0
    return false;
10897
10898
  /* Convert it to "j label", it may be converted to j8 in the final
10899
     pass of relaxation.  Therefore, we do not consider this currently.  */
10900
0
  ptr_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10901
0
            R_NDS32_PTR_RESOLVED, irel->r_addend);
10902
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10903
0
           R_NDS32_EMPTY, irel->r_addend);
10904
10905
0
  if (ptr_irel == irelend || em_irel == irelend)
10906
0
    {
10907
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP4",
10908
0
        (uint64_t) irel->r_offset);
10909
0
      return false;
10910
0
    }
10911
10912
0
  em_irel->r_info =
10913
0
    ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), R_NDS32_25_PCREL_RELA);
10914
0
  ptr_irel->r_addend = 1;
10915
10916
  /* Write instruction.  */
10917
0
  insn = INSN_J;
10918
0
  bfd_putb32 (insn, contents + em_irel->r_offset);
10919
10920
  /* Clear relocations.  */
10921
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
10922
10923
  /* If there is function cse, HI20 can not remove now.  */
10924
0
  call_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
10925
0
             R_NDS32_LONGJUMP4, laddr);
10926
0
  if (call_irel == irelend)
10927
0
    {
10928
0
      *insn_len = 0;
10929
0
      hi_irel->r_info =
10930
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irel->r_info), R_NDS32_NONE);
10931
0
    }
10932
10933
0
  return true;
10934
0
}
10935
10936
/* Relax LONGJUMP5 relocation for nds32_elf_relax_section.  */
10937
10938
static bool
10939
nds32_elf_relax_longjump5 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
10940
         Elf_Internal_Rela *internal_relocs, int *insn_len,
10941
         int *seq_len, bfd_byte *contents,
10942
         Elf_Internal_Sym *isymbuf,
10943
         Elf_Internal_Shdr *symtab_hdr)
10944
0
{
10945
  /* There are 2 variations for LONGJUMP5
10946
     case 2-4;  1st insn convertible, 16-bit on.
10947
     bnes38  rt, ra, .L1  ; LONGJUMP5/9_PCREL/INSN16
10948
     j       label    ; 25_PCREL/INSN16
10949
     $1:
10950
10951
     case 4-4; 1st insn not convertible
10952
     bne  rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
10953
     j    label   ; 25_PCREL/INSN16
10954
     .L1:  */
10955
10956
0
  bfd_vma laddr;
10957
0
  Elf_Internal_Rela *cond_irel,  *irelend;
10958
0
  unsigned int i;
10959
0
  bfd_signed_vma foff;
10960
0
  uint32_t insn, re_insn = 0;
10961
0
  uint16_t insn16, re_insn16 = 0;
10962
0
  unsigned long reloc;
10963
10964
0
  enum elf_nds32_reloc_type checked_types[] =
10965
0
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
10966
0
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
10967
10968
0
  irelend = internal_relocs + sec->reloc_count;
10969
0
  laddr = irel->r_offset;
10970
10971
  /* Get the reloc for the address from which the register is
10972
     being loaded.  This reloc will tell us which function is
10973
     actually being called.  */
10974
10975
0
  cond_irel =
10976
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
10977
0
         R_NDS32_25_PCREL_RELA, irel->r_addend);
10978
0
  if (cond_irel == irelend)
10979
0
    {
10980
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP5",
10981
0
        (uint64_t) irel->r_offset);
10982
0
      return false;
10983
0
    }
10984
10985
  /* Get the value of the symbol referred to by the reloc.  */
10986
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
10987
10988
0
  if (foff == 0
10989
0
      || foff < -CONSERVATIVE_16BIT_S1
10990
0
      || foff >= CONSERVATIVE_16BIT_S1)
10991
0
    return false;
10992
10993
  /* Get the all corresponding instructions.  */
10994
0
  insn = bfd_getb32 (contents + laddr);
10995
  /* Check instruction size.  */
10996
0
  if (insn & 0x80000000)
10997
0
    {
10998
0
      *seq_len = 0;
10999
0
      insn16 = insn >> 16;
11000
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11001
0
    }
11002
0
  else
11003
0
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11004
11005
0
  if (N32_OP6 (re_insn) == N32_OP6_BR1
11006
0
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11007
0
    {
11008
      /* beqs label ; 15_PCREL.  */
11009
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11010
0
      reloc = R_NDS32_15_PCREL_RELA;
11011
0
    }
11012
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
11013
0
     && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11014
0
    {
11015
      /* beqz label ; 17_PCREL.  */
11016
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11017
0
      reloc = R_NDS32_17_PCREL_RELA;
11018
0
    }
11019
0
  else if ( N32_OP6 (re_insn) == N32_OP6_BR3
11020
0
     && foff >= -CONSERVATIVE_8BIT_S1 && foff < CONSERVATIVE_8BIT_S1)
11021
0
    {
11022
      /* beqc label ; 9_PCREL.  */
11023
0
      bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11024
0
      reloc = R_NDS32_WORD_9_PCREL_RELA;
11025
0
    }
11026
0
  else
11027
0
    return false;
11028
11029
  /* Set all relocations.  */
11030
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), reloc);
11031
11032
  /* Clean relocations.  */
11033
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11034
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11035
0
    {
11036
0
      cond_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11037
0
                 checked_types[i], laddr);
11038
0
      if (cond_irel != irelend)
11039
0
  {
11040
0
    if (*seq_len == 0
11041
0
        && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11042
0
      {
11043
        /* If the branch instruction is 2 byte, it cannot remove
11044
     directly.  Only convert it to nop16 and remove it after
11045
     checking alignment issue.  */
11046
0
        insn16 = NDS32_NOP16;
11047
0
        bfd_putb16 (insn16, contents + laddr);
11048
0
        cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11049
0
      }
11050
0
    else
11051
0
      cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11052
0
                R_NDS32_NONE);
11053
0
  }
11054
0
    }
11055
0
  *insn_len = 0;
11056
11057
0
  return true;
11058
0
}
11059
11060
/* Relax LONGJUMP6 relocation for nds32_elf_relax_section.  */
11061
11062
static bool
11063
nds32_elf_relax_longjump6 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11064
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11065
         int *seq_len, bfd_byte *contents,
11066
         Elf_Internal_Sym *isymbuf,
11067
         Elf_Internal_Shdr *symtab_hdr)
11068
0
{
11069
  /* There are 5 variations for LONGJUMP6
11070
     case : 2-4-4-4; 1st insn convertible, 16-bit on.
11071
     bnes38   rt, ra, .L1   ; LONGJUMP6/15_PCREL/INSN16
11072
     sethi    ta, hi20(symbol)    ; HI20/PTR
11073
     ori      ta, ta, lo12(symbol)  ; LO12S0_ORI/PTR
11074
     jr       ta      ; PTR_RES/INSN16/EMPTY
11075
     .L1:
11076
11077
     case : 4-4-4-4; 1st insn not convertible, 16-bit on.
11078
     bne   rt, ra, .L1    ; LONGJUMP6/15_PCREL/INSN16
11079
     sethi ta, hi20(symbol) ; HI20/PTR
11080
     ori   ta, ta, lo12(symbol) ; LO12S0_ORI/PTR
11081
     jr    ta     ; PTR_RES/INSN16/EMPTY
11082
     .L1:  */
11083
11084
0
  enum elf_nds32_reloc_type checked_types[] =
11085
0
    { R_NDS32_17_PCREL_RELA, R_NDS32_15_PCREL_RELA,
11086
0
      R_NDS32_9_PCREL_RELA, R_NDS32_INSN16 };
11087
11088
0
  int reloc_off = 0, cond_removed = 0;
11089
0
  bfd_vma laddr;
11090
0
  Elf_Internal_Rela *cond_irel, *em_irel, *irelend, *insn_irel;
11091
0
  unsigned int i;
11092
0
  bfd_signed_vma foff;
11093
0
  uint32_t insn, re_insn = 0;
11094
0
  uint16_t insn16, re_insn16 = 0;
11095
0
  unsigned long reloc;
11096
11097
0
  irelend = internal_relocs + sec->reloc_count;
11098
0
  laddr = irel->r_offset;
11099
11100
  /* Get the reloc for the address from which the register is
11101
     being loaded.  This reloc will tell us which function is
11102
     actually being called.  */
11103
0
  em_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11104
0
           R_NDS32_EMPTY, irel->r_addend);
11105
11106
0
  if (em_irel == irelend)
11107
0
    {
11108
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP6",
11109
0
        (uint64_t) irel->r_offset);
11110
0
      return false;
11111
0
    }
11112
11113
  /* Get the value of the symbol referred to by the reloc.  */
11114
0
  foff = calculate_offset (abfd, sec, em_irel, isymbuf, symtab_hdr);
11115
11116
0
  if (foff == 0
11117
0
      || foff < -CONSERVATIVE_24BIT_S1
11118
0
      || foff >= CONSERVATIVE_24BIT_S1)
11119
0
    return false;
11120
11121
0
  insn = bfd_getb32 (contents + laddr);
11122
  /* Check instruction size.  */
11123
0
  if (insn & 0x80000000)
11124
0
    {
11125
0
      *seq_len = 0;
11126
0
      insn16 = insn >> 16;
11127
0
      nds32_elf_convert_branch (insn16, 0, &re_insn16, &re_insn);
11128
0
    }
11129
0
  else
11130
0
    nds32_elf_convert_branch (0, insn, &re_insn16, &re_insn);
11131
11132
  /* For simplicity of coding, we are going to modify the section
11133
     contents, the section relocs, and the BFD symbol table.  We
11134
     must tell the rest of the code not to free up this
11135
     information.  It would be possible to instead create a table
11136
     of changes which have to be made, as is done in coff-mips.c;
11137
     that would be more work, but would require less memory when
11138
     the linker is run.  */
11139
11140
0
  if (N32_OP6 (re_insn) == N32_OP6_BR1
11141
0
      && (foff >= -CONSERVATIVE_14BIT_S1 && foff < CONSERVATIVE_14BIT_S1))
11142
0
    {
11143
      /* beqs     label    ; 15_PCREL.  */
11144
0
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
11145
0
      reloc = R_NDS32_15_PCREL_RELA;
11146
0
      cond_removed = 1;
11147
0
    }
11148
0
  else if (N32_OP6 (re_insn) == N32_OP6_BR2
11149
0
     && foff >= -CONSERVATIVE_16BIT_S1 && foff < CONSERVATIVE_16BIT_S1)
11150
0
    {
11151
      /* beqz     label ; 17_PCREL.  */
11152
0
      bfd_putb32 (re_insn, contents + em_irel->r_offset);
11153
0
      reloc = R_NDS32_17_PCREL_RELA;
11154
0
      cond_removed = 1;
11155
0
    }
11156
0
  else if (foff >= -CONSERVATIVE_24BIT_S1 - reloc_off
11157
0
     && foff < CONSERVATIVE_24BIT_S1 - reloc_off)
11158
0
    {
11159
      /* Relax to one of the following 2 variations
11160
11161
   case 2-4;  1st insn convertible, 16-bit on.
11162
   bnes38  rt, ra, .L1  ; LONGJUMP5/9_PCREL/INSN16
11163
   j       label    ; 25_PCREL/INSN16
11164
   $1:
11165
11166
   case 4-4; 1st insn not convertible
11167
   bne  rt, ra, .L1 ; LONGJUMP5/15_PCREL/INSN16
11168
   j    label   ; 25_PCREL/INSN16
11169
   .L1:  */
11170
11171
      /* Use j label as second instruction.  */
11172
0
      insn = INSN_J;
11173
0
      reloc = R_NDS32_25_PCREL_RELA;
11174
0
      bfd_putb32 (insn, contents + em_irel->r_offset);
11175
0
    }
11176
0
  else
11177
0
    return false;
11178
11179
  /* Set all relocations.  */
11180
0
  em_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (em_irel->r_info), reloc);
11181
11182
0
  cond_irel =
11183
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11184
0
         R_NDS32_PTR_RESOLVED, em_irel->r_offset);
11185
0
  cond_irel->r_addend = 1;
11186
11187
  /* Use INSN16 of first branch instruction to distinguish if keeping
11188
     INSN16 of final instruction or not.  */
11189
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11190
0
             R_NDS32_INSN16, irel->r_offset);
11191
0
  if (insn_irel == irelend)
11192
0
    {
11193
      /* Clean the final INSN16.  */
11194
0
      insn_irel =
11195
0
  find_relocs_at_address_addr (irel, internal_relocs, irelend,
11196
0
             R_NDS32_INSN16, em_irel->r_offset);
11197
0
      insn_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11198
0
          R_NDS32_NONE);
11199
0
    }
11200
11201
0
  if (cond_removed == 1)
11202
0
    {
11203
0
      *insn_len = 0;
11204
11205
      /* Clear relocations.  */
11206
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11207
11208
0
      for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11209
0
  {
11210
0
    cond_irel =
11211
0
      find_relocs_at_address_addr (irel, internal_relocs, irelend,
11212
0
           checked_types[i], laddr);
11213
0
    if (cond_irel != irelend)
11214
0
      {
11215
0
        if (*seq_len == 0
11216
0
      && (ELF32_R_TYPE (cond_irel->r_info) == R_NDS32_INSN16))
11217
0
    {
11218
      /* If the branch instruction is 2 byte, it cannot remove
11219
         directly.  Only convert it to nop16 and remove it after
11220
         checking alignment issue.  */
11221
0
      insn16 = NDS32_NOP16;
11222
0
      bfd_putb16 (insn16, contents + laddr);
11223
0
      cond_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11224
0
    }
11225
0
        else
11226
0
    cond_irel->r_info =
11227
0
      ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info), R_NDS32_NONE);
11228
0
      }
11229
0
  }
11230
0
    }
11231
0
  else
11232
0
    {
11233
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11234
0
           R_NDS32_LONGJUMP5);
11235
0
    }
11236
11237
0
  return true;
11238
0
}
11239
11240
/* Relax LONGJUMP7 relocation for nds32_elf_relax_section.  */
11241
11242
static bool
11243
nds32_elf_relax_longjump7 (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11244
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11245
         int *seq_len, bfd_byte *contents,
11246
         Elf_Internal_Sym *isymbuf,
11247
         Elf_Internal_Shdr *symtab_hdr)
11248
0
{
11249
  /* There are 2 variations for LONGJUMP5
11250
     case 2-4;  1st insn convertible, 16-bit on.
11251
     movi55  ta, imm11    ; LONGJUMP7/INSN16
11252
     beq     rt, ta, label  ; 15_PCREL
11253
11254
     case 4-4; 1st insn not convertible
11255
     movi55  ta, imm11    ; LONGJUMP7/INSN16
11256
     beq     rt, ta, label  ; 15_PCREL  */
11257
11258
0
  bfd_vma laddr;
11259
0
  Elf_Internal_Rela *cond_irel,  *irelend, *insn_irel;
11260
0
  bfd_signed_vma foff;
11261
0
  uint32_t insn, re_insn = 0;
11262
0
  uint16_t insn16;
11263
0
  uint32_t imm11;
11264
11265
0
  irelend = internal_relocs + sec->reloc_count;
11266
0
  laddr = irel->r_offset;
11267
11268
  /* Get the reloc for the address from which the register is
11269
     being loaded.  This reloc will tell us which function is
11270
     actually being called.  */
11271
11272
0
  cond_irel =
11273
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11274
0
         R_NDS32_15_PCREL_RELA, irel->r_addend);
11275
0
  if (cond_irel == irelend)
11276
0
    {
11277
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LONGJUMP7",
11278
0
        (uint64_t) irel->r_offset);
11279
0
      return false;
11280
0
    }
11281
11282
  /* Get the value of the symbol referred to by the reloc.  */
11283
0
  foff = calculate_offset (abfd, sec, cond_irel, isymbuf, symtab_hdr);
11284
11285
0
  if (foff == 0
11286
0
      || foff < -CONSERVATIVE_8BIT_S1
11287
0
      || foff >= CONSERVATIVE_8BIT_S1)
11288
0
    return false;
11289
11290
  /* Get the first instruction for its size.  */
11291
0
  insn = bfd_getb32 (contents + laddr);
11292
0
  if (insn & 0x80000000)
11293
0
    {
11294
0
      *seq_len = 0;
11295
      /* Get the immediate from movi55.  */
11296
0
      imm11 = N16_IMM5S (insn >> 16);
11297
0
    }
11298
0
  else
11299
0
    {
11300
      /* Get the immediate from movi.  */
11301
0
      imm11 = N32_IMM20S (insn);
11302
0
    }
11303
11304
  /* Get the branch instruction.  */
11305
0
  insn = bfd_getb32 (contents + irel->r_addend);
11306
  /* Convert instruction to BR3.  */
11307
0
  if ((insn >> 14) & 0x1)
11308
0
    re_insn = N32_BR3 (BNEC, N32_RT5 (insn), imm11, 0);
11309
0
  else
11310
0
    re_insn = N32_BR3 (BEQC, N32_RT5 (insn), imm11, 0);
11311
11312
0
  bfd_putb32 (re_insn, contents + cond_irel->r_offset);
11313
11314
  /* Set all relocations.  */
11315
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11316
0
            R_NDS32_WORD_9_PCREL_RELA);
11317
11318
  /* Clean relocations.  */
11319
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11320
0
  insn_irel = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11321
0
             R_NDS32_INSN16, irel->r_offset);
11322
0
  if (insn_irel != irelend)
11323
0
    {
11324
0
      if (*seq_len == 0)
11325
0
  {
11326
    /* If the first insntruction is 16bit, convert it to nop16.  */
11327
0
    insn16 = NDS32_NOP16;
11328
0
    bfd_putb16 (insn16, contents + laddr);
11329
0
    insn_irel->r_addend = R_NDS32_INSN16_CONVERT_FLAG;
11330
0
  }
11331
0
      else
11332
0
  cond_irel->r_info = ELF32_R_INFO (ELF32_R_SYM (cond_irel->r_info),
11333
0
            R_NDS32_NONE);
11334
0
    }
11335
0
  *insn_len = 0;
11336
11337
0
  return true;
11338
0
}
11339
11340
/* We figure out and reassign the best gp value in nds32_elf_final_sda_base
11341
   for each relax round. But the gp may changed dramatically and then cause
11342
   the truncated to fit errors for the the converted gp instructions.
11343
   Therefore, we must reserve the minimum but safe enough size to prevent it.  */
11344
11345
static bool
11346
nds32_elf_relax_guard (bfd_vma *access_addr, bfd_vma local_sda, asection *sec,
11347
           Elf_Internal_Rela *irel, bool *again,
11348
           bool init,
11349
           struct elf_nds32_link_hash_table *table,
11350
           Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr)
11351
11352
0
{
11353
0
  int offset_to_gp;
11354
0
  static bool sec_pass = false;
11355
0
  static asection *first_sec = NULL, *sym_sec;
11356
  /* Record the number of instructions which may be removed.  */
11357
0
  static int count = 0, record_count;
11358
0
  Elf_Internal_Sym *isym;
11359
0
  struct elf_link_hash_entry *h = NULL;
11360
0
  int indx;
11361
0
  unsigned long r_symndx;
11362
0
  bfd *abfd = sec->owner;
11363
0
  static bfd_vma record_sda = 0;
11364
0
  int sda_offset = 0;
11365
11366
  /* Force doing relaxation when hyper-relax is high.  */
11367
0
  if (table->hyper_relax == 2)
11368
0
    return true;
11369
11370
  /* Do not relax the load/store patterns for the first
11371
     relax round.  */
11372
0
  if (init)
11373
0
    {
11374
0
      if (!first_sec)
11375
0
  first_sec = sec;
11376
0
      else if (first_sec == sec)
11377
0
  {
11378
0
    record_count = count;
11379
0
    count = 0;
11380
0
    sec_pass = true;
11381
0
  }
11382
11383
0
      if (!sec_pass)
11384
0
  *again = true;
11385
11386
0
      return true;
11387
0
    }
11388
11389
  /* Generally, _SDA_BASE_ is fixed or smaller. But the large
11390
     DATA_SEGMENT_ALIGN size in the linker script may make it
11391
     get even bigger.  */
11392
0
  if (record_sda == 0)
11393
0
    record_sda = local_sda;
11394
0
  else if (local_sda > record_sda)
11395
0
    sda_offset = local_sda - record_sda;
11396
11397
  /* Assume the instruction will be removed in the best case.  */
11398
0
  count++;
11399
11400
  /* We record the offset to gp for each symbol, and then check
11401
     if it is changed dramatically after relaxing.
11402
     (global symbol): elf32_nds32_hash_entry (h)->offset_to_gp
11403
     (local symbol) : elf32_nds32_local_gp_offset (abfd)[r_symndx].  */
11404
0
  r_symndx = ELF32_R_SYM (irel->r_info);
11405
0
  if (r_symndx >= symtab_hdr->sh_info)
11406
0
    {
11407
      /* Global symbols.  */
11408
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11409
0
      h = elf_sym_hashes (abfd)[indx];
11410
0
      sym_sec = h->root.u.def.section;
11411
0
      if (NDS32_GUARD_SEC_P (sym_sec->flags)
11412
0
    || bfd_is_abs_section (sym_sec))
11413
0
  {
11414
    /* Forbid doing relaxation when hyper-relax is low.  */
11415
0
    if (table->hyper_relax == 0)
11416
0
      return false;
11417
11418
0
    offset_to_gp = *access_addr - local_sda;
11419
0
    if (elf32_nds32_hash_entry (h)->offset_to_gp == 0)
11420
0
      elf32_nds32_hash_entry (h)->offset_to_gp = offset_to_gp;
11421
0
    else if (abs (elf32_nds32_hash_entry (h)->offset_to_gp)
11422
0
       < abs (offset_to_gp) - sda_offset)
11423
0
      {
11424
        /* This may cause the error, so we reserve the
11425
     safe enough size for relaxing.  */
11426
0
        if (*access_addr >= local_sda)
11427
0
    *access_addr += (record_count * 4);
11428
0
        else
11429
0
    *access_addr -= (record_count * 4);
11430
0
      }
11431
0
    return sec_pass;
11432
0
  }
11433
0
    }
11434
0
  else
11435
0
    {
11436
      /* Local symbols.  */
11437
0
      if (!elf32_nds32_allocate_local_sym_info (abfd))
11438
0
  return false;
11439
0
      isym = isymbuf + r_symndx;
11440
11441
0
      sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
11442
0
      if (NDS32_GUARD_SEC_P (sym_sec->flags))
11443
0
  {
11444
    /* Forbid doing relaxation when hyper-relax is low.  */
11445
0
    if (table->hyper_relax == 0)
11446
0
      return false;
11447
11448
0
    offset_to_gp = *access_addr - local_sda;
11449
0
    if (elf32_nds32_local_gp_offset (abfd)[r_symndx] == 0)
11450
0
      elf32_nds32_local_gp_offset (abfd)[r_symndx] = offset_to_gp;
11451
0
    else if (abs (elf32_nds32_local_gp_offset (abfd)[r_symndx])
11452
0
       < abs (offset_to_gp) - sda_offset)
11453
0
      {
11454
        /* This may cause the error, so we reserve the
11455
     safe enough size for relaxing.  */
11456
0
        if (*access_addr >= local_sda)
11457
0
    *access_addr += (record_count * 4);
11458
0
        else
11459
0
    *access_addr -= (record_count * 4);
11460
0
      }
11461
0
    return sec_pass;
11462
0
  }
11463
0
    }
11464
11465
0
  return true;
11466
0
}
11467
11468
0
#define GET_LOADSTORE_RANGE(addend) (((addend) >> 8) & 0x3f)
11469
11470
/* Relax LOADSTORE relocation for nds32_elf_relax_section.  */
11471
11472
static bool
11473
nds32_elf_relax_loadstore (struct bfd_link_info *link_info, bfd *abfd,
11474
         asection *sec, Elf_Internal_Rela *irel,
11475
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11476
         bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11477
         Elf_Internal_Shdr *symtab_hdr, int load_store_relax,
11478
         struct elf_nds32_link_hash_table *table)
11479
0
{
11480
0
  int eliminate_sethi = 0, range_type;
11481
0
  unsigned int i;
11482
0
  bfd_vma local_sda, laddr;
11483
0
  int seq_len;  /* Original length of instruction sequence.  */
11484
0
  uint32_t insn;
11485
0
  Elf_Internal_Rela *hi_irelfn = NULL, *irelend;
11486
0
  bfd_vma access_addr = 0;
11487
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11488
0
  struct elf_link_hash_entry *h = NULL;
11489
0
  int indx;
11490
0
  enum elf_nds32_reloc_type checked_types[] =
11491
0
    { R_NDS32_HI20_RELA, R_NDS32_GOT_HI20,
11492
0
      R_NDS32_GOTPC_HI20, R_NDS32_GOTOFF_HI20,
11493
0
      R_NDS32_PLTREL_HI20, R_NDS32_PLT_GOTREL_HI20,
11494
0
      R_NDS32_TLS_LE_HI20
11495
0
    };
11496
11497
0
  irelend = internal_relocs + sec->reloc_count;
11498
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
11499
0
  laddr = irel->r_offset;
11500
0
  *insn_len = seq_len;
11501
11502
  /* Get the high part relocation.  */
11503
0
  for (i = 0; i < ARRAY_SIZE (checked_types); i++)
11504
0
    {
11505
0
      hi_irelfn = find_relocs_at_address_addr (irel, internal_relocs, irelend,
11506
0
                 checked_types[i], laddr);
11507
0
      if (hi_irelfn != irelend)
11508
0
  break;
11509
0
    }
11510
11511
0
  if (hi_irelfn == irelend)
11512
0
    {
11513
      /* Not R_NDS32_HI20_RELA.  */
11514
0
      if (i != 0)
11515
0
  _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LOADSTORE",
11516
0
          (uint64_t) irel->r_offset);
11517
0
      return false;
11518
0
    }
11519
11520
0
  range_type = GET_LOADSTORE_RANGE (irel->r_addend);
11521
0
  nds32_elf_final_sda_base (sec->output_section->owner,
11522
0
          link_info, &local_sda, false);
11523
11524
0
  switch (ELF32_R_TYPE (hi_irelfn->r_info))
11525
0
    {
11526
0
    case R_NDS32_HI20_RELA:
11527
0
      insn = bfd_getb32 (contents + laddr);
11528
0
      access_addr =
11529
0
  calculate_memory_address (abfd, hi_irelfn, isymbuf, symtab_hdr);
11530
11531
0
      if (ELF32_R_SYM (hi_irelfn->r_info) >= symtab_hdr->sh_info)
11532
0
  {
11533
0
    indx = ELF32_R_SYM (hi_irelfn->r_info) - symtab_hdr->sh_info;
11534
0
    h = elf_sym_hashes (abfd)[indx];
11535
0
  }
11536
11537
      /* Try movi.  */
11538
0
      if (range_type == NDS32_LOADSTORE_IMM
11539
0
    && access_addr < CONSERVATIVE_20BIT
11540
0
    && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11541
0
  {
11542
0
    eliminate_sethi = 1;
11543
0
    break;
11544
0
  }
11545
11546
0
      if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11547
0
  {
11548
0
    eliminate_sethi = 1;
11549
0
    break;
11550
0
  }
11551
0
      else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, hi_irelfn,
11552
0
               NULL, false, table, isymbuf, symtab_hdr))
11553
0
  return false;
11554
11555
0
      if (!load_store_relax)
11556
0
  return false;
11557
11558
      /* Case for set gp register.  */
11559
0
      if (N32_RT5 (insn) == REG_GP)
11560
0
  return false;
11561
11562
0
      if (range_type == NDS32_LOADSTORE_FLOAT_S
11563
0
    || range_type == NDS32_LOADSTORE_FLOAT_D)
11564
0
  {
11565
0
    range_l = sdata_range[0][0];
11566
0
    range_h = sdata_range[0][1];
11567
0
  }
11568
0
      else
11569
0
  {
11570
0
    range_l = sdata_range[1][0];
11571
0
    range_h = sdata_range[1][1];
11572
0
  }
11573
0
      break;
11574
11575
0
    default:
11576
0
      return false;
11577
0
    }
11578
11579
  /* Delete sethi instruction.  */
11580
0
  if (eliminate_sethi == 1
11581
0
      || (local_sda <= access_addr && (access_addr - local_sda) < range_h)
11582
0
      || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11583
0
    {
11584
0
      hi_irelfn->r_info =
11585
0
  ELF32_R_INFO (ELF32_R_SYM (hi_irelfn->r_info), R_NDS32_NONE);
11586
0
      irel->r_info =
11587
0
  ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11588
0
      *insn_len = 0;
11589
0
      return true;
11590
0
    }
11591
11592
0
  return false;
11593
0
}
11594
11595
/* Relax LO12 relocation for nds32_elf_relax_section.  */
11596
11597
static void
11598
nds32_elf_relax_lo12 (struct bfd_link_info *link_info, bfd *abfd,
11599
          asection *sec, Elf_Internal_Rela *irel,
11600
          Elf_Internal_Rela *internal_relocs, bfd_byte *contents,
11601
          Elf_Internal_Sym *isymbuf, Elf_Internal_Shdr *symtab_hdr,
11602
          struct elf_nds32_link_hash_table *table)
11603
0
{
11604
0
  uint32_t insn;
11605
0
  bfd_vma local_sda, laddr;
11606
0
  unsigned long reloc;
11607
0
  bfd_vma access_addr;
11608
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11609
0
  Elf_Internal_Rela *irelfn = NULL, *irelend;
11610
0
  struct elf_link_hash_entry *h = NULL;
11611
0
  int indx;
11612
11613
  /* For SDA base relative relaxation.  */
11614
0
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11615
0
          &local_sda, false);
11616
11617
0
  irelend = internal_relocs + sec->reloc_count;
11618
0
  laddr = irel->r_offset;
11619
0
  insn = bfd_getb32 (contents + laddr);
11620
11621
0
  if (!is_sda_access_insn (insn) && N32_OP6 (insn) != N32_OP6_ORI)
11622
0
    return;
11623
11624
0
  access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11625
11626
0
  if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
11627
0
    {
11628
0
      indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
11629
0
      h = elf_sym_hashes (abfd)[indx];
11630
0
    }
11631
11632
  /* Try movi.  */
11633
0
  if (N32_OP6 (insn) == N32_OP6_ORI && access_addr < CONSERVATIVE_20BIT
11634
0
      && (!h || (h && strcmp (h->root.root.string, FP_BASE_NAME) != 0)))
11635
0
    {
11636
0
      reloc = R_NDS32_20_RELA;
11637
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11638
0
      insn = N32_TYPE1 (MOVI, N32_RT5 (insn), 0);
11639
0
      bfd_putb32 (insn, contents + laddr);
11640
0
    }
11641
0
  else
11642
0
    {
11643
0
      if (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0)
11644
0
  {
11645
    /* Fall through.  */
11646
0
  }
11647
0
      else if (!nds32_elf_relax_guard (&access_addr, local_sda, sec, irel, NULL,
11648
0
               false, table, isymbuf, symtab_hdr))
11649
0
  return;
11650
11651
0
      range_l = sdata_range[1][0];
11652
0
      range_h = sdata_range[1][1];
11653
0
      switch (ELF32_R_TYPE (irel->r_info))
11654
0
  {
11655
0
  case R_NDS32_LO12S0_RELA:
11656
0
    reloc = R_NDS32_SDA19S0_RELA;
11657
0
    break;
11658
0
  case R_NDS32_LO12S1_RELA:
11659
0
    reloc = R_NDS32_SDA18S1_RELA;
11660
0
    break;
11661
0
  case R_NDS32_LO12S2_RELA:
11662
0
    reloc = R_NDS32_SDA17S2_RELA;
11663
0
    break;
11664
0
  case R_NDS32_LO12S2_DP_RELA:
11665
0
    range_l = sdata_range[0][0];
11666
0
    range_h = sdata_range[0][1];
11667
0
    reloc = R_NDS32_SDA12S2_DP_RELA;
11668
0
    break;
11669
0
  case R_NDS32_LO12S2_SP_RELA:
11670
0
    range_l = sdata_range[0][0];
11671
0
    range_h = sdata_range[0][1];
11672
0
    reloc = R_NDS32_SDA12S2_SP_RELA;
11673
0
    break;
11674
0
  default:
11675
0
    return;
11676
0
  }
11677
11678
      /* There are range_h and range_l because linker has to promise
11679
   all sections move cross one page together.  */
11680
0
      if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11681
0
    || (local_sda > access_addr && (local_sda - access_addr) <= range_l)
11682
0
    || (h && strcmp (h->root.root.string, FP_BASE_NAME) == 0))
11683
0
  {
11684
0
    if (N32_OP6 (insn) == N32_OP6_ORI && N32_RT5 (insn) == REG_GP)
11685
0
      {
11686
        /* Maybe we should add R_NDS32_INSN16 reloc type here
11687
     or manually do some optimization.  sethi can't be
11688
     eliminated when updating $gp so the relative ori
11689
     needs to be preserved.  */
11690
0
        return;
11691
0
      }
11692
0
    if (!turn_insn_to_sda_access (insn, ELF32_R_TYPE (irel->r_info),
11693
0
          &insn))
11694
0
      return;
11695
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11696
0
    bfd_putb32 (insn, contents + laddr);
11697
11698
0
    irelfn = find_relocs_at_address (irel, internal_relocs, irelend,
11699
0
             R_NDS32_INSN16);
11700
    /* SDA17 must keep INSN16 for converting fp_as_gp.  */
11701
0
    if (irelfn != irelend && reloc != R_NDS32_SDA17S2_RELA)
11702
0
      irelfn->r_info =
11703
0
        ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_NDS32_NONE);
11704
11705
0
  }
11706
0
    }
11707
0
  return;
11708
0
}
11709
11710
/* Relax PTR relocation for nds32_elf_relax_section.  */
11711
11712
static bool
11713
nds32_elf_relax_ptr (bfd *abfd, asection *sec, Elf_Internal_Rela *irel,
11714
         Elf_Internal_Rela *internal_relocs, int *insn_len,
11715
         int *seq_len, bfd_byte *contents)
11716
0
{
11717
0
  Elf_Internal_Rela *ptr_irel, *irelend, *count_irel, *re_irel;
11718
11719
0
  irelend = internal_relocs + sec->reloc_count;
11720
11721
0
  re_irel =
11722
0
    find_relocs_at_address_addr (irel, internal_relocs, irelend,
11723
0
         R_NDS32_PTR_RESOLVED, irel->r_addend);
11724
11725
0
  if (re_irel == irelend)
11726
0
    {
11727
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_PTR",
11728
0
        (uint64_t) irel->r_offset);
11729
0
      return false;
11730
0
    }
11731
11732
0
  if (re_irel->r_addend != 1)
11733
0
    return false;
11734
11735
  /* Pointed target is relaxed and no longer needs this void *,
11736
     change the type to NONE.  */
11737
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
11738
11739
  /* Find PTR_COUNT to decide remove it or not.  If PTR_COUNT does
11740
     not exist, it means only count 1 and remove it directly.  */
11741
  /* TODO: I hope we can obsolate R_NDS32_COUNT in the future.  */
11742
0
  count_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11743
0
               R_NDS32_PTR_COUNT);
11744
0
  ptr_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11745
0
             R_NDS32_PTR);
11746
0
  if (count_irel != irelend)
11747
0
    {
11748
0
      if (--count_irel->r_addend > 0)
11749
0
  return false;
11750
0
    }
11751
11752
0
  if (ptr_irel != irelend)
11753
0
    return false;
11754
11755
  /* If the PTR_COUNT is already 0, remove current instruction.  */
11756
0
  *seq_len = nds32_elf_insn_size (abfd, contents, irel->r_offset);
11757
0
  *insn_len = 0;
11758
0
  return true;
11759
0
}
11760
11761
/* Relax LWC relocation for nds32_elf_relax_section.  */
11762
11763
static void
11764
nds32_elf_relax_flsi (struct bfd_link_info *link_info, bfd *abfd,
11765
          asection *sec, Elf_Internal_Rela *irel,
11766
          Elf_Internal_Rela *internal_relocs,
11767
          bfd_byte *contents, Elf_Internal_Sym *isymbuf,
11768
          Elf_Internal_Shdr *symtab_hdr, bool *again)
11769
0
{
11770
  /* Pattern:
11771
     sethi    ra, hi20(symbol)      ; HI20/LOADSTORE
11772
     ori      ra, ra, lo12(symbol)  ; LO12S0/PTR/PTR/.../INSN16
11773
     flsi     fsa, [ra + offset1]   ; LSI/PTR_RESOLVED/INSN16
11774
     flsi     fsb, [ra + offset2]   ; LSI/PTR_RESOLVED/INSN16
11775
     ...  */
11776
11777
0
  uint32_t insn;
11778
0
  bfd_vma local_sda, laddr;
11779
0
  unsigned long reloc;
11780
0
  bfd_vma access_addr, flsi_offset;
11781
0
  bfd_vma range_l = 0, range_h = 0; /* Upper/lower bound.  */
11782
0
  Elf_Internal_Rela *irelend, *re_irel;
11783
0
  unsigned int opcode;
11784
11785
0
  irelend = internal_relocs + sec->reloc_count;
11786
0
  laddr = irel->r_offset;
11787
0
  insn = bfd_getb32 (contents + laddr);
11788
11789
0
  if ((insn & 0x80000000) || !is_sda_access_insn (insn))
11790
0
    return;
11791
11792
  /* Can not do relaxation for bi format.  */
11793
0
  if ((insn & 0x1000))
11794
0
    return;
11795
11796
  /* Only deal with flsi, fssi, fldi, fsdi, so far.  */
11797
0
  opcode = N32_OP6 (insn);
11798
0
  if ((opcode == N32_OP6_LWC) || (opcode == N32_OP6_SWC))
11799
0
    reloc = R_NDS32_SDA12S2_SP_RELA;
11800
0
  else if ((opcode == N32_OP6_LDC) || (opcode == N32_OP6_SDC))
11801
0
    reloc = R_NDS32_SDA12S2_DP_RELA;
11802
0
  else
11803
0
    return;
11804
11805
0
  re_irel = find_relocs_at_address (irel, internal_relocs, irelend,
11806
0
            R_NDS32_PTR_RESOLVED);
11807
0
  if (re_irel == irelend)
11808
0
    {
11809
0
      _bfd_error_handler (unrecognized_reloc_msg, abfd, "R_NDS32_LSI",
11810
0
        (uint64_t) irel->r_offset);
11811
0
      return;
11812
0
    }
11813
11814
  /* For SDA base relative relaxation.  */
11815
0
  nds32_elf_final_sda_base (sec->output_section->owner, link_info,
11816
0
          &local_sda, false);
11817
0
  access_addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
11818
0
  flsi_offset = (insn & 0xfff) << 2;
11819
0
  access_addr += flsi_offset;
11820
0
  range_l = sdata_range[0][0];
11821
0
  range_h = sdata_range[0][1];
11822
11823
0
  if ((local_sda <= access_addr && (access_addr - local_sda) < range_h)
11824
0
      || (local_sda > access_addr && (local_sda - access_addr) <= range_l))
11825
0
    {
11826
      /* Turn flsi instruction into sda access format.  */
11827
0
      insn = (insn & 0x7ff07000) | (REG_GP << 15);
11828
11829
      /* Add relocation type to flsi.  */
11830
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), reloc);
11831
0
      irel->r_addend += flsi_offset;
11832
0
      bfd_putb32 (insn, contents + re_irel->r_offset);
11833
11834
0
      re_irel->r_addend |= 1;
11835
0
      *again = true;
11836
0
    }
11837
0
}
11838
11839
static bool
11840
nds32_relax_adjust_label (bfd *abfd, asection *sec,
11841
        Elf_Internal_Rela *internal_relocs,
11842
        bfd_byte *contents,
11843
        nds32_elf_blank_t **relax_blank_list,
11844
        int optimize, int opt_size)
11845
0
{
11846
  /* This code block is used to adjust 4-byte alignment by relax a pair
11847
     of instruction a time.
11848
11849
     It recognizes three types of relocations.
11850
     1. R_NDS32_LABEL - a alignment.
11851
     2. R_NDS32_INSN16 - relax a 32-bit instruction to 16-bit.
11852
     3. is_16bit_NOP () - remove a 16-bit instruction.  */
11853
11854
  /* TODO: It seems currently implementation only support 4-byte alignment.
11855
     We should handle any-alignment.  */
11856
11857
0
  Elf_Internal_Rela *insn_rel = NULL, *label_rel = NULL, *irel;
11858
0
  Elf_Internal_Rela *tmp_rel, *tmp2_rel = NULL;
11859
0
  Elf_Internal_Rela rel_temp;
11860
0
  Elf_Internal_Rela *irelend;
11861
0
  bfd_vma address;
11862
0
  uint16_t insn16;
11863
11864
  /* Checking for branch relaxation relies on the relocations to
11865
     be sorted on 'r_offset'.  This is not guaranteed so we must sort.  */
11866
0
  nds32_insertion_sort (internal_relocs, sec->reloc_count,
11867
0
      sizeof (Elf_Internal_Rela), compar_reloc);
11868
11869
0
  irelend = internal_relocs + sec->reloc_count;
11870
11871
  /* Force R_NDS32_LABEL before R_NDS32_INSN16.  */
11872
  /* FIXME: Can we generate the right order in assembler?
11873
     So we don't have to swapping them here.  */
11874
11875
0
  for (label_rel = internal_relocs, insn_rel = internal_relocs;
11876
0
       label_rel < irelend; label_rel++)
11877
0
    {
11878
0
      if (ELF32_R_TYPE (label_rel->r_info) != R_NDS32_LABEL)
11879
0
  continue;
11880
11881
      /* Find the first reloc has the same offset with label_rel.  */
11882
0
      while (insn_rel < irelend && insn_rel->r_offset < label_rel->r_offset)
11883
0
  insn_rel++;
11884
11885
0
      for (;insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset;
11886
0
     insn_rel++)
11887
  /* Check if there were R_NDS32_INSN16 and R_NDS32_LABEL at the same
11888
     address.  */
11889
0
  if (ELF32_R_TYPE (insn_rel->r_info) == R_NDS32_INSN16)
11890
0
    break;
11891
11892
0
      if (insn_rel < irelend && insn_rel->r_offset == label_rel->r_offset
11893
0
    && insn_rel < label_rel)
11894
0
  {
11895
    /* Swap the two reloc if the R_NDS32_INSN16 is
11896
       before R_NDS32_LABEL.  */
11897
0
    memcpy (&rel_temp, insn_rel, sizeof (Elf_Internal_Rela));
11898
0
    memcpy (insn_rel, label_rel, sizeof (Elf_Internal_Rela));
11899
0
    memcpy (label_rel, &rel_temp, sizeof (Elf_Internal_Rela));
11900
0
  }
11901
0
    }
11902
11903
0
  label_rel = NULL;
11904
0
  insn_rel = NULL;
11905
  /* If there were a sequence of R_NDS32_LABEL end up with .align 2
11906
     or higher, remove other R_NDS32_LABEL with lower alignment.
11907
     If an R_NDS32_INSN16 in between R_NDS32_LABELs must be converted,
11908
     then the R_NDS32_LABEL sequence is broke.  */
11909
0
  for (tmp_rel = internal_relocs; tmp_rel < irelend; tmp_rel++)
11910
0
    {
11911
0
      if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_LABEL)
11912
0
  {
11913
0
    if (label_rel == NULL)
11914
0
      {
11915
0
        if (tmp_rel->r_addend < 2)
11916
0
    label_rel = tmp_rel;
11917
0
        continue;
11918
0
      }
11919
0
    else if (tmp_rel->r_addend > 1)
11920
0
      {
11921
        /* Remove all LABEL relocation from label_rel to tmp_rel
11922
     including relocations with same offset as tmp_rel.  */
11923
0
        for (tmp2_rel = label_rel; tmp2_rel < tmp_rel; tmp2_rel++)
11924
0
    {
11925
0
      if (tmp2_rel->r_offset == tmp_rel->r_offset)
11926
0
        break;
11927
11928
0
      if (ELF32_R_TYPE (tmp2_rel->r_info) == R_NDS32_LABEL
11929
0
          && tmp2_rel->r_addend < 2)
11930
0
        tmp2_rel->r_info =
11931
0
          ELF32_R_INFO (ELF32_R_SYM (tmp2_rel->r_info),
11932
0
            R_NDS32_NONE);
11933
0
    }
11934
0
        label_rel = NULL;
11935
0
      }
11936
0
  }
11937
0
      else if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16 && label_rel)
11938
0
  {
11939
    /* A new INSN16 which can be converted, so clear label_rel.  */
11940
0
    if (is_convert_32_to_16 (abfd, sec, tmp_rel, internal_relocs,
11941
0
           irelend, &insn16)
11942
0
        || is_16bit_NOP (abfd, sec, tmp_rel))
11943
0
      label_rel = NULL;
11944
0
  }
11945
0
    }
11946
11947
0
  label_rel = NULL;
11948
0
  insn_rel = NULL;
11949
  /* Optimized for speed and nothing has not been relaxed.
11950
     It's time to align labels.
11951
     We may convert a 16-bit instruction right before a label to
11952
     32-bit, in order to align the label if necessary
11953
     all reloc entries has been sorted by r_offset.  */
11954
0
  for (irel = internal_relocs;
11955
0
       irel < irelend && irel->r_offset < sec->size; irel++)
11956
0
    {
11957
0
      if (ELF32_R_TYPE (irel->r_info) != R_NDS32_INSN16
11958
0
    && ELF32_R_TYPE (irel->r_info) != R_NDS32_LABEL)
11959
0
  continue;
11960
11961
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_INSN16)
11962
0
  {
11963
    /* A new INSN16 found, resize the old one.  */
11964
0
    if (is_convert_32_to_16
11965
0
        (abfd, sec, irel, internal_relocs, irelend, &insn16)
11966
0
        || is_16bit_NOP (abfd, sec, irel))
11967
0
      {
11968
0
        if (insn_rel)
11969
0
    {
11970
      /* Previous INSN16 reloc exists, reduce its
11971
         size to 16-bit.  */
11972
0
      if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
11973
0
             irelend, &insn16))
11974
0
        {
11975
0
          nds32_elf_write_16 (abfd, contents, insn_rel,
11976
0
            internal_relocs, irelend, insn16);
11977
11978
0
          if (!insert_nds32_elf_blank_recalc_total
11979
0
        (relax_blank_list, insn_rel->r_offset + 2, 2))
11980
0
      return false;
11981
0
        }
11982
0
      else if (is_16bit_NOP (abfd, sec, insn_rel))
11983
0
        {
11984
0
          if (!insert_nds32_elf_blank_recalc_total
11985
0
        (relax_blank_list, insn_rel->r_offset, 2))
11986
0
      return false;
11987
0
        }
11988
0
      insn_rel->r_info =
11989
0
        ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info), R_NDS32_NONE);
11990
0
    }
11991
        /* Save the new one for later use.  */
11992
0
        insn_rel = irel;
11993
0
      }
11994
0
    else
11995
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
11996
0
           R_NDS32_NONE);
11997
0
  }
11998
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL)
11999
0
  {
12000
    /* Search for label.  */
12001
0
    int force_relax = 0;
12002
12003
    /* Label on 16-bit instruction or optimization
12004
       needless, just reset this reloc.  */
12005
0
    insn16 = bfd_getb16 (contents + irel->r_offset);
12006
0
    if ((irel->r_addend & 0x1f) < 2 && (!optimize || (insn16 & 0x8000)))
12007
0
      {
12008
0
        irel->r_info =
12009
0
    ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_NDS32_NONE);
12010
0
        continue;
12011
0
      }
12012
12013
0
    address =
12014
0
      irel->r_offset - get_nds32_elf_blank_total (relax_blank_list,
12015
0
              irel->r_offset, 1);
12016
12017
0
    if (!insn_rel)
12018
0
      {
12019
        /* Check if there is case which can not be aligned.  */
12020
0
        if (irel->r_addend == 2 && address & 0x2)
12021
0
    return false;
12022
0
        continue;
12023
0
      }
12024
12025
    /* Try to align this label.  */
12026
12027
0
    if ((irel->r_addend & 0x1f) < 2)
12028
0
      {
12029
        /* Check if there is a INSN16 at the same address.
12030
     Label_rel always seats before insn_rel after
12031
     our sort.  */
12032
12033
        /* Search for INSN16 at LABEL location.  If INSN16 is at
12034
     same location and this LABEL alignment is lower than 2,
12035
     the INSN16 can be converted to 2-byte.  */
12036
0
        for (tmp_rel = irel;
12037
0
       tmp_rel < irelend && tmp_rel->r_offset == irel->r_offset;
12038
0
       tmp_rel++)
12039
0
    {
12040
0
      if (ELF32_R_TYPE (tmp_rel->r_info) == R_NDS32_INSN16
12041
0
          && (is_convert_32_to_16
12042
0
        (abfd, sec, tmp_rel, internal_relocs,
12043
0
         irelend, &insn16)
12044
0
        || is_16bit_NOP (abfd, sec, tmp_rel)))
12045
0
        {
12046
0
          force_relax = 1;
12047
0
          break;
12048
0
        }
12049
0
    }
12050
0
      }
12051
12052
0
    if (force_relax || irel->r_addend == 1 || address & 0x2)
12053
0
      {
12054
        /* Label not aligned.  */
12055
        /* Previous reloc exists, reduce its size to 16-bit.  */
12056
0
        if (is_convert_32_to_16 (abfd, sec, insn_rel,
12057
0
               internal_relocs, irelend, &insn16))
12058
0
    {
12059
0
      nds32_elf_write_16 (abfd, contents, insn_rel,
12060
0
              internal_relocs, irelend, insn16);
12061
12062
0
      if (!insert_nds32_elf_blank_recalc_total
12063
0
          (relax_blank_list, insn_rel->r_offset + 2, 2))
12064
0
        return false;
12065
0
    }
12066
0
        else if (is_16bit_NOP (abfd, sec, insn_rel))
12067
0
    {
12068
0
      if (!insert_nds32_elf_blank_recalc_total
12069
0
          (relax_blank_list, insn_rel->r_offset, 2))
12070
0
        return false;
12071
0
    }
12072
12073
0
      }
12074
    /* INSN16 reloc is used.  */
12075
0
    insn_rel = NULL;
12076
0
  }
12077
0
    }
12078
12079
0
  address =
12080
0
    sec->size - get_nds32_elf_blank_total (relax_blank_list, sec->size, 0);
12081
0
  if (insn_rel && (address & 0x2 || opt_size))
12082
0
    {
12083
0
      if (is_convert_32_to_16 (abfd, sec, insn_rel, internal_relocs,
12084
0
             irelend, &insn16))
12085
0
  {
12086
0
    nds32_elf_write_16 (abfd, contents, insn_rel, internal_relocs,
12087
0
            irelend, insn16);
12088
0
    if (!insert_nds32_elf_blank_recalc_total
12089
0
        (relax_blank_list, insn_rel->r_offset + 2, 2))
12090
0
      return false;
12091
0
    insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12092
0
             R_NDS32_NONE);
12093
0
  }
12094
0
      else if (is_16bit_NOP (abfd, sec, insn_rel))
12095
0
  {
12096
0
    if (!insert_nds32_elf_blank_recalc_total
12097
0
        (relax_blank_list, insn_rel->r_offset, 2))
12098
0
      return false;
12099
0
    insn_rel->r_info = ELF32_R_INFO (ELF32_R_SYM (insn_rel->r_info),
12100
0
             R_NDS32_NONE);
12101
0
  }
12102
0
    }
12103
0
  insn_rel = NULL;
12104
0
  return true;
12105
0
}
12106
12107
static bool
12108
nds32_elf_relax_section (bfd *abfd, asection *sec,
12109
       struct bfd_link_info *link_info, bool *again)
12110
0
{
12111
0
  nds32_elf_blank_t *relax_blank_list = NULL;
12112
0
  Elf_Internal_Shdr *symtab_hdr;
12113
0
  Elf_Internal_Rela *internal_relocs;
12114
0
  Elf_Internal_Rela *irel;
12115
0
  Elf_Internal_Rela *irelend;
12116
0
  Elf_Internal_Sym *isymbuf = NULL;
12117
0
  bfd_byte *contents = NULL;
12118
0
  bool result = true;
12119
0
  int optimize = 0;
12120
0
  int opt_size = 0;
12121
0
  uint32_t insn;
12122
0
  uint16_t insn16;
12123
12124
  /* Target dependnet option.  */
12125
0
  struct elf_nds32_link_hash_table *table;
12126
0
  int load_store_relax;
12127
12128
0
  relax_blank_list = NULL;
12129
12130
0
  *again = false;
12131
12132
  /* Nothing to do for
12133
   * relocatable link or
12134
   * non-relocatable section or
12135
   * non-code section or
12136
   * empty content or
12137
   * no reloc entry.  */
12138
0
  if (bfd_link_relocatable (link_info)
12139
0
      || (sec->flags & SEC_RELOC) == 0
12140
0
      || (sec->flags & SEC_EXCLUDE) != 0
12141
0
      || (sec->flags & SEC_CODE) == 0
12142
0
      || sec->size == 0
12143
0
      || sec->reloc_count == 0)
12144
0
    return true;
12145
12146
  /* 09.12.11 Workaround.  */
12147
  /*  We have to adjust align for R_NDS32_LABEL if needed.
12148
      The adjust approach only can fix 2-byte align once.  */
12149
0
  if (sec->alignment_power > 2)
12150
0
    return true;
12151
12152
  /* Do TLS model conversion once at first.  */
12153
0
  nds32_elf_unify_tls_model (abfd, sec, contents, link_info);
12154
12155
  /* The optimization type to do.  */
12156
12157
0
  table = nds32_elf_hash_table (link_info);
12158
12159
  /* Save the first section for abs symbol relaxation.
12160
     This is used for checking gp relaxation in the
12161
     nds32_elf_relax_loadstore and nds32_elf_relax_lo12.  */
12162
0
  nds32_elf_relax_guard (NULL, 0, sec, NULL, again, true,
12163
0
       table, NULL, NULL);
12164
12165
  /* The begining of general relaxation.  */
12166
12167
0
  if (is_SDA_BASE_set == 0)
12168
0
    {
12169
0
      bfd_vma gp;
12170
0
      is_SDA_BASE_set = 1;
12171
0
      nds32_elf_final_sda_base (sec->output_section->owner, link_info,
12172
0
        &gp, false);
12173
0
      relax_range_measurement (abfd, link_info);
12174
0
    }
12175
12176
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12177
  /* Relocations MUST be kept in memory, because relaxation adjust them.  */
12178
0
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
12179
0
                 true /* keep_memory */);
12180
0
  if (internal_relocs == NULL)
12181
0
    goto error_return;
12182
12183
0
  irelend = internal_relocs + sec->reloc_count;
12184
0
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
12185
0
         irelend, R_NDS32_RELAX_ENTRY);
12186
12187
0
  if (irel == irelend)
12188
0
    return true;
12189
12190
0
  if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_ENTRY)
12191
0
    {
12192
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG)
12193
0
  return true;
12194
12195
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FLAG)
12196
0
  optimize = 1;
12197
12198
0
      if (irel->r_addend & R_NDS32_RELAX_ENTRY_OPTIMIZE_FOR_SPACE_FLAG)
12199
0
  opt_size = 1;
12200
0
    }
12201
12202
0
  load_store_relax = table->load_store_relax;
12203
12204
  /* Get symbol table and section content.  */
12205
0
  contents = NULL;
12206
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true)
12207
0
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
12208
0
    goto error_return;
12209
12210
  /* Do relax loop only when finalize is not done.
12211
     Take care of relaxable relocs except INSN16.  */
12212
0
  for (irel = internal_relocs; irel < irelend; irel++)
12213
0
    {
12214
0
      int seq_len;    /* Original length of instruction sequence.  */
12215
0
      int insn_len = 0;   /* Final length of instruction sequence.  */
12216
0
      bool removed;
12217
12218
0
      insn = 0;
12219
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_LABEL
12220
0
    && (irel->r_addend & 0x1f) >= 2)
12221
0
  optimize = 1;
12222
12223
      /* Relocation Types
12224
   R_NDS32_LONGCALL1  53
12225
   R_NDS32_LONGCALL2  54
12226
   R_NDS32_LONGCALL3  55
12227
   R_NDS32_LONGJUMP1  56
12228
   R_NDS32_LONGJUMP2  57
12229
   R_NDS32_LONGJUMP3  58
12230
   R_NDS32_LOADSTORE  59  */
12231
0
      if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL1
12232
0
    && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LOADSTORE)
12233
0
  seq_len = GET_SEQ_LEN (irel->r_addend);
12234
12235
      /* Relocation Types
12236
   R_NDS32_LONGCALL4  107
12237
   R_NDS32_LONGCALL5  108
12238
   R_NDS32_LONGCALL6  109
12239
   R_NDS32_LONGJUMP4  110
12240
   R_NDS32_LONGJUMP5  111
12241
   R_NDS32_LONGJUMP6  112
12242
   R_NDS32_LONGJUMP7  113  */
12243
0
      else if (ELF32_R_TYPE (irel->r_info) >= R_NDS32_LONGCALL4
12244
0
         && ELF32_R_TYPE (irel->r_info) <= R_NDS32_LONGJUMP7)
12245
0
  seq_len = 4;
12246
12247
  /* Relocation Types
12248
   R_NDS32_LO12S0_RELA    30
12249
   R_NDS32_LO12S1_RELA    29
12250
   R_NDS32_LO12S2_RELA    28
12251
   R_NDS32_LO12S2_SP_RELA   71
12252
   R_NDS32_LO12S2_DP_RELA   70
12253
   R_NDS32_GOT_LO12   46
12254
   R_NDS32_GOTOFF_LO12    50
12255
   R_NDS32_PLTREL_LO12    65
12256
   R_NDS32_PLT_GOTREL_LO12  67
12257
   R_NDS32_17IFC_PCREL_RELA 96
12258
   R_NDS32_GOT_SUFF   193
12259
   R_NDS32_GOTOFF_SUFF    194
12260
   R_NDS32_PLT_GOT_SUFF   195
12261
   R_NDS32_MULCALL_SUFF   196
12262
   R_NDS32_PTR      197  */
12263
0
      else if ((ELF32_R_TYPE (irel->r_info) <= R_NDS32_LO12S0_RELA
12264
0
    && ELF32_R_TYPE (irel->r_info) >= R_NDS32_LO12S2_RELA)
12265
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_SP_RELA
12266
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LO12S2_DP_RELA
12267
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOT_LO12
12268
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTOFF_LO12
12269
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_GOTPC_LO12
12270
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLTREL_LO12
12271
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_PLT_GOTREL_LO12
12272
0
         || (ELF32_R_TYPE (irel->r_info) >= R_NDS32_GOT_SUFF
12273
0
       && ELF32_R_TYPE (irel->r_info) <= R_NDS32_PTR)
12274
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12275
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LO12
12276
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_ADD
12277
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_TLS_LE_LS
12278
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_LSI)
12279
0
  seq_len = 0;
12280
0
      else
12281
0
  continue;
12282
12283
0
      insn_len = seq_len;
12284
0
      removed = false;
12285
12286
0
      switch (ELF32_R_TYPE (irel->r_info))
12287
0
  {
12288
0
  case R_NDS32_LONGCALL1:
12289
0
    removed = nds32_elf_relax_longcall1 (abfd, sec, irel, internal_relocs,
12290
0
                 &insn_len, contents, isymbuf,
12291
0
                 symtab_hdr);
12292
0
    break;
12293
0
  case R_NDS32_LONGCALL2:
12294
0
    removed = nds32_elf_relax_longcall2 (abfd, sec, irel, internal_relocs,
12295
0
                 &insn_len, contents, isymbuf,
12296
0
                 symtab_hdr);
12297
0
    break;
12298
0
  case R_NDS32_LONGCALL3:
12299
0
    removed = nds32_elf_relax_longcall3 (abfd, sec, irel, internal_relocs,
12300
0
                 &insn_len, contents, isymbuf,
12301
0
                 symtab_hdr);
12302
0
    break;
12303
0
  case R_NDS32_LONGJUMP1:
12304
0
    removed = nds32_elf_relax_longjump1 (abfd, sec, irel, internal_relocs,
12305
0
                 &insn_len, contents, isymbuf,
12306
0
                 symtab_hdr);
12307
0
    break;
12308
0
  case R_NDS32_LONGJUMP2:
12309
0
    removed = nds32_elf_relax_longjump2 (abfd, sec, irel, internal_relocs,
12310
0
                 &insn_len, contents, isymbuf,
12311
0
                 symtab_hdr);
12312
0
    break;
12313
0
  case R_NDS32_LONGJUMP3:
12314
0
    removed = nds32_elf_relax_longjump3 (abfd, sec, irel, internal_relocs,
12315
0
                 &insn_len, contents, isymbuf,
12316
0
                 symtab_hdr);
12317
0
    break;
12318
0
  case R_NDS32_LONGCALL4:
12319
0
    removed = nds32_elf_relax_longcall4 (abfd, sec, irel, internal_relocs,
12320
0
                 &insn_len, contents, isymbuf,
12321
0
                 symtab_hdr);
12322
0
    break;
12323
0
  case R_NDS32_LONGCALL5:
12324
0
    removed = nds32_elf_relax_longcall5 (abfd, sec, irel, internal_relocs,
12325
0
                 &insn_len, contents, isymbuf,
12326
0
                 symtab_hdr);
12327
0
    break;
12328
0
  case R_NDS32_LONGCALL6:
12329
0
    removed = nds32_elf_relax_longcall6 (abfd, sec, irel, internal_relocs,
12330
0
                 &insn_len, contents, isymbuf,
12331
0
                 symtab_hdr);
12332
0
    break;
12333
0
  case R_NDS32_LONGJUMP4:
12334
0
    removed = nds32_elf_relax_longjump4 (abfd, sec, irel, internal_relocs,
12335
0
                 &insn_len, contents, isymbuf,
12336
0
                 symtab_hdr);
12337
0
    break;
12338
0
  case R_NDS32_LONGJUMP5:
12339
0
    removed = nds32_elf_relax_longjump5 (abfd, sec, irel, internal_relocs,
12340
0
                 &insn_len, &seq_len, contents,
12341
0
                 isymbuf, symtab_hdr);
12342
0
    break;
12343
0
  case R_NDS32_LONGJUMP6:
12344
0
    removed = nds32_elf_relax_longjump6 (abfd, sec, irel, internal_relocs,
12345
0
                 &insn_len, &seq_len, contents,
12346
0
                 isymbuf, symtab_hdr);
12347
0
    break;
12348
0
  case R_NDS32_LONGJUMP7:
12349
0
    removed = nds32_elf_relax_longjump7 (abfd, sec, irel, internal_relocs,
12350
0
                 &insn_len, &seq_len, contents,
12351
0
                 isymbuf, symtab_hdr);
12352
0
    break;
12353
0
  case R_NDS32_LOADSTORE:
12354
0
    removed = nds32_elf_relax_loadstore (link_info, abfd, sec, irel,
12355
0
                 internal_relocs, &insn_len,
12356
0
                 contents, isymbuf, symtab_hdr,
12357
0
                 load_store_relax, table);
12358
0
    break;
12359
0
  case R_NDS32_LO12S0_RELA:
12360
0
  case R_NDS32_LO12S1_RELA:
12361
0
  case R_NDS32_LO12S2_RELA:
12362
0
  case R_NDS32_LO12S2_DP_RELA:
12363
0
  case R_NDS32_LO12S2_SP_RELA:
12364
    /* Relax for low part.  */
12365
0
    nds32_elf_relax_lo12 (link_info, abfd, sec, irel, internal_relocs,
12366
0
        contents, isymbuf, symtab_hdr, table);
12367
12368
    /* It is impossible to delete blank, so just continue.  */
12369
0
    continue;
12370
0
  case R_NDS32_PTR:
12371
0
    removed = nds32_elf_relax_ptr (abfd, sec, irel, internal_relocs,
12372
0
           &insn_len, &seq_len, contents);
12373
0
    break;
12374
0
  case R_NDS32_LSI:
12375
0
    nds32_elf_relax_flsi (link_info, abfd, sec, irel, internal_relocs,
12376
0
        contents, isymbuf, symtab_hdr, again);
12377
0
    continue;
12378
0
  case R_NDS32_GOT_LO12:
12379
0
  case R_NDS32_GOTOFF_LO12:
12380
0
  case R_NDS32_PLTREL_LO12:
12381
0
  case R_NDS32_PLT_GOTREL_LO12:
12382
0
  case R_NDS32_GOTPC_LO12:
12383
0
  case R_NDS32_TLS_LE_LO12:
12384
0
  case R_NDS32_TLS_LE_ADD:
12385
0
  case R_NDS32_TLS_LE_LS:
12386
0
  case R_NDS32_PLT_GOT_SUFF:
12387
0
  case R_NDS32_GOT_SUFF:
12388
0
  case R_NDS32_GOTOFF_SUFF:
12389
0
    continue;
12390
0
  default:
12391
0
    continue;
12392
0
  }
12393
12394
0
      if (removed && seq_len - insn_len > 0)
12395
0
  {
12396
0
    if (!insert_nds32_elf_blank
12397
0
        (&relax_blank_list, irel->r_offset + insn_len,
12398
0
         seq_len - insn_len))
12399
0
      goto error_return;
12400
0
    *again = true;
12401
0
  }
12402
0
    }
12403
12404
0
  calc_nds32_blank_total (relax_blank_list);
12405
12406
0
  if (table->relax_fp_as_gp)
12407
0
    {
12408
0
      if (!nds32_relax_fp_as_gp (link_info, abfd, sec, internal_relocs,
12409
0
         irelend, isymbuf))
12410
0
  goto error_return;
12411
12412
0
      if (!*again)
12413
0
  {
12414
0
    if (!nds32_fag_remove_unused_fpbase (abfd, sec, internal_relocs,
12415
0
                 irelend))
12416
0
      goto error_return;
12417
0
  }
12418
0
    }
12419
12420
0
  if (!*again)
12421
0
    {
12422
0
      if (!nds32_relax_adjust_label (abfd, sec, internal_relocs, contents,
12423
0
             &relax_blank_list, optimize, opt_size))
12424
0
  goto error_return;
12425
0
    }
12426
12427
  /* It doesn't matter optimize_for_space_no_align anymore.
12428
       If object file is assembled with flag '-Os',
12429
       the we don't adjust jump-destination on 4-byte boundary.  */
12430
12431
0
  if (relax_blank_list)
12432
0
    {
12433
0
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
12434
0
      relax_blank_list = NULL;
12435
0
    }
12436
12437
0
  if (!*again)
12438
0
    {
12439
      /* Closing the section, so we don't relax it anymore.  */
12440
0
      bfd_vma sec_size_align;
12441
0
      Elf_Internal_Rela *tmp_rel;
12442
12443
      /* Pad to alignment boundary.  Only handle current section alignment.  */
12444
0
      sec_size_align = (sec->size + (~((-1U) << sec->alignment_power)))
12445
0
           & ((-1U) << sec->alignment_power);
12446
0
      if ((sec_size_align - sec->size) & 0x2)
12447
0
  {
12448
0
    insn16 = NDS32_NOP16;
12449
0
    bfd_putb16 (insn16, contents + sec->size);
12450
0
    sec->size += 2;
12451
0
  }
12452
12453
0
      while (sec_size_align != sec->size)
12454
0
  {
12455
0
    insn = NDS32_NOP32;
12456
0
    bfd_putb32 (insn, contents + sec->size);
12457
0
    sec->size += 4;
12458
0
  }
12459
12460
0
      tmp_rel = find_relocs_at_address (internal_relocs, internal_relocs,
12461
0
          irelend, R_NDS32_RELAX_ENTRY);
12462
0
      if (tmp_rel != irelend)
12463
0
  tmp_rel->r_addend |= R_NDS32_RELAX_ENTRY_DISABLE_RELAX_FLAG;
12464
12465
0
      clean_nds32_elf_blank ();
12466
0
    }
12467
12468
0
 finish:
12469
0
  if (elf_section_data (sec)->relocs != internal_relocs)
12470
0
    free (internal_relocs);
12471
12472
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
12473
0
    free (contents);
12474
12475
0
  if (symtab_hdr->contents != (bfd_byte *) isymbuf)
12476
0
    free (isymbuf);
12477
12478
0
  return result;
12479
12480
0
 error_return:
12481
0
  result = false;
12482
0
  goto finish;
12483
0
}
12484
12485
static struct bfd_elf_special_section const nds32_elf_special_sections[] =
12486
{
12487
  {".sdata", 6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE},
12488
  {".sbss", 5, -2, SHT_NOBITS, SHF_ALLOC + SHF_WRITE},
12489
  {NULL, 0, 0, 0, 0}
12490
};
12491
12492
static bool
12493
nds32_elf_section_flags (const Elf_Internal_Shdr *hdr)
12494
0
{
12495
0
  const char *name = hdr->bfd_section->name;
12496
12497
0
  if (startswith (name, ".sbss")
12498
0
      || startswith (name, ".sdata"))
12499
0
    hdr->bfd_section->flags |= SEC_SMALL_DATA;
12500
12501
0
  return true;
12502
0
}
12503
12504
static bool
12505
nds32_elf_output_arch_syms (bfd *output_bfd ATTRIBUTE_UNUSED,
12506
          struct bfd_link_info *info,
12507
          void *finfo ATTRIBUTE_UNUSED,
12508
          int (*func) (void *, const char *,
12509
           Elf_Internal_Sym *,
12510
           asection *,
12511
           struct elf_link_hash_entry *)
12512
          ATTRIBUTE_UNUSED)
12513
0
{
12514
0
  FILE *sym_ld_script = NULL;
12515
0
  struct elf_nds32_link_hash_table *table;
12516
12517
0
  table = nds32_elf_hash_table (info);
12518
0
  sym_ld_script = table->sym_ld_script;
12519
12520
0
  if (check_start_export_sym)
12521
0
    fprintf (sym_ld_script, "}\n");
12522
12523
0
  return true;
12524
0
}
12525
12526
static enum elf_reloc_type_class
12527
nds32_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
12528
          const asection *rel_sec ATTRIBUTE_UNUSED,
12529
          const Elf_Internal_Rela *rela)
12530
0
{
12531
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
12532
0
    {
12533
0
    case R_NDS32_RELATIVE:
12534
0
      return reloc_class_relative;
12535
0
    case R_NDS32_JMP_SLOT:
12536
0
      return reloc_class_plt;
12537
0
    case R_NDS32_COPY:
12538
0
      return reloc_class_copy;
12539
0
    default:
12540
0
      return reloc_class_normal;
12541
0
    }
12542
0
}
12543
12544
/* Put target dependent option into info hash table.  */
12545
void
12546
bfd_elf32_nds32_set_target_option (struct bfd_link_info *link_info,
12547
           int relax_fp_as_gp,
12548
           int eliminate_gc_relocs,
12549
           FILE * sym_ld_script,
12550
           int hyper_relax,
12551
           int tls_desc_trampoline,
12552
           int load_store_relax)
12553
0
{
12554
0
  struct elf_nds32_link_hash_table *table;
12555
12556
0
  table = nds32_elf_hash_table (link_info);
12557
0
  if (table == NULL)
12558
0
    return;
12559
12560
0
  table->relax_fp_as_gp = relax_fp_as_gp;
12561
0
  table->eliminate_gc_relocs = eliminate_gc_relocs;
12562
0
  table->sym_ld_script = sym_ld_script;
12563
0
  table->hyper_relax = hyper_relax;
12564
0
  table->tls_desc_trampoline = tls_desc_trampoline;
12565
0
  table ->load_store_relax = load_store_relax;
12566
0
}
12567

12568
12569
/* These functions and data-structures are used for fp-as-gp
12570
   optimization.  */
12571
12572
0
#define FAG_THRESHOLD 3  /* At least 3 gp-access.  */
12573
/* lwi37.fp covers 508 bytes, but there may be 32-byte padding between
12574
   the read-only section and read-write section.  */
12575
0
#define FAG_WINDOW  (508 - 32)
12576
12577
/* An nds32_fag represent a gp-relative access.
12578
   We find best fp-base by using a sliding window
12579
   to find a base address which can cover most gp-access.  */
12580
struct nds32_fag
12581
{
12582
  struct nds32_fag *next; /* NULL-teminated linked list.  */
12583
  bfd_vma addr;     /* The address of this fag.  */
12584
  Elf_Internal_Rela **relas;  /* The relocations associated with this fag.
12585
           It is used for applying FP7U2_FLAG.  */
12586
  int count;      /* How many times this address is referred.
12587
           There should be exactly `count' relocations
12588
           in relas.  */
12589
  int relas_capcity;    /* The buffer size of relas.
12590
           We use an array instead of linked-list,
12591
           and realloc is used to adjust buffer size.  */
12592
};
12593
12594
static void
12595
nds32_fag_init (struct nds32_fag *head)
12596
0
{
12597
0
  memset (head, 0, sizeof (struct nds32_fag));
12598
0
}
12599
12600
static void
12601
nds32_fag_verify (struct nds32_fag *head)
12602
0
{
12603
0
  struct nds32_fag *iter;
12604
0
  struct nds32_fag *prev;
12605
12606
0
  prev = NULL;
12607
0
  iter = head->next;
12608
0
  while (iter)
12609
0
    {
12610
0
      if (prev && prev->addr >= iter->addr)
12611
0
  puts ("Bug in fp-as-gp insertion.");
12612
0
      prev = iter;
12613
0
      iter = iter->next;
12614
0
    }
12615
0
}
12616
12617
/* Insert a fag in ascending order.
12618
   If a fag of the same address already exists,
12619
   they are chained by relas array.  */
12620
12621
static void
12622
nds32_fag_insert (struct nds32_fag *head, bfd_vma addr,
12623
      Elf_Internal_Rela * rel)
12624
0
{
12625
0
  struct nds32_fag *iter;
12626
0
  struct nds32_fag *new_fag;
12627
0
  const int INIT_RELAS_CAP = 4;
12628
12629
0
  for (iter = head;
12630
0
       iter->next && iter->next->addr <= addr;
12631
0
       iter = iter->next)
12632
0
    /* Find somewhere to insert.  */ ;
12633
12634
  /* `iter' will be equal to `head' if the list is empty.  */
12635
0
  if (iter != head && iter->addr == addr)
12636
0
    {
12637
      /* The address exists in the list.
12638
   Insert `rel' into relocation list, relas.  */
12639
12640
      /* Check whether relas is big enough.  */
12641
0
      if (iter->count >= iter->relas_capcity)
12642
0
  {
12643
0
    iter->relas_capcity *= 2;
12644
0
    iter->relas = bfd_realloc
12645
0
      (iter->relas, iter->relas_capcity * sizeof (void *));
12646
0
  }
12647
0
      iter->relas[iter->count++] = rel;
12648
0
      return;
12649
0
    }
12650
12651
  /* This is a new address.  Create a fag node for it.  */
12652
0
  new_fag = bfd_malloc (sizeof (struct nds32_fag));
12653
0
  memset (new_fag, 0, sizeof (*new_fag));
12654
0
  new_fag->addr = addr;
12655
0
  new_fag->count = 1;
12656
0
  new_fag->next = iter->next;
12657
0
  new_fag->relas_capcity = INIT_RELAS_CAP;
12658
0
  new_fag->relas = (Elf_Internal_Rela **)
12659
0
    bfd_malloc (new_fag->relas_capcity * sizeof (void *));
12660
0
  new_fag->relas[0] = rel;
12661
0
  iter->next = new_fag;
12662
12663
0
  nds32_fag_verify (head);
12664
0
}
12665
12666
static void
12667
nds32_fag_free_list (struct nds32_fag *head)
12668
0
{
12669
0
  struct nds32_fag *iter;
12670
12671
0
  iter = head->next;
12672
0
  while (iter)
12673
0
    {
12674
0
      struct nds32_fag *tmp = iter;
12675
0
      iter = iter->next;
12676
0
      free (tmp->relas);
12677
0
      tmp->relas = NULL;
12678
0
      free (tmp);
12679
0
    }
12680
0
}
12681
12682
/* Find the best fp-base address.
12683
   The relocation associated with that address is returned,
12684
   so we can track the symbol instead of a fixed address.
12685
12686
   When relaxation, the address of an datum may change,
12687
   because a text section is shrinked, so the data section
12688
   moves forward.  If the aligments of text and data section
12689
   are different, their distance may change too.
12690
   Therefore, tracking a fixed address is not appriate.  */
12691
12692
static int
12693
nds32_fag_find_base (struct nds32_fag *head, struct nds32_fag **bestpp)
12694
0
{
12695
0
  struct nds32_fag *base; /* First fag in the window.  */
12696
0
  struct nds32_fag *last; /* First fag outside the window.  */
12697
0
  int accu = 0;     /* Usage accumulation.  */
12698
0
  struct nds32_fag *best; /* Best fag.  */
12699
0
  int baccu = 0;    /* Best accumulation.  */
12700
12701
  /* Use first fag for initial, and find the last fag in the window.
12702
12703
     In each iteration, we could simply subtract previous fag
12704
     and accumulate following fags which are inside the window,
12705
     untill we each the end.  */
12706
12707
0
  if (head->next == NULL)
12708
0
    {
12709
0
      *bestpp = NULL;
12710
0
      return 0;
12711
0
    }
12712
12713
  /* Initialize base.  */
12714
0
  base = head->next;
12715
0
  best = base;
12716
0
  for (last = base;
12717
0
       last && last->addr < base->addr + FAG_WINDOW;
12718
0
       last = last->next)
12719
0
    accu += last->count;
12720
12721
0
  baccu = accu;
12722
12723
  /* Record the best base in each iteration.  */
12724
0
  while (base->next)
12725
0
    {
12726
0
      accu -= base->count;
12727
0
      base = base->next;
12728
      /* Account fags in window.  */
12729
0
      for (/* Nothing.  */;
12730
0
     last && last->addr < base->addr + FAG_WINDOW;
12731
0
     last = last->next)
12732
0
  accu += last->count;
12733
12734
      /* A better fp-base?  */
12735
0
      if (accu > baccu)
12736
0
  {
12737
0
    best = base;
12738
0
    baccu = accu;
12739
0
  }
12740
0
    }
12741
12742
0
  if (bestpp)
12743
0
    *bestpp = best;
12744
0
  return baccu;
12745
0
}
12746
12747
/* Apply R_NDS32_INSN16_FP7U2_FLAG on gp-relative accesses,
12748
   so we can convert it fo fp-relative access later.
12749
   `best_fag' is the best fp-base.  Only those inside the window
12750
   of best_fag is applied the flag.  */
12751
12752
static bool
12753
nds32_fag_mark_relax (struct bfd_link_info *link_info,
12754
          asection *sec, struct nds32_fag *best_fag,
12755
          Elf_Internal_Rela *internal_relocs,
12756
          Elf_Internal_Rela *irelend)
12757
0
{
12758
0
  struct nds32_fag *ifag;
12759
0
  bfd_vma best_fpbase, gp;
12760
0
  bfd *output_bfd;
12761
12762
0
  output_bfd = sec->output_section->owner;
12763
0
  nds32_elf_final_sda_base (output_bfd, link_info, &gp, false);
12764
0
  best_fpbase = best_fag->addr;
12765
12766
0
  if (best_fpbase > gp + sdata_range[1][1]
12767
0
      || best_fpbase < gp - sdata_range[1][0])
12768
0
    return false;
12769
12770
  /* Mark these inside the window R_NDS32_INSN16_FP7U2_FLAG flag,
12771
     so we know they can be converted to lwi37.fp.   */
12772
0
  for (ifag = best_fag;
12773
0
       ifag && ifag->addr < best_fpbase + FAG_WINDOW; ifag = ifag->next)
12774
0
    {
12775
0
      int i;
12776
12777
0
      for (i = 0; i < ifag->count; i++)
12778
0
  {
12779
0
    Elf_Internal_Rela *insn16_rel;
12780
0
    Elf_Internal_Rela *fag_rel;
12781
12782
0
    fag_rel = ifag->relas[i];
12783
12784
    /* Only if this is within the WINDOWS, FP7U2_FLAG
12785
       is applied.  */
12786
12787
0
    insn16_rel = find_relocs_at_address
12788
0
      (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12789
12790
0
    if (insn16_rel != irelend)
12791
0
      insn16_rel->r_addend = R_NDS32_INSN16_FP7U2_FLAG;
12792
0
  }
12793
0
    }
12794
0
  return true;
12795
0
}
12796
12797
/* Reset INSN16 to clean fp as gp.  */
12798
12799
static void
12800
nds32_fag_unmark_relax (struct nds32_fag *fag,
12801
      Elf_Internal_Rela *internal_relocs,
12802
      Elf_Internal_Rela *irelend)
12803
0
{
12804
0
  struct nds32_fag *ifag;
12805
0
  int i;
12806
0
  Elf_Internal_Rela *insn16_rel;
12807
0
  Elf_Internal_Rela *fag_rel;
12808
12809
0
  for (ifag = fag; ifag; ifag = ifag->next)
12810
0
    {
12811
0
      for (i = 0; i < ifag->count; i++)
12812
0
  {
12813
0
    fag_rel = ifag->relas[i];
12814
12815
    /* Restore the INSN16 relocation.  */
12816
0
    insn16_rel = find_relocs_at_address
12817
0
      (fag_rel, internal_relocs, irelend, R_NDS32_INSN16);
12818
12819
0
    if (insn16_rel != irelend)
12820
0
      insn16_rel->r_addend &= ~R_NDS32_INSN16_FP7U2_FLAG;
12821
0
  }
12822
0
    }
12823
0
}
12824
12825
/* This is the main function of fp-as-gp optimization.
12826
   It should be called by relax_section.  */
12827
12828
static bool
12829
nds32_relax_fp_as_gp (struct bfd_link_info *link_info,
12830
          bfd *abfd, asection *sec,
12831
          Elf_Internal_Rela *internal_relocs,
12832
          Elf_Internal_Rela *irelend,
12833
          Elf_Internal_Sym *isymbuf)
12834
0
{
12835
0
  Elf_Internal_Rela *begin_rel = NULL;
12836
0
  Elf_Internal_Rela *irel;
12837
0
  struct nds32_fag fag_head;
12838
0
  Elf_Internal_Shdr *symtab_hdr;
12839
0
  bfd_byte *contents;
12840
0
  bool ifc_inside = false;
12841
12842
  /* FIXME: Can we bfd_elf_link_read_relocs for the relocs?  */
12843
12844
  /* Per-function fp-base selection.
12845
     1. Create a list for all the gp-relative access.
12846
     2. Base on those gp-relative address,
12847
  find a fp-base which can cover most access.
12848
     3. Use the fp-base for fp-as-gp relaxation.
12849
12850
     NOTE: If fp-as-gp is not worth to do, (e.g., less than 3 times),
12851
     we should
12852
     1. delete the `la $fp, _FP_BASE_' instruction and
12853
     2. not convert lwi.gp to lwi37.fp.
12854
12855
     To delete the _FP_BASE_ instruction, we simply apply
12856
     R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG flag in the r_addend to disable it.
12857
12858
     To suppress the conversion, we simply NOT to apply
12859
     R_NDS32_INSN16_FP7U2_FLAG flag.  */
12860
12861
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
12862
12863
0
  contents = NULL;
12864
0
  if (!nds32_get_section_contents (abfd, sec, &contents, true)
12865
0
      || !nds32_get_local_syms (abfd, sec, &isymbuf))
12866
0
    return false;
12867
12868
  /* Check whether it is worth for fp-as-gp optimization,
12869
     i.e., at least 3 gp-load.
12870
12871
     Set R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG if we should NOT
12872
     apply this optimization.  */
12873
12874
0
  for (irel = internal_relocs; irel < irelend; irel++)
12875
0
    {
12876
      /* We recognize R_NDS32_RELAX_REGION_BEGIN/_END for the region.
12877
   One we enter the begin of the region, we track all the LW/ST
12878
   instructions, so when we leave the region, we try to find
12879
   the best fp-base address for those LW/ST instructions.  */
12880
12881
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
12882
0
    && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12883
0
  {
12884
    /* Begin of the region.  */
12885
0
    if (begin_rel)
12886
      /* xgettext:c-format */
12887
0
      _bfd_error_handler (_("%pB: nested OMIT_FP in %pA"), abfd, sec);
12888
12889
0
    begin_rel = irel;
12890
0
    nds32_fag_init (&fag_head);
12891
0
    ifc_inside = false;
12892
0
  }
12893
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
12894
0
         && (irel->r_addend & R_NDS32_RELAX_REGION_OMIT_FP_FLAG))
12895
0
  {
12896
0
    int accu;
12897
0
    struct nds32_fag *best_fag, *tmp_fag;
12898
0
    int dist;
12899
12900
    /* End of the region.
12901
       Check whether it is worth to do fp-as-gp.  */
12902
12903
0
    if (begin_rel == NULL)
12904
0
      {
12905
        /* xgettext:c-format */
12906
0
        _bfd_error_handler (_("%pB: unmatched OMIT_FP in %pA"),
12907
0
          abfd, sec);
12908
0
        continue;
12909
0
      }
12910
12911
0
    accu = nds32_fag_find_base (&fag_head, &best_fag);
12912
12913
    /* Clean FP7U2_FLAG because they may set ever.  */
12914
0
    tmp_fag = fag_head.next;
12915
0
    nds32_fag_unmark_relax (tmp_fag, internal_relocs, irelend);
12916
12917
    /* Check if it is worth, and FP_BASE is near enough to SDA_BASE.  */
12918
0
    if (accu < FAG_THRESHOLD
12919
0
        || !nds32_fag_mark_relax (link_info, sec, best_fag,
12920
0
          internal_relocs, irelend))
12921
0
      {
12922
        /* Not worth to do fp-as-gp.  */
12923
0
        begin_rel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12924
0
        begin_rel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12925
0
        irel->r_addend |= R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG;
12926
0
        irel->r_addend &= ~R_NDS32_RELAX_REGION_OMIT_FP_FLAG;
12927
0
        nds32_fag_free_list (&fag_head);
12928
0
        begin_rel = NULL;
12929
0
        continue;
12930
0
      }
12931
12932
    /* R_SYM of R_NDS32_RELAX_REGION_BEGIN is not used by assembler,
12933
       so we use it to record the distance to the reloction of best
12934
       fp-base.  */
12935
0
    dist = best_fag->relas[0] - begin_rel;
12936
0
    BFD_ASSERT (dist > 0 && dist < 0xffffff);
12937
    /* Use high 16 bits of addend to record the _FP_BASE_ matched
12938
       relocation.  And get the base value when relocating.  */
12939
0
    begin_rel->r_addend &= (0x1 << 16) - 1;
12940
0
    begin_rel->r_addend |= dist << 16;
12941
12942
0
    nds32_fag_free_list (&fag_head);
12943
0
    begin_rel = NULL;
12944
0
  }
12945
12946
0
      if (begin_rel == NULL || ifc_inside)
12947
  /* Skip if we are not in the region of fp-as-gp.  */
12948
0
  continue;
12949
12950
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S2_RELA
12951
0
    || ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA17S2_RELA)
12952
0
  {
12953
0
    bfd_vma addr;
12954
0
    uint32_t insn;
12955
12956
    /* A gp-relative access is found.  Insert it to the fag-list.  */
12957
12958
    /* Rt is necessary an RT3, so it can be converted to lwi37.fp.  */
12959
0
    insn = bfd_getb32 (contents + irel->r_offset);
12960
0
    if (!N32_IS_RT3 (insn))
12961
0
      continue;
12962
12963
0
    addr = calculate_memory_address (abfd, irel, isymbuf, symtab_hdr);
12964
0
    nds32_fag_insert (&fag_head, addr, irel);
12965
0
  }
12966
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA_FP7U2_RELA)
12967
0
  {
12968
0
    begin_rel = NULL;
12969
0
  }
12970
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_17IFC_PCREL_RELA
12971
0
         || ELF32_R_TYPE (irel->r_info) == R_NDS32_10IFCU_PCREL_RELA)
12972
0
  {
12973
    /* Suppress fp as gp when encounter ifc.  */
12974
0
    ifc_inside = true;
12975
0
  }
12976
0
    }
12977
12978
0
  return true;
12979
0
}
12980
12981
/* Remove unused `la $fp, _FD_BASE_' instruction.  */
12982
12983
static bool
12984
nds32_fag_remove_unused_fpbase (bfd *abfd, asection *sec,
12985
        Elf_Internal_Rela *internal_relocs,
12986
        Elf_Internal_Rela *irelend)
12987
0
{
12988
0
  Elf_Internal_Rela *irel;
12989
0
  Elf_Internal_Shdr *symtab_hdr;
12990
0
  bfd_byte *contents = NULL;
12991
0
  nds32_elf_blank_t *relax_blank_list = NULL;
12992
0
  bool result = true;
12993
0
  bool unused_region = false;
12994
12995
  /*
12996
     NOTE: Disable fp-as-gp if we encounter ifcall relocations:
12997
       R_NDS32_17IFC_PCREL_RELA
12998
       R_NDS32_10IFCU_PCREL_RELA.  */
12999
13000
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
13001
0
  nds32_get_section_contents (abfd, sec, &contents, true);
13002
13003
0
  for (irel = internal_relocs; irel < irelend; irel++)
13004
0
    {
13005
      /* To remove unused fp-base, we simply find the REGION_NOT_OMIT_FP
13006
   we marked to in previous pass.
13007
   DO NOT scan relocations again, since we've alreadly decided it
13008
   and set the flag.  */
13009
0
      const char *syname;
13010
0
      int syndx;
13011
0
      uint32_t insn;
13012
13013
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_BEGIN
13014
0
    && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13015
0
  unused_region = true;
13016
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_RELAX_REGION_END
13017
0
         && (irel->r_addend & R_NDS32_RELAX_REGION_NOT_OMIT_FP_FLAG))
13018
0
  unused_region = false;
13019
13020
      /* We're not in the region.  */
13021
0
      if (!unused_region)
13022
0
  continue;
13023
13024
      /* _FP_BASE_ must be a GLOBAL symbol.  */
13025
0
      syndx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
13026
0
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
13027
0
  continue;
13028
13029
      /* The symbol name must be _FP_BASE_.  */
13030
0
      syname = elf_sym_hashes (abfd)[syndx]->root.root.string;
13031
0
      if (strcmp (syname, FP_BASE_NAME) != 0)
13032
0
  continue;
13033
13034
0
      if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA19S0_RELA)
13035
0
  {
13036
    /* addi.gp  $fp, -256  */
13037
0
    insn = bfd_getb32 (contents + irel->r_offset);
13038
0
    if (insn != INSN_ADDIGP_TO_FP)
13039
0
      continue;
13040
0
  }
13041
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_SDA15S0_RELA)
13042
0
  {
13043
    /* addi  $fp, $gp, -256  */
13044
0
    insn = bfd_getb32 (contents + irel->r_offset);
13045
0
    if (insn != INSN_ADDI_GP_TO_FP)
13046
0
      continue;
13047
0
  }
13048
0
      else if (ELF32_R_TYPE (irel->r_info) == R_NDS32_20_RELA)
13049
0
  {
13050
    /* movi  $fp, FP_BASE  */
13051
0
    insn = bfd_getb32 (contents + irel->r_offset);
13052
0
    if (insn != INSN_MOVI_TO_FP)
13053
0
      continue;
13054
0
  }
13055
0
      else
13056
0
  continue;
13057
13058
      /* We got here because a FP_BASE instruction is found.  */
13059
0
      if (!insert_nds32_elf_blank_recalc_total
13060
0
    (&relax_blank_list, irel->r_offset, 4))
13061
0
  goto error_return;
13062
0
    }
13063
13064
0
 finish:
13065
0
  if (relax_blank_list)
13066
0
    {
13067
0
      nds32_elf_relax_delete_blanks (abfd, sec, relax_blank_list);
13068
0
      relax_blank_list = NULL;
13069
0
    }
13070
0
  return result;
13071
13072
0
 error_return:
13073
0
  result = false;
13074
0
  goto finish;
13075
0
}
13076
13077
/* This is a version of bfd_generic_get_relocated_section_contents.
13078
   We need this variety because relaxation will modify the dwarf
13079
   infomation.  When there is undefined symbol reference error mesage,
13080
   linker need to dump line number where the symbol be used.  However
13081
   the address is be relaxed, it can not get the original dwarf contents.
13082
   The variety only modify function call for reading in the section.  */
13083
13084
static bfd_byte *
13085
nds32_elf_get_relocated_section_contents (bfd *abfd,
13086
            struct bfd_link_info *link_info,
13087
            struct bfd_link_order *link_order,
13088
            bfd_byte *data,
13089
            bool relocatable,
13090
            asymbol **symbols)
13091
0
{
13092
0
  bfd *input_bfd = link_order->u.indirect.section->owner;
13093
0
  asection *input_section = link_order->u.indirect.section;
13094
0
  long reloc_size;
13095
0
  arelent **reloc_vector;
13096
0
  long reloc_count;
13097
13098
0
  reloc_size = bfd_get_reloc_upper_bound (input_bfd, input_section);
13099
0
  if (reloc_size < 0)
13100
0
    return NULL;
13101
13102
  /* Read in the section.  */
13103
0
  bfd_byte *orig_data = data;
13104
0
  if (!nds32_get_section_contents (input_bfd, input_section, &data, false))
13105
0
    return NULL;
13106
13107
0
  if (reloc_size == 0)
13108
0
    return data;
13109
13110
0
  reloc_vector = (arelent **) bfd_malloc (reloc_size);
13111
0
  if (reloc_vector == NULL)
13112
0
    goto error_return;
13113
13114
0
  reloc_count = bfd_canonicalize_reloc (input_bfd, input_section,
13115
0
          reloc_vector, symbols);
13116
0
  if (reloc_count < 0)
13117
0
    goto error_return;
13118
13119
0
  if (reloc_count > 0)
13120
0
    {
13121
0
      arelent **parent;
13122
0
      for (parent = reloc_vector; *parent != NULL; parent++)
13123
0
  {
13124
0
    char *error_message = NULL;
13125
0
    asymbol *symbol;
13126
0
    bfd_reloc_status_type r;
13127
13128
0
    symbol = *(*parent)->sym_ptr_ptr;
13129
0
    if (symbol->section && discarded_section (symbol->section))
13130
0
      {
13131
0
        bfd_vma off;
13132
0
        static reloc_howto_type none_howto
13133
0
    = HOWTO (0, 0, 0, 0, false, 0, complain_overflow_dont, NULL,
13134
0
       "unused", false, 0, 0, false);
13135
13136
0
        off = (*parent)->address * OCTETS_PER_BYTE (input_bfd,
13137
0
                input_section);
13138
0
        _bfd_clear_contents ((*parent)->howto, input_bfd,
13139
0
           input_section, data, off);
13140
0
        (*parent)->sym_ptr_ptr = bfd_abs_section_ptr->symbol_ptr_ptr;
13141
0
        (*parent)->addend = 0;
13142
0
        (*parent)->howto = &none_howto;
13143
0
        r = bfd_reloc_ok;
13144
0
      }
13145
0
    else
13146
0
      r = bfd_perform_relocation (input_bfd, *parent, data,
13147
0
          input_section,
13148
0
          relocatable ? abfd : NULL,
13149
0
          &error_message);
13150
13151
0
    if (relocatable)
13152
0
      {
13153
0
        asection *os = input_section->output_section;
13154
13155
        /* A partial link, so keep the relocs.  */
13156
0
        os->orelocation[os->reloc_count] = *parent;
13157
0
        os->reloc_count++;
13158
0
      }
13159
13160
0
    if (r != bfd_reloc_ok)
13161
0
      {
13162
0
        switch (r)
13163
0
    {
13164
0
    case bfd_reloc_undefined:
13165
0
      (*link_info->callbacks->undefined_symbol)
13166
0
        (link_info, bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13167
0
         input_bfd, input_section, (*parent)->address, true);
13168
0
      break;
13169
0
    case bfd_reloc_dangerous:
13170
0
      BFD_ASSERT (error_message != NULL);
13171
0
      (*link_info->callbacks->reloc_dangerous)
13172
0
        (link_info, error_message,
13173
0
         input_bfd, input_section, (*parent)->address);
13174
0
      break;
13175
0
    case bfd_reloc_overflow:
13176
0
      (*link_info->callbacks->reloc_overflow)
13177
0
        (link_info, NULL,
13178
0
         bfd_asymbol_name (*(*parent)->sym_ptr_ptr),
13179
0
         (*parent)->howto->name, (*parent)->addend,
13180
0
         input_bfd, input_section, (*parent)->address);
13181
0
      break;
13182
0
    case bfd_reloc_outofrange:
13183
      /* PR ld/13730:
13184
         This error can result when processing some partially
13185
         complete binaries.  Do not abort, but issue an error
13186
         message instead.  */
13187
0
      link_info->callbacks->einfo
13188
        /* xgettext:c-format */
13189
0
        (_("%X%P: %pB(%pA): relocation \"%pR\" goes out of range\n"),
13190
0
         abfd, input_section, * parent);
13191
0
      goto error_return;
13192
13193
0
    default:
13194
0
      abort ();
13195
0
      break;
13196
0
    }
13197
0
      }
13198
0
  }
13199
0
    }
13200
13201
0
  free (reloc_vector);
13202
0
  return data;
13203
13204
0
 error_return:
13205
0
  free (reloc_vector);
13206
0
  if (orig_data == NULL)
13207
0
    free (data);
13208
0
  return NULL;
13209
0
}
13210
13211
/* Check target symbol.  */
13212
13213
static bool
13214
nds32_elf_is_target_special_symbol (bfd *abfd ATTRIBUTE_UNUSED, asymbol *sym)
13215
0
{
13216
0
  if (!sym || !sym->name || sym->name[0] != '$')
13217
0
    return false;
13218
0
  return true;
13219
0
}
13220
13221
/* nds32 find maybe function sym.  Ignore target special symbol
13222
   first, and then go the general function.  */
13223
13224
static bfd_size_type
13225
nds32_elf_maybe_function_sym (const asymbol *sym, asection *sec,
13226
            bfd_vma *code_off)
13227
0
{
13228
0
  if (nds32_elf_is_target_special_symbol (NULL, (asymbol *) sym))
13229
0
    return 0;
13230
13231
0
  return _bfd_elf_maybe_function_sym (sym, sec, code_off);
13232
0
}
13233

13234
13235
/* Do TLS model conversion.  */
13236
13237
typedef struct relax_group_list_t
13238
{
13239
  Elf_Internal_Rela *relo;
13240
  struct relax_group_list_t *next;
13241
  struct relax_group_list_t *next_sibling;
13242
  int id;
13243
} relax_group_list_t;
13244
13245
int
13246
list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem);
13247
13248
int
13249
list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem);
13250
13251
void
13252
dump_chain (relax_group_list_t *pHead);
13253
13254
int
13255
list_insert (relax_group_list_t *pHead, Elf_Internal_Rela *pElem)
13256
0
{
13257
0
  relax_group_list_t *pNext = pHead;
13258
13259
  /* Find place.  */
13260
0
  while (pNext->next)
13261
0
    {
13262
0
      if (pNext->next->id > (int) pElem->r_addend)
13263
0
  break;
13264
13265
0
      pNext = pNext->next;
13266
0
    }
13267
13268
  /* Insert node.  */
13269
0
  relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13270
0
  if (!pNew)
13271
0
    return false;
13272
13273
0
  relax_group_list_t *tmp = pNext->next;
13274
0
  pNext->next = pNew;
13275
13276
0
  pNew->id = pElem->r_addend;
13277
0
  pNew->relo = pElem;
13278
0
  pNew->next = tmp;
13279
0
  pNew->next_sibling = NULL;
13280
13281
0
  return true;
13282
0
}
13283
13284
int
13285
list_insert_sibling (relax_group_list_t *pNode, Elf_Internal_Rela *pElem)
13286
0
{
13287
0
  relax_group_list_t *pNext = pNode;
13288
13289
  /* Find place.  */
13290
0
  while (pNext->next_sibling)
13291
0
    {
13292
0
      pNext = pNext->next_sibling;
13293
0
    }
13294
13295
  /* Insert node.  */
13296
0
  relax_group_list_t *pNew = bfd_malloc (sizeof (relax_group_list_t));
13297
0
  if (!pNew)
13298
0
    return false;
13299
13300
0
  relax_group_list_t *tmp = pNext->next_sibling;
13301
0
  pNext->next_sibling = pNew;
13302
13303
0
  pNew->id = -1;
13304
0
  pNew->relo = pElem;
13305
0
  pNew->next = NULL;
13306
0
  pNew->next_sibling = tmp;
13307
13308
0
  return true;
13309
0
}
13310
13311
void
13312
dump_chain (relax_group_list_t *pHead)
13313
0
{
13314
0
  relax_group_list_t *pNext = pHead->next;
13315
0
  while (pNext)
13316
0
    {
13317
0
      printf("group %d @ 0x%08x", pNext->id, (unsigned)pNext->relo->r_offset);
13318
0
      relax_group_list_t *pNextSib = pNext->next_sibling;
13319
0
      while (pNextSib)
13320
0
  {
13321
0
    printf(", %d", (unsigned) ELF32_R_TYPE (pNextSib->relo->r_info));
13322
0
    pNextSib = pNextSib->next_sibling;
13323
0
  }
13324
0
      pNext = pNext->next;
13325
0
      printf("\n");
13326
0
    }
13327
0
}
13328
13329
/* Check R_NDS32_RELAX_GROUP of each section.
13330
   There might be multiple sections in one object file.  */
13331
13332
int
13333
elf32_nds32_check_relax_group (bfd *abfd, asection *asec)
13334
0
{
13335
0
  elf32_nds32_relax_group_t *relax_group_ptr =
13336
0
    elf32_nds32_relax_group_ptr (abfd);
13337
13338
0
  int min_id = relax_group_ptr->min_id;
13339
0
  int max_id = relax_group_ptr->max_id;
13340
13341
0
  Elf_Internal_Rela *rel;
13342
0
  Elf_Internal_Rela *relend;
13343
0
  Elf_Internal_Rela *relocs;
13344
0
  enum elf_nds32_reloc_type rtype;
13345
13346
0
  do
13347
0
    {
13348
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13349
0
      relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13350
0
            true /* keep_memory  */);
13351
0
      if (relocs == NULL)
13352
0
  break;
13353
13354
      /* Check R_NDS32_RELAX_GROUP.  */
13355
0
      relend = relocs + asec->reloc_count;
13356
0
      for (rel = relocs; rel < relend; rel++)
13357
0
  {
13358
0
    int id;
13359
0
    rtype = ELF32_R_TYPE (rel->r_info);
13360
0
    if (rtype != R_NDS32_RELAX_GROUP)
13361
0
      continue;
13362
13363
0
    id = rel->r_addend;
13364
0
    if (id < min_id)
13365
0
      min_id = id;
13366
0
    else if (id > max_id)
13367
0
      max_id = id;
13368
0
  }
13369
0
    }
13370
0
  while (false);
13371
13372
0
  if (elf_section_data (asec)->relocs != relocs)
13373
0
    free (relocs);
13374
13375
0
  if ((min_id != relax_group_ptr->min_id)
13376
0
      || (max_id != relax_group_ptr->max_id))
13377
0
    {
13378
0
      relax_group_ptr->count = max_id - min_id + 1;
13379
0
      BFD_ASSERT(min_id <= relax_group_ptr->min_id);
13380
0
      relax_group_ptr->min_id = min_id;
13381
0
      BFD_ASSERT(max_id >= relax_group_ptr->max_id);
13382
0
      relax_group_ptr->max_id = max_id;
13383
0
    }
13384
13385
0
  return relax_group_ptr->count;
13386
0
}
13387
13388
/* Reorder RELAX_GROUP ID when command line option '-r' is applied.  */
13389
static struct section_id_list_t *relax_group_section_id_list = NULL;
13390
13391
struct section_id_list_t *
13392
elf32_nds32_lookup_section_id (int id, struct section_id_list_t **lst_ptr)
13393
0
{
13394
0
  struct section_id_list_t *result = NULL;
13395
0
  struct section_id_list_t *lst = *lst_ptr;
13396
13397
0
  if (NULL == lst)
13398
0
    {
13399
0
      result = (struct section_id_list_t *) calloc
13400
0
  (1, sizeof (struct section_id_list_t));
13401
0
      BFD_ASSERT (result); /* Feed me.  */
13402
0
      result->id = id;
13403
0
      *lst_ptr = result;
13404
0
    }
13405
0
  else
13406
0
    {
13407
0
      struct section_id_list_t *cur = lst;
13408
0
      struct section_id_list_t *prv = NULL;
13409
0
      struct section_id_list_t *sec = NULL;
13410
13411
0
      while (cur)
13412
0
  {
13413
0
    if (cur->id < id)
13414
0
      {
13415
0
        prv = cur;
13416
0
        cur = cur->next;
13417
0
        continue;
13418
0
      }
13419
13420
0
    if (cur->id > id)
13421
0
      {
13422
0
        cur = NULL; /* To insert after prv.  */
13423
0
        sec = cur;  /* In case prv == NULL.  */
13424
0
      }
13425
13426
0
    break;
13427
0
  }
13428
13429
0
      if (NULL == cur)
13430
0
  {
13431
    /* Insert after prv.  */
13432
0
    result = (struct section_id_list_t *) calloc
13433
0
      (1, sizeof (struct section_id_list_t));
13434
0
    BFD_ASSERT (result); /* Feed me.  */
13435
0
    result->id = id;
13436
0
    if (NULL != prv)
13437
0
      {
13438
0
        result->next = prv->next;
13439
0
        prv->next = result;
13440
0
      }
13441
0
    else
13442
0
      {
13443
0
        *lst_ptr = result;
13444
0
        result->next = sec;
13445
0
      }
13446
0
  }
13447
0
    }
13448
13449
0
  return result;
13450
0
}
13451
13452
int
13453
elf32_nds32_unify_relax_group (bfd *abfd, asection *asec)
13454
0
{
13455
0
  static int next_relax_group_bias = 0;
13456
13457
0
  elf32_nds32_relax_group_t *relax_group_ptr =
13458
0
    elf32_nds32_relax_group_ptr (abfd);
13459
13460
0
  bool result = true;
13461
0
  Elf_Internal_Rela *rel;
13462
0
  Elf_Internal_Rela *relend;
13463
0
  Elf_Internal_Rela *relocs = NULL;
13464
0
  enum elf_nds32_reloc_type rtype;
13465
0
  struct section_id_list_t *node = NULL;
13466
13467
0
  do
13468
0
    {
13469
0
      if (0 == relax_group_ptr->count)
13470
0
  break;
13471
13472
      /* Check if this section has been handled.  */
13473
0
      node = elf32_nds32_lookup_section_id (asec->id, &relax_group_section_id_list);
13474
0
      if (NULL == node)
13475
0
  break; /* Hit, the section id has handled.  */
13476
13477
      /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13478
0
      relocs = _bfd_elf_link_read_relocs (abfd, asec, NULL, NULL,
13479
0
            true /* keep_memory  */);
13480
0
      if (relocs == NULL)
13481
0
  {
13482
0
    BFD_ASSERT (0); /* feed me */
13483
0
    break;
13484
0
  }
13485
13486
      /* Allocate group id bias for this bfd!  */
13487
0
      if (0 == relax_group_ptr->init)
13488
0
  {
13489
0
    relax_group_ptr->bias = next_relax_group_bias;
13490
0
    next_relax_group_bias += relax_group_ptr->count;
13491
0
    relax_group_ptr->init = 1;
13492
0
  }
13493
13494
      /* Reorder relax group groups.  */
13495
0
      relend = relocs + asec->reloc_count;
13496
0
      for (rel = relocs; rel < relend; rel++)
13497
0
  {
13498
0
    rtype = ELF32_R_TYPE(rel->r_info);
13499
0
    if (rtype != R_NDS32_RELAX_GROUP)
13500
0
      continue;
13501
13502
    /* Change it.  */
13503
0
    rel->r_addend += relax_group_ptr->bias;
13504
0
  }
13505
0
    }
13506
0
  while (false);
13507
13508
0
  if (elf_section_data (asec)->relocs != relocs)
13509
0
    free (relocs);
13510
13511
0
  return result;
13512
0
}
13513
13514
int
13515
nds32_elf_unify_tls_model (bfd *inbfd, asection *insec, bfd_byte *incontents,
13516
         struct bfd_link_info *lnkinfo)
13517
0
{
13518
0
  bool result = true;
13519
0
  Elf_Internal_Rela *irel;
13520
0
  Elf_Internal_Rela *irelend;
13521
0
  Elf_Internal_Rela *internal_relocs;
13522
0
  unsigned long r_symndx;
13523
0
  enum elf_nds32_reloc_type r_type;
13524
13525
0
  Elf_Internal_Sym *local_syms = NULL;
13526
0
  bfd_byte *contents = NULL;
13527
13528
0
  relax_group_list_t chain = { .id = -1, .next = NULL, .next_sibling = NULL };
13529
13530
0
  Elf_Internal_Shdr *symtab_hdr = &elf_tdata (inbfd)->symtab_hdr;
13531
0
  struct elf_link_hash_entry **sym_hashes;
13532
0
  sym_hashes = elf_sym_hashes (inbfd);
13533
13534
  /* Reorder RELAX_GROUP when command line option '-r' is applied.  */
13535
0
  if (bfd_link_relocatable (lnkinfo))
13536
0
    {
13537
0
      elf32_nds32_unify_relax_group (inbfd, insec);
13538
0
      return result;
13539
0
    }
13540
13541
  /* Relocations MUST be kept in memory, because relaxation adjust them.  */
13542
0
  internal_relocs = _bfd_elf_link_read_relocs (inbfd, insec, NULL, NULL,
13543
0
                 true /* keep_memory  */);
13544
0
  if (internal_relocs == NULL)
13545
0
    goto error_return;
13546
13547
0
  irelend = internal_relocs + insec->reloc_count;
13548
0
  irel = find_relocs_at_address (internal_relocs, internal_relocs,
13549
0
         irelend, R_NDS32_RELAX_ENTRY);
13550
0
  if (irel == irelend)
13551
0
    goto finish;
13552
13553
  /* Chain/remove groups.  */
13554
0
  for (irel = internal_relocs; irel < irelend; irel++)
13555
0
    {
13556
0
      r_symndx = ELF32_R_SYM (irel->r_info);
13557
0
      r_type = ELF32_R_TYPE (irel->r_info);
13558
0
      if (r_type != R_NDS32_RELAX_GROUP)
13559
0
  continue;
13560
13561
      /* Remove it.  */
13562
0
      irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_NONE);
13563
      /* Chain it now.  */
13564
0
      if (!list_insert (&chain, irel))
13565
0
  goto error_return;
13566
0
    }
13567
13568
  /* Collect group relocations.  */
13569
  /* Presume relocations are sorted.  */
13570
0
  relax_group_list_t *pNext = chain.next;
13571
0
  while (pNext)
13572
0
    {
13573
0
      for (irel = internal_relocs; irel < irelend; irel++)
13574
0
  {
13575
0
    if (irel->r_offset == pNext->relo->r_offset)
13576
0
      {
13577
        /* Ignore Non-TLS relocation types.  */
13578
0
        r_type = ELF32_R_TYPE (irel->r_info);
13579
0
        if ((R_NDS32_TLS_LE_HI20 > r_type)
13580
0
      || (R_NDS32_RELAX_ENTRY == r_type))
13581
0
    continue;
13582
13583
0
        if (!list_insert_sibling (pNext, irel))
13584
0
    goto error_return;
13585
0
      }
13586
0
    else if (irel->r_offset > pNext->relo->r_offset)
13587
0
      {
13588
0
        pNext = pNext->next;
13589
0
        if (!pNext)
13590
0
    break;
13591
13592
0
        bfd_vma current_offset = pNext->relo->r_offset;
13593
0
        if (irel->r_offset > current_offset)
13594
0
    irel = internal_relocs; /* restart from head */
13595
0
        else
13596
0
    --irel; /* Check current irel again.  */
13597
0
        continue;
13598
0
      }
13599
0
    else
13600
0
      {
13601
        /* This shouldn't be reached.  */
13602
0
      }
13603
0
  }
13604
0
      if (pNext)
13605
0
  pNext = pNext->next;
13606
0
    }
13607
13608
#ifdef DUBUG_VERBOSE
13609
  dump_chain(&chain);
13610
#endif
13611
13612
  /* Get symbol table and section content.  */
13613
0
  if (incontents)
13614
0
    contents = incontents;
13615
0
  else if (!nds32_get_section_contents (inbfd, insec, &contents, true)
13616
0
     || !nds32_get_local_syms (inbfd, insec, &local_syms))
13617
0
    goto error_return;
13618
13619
0
  char *local_got_tls_type = elf32_nds32_local_got_tls_type (inbfd);
13620
13621
  /* Convert TLS model each group if necessary.  */
13622
0
  pNext = chain.next;
13623
13624
0
  int cur_grp_id = -1;
13625
0
  int sethi_rt = -1;
13626
0
  int add_rt = -1;
13627
0
  enum elf_nds32_tls_type tls_type, org_tls_type, eff_tls_type;
13628
13629
0
  tls_type = org_tls_type = eff_tls_type = 0;
13630
13631
0
  while (pNext)
13632
0
    {
13633
0
      relax_group_list_t *pNextSig = pNext->next_sibling;
13634
0
      while (pNextSig)
13635
0
  {
13636
0
    struct elf_link_hash_entry *h = NULL;
13637
13638
0
    irel = pNextSig->relo;
13639
0
    r_symndx = ELF32_R_SYM(irel->r_info);
13640
0
    r_type = ELF32_R_TYPE(irel->r_info);
13641
13642
0
    if (pNext->id != cur_grp_id)
13643
0
      {
13644
0
        cur_grp_id = pNext->id;
13645
0
        org_tls_type = get_tls_type (r_type, NULL);
13646
0
        if (r_symndx >= symtab_hdr->sh_info)
13647
0
    {
13648
0
      h = sym_hashes[r_symndx - symtab_hdr->sh_info];
13649
0
      while (h->root.type == bfd_link_hash_indirect
13650
0
       || h->root.type == bfd_link_hash_warning)
13651
0
        h = (struct elf_link_hash_entry *) h->root.u.i.link;
13652
0
      tls_type = ((struct elf_nds32_link_hash_entry *) h)->tls_type;
13653
0
    }
13654
0
        else
13655
0
    {
13656
0
      tls_type = local_got_tls_type
13657
0
        ? local_got_tls_type[r_symndx]
13658
0
        : GOT_NORMAL;
13659
0
    }
13660
13661
0
        eff_tls_type = 1 << (fls (tls_type) - 1);
13662
0
        sethi_rt = N32_RT5(bfd_getb32 (contents + irel->r_offset));
13663
0
      }
13664
13665
0
    if (eff_tls_type != org_tls_type)
13666
0
      {
13667
0
        switch (org_tls_type)
13668
0
    {
13669
      /* DESC to IEGP/IE/LE.  */
13670
0
    case GOT_TLS_DESC:
13671
0
      switch (eff_tls_type)
13672
0
        {
13673
0
        case GOT_TLS_IE:
13674
0
          switch (r_type)
13675
0
      {
13676
0
      case R_NDS32_TLS_DESC_HI20:
13677
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13678
0
                  R_NDS32_TLS_IE_HI20);
13679
0
        break;
13680
0
      case R_NDS32_TLS_DESC_LO12:
13681
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13682
0
                  R_NDS32_TLS_IE_LO12);
13683
0
        break;
13684
0
      case R_NDS32_TLS_DESC_ADD:
13685
0
        {
13686
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13687
0
          add_rt = N32_RT5 (insn);
13688
0
          insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13689
0
          bfd_putb32 (insn, contents + irel->r_offset);
13690
13691
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13692
0
        }
13693
0
        break;
13694
0
      case R_NDS32_TLS_DESC_FUNC:
13695
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13696
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13697
0
                  R_NDS32_RELAX_REMOVE);
13698
0
        break;
13699
0
      case R_NDS32_TLS_DESC_CALL:
13700
0
        {
13701
0
          uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13702
0
                 REG_TP);
13703
0
          bfd_putb32 (insn, contents + irel->r_offset);
13704
13705
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13706
0
        }
13707
0
        break;
13708
0
      case R_NDS32_LOADSTORE:
13709
0
      case R_NDS32_PTR:
13710
0
      case R_NDS32_PTR_RESOLVED:
13711
0
      case R_NDS32_NONE:
13712
0
      case R_NDS32_LABEL:
13713
0
        break;
13714
0
      default:
13715
0
        BFD_ASSERT(0);
13716
0
        break;
13717
0
      }
13718
0
          break;
13719
0
        case GOT_TLS_IEGP:
13720
0
          switch (r_type)
13721
0
      {
13722
0
      case R_NDS32_TLS_DESC_HI20:
13723
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13724
0
                  R_NDS32_TLS_IEGP_HI20);
13725
0
        break;
13726
0
      case R_NDS32_TLS_DESC_LO12:
13727
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13728
0
                  R_NDS32_TLS_IEGP_LO12);
13729
0
        break;
13730
0
      case R_NDS32_TLS_DESC_ADD:
13731
0
        {
13732
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13733
0
          add_rt = N32_RT5 (insn);
13734
0
          insn = N32_MEM(LW, add_rt, sethi_rt, REG_GP, 0);
13735
0
          bfd_putb32 (insn, contents + irel->r_offset);
13736
13737
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13738
0
        }
13739
0
        break;
13740
0
      case R_NDS32_TLS_DESC_FUNC:
13741
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13742
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13743
0
                  R_NDS32_RELAX_REMOVE);
13744
0
        break;
13745
0
      case R_NDS32_TLS_DESC_CALL:
13746
0
        {
13747
0
          uint32_t insn = N32_ALU1(ADD, REG_R0, add_rt,
13748
0
                 REG_TP);
13749
0
          bfd_putb32 (insn, contents + irel->r_offset);
13750
13751
0
          irel->r_info = ELF32_R_INFO(r_symndx, R_NDS32_NONE);
13752
0
        }
13753
0
        break;
13754
0
      case R_NDS32_LOADSTORE:
13755
0
      case R_NDS32_PTR:
13756
0
      case R_NDS32_PTR_RESOLVED:
13757
0
      case R_NDS32_NONE:
13758
0
      case R_NDS32_LABEL:
13759
0
        break;
13760
0
      default:
13761
0
        BFD_ASSERT(0);
13762
0
        break;
13763
0
      }
13764
0
          break;
13765
0
        case GOT_TLS_LE:
13766
0
          switch (r_type)
13767
0
      {
13768
0
      case R_NDS32_TLS_DESC_HI20:
13769
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13770
0
        break;
13771
0
      case R_NDS32_TLS_DESC_LO12:
13772
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13773
0
        break;
13774
0
      case R_NDS32_TLS_DESC_ADD:
13775
0
        {
13776
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13777
13778
0
          add_rt = N32_RT5 (insn);
13779
0
          insn = N32_ALU1 (ADD, REG_R0, sethi_rt, REG_TP);
13780
0
          bfd_putb32 (insn, contents + irel->r_offset);
13781
13782
0
          irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_ADD);
13783
0
        }
13784
0
        break;
13785
0
      case R_NDS32_TLS_DESC_FUNC:
13786
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13787
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13788
0
        break;
13789
0
      case R_NDS32_TLS_DESC_CALL:
13790
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13791
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13792
0
        break;
13793
0
      case R_NDS32_LOADSTORE:
13794
0
      case R_NDS32_PTR:
13795
0
      case R_NDS32_PTR_RESOLVED:
13796
0
      case R_NDS32_NONE:
13797
0
      case R_NDS32_LABEL:
13798
0
        break;
13799
0
      default:
13800
0
        BFD_ASSERT(0);
13801
0
        break;
13802
0
      }
13803
0
          break;
13804
0
        default:
13805
0
          break;
13806
0
        }
13807
0
      break;
13808
      /* IEGP to IE/LE.  */
13809
0
    case GOT_TLS_IEGP:
13810
0
      switch (eff_tls_type)
13811
0
        {
13812
0
        case GOT_TLS_IE:
13813
0
          switch (r_type)
13814
0
      {
13815
0
      case R_NDS32_TLS_IEGP_HI20:
13816
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13817
0
                  R_NDS32_TLS_IE_HI20);
13818
0
        break;
13819
0
      case R_NDS32_TLS_IEGP_LO12:
13820
0
        irel->r_info = ELF32_R_INFO(r_symndx,
13821
0
                  R_NDS32_TLS_IE_LO12);
13822
0
        break;
13823
0
      case R_NDS32_PTR_RESOLVED:
13824
0
        {
13825
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13826
13827
0
          add_rt = N32_RT5 (insn);
13828
0
          insn = N32_TYPE2 (LWI, add_rt, sethi_rt, 0);
13829
0
          bfd_putb32 (insn, contents + irel->r_offset);
13830
0
        }
13831
0
        break;
13832
0
      case R_NDS32_TLS_IEGP_LW:
13833
0
        break;
13834
0
      case R_NDS32_LOADSTORE:
13835
0
      case R_NDS32_PTR:
13836
0
      case R_NDS32_NONE:
13837
0
      case R_NDS32_LABEL:
13838
0
        break;
13839
0
      default:
13840
0
        BFD_ASSERT(0);
13841
0
        break;
13842
0
      }
13843
0
          break;
13844
0
        case GOT_TLS_LE:
13845
0
          switch (r_type)
13846
0
      {
13847
0
      case R_NDS32_TLS_IEGP_HI20:
13848
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13849
0
        break;
13850
0
      case R_NDS32_TLS_IEGP_LO12:
13851
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13852
0
        break;
13853
0
      case R_NDS32_TLS_IEGP_LW:
13854
0
        bfd_putb32 (INSN_NOP, contents + irel->r_offset);
13855
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_RELAX_REMOVE);
13856
0
        break;
13857
0
      case R_NDS32_LOADSTORE:
13858
0
      case R_NDS32_PTR:
13859
0
      case R_NDS32_NONE:
13860
0
      case R_NDS32_LABEL:
13861
0
      case R_NDS32_PTR_RESOLVED:
13862
0
        break;
13863
0
      default:
13864
0
        BFD_ASSERT(0);
13865
0
        break;
13866
0
      }
13867
0
          break;
13868
0
        default:
13869
0
          break;
13870
0
        }
13871
0
      break;
13872
      /* IE to LE. */
13873
0
    case GOT_TLS_IE:
13874
0
      switch (eff_tls_type)
13875
0
        {
13876
0
        case GOT_TLS_LE:
13877
0
          switch (r_type)
13878
0
      {
13879
0
      case R_NDS32_TLS_IE_HI20:
13880
0
        irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_HI20);
13881
0
        break;
13882
0
      case R_NDS32_TLS_IE_LO12S2:
13883
0
        {
13884
0
          uint32_t insn = bfd_getb32 (contents + irel->r_offset);
13885
13886
0
          add_rt = N32_RT5 (insn);
13887
0
          insn = N32_TYPE2 (ORI, add_rt, sethi_rt, 0);
13888
0
          bfd_putb32 (insn, contents + irel->r_offset);
13889
13890
0
          irel->r_info = ELF32_R_INFO (r_symndx, R_NDS32_TLS_LE_LO12);
13891
0
        }
13892
0
        break;
13893
0
      case R_NDS32_LOADSTORE:
13894
0
      case R_NDS32_PTR:
13895
0
      case R_NDS32_NONE:
13896
0
      case R_NDS32_LABEL:
13897
0
        break;
13898
0
      default:
13899
0
        BFD_ASSERT(0);
13900
0
        break;
13901
0
      }
13902
0
          break;
13903
0
        default:
13904
0
          break;
13905
0
        }
13906
0
      break;
13907
0
    default:
13908
0
      break;
13909
0
    }
13910
0
      }
13911
0
    pNextSig = pNextSig->next_sibling;
13912
0
  }
13913
13914
0
#if 1
13915
0
      pNext = pNext->next;
13916
#else
13917
      while (pNext)
13918
  {
13919
    if (pNext->id != cur_grp_id)
13920
      break;
13921
    pNext = pNext->next;
13922
  }
13923
#endif
13924
0
    }
13925
13926
0
 finish:
13927
0
  if (incontents)
13928
0
    contents = NULL;
13929
13930
0
  if (elf_section_data (insec)->relocs != internal_relocs)
13931
0
    free (internal_relocs);
13932
13933
0
  if (elf_section_data (insec)->this_hdr.contents != contents)
13934
0
    free (contents);
13935
13936
0
  if (symtab_hdr->contents != (bfd_byte *) local_syms)
13937
0
    free (local_syms);
13938
13939
0
  if (chain.next)
13940
0
    {
13941
0
      pNext = chain.next;
13942
0
      relax_group_list_t *pDel;
13943
0
      while (pNext)
13944
0
  {
13945
0
    pDel = pNext;
13946
0
    pNext = pNext->next;
13947
0
    free (pDel);
13948
0
  }
13949
0
    }
13950
13951
0
  return result;
13952
13953
0
 error_return:
13954
0
  result = false;
13955
0
  goto finish;
13956
0
}
13957
13958
/* End TLS model conversion.  */
13959
13960
#define ELF_ARCH        bfd_arch_nds32
13961
#define ELF_MACHINE_CODE      EM_NDS32
13962
#define ELF_MAXPAGESIZE       0x1000
13963
#define ELF_TARGET_ID       NDS32_ELF_DATA
13964
13965
#define TARGET_BIG_SYM        nds32_elf32_be_vec
13966
#define TARGET_BIG_NAME       "elf32-nds32be"
13967
#define TARGET_LITTLE_SYM     nds32_elf32_le_vec
13968
#define TARGET_LITTLE_NAME      "elf32-nds32le"
13969
13970
#define elf_info_to_howto     nds32_info_to_howto
13971
#define elf_info_to_howto_rel     nds32_info_to_howto_rel
13972
13973
#define bfd_elf32_bfd_link_hash_table_create  nds32_elf_link_hash_table_create
13974
#define bfd_elf32_bfd_merge_private_bfd_data  nds32_elf_merge_private_bfd_data
13975
#define bfd_elf32_bfd_print_private_bfd_data  nds32_elf_print_private_bfd_data
13976
#define bfd_elf32_bfd_relax_section   nds32_elf_relax_section
13977
#define bfd_elf32_bfd_set_private_flags   nds32_elf_set_private_flags
13978
13979
#define bfd_elf32_mkobject      nds32_elf_mkobject
13980
#define elf_backend_action_discarded    nds32_elf_action_discarded
13981
#define elf_backend_add_symbol_hook   nds32_elf_add_symbol_hook
13982
#define elf_backend_check_relocs    nds32_elf_check_relocs
13983
#define elf_backend_adjust_dynamic_symbol nds32_elf_adjust_dynamic_symbol
13984
#define elf_backend_create_dynamic_sections nds32_elf_create_dynamic_sections
13985
#define elf_backend_finish_dynamic_sections nds32_elf_finish_dynamic_sections
13986
#define elf_backend_finish_dynamic_symbol nds32_elf_finish_dynamic_symbol
13987
#define elf_backend_size_dynamic_sections nds32_elf_size_dynamic_sections
13988
#define elf_backend_relocate_section    nds32_elf_relocate_section
13989
#define elf_backend_gc_mark_hook    nds32_elf_gc_mark_hook
13990
#define elf_backend_grok_prstatus   nds32_elf_grok_prstatus
13991
#define elf_backend_grok_psinfo     nds32_elf_grok_psinfo
13992
#define elf_backend_reloc_type_class    nds32_elf_reloc_type_class
13993
#define elf_backend_copy_indirect_symbol  nds32_elf_copy_indirect_symbol
13994
#define elf_backend_link_output_symbol_hook nds32_elf_output_symbol_hook
13995
#define elf_backend_output_arch_syms    nds32_elf_output_arch_syms
13996
#define elf_backend_object_p      nds32_elf_object_p
13997
#define elf_backend_final_write_processing  nds32_elf_final_write_processing
13998
#define elf_backend_special_sections    nds32_elf_special_sections
13999
#define elf_backend_section_flags   nds32_elf_section_flags
14000
#define bfd_elf32_bfd_get_relocated_section_contents \
14001
        nds32_elf_get_relocated_section_contents
14002
#define bfd_elf32_bfd_is_target_special_symbol  nds32_elf_is_target_special_symbol
14003
#define elf_backend_maybe_function_sym    nds32_elf_maybe_function_sym
14004
14005
#define elf_backend_can_gc_sections   1
14006
#define elf_backend_can_refcount    1
14007
#define elf_backend_want_got_plt    1
14008
#define elf_backend_plt_readonly    1
14009
#define elf_backend_want_plt_sym    0
14010
#define elf_backend_got_header_size   12
14011
#define elf_backend_may_use_rel_p   1
14012
#define elf_backend_default_use_rela_p    1
14013
#define elf_backend_may_use_rela_p    1
14014
#define elf_backend_dtrel_excludes_plt    0
14015
14016
#include "elf32-target.h"
14017
14018
#undef ELF_MAXPAGESIZE
14019
#define ELF_MAXPAGESIZE       0x2000
14020
14021
#undef  TARGET_BIG_SYM
14022
#define TARGET_BIG_SYM        nds32_elf32_linux_be_vec
14023
#undef  TARGET_BIG_NAME
14024
#define TARGET_BIG_NAME       "elf32-nds32be-linux"
14025
#undef  TARGET_LITTLE_SYM
14026
#define TARGET_LITTLE_SYM     nds32_elf32_linux_le_vec
14027
#undef  TARGET_LITTLE_NAME
14028
#define TARGET_LITTLE_NAME      "elf32-nds32le-linux"
14029
#undef  elf32_bed
14030
#define elf32_bed       elf32_nds32_lin_bed
14031
14032
#include "elf32-target.h"