Coverage Report

Created: 2026-03-10 08:46

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-frv.c
Line
Count
Source
1
/* FRV-specific support for 32-bit ELF.
2
   Copyright (C) 2002-2026 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include "bfd.h"
23
#include "libbfd.h"
24
#include "elf-bfd.h"
25
#include "elf/frv.h"
26
#include "dwarf2.h"
27
#include "hashtab.h"
28
#include "libiberty.h"
29
30
/* Forward declarations.  */
31
32
33
static reloc_howto_type elf32_frv_howto_table [] =
34
{
35
  /* This reloc does nothing.  */
36
  HOWTO (R_FRV_NONE,    /* type */
37
   0,     /* rightshift */
38
   0,     /* size */
39
   0,     /* bitsize */
40
   false,     /* pc_relative */
41
   0,     /* bitpos */
42
   complain_overflow_dont, /* complain_on_overflow */
43
   bfd_elf_generic_reloc, /* special_function */
44
   "R_FRV_NONE",    /* name */
45
   false,     /* partial_inplace */
46
   0,     /* src_mask */
47
   0,     /* dst_mask */
48
   false),    /* pcrel_offset */
49
50
  /* A 32 bit absolute relocation.  */
51
  HOWTO (R_FRV_32,    /* type */
52
   0,     /* rightshift */
53
   4,     /* size */
54
   32,      /* bitsize */
55
   false,     /* pc_relative */
56
   0,     /* bitpos */
57
   complain_overflow_bitfield, /* complain_on_overflow */
58
   bfd_elf_generic_reloc, /* special_function */
59
   "R_FRV_32",    /* name */
60
   false,     /* partial_inplace */
61
   0xffffffff,    /* src_mask */
62
   0xffffffff,    /* dst_mask */
63
   false),    /* pcrel_offset */
64
65
  /* A 16 bit pc-relative relocation.  */
66
  HOWTO (R_FRV_LABEL16,   /* type */
67
   2,     /* rightshift */
68
   4,     /* size */
69
   16,      /* bitsize */
70
   true,      /* pc_relative */
71
   0,     /* bitpos */
72
   complain_overflow_signed, /* complain_on_overflow */
73
   bfd_elf_generic_reloc, /* special_function */
74
   "R_FRV_LABEL16", /* name */
75
   false,     /* partial_inplace */
76
   0xffff,    /* src_mask */
77
   0xffff,    /* dst_mask */
78
   true),     /* pcrel_offset */
79
80
  /* A 24-bit pc-relative relocation.  */
81
  HOWTO (R_FRV_LABEL24,   /* type */
82
   2,     /* rightshift */
83
   4,     /* size */
84
   26,      /* bitsize */
85
   true,      /* pc_relative */
86
   0,     /* bitpos */
87
   complain_overflow_bitfield, /* complain_on_overflow */
88
   bfd_elf_generic_reloc, /* special_function */
89
   "R_FRV_LABEL24", /* name */
90
   false,     /* partial_inplace */
91
   0x7e03ffff,    /* src_mask */
92
   0x7e03ffff,    /* dst_mask */
93
   true),     /* pcrel_offset */
94
95
  HOWTO (R_FRV_LO16,    /* type */
96
   0,     /* rightshift */
97
   4,     /* size */
98
   16,      /* bitsize */
99
   false,     /* pc_relative */
100
   0,     /* bitpos */
101
   complain_overflow_dont, /* complain_on_overflow */
102
   bfd_elf_generic_reloc, /* special_function */
103
   "R_FRV_LO16",    /* name */
104
   false,     /* partial_inplace */
105
   0xffff,    /* src_mask */
106
   0xffff,    /* dst_mask */
107
   false),    /* pcrel_offset */
108
109
  HOWTO (R_FRV_HI16,    /* type */
110
   0,     /* rightshift */
111
   4,     /* size */
112
   16,      /* bitsize */
113
   false,     /* pc_relative */
114
   0,     /* bitpos */
115
   complain_overflow_dont, /* complain_on_overflow */
116
   bfd_elf_generic_reloc, /* special_function */
117
   "R_FRV_HI16",    /* name */
118
   false,     /* partial_inplace */
119
   0xffff,    /* src_mask */
120
   0xffff,    /* dst_mask */
121
   false),    /* pcrel_offset */
122
123
  HOWTO (R_FRV_GPREL12,   /* type */
124
   0,     /* rightshift */
125
   4,     /* size */
126
   12,      /* bitsize */
127
   false,     /* pc_relative */
128
   0,     /* bitpos */
129
   complain_overflow_dont, /* complain_on_overflow */
130
   bfd_elf_generic_reloc, /* special_function */
131
   "R_FRV_GPREL12", /* name */
132
   false,     /* partial_inplace */
133
   0xfff,     /* src_mask */
134
   0xfff,     /* dst_mask */
135
   false),    /* pcrel_offset */
136
137
  HOWTO (R_FRV_GPRELU12,  /* type */
138
   0,     /* rightshift */
139
   4,     /* size */
140
   12,      /* bitsize */
141
   false,     /* pc_relative */
142
   0,     /* bitpos */
143
   complain_overflow_dont, /* complain_on_overflow */
144
   bfd_elf_generic_reloc, /* special_function */
145
   "R_FRV_GPRELU12",  /* name */
146
   false,     /* partial_inplace */
147
   0xfff,     /* src_mask */
148
   0x3f03f,   /* dst_mask */
149
   false),    /* pcrel_offset */
150
151
  HOWTO (R_FRV_GPREL32,   /* type */
152
   0,     /* rightshift */
153
   4,     /* size */
154
   32,      /* bitsize */
155
   false,     /* pc_relative */
156
   0,     /* bitpos */
157
   complain_overflow_dont, /* complain_on_overflow */
158
   bfd_elf_generic_reloc, /* special_function */
159
   "R_FRV_GPREL32", /* name */
160
   false,     /* partial_inplace */
161
   0xffffffff,    /* src_mask */
162
   0xffffffff,    /* dst_mask */
163
   false),    /* pcrel_offset */
164
165
  HOWTO (R_FRV_GPRELHI,   /* type */
166
   0,     /* rightshift */
167
   4,     /* size */
168
   16,      /* bitsize */
169
   false,     /* pc_relative */
170
   0,     /* bitpos */
171
   complain_overflow_dont, /* complain_on_overflow */
172
   bfd_elf_generic_reloc, /* special_function */
173
   "R_FRV_GPRELHI", /* name */
174
   false,     /* partial_inplace */
175
   0xffff,    /* src_mask */
176
   0xffff,    /* dst_mask */
177
   false),    /* pcrel_offset */
178
179
  HOWTO (R_FRV_GPRELLO,   /* type */
180
   0,     /* rightshift */
181
   4,     /* size */
182
   16,      /* bitsize */
183
   false,     /* pc_relative */
184
   0,     /* bitpos */
185
   complain_overflow_dont, /* complain_on_overflow */
186
   bfd_elf_generic_reloc, /* special_function */
187
   "R_FRV_GPRELLO", /* name */
188
   false,     /* partial_inplace */
189
   0xffff,    /* src_mask */
190
   0xffff,    /* dst_mask */
191
   false),    /* pcrel_offset */
192
193
  /* A 12-bit signed operand with the GOT offset for the address of
194
     the symbol.  */
195
  HOWTO (R_FRV_GOT12,   /* type */
196
   0,     /* rightshift */
197
   4,     /* size */
198
   12,      /* bitsize */
199
   false,     /* pc_relative */
200
   0,     /* bitpos */
201
   complain_overflow_signed, /* complain_on_overflow */
202
   bfd_elf_generic_reloc, /* special_function */
203
   "R_FRV_GOT12",   /* name */
204
   false,     /* partial_inplace */
205
   0xfff,     /* src_mask */
206
   0xfff,     /* dst_mask */
207
   false),    /* pcrel_offset */
208
209
  /* The upper 16 bits of the GOT offset for the address of the
210
     symbol.  */
211
  HOWTO (R_FRV_GOTHI,   /* type */
212
   0,     /* rightshift */
213
   4,     /* size */
214
   16,      /* bitsize */
215
   false,     /* pc_relative */
216
   0,     /* bitpos */
217
   complain_overflow_dont, /* complain_on_overflow */
218
   bfd_elf_generic_reloc, /* special_function */
219
   "R_FRV_GOTHI",   /* name */
220
   false,     /* partial_inplace */
221
   0xffff,    /* src_mask */
222
   0xffff,    /* dst_mask */
223
   false),    /* pcrel_offset */
224
225
  /* The lower 16 bits of the GOT offset for the address of the
226
     symbol.  */
227
  HOWTO (R_FRV_GOTLO,   /* type */
228
   0,     /* rightshift */
229
   4,     /* size */
230
   16,      /* bitsize */
231
   false,     /* pc_relative */
232
   0,     /* bitpos */
233
   complain_overflow_dont, /* complain_on_overflow */
234
   bfd_elf_generic_reloc, /* special_function */
235
   "R_FRV_GOTLO",   /* name */
236
   false,     /* partial_inplace */
237
   0xffff,    /* src_mask */
238
   0xffff,    /* dst_mask */
239
   false),    /* pcrel_offset */
240
241
  /* The 32-bit address of the canonical descriptor of a function.  */
242
  HOWTO (R_FRV_FUNCDESC,  /* type */
243
   0,     /* rightshift */
244
   4,     /* size */
245
   32,      /* bitsize */
246
   false,     /* pc_relative */
247
   0,     /* bitpos */
248
   complain_overflow_bitfield, /* complain_on_overflow */
249
   bfd_elf_generic_reloc, /* special_function */
250
   "R_FRV_FUNCDESC",  /* name */
251
   false,     /* partial_inplace */
252
   0xffffffff,    /* src_mask */
253
   0xffffffff,    /* dst_mask */
254
   false),    /* pcrel_offset */
255
256
  /* A 12-bit signed operand with the GOT offset for the address of
257
     canonical descriptor of a function.  */
258
  HOWTO (R_FRV_FUNCDESC_GOT12,  /* type */
259
   0,     /* rightshift */
260
   4,     /* size */
261
   12,      /* bitsize */
262
   false,     /* pc_relative */
263
   0,     /* bitpos */
264
   complain_overflow_signed, /* complain_on_overflow */
265
   bfd_elf_generic_reloc, /* special_function */
266
   "R_FRV_FUNCDESC_GOT12", /* name */
267
   false,     /* partial_inplace */
268
   0xfff,     /* src_mask */
269
   0xfff,     /* dst_mask */
270
   false),    /* pcrel_offset */
271
272
  /* The upper 16 bits of the GOT offset for the address of the
273
     canonical descriptor of a function.  */
274
  HOWTO (R_FRV_FUNCDESC_GOTHI,  /* type */
275
   0,     /* rightshift */
276
   4,     /* size */
277
   16,      /* bitsize */
278
   false,     /* pc_relative */
279
   0,     /* bitpos */
280
   complain_overflow_dont, /* complain_on_overflow */
281
   bfd_elf_generic_reloc, /* special_function */
282
   "R_FRV_FUNCDESC_GOTHI", /* name */
283
   false,     /* partial_inplace */
284
   0xffff,    /* src_mask */
285
   0xffff,    /* dst_mask */
286
   false),    /* pcrel_offset */
287
288
  /* The lower 16 bits of the GOT offset for the address of the
289
     canonical descriptor of a function.  */
290
  HOWTO (R_FRV_FUNCDESC_GOTLO,  /* type */
291
   0,     /* rightshift */
292
   4,     /* size */
293
   16,      /* bitsize */
294
   false,     /* pc_relative */
295
   0,     /* bitpos */
296
   complain_overflow_dont, /* complain_on_overflow */
297
   bfd_elf_generic_reloc, /* special_function */
298
   "R_FRV_FUNCDESC_GOTLO", /* name */
299
   false,     /* partial_inplace */
300
   0xffff,    /* src_mask */
301
   0xffff,    /* dst_mask */
302
   false),    /* pcrel_offset */
303
304
  /* The 64-bit descriptor of a function.  */
305
  HOWTO (R_FRV_FUNCDESC_VALUE,  /* type */
306
   0,     /* rightshift */
307
   4,     /* size */
308
   64,      /* bitsize */
309
   false,     /* pc_relative */
310
   0,     /* bitpos */
311
   complain_overflow_bitfield, /* complain_on_overflow */
312
   bfd_elf_generic_reloc, /* special_function */
313
   "R_FRV_FUNCDESC_VALUE", /* name */
314
   false,     /* partial_inplace */
315
   0xffffffff,    /* src_mask */
316
   0xffffffff,    /* dst_mask */
317
   false),    /* pcrel_offset */
318
319
  /* A 12-bit signed operand with the GOT offset for the address of
320
     canonical descriptor of a function.  */
321
  HOWTO (R_FRV_FUNCDESC_GOTOFF12, /* type */
322
   0,     /* rightshift */
323
   4,     /* size */
324
   12,      /* bitsize */
325
   false,     /* pc_relative */
326
   0,     /* bitpos */
327
   complain_overflow_signed, /* complain_on_overflow */
328
   bfd_elf_generic_reloc, /* special_function */
329
   "R_FRV_FUNCDESC_GOTOFF12", /* name */
330
   false,     /* partial_inplace */
331
   0xfff,     /* src_mask */
332
   0xfff,     /* dst_mask */
333
   false),    /* pcrel_offset */
334
335
  /* The upper 16 bits of the GOT offset for the address of the
336
     canonical descriptor of a function.  */
337
  HOWTO (R_FRV_FUNCDESC_GOTOFFHI, /* type */
338
   0,     /* rightshift */
339
   4,     /* size */
340
   16,      /* bitsize */
341
   false,     /* pc_relative */
342
   0,     /* bitpos */
343
   complain_overflow_dont, /* complain_on_overflow */
344
   bfd_elf_generic_reloc, /* special_function */
345
   "R_FRV_FUNCDESC_GOTOFFHI", /* name */
346
   false,     /* partial_inplace */
347
   0xffff,    /* src_mask */
348
   0xffff,    /* dst_mask */
349
   false),    /* pcrel_offset */
350
351
  /* The lower 16 bits of the GOT offset for the address of the
352
     canonical descriptor of a function.  */
353
  HOWTO (R_FRV_FUNCDESC_GOTOFFLO, /* type */
354
   0,     /* rightshift */
355
   4,     /* size */
356
   16,      /* bitsize */
357
   false,     /* pc_relative */
358
   0,     /* bitpos */
359
   complain_overflow_dont, /* complain_on_overflow */
360
   bfd_elf_generic_reloc, /* special_function */
361
   "R_FRV_FUNCDESC_GOTOFFLO", /* name */
362
   false,     /* partial_inplace */
363
   0xffff,    /* src_mask */
364
   0xffff,    /* dst_mask */
365
   false),    /* pcrel_offset */
366
367
  /* A 12-bit signed operand with the GOT offset for the address of
368
     the symbol.  */
369
  HOWTO (R_FRV_GOTOFF12,  /* type */
370
   0,     /* rightshift */
371
   4,     /* size */
372
   12,      /* bitsize */
373
   false,     /* pc_relative */
374
   0,     /* bitpos */
375
   complain_overflow_signed, /* complain_on_overflow */
376
   bfd_elf_generic_reloc, /* special_function */
377
   "R_FRV_GOTOFF12",  /* name */
378
   false,     /* partial_inplace */
379
   0xfff,     /* src_mask */
380
   0xfff,     /* dst_mask */
381
   false),    /* pcrel_offset */
382
383
  /* The upper 16 bits of the GOT offset for the address of the
384
     symbol.  */
385
  HOWTO (R_FRV_GOTOFFHI,  /* type */
386
   0,     /* rightshift */
387
   4,     /* size */
388
   16,      /* bitsize */
389
   false,     /* pc_relative */
390
   0,     /* bitpos */
391
   complain_overflow_dont, /* complain_on_overflow */
392
   bfd_elf_generic_reloc, /* special_function */
393
   "R_FRV_GOTOFFHI",  /* name */
394
   false,     /* partial_inplace */
395
   0xffff,    /* src_mask */
396
   0xffff,    /* dst_mask */
397
   false),    /* pcrel_offset */
398
399
  /* The lower 16 bits of the GOT offset for the address of the
400
     symbol.  */
401
  HOWTO (R_FRV_GOTOFFLO,  /* type */
402
   0,     /* rightshift */
403
   4,     /* size */
404
   16,      /* bitsize */
405
   false,     /* pc_relative */
406
   0,     /* bitpos */
407
   complain_overflow_dont, /* complain_on_overflow */
408
   bfd_elf_generic_reloc, /* special_function */
409
   "R_FRV_GOTOFFLO",  /* name */
410
   false,     /* partial_inplace */
411
   0xffff,    /* src_mask */
412
   0xffff,    /* dst_mask */
413
   false),    /* pcrel_offset */
414
415
  /* A 24-bit pc-relative relocation referencing the TLS PLT entry for
416
     a thread-local symbol.  If the symbol number is 0, it refers to
417
     the module.  */
418
  HOWTO (R_FRV_GETTLSOFF, /* type */
419
   2,     /* rightshift */
420
   4,     /* size */
421
   26,      /* bitsize */
422
   true,      /* pc_relative */
423
   0,     /* bitpos */
424
   complain_overflow_bitfield, /* complain_on_overflow */
425
   bfd_elf_generic_reloc, /* special_function */
426
   "R_FRV_GETTLSOFF", /* name */
427
   false,     /* partial_inplace */
428
   0x7e03ffff,    /* src_mask */
429
   0x7e03ffff,    /* dst_mask */
430
   true),     /* pcrel_offset */
431
432
  /* A 64-bit TLS descriptor for a symbol.  This relocation is only
433
     valid as a REL, dynamic relocation.  */
434
  HOWTO (R_FRV_TLSDESC_VALUE, /* type */
435
   0,     /* rightshift */
436
   4,     /* size */
437
   64,      /* bitsize */
438
   false,     /* pc_relative */
439
   0,     /* bitpos */
440
   complain_overflow_bitfield, /* complain_on_overflow */
441
   bfd_elf_generic_reloc, /* special_function */
442
   "R_FRV_TLSDESC_VALUE", /* name */
443
   false,     /* partial_inplace */
444
   0xffffffff,    /* src_mask */
445
   0xffffffff,    /* dst_mask */
446
   false),    /* pcrel_offset */
447
448
  /* A 12-bit signed operand with the GOT offset for the TLS
449
     descriptor of the symbol.  */
450
  HOWTO (R_FRV_GOTTLSDESC12,  /* type */
451
   0,     /* rightshift */
452
   4,     /* size */
453
   12,      /* bitsize */
454
   false,     /* pc_relative */
455
   0,     /* bitpos */
456
   complain_overflow_signed, /* complain_on_overflow */
457
   bfd_elf_generic_reloc, /* special_function */
458
   "R_FRV_GOTTLSDESC12",  /* name */
459
   false,     /* partial_inplace */
460
   0xfff,     /* src_mask */
461
   0xfff,     /* dst_mask */
462
   false),    /* pcrel_offset */
463
464
  /* The upper 16 bits of the GOT offset for the TLS descriptor of the
465
     symbol.  */
466
  HOWTO (R_FRV_GOTTLSDESCHI,  /* type */
467
   0,     /* rightshift */
468
   4,     /* size */
469
   16,      /* bitsize */
470
   false,     /* pc_relative */
471
   0,     /* bitpos */
472
   complain_overflow_dont, /* complain_on_overflow */
473
   bfd_elf_generic_reloc, /* special_function */
474
   "R_FRV_GOTTLSDESCHI",  /* name */
475
   false,     /* partial_inplace */
476
   0xffff,    /* src_mask */
477
   0xffff,    /* dst_mask */
478
   false),    /* pcrel_offset */
479
480
  /* The lower 16 bits of the GOT offset for the TLS descriptor of the
481
     symbol.  */
482
  HOWTO (R_FRV_GOTTLSDESCLO,  /* type */
483
   0,     /* rightshift */
484
   4,     /* size */
485
   16,      /* bitsize */
486
   false,     /* pc_relative */
487
   0,     /* bitpos */
488
   complain_overflow_dont, /* complain_on_overflow */
489
   bfd_elf_generic_reloc, /* special_function */
490
   "R_FRV_GOTTLSDESCLO",  /* name */
491
   false,     /* partial_inplace */
492
   0xffff,    /* src_mask */
493
   0xffff,    /* dst_mask */
494
   false),    /* pcrel_offset */
495
496
  /* A 12-bit signed operand with the offset from the module base
497
     address to the thread-local symbol address.  */
498
  HOWTO (R_FRV_TLSMOFF12,  /* type */
499
   0,     /* rightshift */
500
   4,     /* size */
501
   12,      /* bitsize */
502
   false,     /* pc_relative */
503
   0,     /* bitpos */
504
   complain_overflow_signed, /* complain_on_overflow */
505
   bfd_elf_generic_reloc, /* special_function */
506
   "R_FRV_TLSMOFF12", /* name */
507
   false,     /* partial_inplace */
508
   0xfff,     /* src_mask */
509
   0xfff,     /* dst_mask */
510
   false),    /* pcrel_offset */
511
512
  /* The upper 16 bits of the offset from the module base address to
513
     the thread-local symbol address.  */
514
  HOWTO (R_FRV_TLSMOFFHI, /* type */
515
   0,     /* rightshift */
516
   4,     /* size */
517
   16,      /* bitsize */
518
   false,     /* pc_relative */
519
   0,     /* bitpos */
520
   complain_overflow_dont, /* complain_on_overflow */
521
   bfd_elf_generic_reloc, /* special_function */
522
   "R_FRV_TLSMOFFHI", /* name */
523
   false,     /* partial_inplace */
524
   0xffff,    /* src_mask */
525
   0xffff,    /* dst_mask */
526
   false),    /* pcrel_offset */
527
528
  /* The lower 16 bits of the offset from the module base address to
529
     the thread-local symbol address.  */
530
  HOWTO (R_FRV_TLSMOFFLO, /* type */
531
   0,     /* rightshift */
532
   4,     /* size */
533
   16,      /* bitsize */
534
   false,     /* pc_relative */
535
   0,     /* bitpos */
536
   complain_overflow_dont, /* complain_on_overflow */
537
   bfd_elf_generic_reloc, /* special_function */
538
   "R_FRV_TLSMOFFLO", /* name */
539
   false,     /* partial_inplace */
540
   0xffff,    /* src_mask */
541
   0xffff,    /* dst_mask */
542
   false),    /* pcrel_offset */
543
544
  /* A 12-bit signed operand with the GOT offset for the TLSOFF entry
545
     for a symbol.  */
546
  HOWTO (R_FRV_GOTTLSOFF12, /* type */
547
   0,     /* rightshift */
548
   4,     /* size */
549
   12,      /* bitsize */
550
   false,     /* pc_relative */
551
   0,     /* bitpos */
552
   complain_overflow_signed, /* complain_on_overflow */
553
   bfd_elf_generic_reloc, /* special_function */
554
   "R_FRV_GOTTLSOFF12", /* name */
555
   false,     /* partial_inplace */
556
   0xfff,     /* src_mask */
557
   0xfff,     /* dst_mask */
558
   false),    /* pcrel_offset */
559
560
  /* The upper 16 bits of the GOT offset for the TLSOFF entry for a
561
     symbol.  */
562
  HOWTO (R_FRV_GOTTLSOFFHI, /* type */
563
   0,     /* rightshift */
564
   4,     /* size */
565
   16,      /* bitsize */
566
   false,     /* pc_relative */
567
   0,     /* bitpos */
568
   complain_overflow_dont, /* complain_on_overflow */
569
   bfd_elf_generic_reloc, /* special_function */
570
   "R_FRV_GOTTLSOFFHI", /* name */
571
   false,     /* partial_inplace */
572
   0xffff,    /* src_mask */
573
   0xffff,    /* dst_mask */
574
   false),    /* pcrel_offset */
575
576
  /* The lower 16 bits of the GOT offset for the TLSOFF entry for a
577
     symbol.  */
578
  HOWTO (R_FRV_GOTTLSOFFLO, /* type */
579
   0,     /* rightshift */
580
   4,     /* size */
581
   16,      /* bitsize */
582
   false,     /* pc_relative */
583
   0,     /* bitpos */
584
   complain_overflow_dont, /* complain_on_overflow */
585
   bfd_elf_generic_reloc, /* special_function */
586
   "R_FRV_GOTTLSOFFLO", /* name */
587
   false,     /* partial_inplace */
588
   0xffff,    /* src_mask */
589
   0xffff,    /* dst_mask */
590
   false),    /* pcrel_offset */
591
592
  /* The 32-bit offset from the thread pointer (not the module base
593
     address) to a thread-local symbol.  */
594
  HOWTO (R_FRV_TLSOFF,    /* type */
595
   0,     /* rightshift */
596
   4,     /* size */
597
   32,      /* bitsize */
598
   false,     /* pc_relative */
599
   0,     /* bitpos */
600
   complain_overflow_dont, /* complain_on_overflow */
601
   bfd_elf_generic_reloc, /* special_function */
602
   "R_FRV_TLSOFF",  /* name */
603
   false,     /* partial_inplace */
604
   0xffffffff,    /* src_mask */
605
   0xffffffff,    /* dst_mask */
606
   false),    /* pcrel_offset */
607
608
  /* An annotation for linker relaxation, that denotes the
609
     symbol+addend whose TLS descriptor is referenced by the sum of
610
     the two input registers of an ldd instruction.  */
611
  HOWTO (R_FRV_TLSDESC_RELAX, /* type */
612
   0,     /* rightshift */
613
   4,     /* size */
614
   0,     /* bitsize */
615
   false,     /* pc_relative */
616
   0,     /* bitpos */
617
   complain_overflow_dont, /* complain_on_overflow */
618
   bfd_elf_generic_reloc, /* special_function */
619
   "R_FRV_TLSDESC_RELAX", /* name */
620
   false,     /* partial_inplace */
621
   0,     /* src_mask */
622
   0,     /* dst_mask */
623
   false),    /* pcrel_offset */
624
625
  /* An annotation for linker relaxation, that denotes the
626
     symbol+addend whose TLS resolver entry point is given by the sum
627
     of the two register operands of an calll instruction.  */
628
  HOWTO (R_FRV_GETTLSOFF_RELAX, /* type */
629
   0,     /* rightshift */
630
   4,     /* size */
631
   0,     /* bitsize */
632
   false,     /* pc_relative */
633
   0,     /* bitpos */
634
   complain_overflow_dont, /* complain_on_overflow */
635
   bfd_elf_generic_reloc, /* special_function */
636
   "R_FRV_GETTLSOFF_RELAX", /* name */
637
   false,     /* partial_inplace */
638
   0,     /* src_mask */
639
   0,     /* dst_mask */
640
   false),    /* pcrel_offset */
641
642
  /* An annotation for linker relaxation, that denotes the
643
     symbol+addend whose TLS offset GOT entry is given by the sum of
644
     the two input registers of an ld instruction.  */
645
  HOWTO (R_FRV_TLSOFF_RELAX,  /* type */
646
   0,     /* rightshift */
647
   4,     /* size */
648
   0,     /* bitsize */
649
   false,     /* pc_relative */
650
   0,     /* bitpos */
651
   complain_overflow_bitfield, /* complain_on_overflow */
652
   bfd_elf_generic_reloc, /* special_function */
653
   "R_FRV_TLSOFF_RELAX",  /* name */
654
   false,     /* partial_inplace */
655
   0,     /* src_mask */
656
   0,     /* dst_mask */
657
   false),    /* pcrel_offset */
658
659
  /* A 32-bit offset from the module base address to
660
     the thread-local symbol address.  */
661
  HOWTO (R_FRV_TLSMOFF,   /* type */
662
   0,     /* rightshift */
663
   4,     /* size */
664
   32,      /* bitsize */
665
   false,     /* pc_relative */
666
   0,     /* bitpos */
667
   complain_overflow_dont, /* complain_on_overflow */
668
   bfd_elf_generic_reloc, /* special_function */
669
   "R_FRV_TLSMOFF", /* name */
670
   false,     /* partial_inplace */
671
   0xffffffff,    /* src_mask */
672
   0xffffffff,    /* dst_mask */
673
   false),    /* pcrel_offset */
674
};
675
676
/* GNU extension to record C++ vtable hierarchy.  */
677
static reloc_howto_type elf32_frv_vtinherit_howto =
678
  HOWTO (R_FRV_GNU_VTINHERIT, /* type */
679
   0,     /* rightshift */
680
   4,     /* size */
681
   0,     /* bitsize */
682
   false,     /* pc_relative */
683
   0,     /* bitpos */
684
   complain_overflow_dont, /* complain_on_overflow */
685
   NULL,      /* special_function */
686
   "R_FRV_GNU_VTINHERIT", /* name */
687
   false,     /* partial_inplace */
688
   0,     /* src_mask */
689
   0,     /* dst_mask */
690
   false);    /* pcrel_offset */
691
692
  /* GNU extension to record C++ vtable member usage.  */
693
static reloc_howto_type elf32_frv_vtentry_howto =
694
  HOWTO (R_FRV_GNU_VTENTRY, /* type */
695
   0,     /* rightshift */
696
   4,     /* size */
697
   0,     /* bitsize */
698
   false,     /* pc_relative */
699
   0,     /* bitpos */
700
   complain_overflow_dont, /* complain_on_overflow */
701
   _bfd_elf_rel_vtable_reloc_fn, /* special_function */
702
   "R_FRV_GNU_VTENTRY", /* name */
703
   false,     /* partial_inplace */
704
   0,     /* src_mask */
705
   0,     /* dst_mask */
706
   false);    /* pcrel_offset */
707
708
/* The following 3 relocations are REL.  The only difference to the
709
   entries in the table above are that partial_inplace is true.  */
710
static reloc_howto_type elf32_frv_rel_32_howto =
711
  HOWTO (R_FRV_32,    /* type */
712
   0,     /* rightshift */
713
   4,     /* size */
714
   32,      /* bitsize */
715
   false,     /* pc_relative */
716
   0,     /* bitpos */
717
   complain_overflow_bitfield, /* complain_on_overflow */
718
   bfd_elf_generic_reloc, /* special_function */
719
   "R_FRV_32",    /* name */
720
   true,      /* partial_inplace */
721
   0xffffffff,    /* src_mask */
722
   0xffffffff,    /* dst_mask */
723
   false);    /* pcrel_offset */
724
725
static reloc_howto_type elf32_frv_rel_funcdesc_howto =
726
  HOWTO (R_FRV_FUNCDESC,  /* type */
727
   0,     /* rightshift */
728
   4,     /* size */
729
   32,      /* bitsize */
730
   false,     /* pc_relative */
731
   0,     /* bitpos */
732
   complain_overflow_bitfield, /* complain_on_overflow */
733
   bfd_elf_generic_reloc, /* special_function */
734
   "R_FRV_FUNCDESC",  /* name */
735
   true,      /* partial_inplace */
736
   0xffffffff,    /* src_mask */
737
   0xffffffff,    /* dst_mask */
738
   false);    /* pcrel_offset */
739
740
static reloc_howto_type elf32_frv_rel_funcdesc_value_howto =
741
  HOWTO (R_FRV_FUNCDESC_VALUE,  /* type */
742
   0,     /* rightshift */
743
   4,     /* size */
744
   64,      /* bitsize */
745
   false,     /* pc_relative */
746
   0,     /* bitpos */
747
   complain_overflow_bitfield, /* complain_on_overflow */
748
   bfd_elf_generic_reloc, /* special_function */
749
   "R_FRV_FUNCDESC_VALUE", /* name */
750
   true,      /* partial_inplace */
751
   0xffffffff,    /* src_mask */
752
   0xffffffff,    /* dst_mask */
753
   false);    /* pcrel_offset */
754
755
static reloc_howto_type elf32_frv_rel_tlsdesc_value_howto =
756
  /* A 64-bit TLS descriptor for a symbol.  The first word resolves to
757
     an entry point, and the second resolves to a special argument.
758
     If the symbol turns out to be in static TLS, the entry point is a
759
     return instruction, and the special argument is the TLS offset
760
     for the symbol.  If it's in dynamic TLS, the entry point is a TLS
761
     offset resolver, and the special argument is a pointer to a data
762
     structure allocated by the dynamic loader, containing the GOT
763
     address for the offset resolver, the module id, the offset within
764
     the module, and anything else the TLS offset resolver might need
765
     to determine the TLS offset for the symbol in the running
766
     thread.  */
767
  HOWTO (R_FRV_TLSDESC_VALUE, /* type */
768
   0,     /* rightshift */
769
   4,     /* size */
770
   64,      /* bitsize */
771
   false,     /* pc_relative */
772
   0,     /* bitpos */
773
   complain_overflow_bitfield, /* complain_on_overflow */
774
   bfd_elf_generic_reloc, /* special_function */
775
   "R_FRV_TLSDESC_VALUE", /* name */
776
   true,      /* partial_inplace */
777
   0xffffffff,    /* src_mask */
778
   0xffffffff,    /* dst_mask */
779
   false);    /* pcrel_offset */
780
781
static reloc_howto_type elf32_frv_rel_tlsoff_howto =
782
  /* The 32-bit offset from the thread pointer (not the module base
783
     address) to a thread-local symbol.  */
784
  HOWTO (R_FRV_TLSOFF,    /* type */
785
   0,     /* rightshift */
786
   4,     /* size */
787
   32,      /* bitsize */
788
   false,     /* pc_relative */
789
   0,     /* bitpos */
790
   complain_overflow_bitfield, /* complain_on_overflow */
791
   bfd_elf_generic_reloc, /* special_function */
792
   "R_FRV_TLSOFF",  /* name */
793
   true,      /* partial_inplace */
794
   0xffffffff,    /* src_mask */
795
   0xffffffff,    /* dst_mask */
796
   false);    /* pcrel_offset */
797
798
799

800
extern const bfd_target frv_elf32_fdpic_vec;
801
3
#define IS_FDPIC(bfd) ((bfd)->xvec == &frv_elf32_fdpic_vec)
802
803
/* An extension of the elf hash table data structure, containing some
804
   additional FRV-specific data.  */
805
struct frvfdpic_elf_link_hash_table
806
{
807
  struct elf_link_hash_table elf;
808
809
  /* A pointer to the .rofixup section.  */
810
  asection *sgotfixup;
811
  /* GOT base offset.  */
812
  bfd_vma got0;
813
  /* Location of the first non-lazy PLT entry, i.e., the number of
814
     bytes taken by lazy PLT entries.  If locally-bound TLS
815
     descriptors require a ret instruction, it will be placed at this
816
     offset.  */
817
  bfd_vma plt0;
818
  /* A hash table holding information about which symbols were
819
     referenced with which PIC-related relocations.  */
820
  struct htab *relocs_info;
821
  /* Summary reloc information collected by
822
     _frvfdpic_count_got_plt_entries.  */
823
  struct _frvfdpic_dynamic_got_info *g;
824
};
825
826
/* Get the FRV ELF linker hash table from a link_info structure.  */
827
828
#define frvfdpic_hash_table(p) \
829
0
  ((is_elf_hash_table ((p)->hash)          \
830
0
    && elf_hash_table_id (elf_hash_table (p)) == FRV_ELF_DATA)   \
831
0
   ? (struct frvfdpic_elf_link_hash_table *) (p)->hash : NULL)
832
833
#define frvfdpic_got_section(info) \
834
0
  (frvfdpic_hash_table (info)->elf.sgot)
835
#define frvfdpic_gotrel_section(info) \
836
0
  (frvfdpic_hash_table (info)->elf.srelgot)
837
#define frvfdpic_gotfixup_section(info) \
838
0
  (frvfdpic_hash_table (info)->sgotfixup)
839
#define frvfdpic_plt_section(info) \
840
0
  (frvfdpic_hash_table (info)->elf.splt)
841
#define frvfdpic_pltrel_section(info) \
842
0
  (frvfdpic_hash_table (info)->elf.srelplt)
843
#define frvfdpic_relocs_info(info) \
844
0
  (frvfdpic_hash_table (info)->relocs_info)
845
#define frvfdpic_got_initial_offset(info) \
846
0
  (frvfdpic_hash_table (info)->got0)
847
#define frvfdpic_plt_initial_offset(info) \
848
0
  (frvfdpic_hash_table (info)->plt0)
849
#define frvfdpic_dynamic_got_plt_info(info) \
850
0
  (frvfdpic_hash_table (info)->g)
851
852
/* Currently it's the same, but if some day we have a reason to change
853
   it, we'd better be using a different macro.
854
855
   FIXME: if there's any TLS PLT entry that uses local-exec or
856
   initial-exec models, we could use the ret at the end of any of them
857
   instead of adding one more.  */
858
#define frvfdpic_plt_tls_ret_offset(info) \
859
  (frvfdpic_plt_initial_offset (info))
860
861
/* The name of the dynamic interpreter.  This is put in the .interp
862
   section.  */
863
864
0
#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
865
866
0
#define DEFAULT_STACK_SIZE 0x20000
867
868
/* This structure is used to collect the number of entries present in
869
   each addressable range of the got.  */
870
struct _frvfdpic_dynamic_got_info
871
{
872
  /* Several bits of information about the current link.  */
873
  struct bfd_link_info *info;
874
  /* Total GOT size needed for GOT entries within the 12-, 16- or 32-bit
875
     ranges.  */
876
  bfd_vma got12, gotlos, gothilo;
877
  /* Total GOT size needed for function descriptor entries within the 12-,
878
     16- or 32-bit ranges.  */
879
  bfd_vma fd12, fdlos, fdhilo;
880
  /* Total GOT size needed by function descriptor entries referenced
881
     in PLT entries, that would be profitable to place in offsets
882
     close to the PIC register.  */
883
  bfd_vma fdplt;
884
  /* Total PLT size needed by lazy PLT entries.  */
885
  bfd_vma lzplt;
886
  /* Total GOT size needed for TLS descriptor entries within the 12-,
887
     16- or 32-bit ranges.  */
888
  bfd_vma tlsd12, tlsdlos, tlsdhilo;
889
  /* Total GOT size needed by TLS descriptors referenced in PLT
890
     entries, that would be profitable to place in offers close to the
891
     PIC register.  */
892
  bfd_vma tlsdplt;
893
  /* Total PLT size needed by TLS lazy PLT entries.  */
894
  bfd_vma tlslzplt;
895
  /* Number of relocations carried over from input object files.  */
896
  unsigned long relocs;
897
  /* Number of fixups introduced by relocations in input object files.  */
898
  unsigned long fixups;
899
  /* The number of fixups that reference the ret instruction added to
900
     the PLT for locally-resolved TLS descriptors.  */
901
  unsigned long tls_ret_refs;
902
};
903
904
/* This structure is used to assign offsets to got entries, function
905
   descriptors, plt entries and lazy plt entries.  */
906
907
struct _frvfdpic_dynamic_got_plt_info
908
{
909
  /* Summary information collected with _frvfdpic_count_got_plt_entries.  */
910
  struct _frvfdpic_dynamic_got_info g;
911
912
  /* For each addressable range, we record a MAX (positive) and MIN
913
     (negative) value.  CUR is used to assign got entries, and it's
914
     incremented from an initial positive value to MAX, then from MIN
915
     to FDCUR (unless FDCUR wraps around first).  FDCUR is used to
916
     assign function descriptors, and it's decreased from an initial
917
     non-positive value to MIN, then from MAX down to CUR (unless CUR
918
     wraps around first).  All of MIN, MAX, CUR and FDCUR always point
919
     to even words.  ODD, if non-zero, indicates an odd word to be
920
     used for the next got entry, otherwise CUR is used and
921
     incremented by a pair of words, wrapping around when it reaches
922
     MAX.  FDCUR is decremented (and wrapped) before the next function
923
     descriptor is chosen.  FDPLT indicates the number of remaining
924
     slots that can be used for function descriptors used only by PLT
925
     entries.
926
927
     TMAX, TMIN and TCUR are used to assign TLS descriptors.  TCUR
928
     starts as MAX, and grows up to TMAX, then wraps around to TMIN
929
     and grows up to MIN.  TLSDPLT indicates the number of remaining
930
     slots that can be used for TLS descriptors used only by TLS PLT
931
     entries.  */
932
  struct _frvfdpic_dynamic_got_alloc_data
933
  {
934
    bfd_signed_vma max, cur, odd, fdcur, min;
935
    bfd_signed_vma tmax, tcur, tmin;
936
    bfd_vma fdplt, tlsdplt;
937
  } got12, gotlos, gothilo;
938
};
939
940
/* Create an FRV ELF linker hash table.  */
941
942
static struct bfd_link_hash_table *
943
frvfdpic_elf_link_hash_table_create (bfd *abfd)
944
0
{
945
0
  struct frvfdpic_elf_link_hash_table *ret;
946
0
  size_t amt = sizeof (struct frvfdpic_elf_link_hash_table);
947
948
0
  ret = bfd_zmalloc (amt);
949
0
  if (ret == NULL)
950
0
    return NULL;
951
952
0
  if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
953
0
              _bfd_elf_link_hash_newfunc,
954
0
              sizeof (struct elf_link_hash_entry)))
955
0
    {
956
0
      free (ret);
957
0
      return NULL;
958
0
    }
959
960
0
  return &ret->elf.root;
961
0
}
962
963
/* Decide whether a reference to a symbol can be resolved locally or
964
   not.  If the symbol is protected, we want the local address, but
965
   its function descriptor must be assigned by the dynamic linker.  */
966
#define FRVFDPIC_SYM_LOCAL(INFO, H) \
967
0
  (_bfd_elf_symbol_refs_local_p ((H), (INFO), 1) \
968
0
   || ! elf_hash_table (INFO)->dynamic_sections_created)
969
#define FRVFDPIC_FUNCDESC_LOCAL(INFO, H) \
970
0
  ((H)->dynindx == -1 || ! elf_hash_table (INFO)->dynamic_sections_created)
971
972
/* This structure collects information on what kind of GOT, PLT or
973
   function descriptors are required by relocations that reference a
974
   certain symbol.  */
975
struct frvfdpic_relocs_info
976
{
977
  /* The index of the symbol, as stored in the relocation r_info, if
978
     we have a local symbol; -1 otherwise.  */
979
  long symndx;
980
  union
981
  {
982
    /* The input bfd in which the symbol is defined, if it's a local
983
       symbol.  */
984
    bfd *abfd;
985
    /* If symndx == -1, the hash table entry corresponding to a global
986
       symbol (even if it turns out to bind locally, in which case it
987
       should ideally be replaced with section's symndx + addend).  */
988
    struct elf_link_hash_entry *h;
989
  } d;
990
  /* The addend of the relocation that references the symbol.  */
991
  bfd_vma addend;
992
993
  /* The fields above are used to identify an entry.  The fields below
994
     contain information on how an entry is used and, later on, which
995
     locations it was assigned.  */
996
  /* The following 3 fields record whether the symbol+addend above was
997
     ever referenced with a GOT relocation.  The 12 suffix indicates a
998
     GOT12 relocation; los is used for GOTLO relocations that are not
999
     matched by a GOTHI relocation; hilo is used for GOTLO/GOTHI
1000
     pairs.  */
1001
  unsigned got12:1;
1002
  unsigned gotlos:1;
1003
  unsigned gothilo:1;
1004
  /* Whether a FUNCDESC relocation references symbol+addend.  */
1005
  unsigned fd:1;
1006
  /* Whether a FUNCDESC_GOT relocation references symbol+addend.  */
1007
  unsigned fdgot12:1;
1008
  unsigned fdgotlos:1;
1009
  unsigned fdgothilo:1;
1010
  /* Whether a FUNCDESC_GOTOFF relocation references symbol+addend.  */
1011
  unsigned fdgoff12:1;
1012
  unsigned fdgofflos:1;
1013
  unsigned fdgoffhilo:1;
1014
  /* Whether a GETTLSOFF relocation references symbol+addend.  */
1015
  unsigned tlsplt:1;
1016
  /* FIXME: we should probably add tlspltdesc, tlspltoff and
1017
     tlspltimm, to tell what kind of TLS PLT entry we're generating.
1018
     We might instead just pre-compute flags telling whether the
1019
     object is suitable for local exec, initial exec or general
1020
     dynamic addressing, and use that all over the place.  We could
1021
     also try to do a better job of merging TLSOFF and TLSDESC entries
1022
     in main executables, but perhaps we can get rid of TLSDESC
1023
     entirely in them instead.  */
1024
  /* Whether a GOTTLSDESC relocation references symbol+addend.  */
1025
  unsigned tlsdesc12:1;
1026
  unsigned tlsdesclos:1;
1027
  unsigned tlsdeschilo:1;
1028
  /* Whether a GOTTLSOFF relocation references symbol+addend.  */
1029
  unsigned tlsoff12:1;
1030
  unsigned tlsofflos:1;
1031
  unsigned tlsoffhilo:1;
1032
  /* Whether symbol+addend is referenced with GOTOFF12, GOTOFFLO or
1033
     GOTOFFHI relocations.  The addend doesn't really matter, since we
1034
     envision that this will only be used to check whether the symbol
1035
     is mapped to the same segment as the got.  */
1036
  unsigned gotoff:1;
1037
  /* Whether symbol+addend is referenced by a LABEL24 relocation.  */
1038
  unsigned call:1;
1039
  /* Whether symbol+addend is referenced by a 32 or FUNCDESC_VALUE
1040
     relocation.  */
1041
  unsigned sym:1;
1042
  /* Whether we need a PLT entry for a symbol.  Should be implied by
1043
     something like:
1044
     (call && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h))  */
1045
  unsigned plt:1;
1046
  /* Whether a function descriptor should be created in this link unit
1047
     for symbol+addend.  Should be implied by something like:
1048
     (plt || fdgotoff12 || fdgotofflos || fdgotofflohi
1049
      || ((fd || fdgot12 || fdgotlos || fdgothilo)
1050
    && (symndx != -1 || FRVFDPIC_FUNCDESC_LOCAL (info, d.h))))  */
1051
  unsigned privfd:1;
1052
  /* Whether a lazy PLT entry is needed for this symbol+addend.
1053
     Should be implied by something like:
1054
     (privfd && symndx == -1 && ! FRVFDPIC_SYM_LOCAL (info, d.h)
1055
      && ! (info->flags & DF_BIND_NOW))  */
1056
  unsigned lazyplt:1;
1057
  /* Whether we've already emitted GOT relocations and PLT entries as
1058
     needed for this symbol.  */
1059
  unsigned done:1;
1060
1061
  /* The number of R_FRV_32, R_FRV_FUNCDESC, R_FRV_FUNCDESC_VALUE and
1062
     R_FRV_TLSDESC_VALUE, R_FRV_TLSOFF relocations referencing
1063
     symbol+addend.  */
1064
  unsigned relocs32, relocsfd, relocsfdv, relocstlsd, relocstlsoff;
1065
1066
  /* The number of .rofixups entries and dynamic relocations allocated
1067
     for this symbol, minus any that might have already been used.  */
1068
  unsigned fixups, dynrelocs;
1069
1070
  /* The offsets of the GOT entries assigned to symbol+addend, to the
1071
     function descriptor's address, and to a function descriptor,
1072
     respectively.  Should be zero if unassigned.  The offsets are
1073
     counted from the value that will be assigned to the PIC register,
1074
     not from the beginning of the .got section.  */
1075
  bfd_signed_vma got_entry, fdgot_entry, fd_entry;
1076
  /* The offsets of the PLT entries assigned to symbol+addend,
1077
     non-lazy and lazy, respectively.  If unassigned, should be
1078
     (bfd_vma)-1.  */
1079
  bfd_vma plt_entry, lzplt_entry;
1080
  /* The offsets of the GOT entries for TLS offset and TLS descriptor.  */
1081
  bfd_signed_vma tlsoff_entry, tlsdesc_entry;
1082
  /* The offset of the TLS offset PLT entry.  */
1083
  bfd_vma tlsplt_entry;
1084
};
1085
1086
/* Compute a hash with the key fields of an frvfdpic_relocs_info entry.  */
1087
static hashval_t
1088
frvfdpic_relocs_info_hash (const void *entry_)
1089
0
{
1090
0
  const struct frvfdpic_relocs_info *entry = entry_;
1091
1092
0
  return (entry->symndx == -1
1093
0
    ? (long) entry->d.h->root.root.hash
1094
0
    : entry->symndx + (long) entry->d.abfd->id * 257) + entry->addend;
1095
0
}
1096
1097
/* Test whether the key fields of two frvfdpic_relocs_info entries are
1098
   identical.  */
1099
static int
1100
frvfdpic_relocs_info_eq (const void *entry1, const void *entry2)
1101
0
{
1102
0
  const struct frvfdpic_relocs_info *e1 = entry1;
1103
0
  const struct frvfdpic_relocs_info *e2 = entry2;
1104
1105
0
  return e1->symndx == e2->symndx && e1->addend == e2->addend
1106
0
    && (e1->symndx == -1 ? e1->d.h == e2->d.h : e1->d.abfd == e2->d.abfd);
1107
0
}
1108
1109
/* Find or create an entry in a hash table HT that matches the key
1110
   fields of the given ENTRY.  If it's not found, memory for a new
1111
   entry is allocated in ABFD's obstack.  */
1112
static struct frvfdpic_relocs_info *
1113
frvfdpic_relocs_info_find (struct htab *ht,
1114
         bfd *abfd,
1115
         const struct frvfdpic_relocs_info *entry,
1116
         enum insert_option insert)
1117
0
{
1118
0
  struct frvfdpic_relocs_info **loc =
1119
0
    (struct frvfdpic_relocs_info **) htab_find_slot (ht, entry, insert);
1120
1121
0
  if (! loc)
1122
0
    return NULL;
1123
1124
0
  if (*loc)
1125
0
    return *loc;
1126
1127
0
  *loc = bfd_zalloc (abfd, sizeof (**loc));
1128
1129
0
  if (! *loc)
1130
0
    return *loc;
1131
1132
0
  (*loc)->symndx = entry->symndx;
1133
0
  (*loc)->d = entry->d;
1134
0
  (*loc)->addend = entry->addend;
1135
0
  (*loc)->plt_entry = (bfd_vma)-1;
1136
0
  (*loc)->lzplt_entry = (bfd_vma)-1;
1137
0
  (*loc)->tlsplt_entry = (bfd_vma)-1;
1138
1139
0
  return *loc;
1140
0
}
1141
1142
/* Obtain the address of the entry in HT associated with H's symbol +
1143
   addend, creating a new entry if none existed.  ABFD is only used
1144
   for memory allocation purposes.  */
1145
inline static struct frvfdpic_relocs_info *
1146
frvfdpic_relocs_info_for_global (struct htab *ht,
1147
         bfd *abfd,
1148
         struct elf_link_hash_entry *h,
1149
         bfd_vma addend,
1150
         enum insert_option insert)
1151
0
{
1152
0
  struct frvfdpic_relocs_info entry;
1153
1154
0
  entry.symndx = -1;
1155
0
  entry.d.h = h;
1156
0
  entry.addend = addend;
1157
1158
0
  return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
1159
0
}
1160
1161
/* Obtain the address of the entry in HT associated with the SYMNDXth
1162
   local symbol of the input bfd ABFD, plus the addend, creating a new
1163
   entry if none existed.  */
1164
inline static struct frvfdpic_relocs_info *
1165
frvfdpic_relocs_info_for_local (struct htab *ht,
1166
        bfd *abfd,
1167
        long symndx,
1168
        bfd_vma addend,
1169
        enum insert_option insert)
1170
0
{
1171
0
  struct frvfdpic_relocs_info entry;
1172
1173
0
  entry.symndx = symndx;
1174
0
  entry.d.abfd = abfd;
1175
0
  entry.addend = addend;
1176
1177
0
  return frvfdpic_relocs_info_find (ht, abfd, &entry, insert);
1178
0
}
1179
1180
/* Merge fields set by check_relocs() of two entries that end up being
1181
   mapped to the same (presumably global) symbol.  */
1182
1183
inline static void
1184
frvfdpic_pic_merge_early_relocs_info (struct frvfdpic_relocs_info *e2,
1185
              struct frvfdpic_relocs_info const *e1)
1186
0
{
1187
0
  e2->got12 |= e1->got12;
1188
0
  e2->gotlos |= e1->gotlos;
1189
0
  e2->gothilo |= e1->gothilo;
1190
0
  e2->fd |= e1->fd;
1191
0
  e2->fdgot12 |= e1->fdgot12;
1192
0
  e2->fdgotlos |= e1->fdgotlos;
1193
0
  e2->fdgothilo |= e1->fdgothilo;
1194
0
  e2->fdgoff12 |= e1->fdgoff12;
1195
0
  e2->fdgofflos |= e1->fdgofflos;
1196
0
  e2->fdgoffhilo |= e1->fdgoffhilo;
1197
0
  e2->tlsplt |= e1->tlsplt;
1198
0
  e2->tlsdesc12 |= e1->tlsdesc12;
1199
0
  e2->tlsdesclos |= e1->tlsdesclos;
1200
0
  e2->tlsdeschilo |= e1->tlsdeschilo;
1201
0
  e2->tlsoff12 |= e1->tlsoff12;
1202
0
  e2->tlsofflos |= e1->tlsofflos;
1203
0
  e2->tlsoffhilo |= e1->tlsoffhilo;
1204
0
  e2->gotoff |= e1->gotoff;
1205
0
  e2->call |= e1->call;
1206
0
  e2->sym |= e1->sym;
1207
0
}
1208
1209
/* Every block of 65535 lazy PLT entries shares a single call to the
1210
   resolver, inserted in the 32768th lazy PLT entry (i.e., entry #
1211
   32767, counting from 0).  All other lazy PLT entries branch to it
1212
   in a single instruction.  */
1213
1214
0
#define FRVFDPIC_LZPLT_BLOCK_SIZE ((bfd_vma) 8 * 65535 + 4)
1215
0
#define FRVFDPIC_LZPLT_RESOLV_LOC (8 * 32767)
1216
1217
/* Add a dynamic relocation to the SRELOC section.  */
1218
1219
inline static bfd_vma
1220
_frvfdpic_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
1221
       int reloc_type, long dynindx, bfd_vma addend,
1222
       struct frvfdpic_relocs_info *entry)
1223
0
{
1224
0
  Elf_Internal_Rela outrel;
1225
0
  bfd_vma reloc_offset;
1226
1227
0
  outrel.r_offset = offset;
1228
0
  outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
1229
0
  outrel.r_addend = addend;
1230
1231
0
  reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rel);
1232
0
  BFD_ASSERT (reloc_offset < sreloc->size);
1233
0
  bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1234
0
          sreloc->contents + reloc_offset);
1235
0
  sreloc->reloc_count++;
1236
1237
  /* If the entry's index is zero, this relocation was probably to a
1238
     linkonce section that got discarded.  We reserved a dynamic
1239
     relocation, but it was for another entry than the one we got at
1240
     the time of emitting the relocation.  Unfortunately there's no
1241
     simple way for us to catch this situation, since the relocation
1242
     is cleared right before calling relocate_section, at which point
1243
     we no longer know what the relocation used to point to.  */
1244
0
  if (entry->symndx)
1245
0
    {
1246
0
      BFD_ASSERT (entry->dynrelocs > 0);
1247
0
      entry->dynrelocs--;
1248
0
    }
1249
1250
0
  return reloc_offset;
1251
0
}
1252
1253
/* Add a fixup to the ROFIXUP section.  */
1254
1255
static bfd_vma
1256
_frvfdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
1257
           struct frvfdpic_relocs_info *entry)
1258
0
{
1259
0
  bfd_vma fixup_offset;
1260
1261
0
  if (rofixup->flags & SEC_EXCLUDE)
1262
0
    return -1;
1263
1264
0
  fixup_offset = rofixup->reloc_count * 4;
1265
0
  if (rofixup->contents)
1266
0
    {
1267
0
      BFD_ASSERT (fixup_offset < rofixup->size);
1268
0
      bfd_put_32 (output_bfd, offset, rofixup->contents + fixup_offset);
1269
0
    }
1270
0
  rofixup->reloc_count++;
1271
1272
0
  if (entry && entry->symndx)
1273
0
    {
1274
      /* See discussion about symndx == 0 in _frvfdpic_add_dyn_reloc
1275
   above.  */
1276
0
      BFD_ASSERT (entry->fixups > 0);
1277
0
      entry->fixups--;
1278
0
    }
1279
1280
0
  return fixup_offset;
1281
0
}
1282
1283
/* Find the segment number in which OSEC, and output section, is
1284
   located.  */
1285
1286
static unsigned
1287
_frvfdpic_osec_to_segment (bfd *output_bfd, asection *osec)
1288
0
{
1289
0
  Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
1290
1291
0
  return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
1292
0
}
1293
1294
inline static bool
1295
_frvfdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
1296
0
{
1297
0
  unsigned seg = _frvfdpic_osec_to_segment (output_bfd, osec);
1298
1299
0
  return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
1300
0
}
1301
1302
0
#define FRVFDPIC_TLS_BIAS (2048 - 16)
1303
1304
/* Return the base VMA address which should be subtracted from real addresses
1305
   when resolving TLSMOFF relocation.
1306
   This is PT_TLS segment p_vaddr, plus the 2048-16 bias.  */
1307
1308
static bfd_vma
1309
tls_biased_base (struct bfd_link_info *info)
1310
0
{
1311
  /* If tls_sec is NULL, we should have signalled an error already.  */
1312
0
  if (elf_hash_table (info)->tls_sec == NULL)
1313
0
    return FRVFDPIC_TLS_BIAS;
1314
0
  return elf_hash_table (info)->tls_sec->vma + FRVFDPIC_TLS_BIAS;
1315
0
}
1316
1317
/* Generate relocations for GOT entries, function descriptors, and
1318
   code for PLT and lazy PLT entries.  */
1319
1320
inline static bool
1321
_frvfdpic_emit_got_relocs_plt_entries (struct frvfdpic_relocs_info *entry,
1322
               bfd *output_bfd,
1323
               struct bfd_link_info *info,
1324
               asection *sec,
1325
               Elf_Internal_Sym *sym,
1326
               bfd_vma addend)
1327
1328
0
{
1329
0
  bfd_vma fd_lazy_rel_offset = (bfd_vma)-1;
1330
0
  int dynindx = -1;
1331
1332
0
  if (entry->done)
1333
0
    return true;
1334
0
  entry->done = 1;
1335
1336
0
  if (entry->got_entry || entry->fdgot_entry || entry->fd_entry
1337
0
      || entry->tlsoff_entry || entry->tlsdesc_entry)
1338
0
    {
1339
      /* If the symbol is dynamic, consider it for dynamic
1340
   relocations, otherwise decay to section + offset.  */
1341
0
      if (entry->symndx == -1 && entry->d.h->dynindx != -1)
1342
0
  dynindx = entry->d.h->dynindx;
1343
0
      else
1344
0
  {
1345
0
    if (sec
1346
0
        && sec->output_section
1347
0
        && ! bfd_is_abs_section (sec->output_section)
1348
0
        && ! bfd_is_und_section (sec->output_section))
1349
0
      dynindx = elf_section_data (sec->output_section)->dynindx;
1350
0
    else
1351
0
      dynindx = 0;
1352
0
  }
1353
0
    }
1354
1355
  /* Generate relocation for GOT entry pointing to the symbol.  */
1356
0
  if (entry->got_entry)
1357
0
    {
1358
0
      int idx = dynindx;
1359
0
      bfd_vma ad = addend;
1360
1361
      /* If the symbol is dynamic but binds locally, use
1362
   section+offset.  */
1363
0
      if (sec && (entry->symndx != -1
1364
0
      || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1365
0
  {
1366
0
    if (entry->symndx == -1)
1367
0
      ad += entry->d.h->root.u.def.value;
1368
0
    else
1369
0
      ad += sym->st_value;
1370
0
    ad += sec->output_offset;
1371
0
    if (sec->output_section && elf_section_data (sec->output_section))
1372
0
      idx = elf_section_data (sec->output_section)->dynindx;
1373
0
    else
1374
0
      idx = 0;
1375
0
  }
1376
1377
      /* If we're linking an executable at a fixed address, we can
1378
   omit the dynamic relocation as long as the symbol is local to
1379
   this module.  */
1380
0
      if (bfd_link_pde (info)
1381
0
    && (entry->symndx != -1
1382
0
        || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1383
0
  {
1384
0
    if (sec)
1385
0
      ad += sec->output_section->vma;
1386
0
    if (entry->symndx != -1
1387
0
        || entry->d.h->root.type != bfd_link_hash_undefweak)
1388
0
      _frvfdpic_add_rofixup (output_bfd,
1389
0
           frvfdpic_gotfixup_section (info),
1390
0
           frvfdpic_got_section (info)->output_section
1391
0
           ->vma
1392
0
           + frvfdpic_got_section (info)->output_offset
1393
0
           + frvfdpic_got_initial_offset (info)
1394
0
           + entry->got_entry, entry);
1395
0
  }
1396
0
      else
1397
0
  _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1398
0
         _bfd_elf_section_offset
1399
0
         (output_bfd, info,
1400
0
          frvfdpic_got_section (info),
1401
0
          frvfdpic_got_initial_offset (info)
1402
0
          + entry->got_entry)
1403
0
         + frvfdpic_got_section (info)
1404
0
         ->output_section->vma
1405
0
         + frvfdpic_got_section (info)->output_offset,
1406
0
         R_FRV_32, idx, ad, entry);
1407
1408
0
      bfd_put_32 (output_bfd, ad,
1409
0
      frvfdpic_got_section (info)->contents
1410
0
      + frvfdpic_got_initial_offset (info)
1411
0
      + entry->got_entry);
1412
0
    }
1413
1414
  /* Generate relocation for GOT entry pointing to a canonical
1415
     function descriptor.  */
1416
0
  if (entry->fdgot_entry)
1417
0
    {
1418
0
      int reloc, idx;
1419
0
      bfd_vma ad = 0;
1420
1421
0
      if (! (entry->symndx == -1
1422
0
       && entry->d.h->root.type == bfd_link_hash_undefweak
1423
0
       && FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1424
0
  {
1425
    /* If the symbol is dynamic and there may be dynamic symbol
1426
       resolution because we are, or are linked with, a shared
1427
       library, emit a FUNCDESC relocation such that the dynamic
1428
       linker will allocate the function descriptor.  If the
1429
       symbol needs a non-local function descriptor but binds
1430
       locally (e.g., its visibility is protected, emit a
1431
       dynamic relocation decayed to section+offset.  */
1432
0
    if (entry->symndx == -1
1433
0
        && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)
1434
0
        && FRVFDPIC_SYM_LOCAL (info, entry->d.h)
1435
0
        && !bfd_link_pde (info))
1436
0
      {
1437
0
        reloc = R_FRV_FUNCDESC;
1438
0
        idx = elf_section_data (entry->d.h->root.u.def.section
1439
0
              ->output_section)->dynindx;
1440
0
        ad = entry->d.h->root.u.def.section->output_offset
1441
0
    + entry->d.h->root.u.def.value;
1442
0
      }
1443
0
    else if (entry->symndx == -1
1444
0
       && ! FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h))
1445
0
      {
1446
0
        reloc = R_FRV_FUNCDESC;
1447
0
        idx = dynindx;
1448
0
        ad = addend;
1449
0
        if (ad)
1450
0
    {
1451
0
      (*info->callbacks->reloc_dangerous)
1452
0
        (info, _("relocation requires zero addend"),
1453
0
         elf_hash_table (info)->dynobj,
1454
0
         frvfdpic_got_section (info),
1455
0
         entry->fdgot_entry);
1456
0
      return false;
1457
0
    }
1458
0
      }
1459
0
    else
1460
0
      {
1461
        /* Otherwise, we know we have a private function descriptor,
1462
     so reference it directly.  */
1463
0
        if (elf_hash_table (info)->dynamic_sections_created)
1464
0
    BFD_ASSERT (entry->privfd);
1465
0
        reloc = R_FRV_32;
1466
0
        idx = elf_section_data (frvfdpic_got_section (info)
1467
0
              ->output_section)->dynindx;
1468
0
        ad = frvfdpic_got_section (info)->output_offset
1469
0
    + frvfdpic_got_initial_offset (info) + entry->fd_entry;
1470
0
      }
1471
1472
    /* If there is room for dynamic symbol resolution, emit the
1473
       dynamic relocation.  However, if we're linking an
1474
       executable at a fixed location, we won't have emitted a
1475
       dynamic symbol entry for the got section, so idx will be
1476
       zero, which means we can and should compute the address
1477
       of the private descriptor ourselves.  */
1478
0
    if (bfd_link_pde (info)
1479
0
        && (entry->symndx != -1
1480
0
      || FRVFDPIC_FUNCDESC_LOCAL (info, entry->d.h)))
1481
0
      {
1482
0
        ad += frvfdpic_got_section (info)->output_section->vma;
1483
0
        _frvfdpic_add_rofixup (output_bfd,
1484
0
             frvfdpic_gotfixup_section (info),
1485
0
             frvfdpic_got_section (info)
1486
0
             ->output_section->vma
1487
0
             + frvfdpic_got_section (info)
1488
0
             ->output_offset
1489
0
             + frvfdpic_got_initial_offset (info)
1490
0
             + entry->fdgot_entry, entry);
1491
0
      }
1492
0
    else
1493
0
      _frvfdpic_add_dyn_reloc (output_bfd,
1494
0
             frvfdpic_gotrel_section (info),
1495
0
             _bfd_elf_section_offset
1496
0
             (output_bfd, info,
1497
0
              frvfdpic_got_section (info),
1498
0
              frvfdpic_got_initial_offset (info)
1499
0
              + entry->fdgot_entry)
1500
0
             + frvfdpic_got_section (info)
1501
0
             ->output_section->vma
1502
0
             + frvfdpic_got_section (info)
1503
0
             ->output_offset,
1504
0
             reloc, idx, ad, entry);
1505
0
  }
1506
1507
0
      bfd_put_32 (output_bfd, ad,
1508
0
      frvfdpic_got_section (info)->contents
1509
0
      + frvfdpic_got_initial_offset (info)
1510
0
      + entry->fdgot_entry);
1511
0
    }
1512
1513
  /* Generate relocation to fill in a private function descriptor in
1514
     the GOT.  */
1515
0
  if (entry->fd_entry)
1516
0
    {
1517
0
      int idx = dynindx;
1518
0
      bfd_vma ad = addend;
1519
0
      bfd_vma ofst;
1520
0
      long lowword, highword;
1521
1522
      /* If the symbol is dynamic but binds locally, use
1523
   section+offset.  */
1524
0
      if (sec && (entry->symndx != -1
1525
0
      || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1526
0
  {
1527
0
    if (entry->symndx == -1)
1528
0
      ad += entry->d.h->root.u.def.value;
1529
0
    else
1530
0
      ad += sym->st_value;
1531
0
    ad += sec->output_offset;
1532
0
    if (sec->output_section && elf_section_data (sec->output_section))
1533
0
      idx = elf_section_data (sec->output_section)->dynindx;
1534
0
    else
1535
0
      idx = 0;
1536
0
  }
1537
1538
      /* If we're linking an executable at a fixed address, we can
1539
   omit the dynamic relocation as long as the symbol is local to
1540
   this module.  */
1541
0
      if (bfd_link_pde (info)
1542
0
    && (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1543
0
  {
1544
0
    if (sec)
1545
0
      ad += sec->output_section->vma;
1546
0
    ofst = 0;
1547
0
    if (entry->symndx != -1
1548
0
        || entry->d.h->root.type != bfd_link_hash_undefweak)
1549
0
      {
1550
0
        _frvfdpic_add_rofixup (output_bfd,
1551
0
             frvfdpic_gotfixup_section (info),
1552
0
             frvfdpic_got_section (info)
1553
0
             ->output_section->vma
1554
0
             + frvfdpic_got_section (info)
1555
0
             ->output_offset
1556
0
             + frvfdpic_got_initial_offset (info)
1557
0
             + entry->fd_entry, entry);
1558
0
        _frvfdpic_add_rofixup (output_bfd,
1559
0
             frvfdpic_gotfixup_section (info),
1560
0
             frvfdpic_got_section (info)
1561
0
             ->output_section->vma
1562
0
             + frvfdpic_got_section (info)
1563
0
             ->output_offset
1564
0
             + frvfdpic_got_initial_offset (info)
1565
0
             + entry->fd_entry + 4, entry);
1566
0
      }
1567
0
  }
1568
0
      else
1569
0
  {
1570
0
    ofst =
1571
0
      _frvfdpic_add_dyn_reloc (output_bfd,
1572
0
             entry->lazyplt
1573
0
             ? frvfdpic_pltrel_section (info)
1574
0
             : frvfdpic_gotrel_section (info),
1575
0
             _bfd_elf_section_offset
1576
0
             (output_bfd, info,
1577
0
              frvfdpic_got_section (info),
1578
0
              frvfdpic_got_initial_offset (info)
1579
0
              + entry->fd_entry)
1580
0
             + frvfdpic_got_section (info)
1581
0
             ->output_section->vma
1582
0
             + frvfdpic_got_section (info)
1583
0
             ->output_offset,
1584
0
             R_FRV_FUNCDESC_VALUE, idx, ad, entry);
1585
0
  }
1586
1587
      /* If we've omitted the dynamic relocation, just emit the fixed
1588
   addresses of the symbol and of the local GOT base offset.  */
1589
0
      if (bfd_link_pde (info)
1590
0
    && sec
1591
0
    && sec->output_section)
1592
0
  {
1593
0
    lowword = ad;
1594
0
    highword = frvfdpic_got_section (info)->output_section->vma
1595
0
      + frvfdpic_got_section (info)->output_offset
1596
0
      + frvfdpic_got_initial_offset (info);
1597
0
  }
1598
0
      else if (entry->lazyplt)
1599
0
  {
1600
0
    if (ad)
1601
0
      {
1602
0
        (*info->callbacks->reloc_dangerous)
1603
0
    (info, _("relocation requires zero addend"),
1604
0
     elf_hash_table (info)->dynobj,
1605
0
     frvfdpic_got_section (info),
1606
0
     entry->fd_entry);
1607
0
        return false;
1608
0
      }
1609
1610
0
    fd_lazy_rel_offset = ofst;
1611
1612
    /* A function descriptor used for lazy or local resolving is
1613
       initialized such that its high word contains the output
1614
       section index in which the PLT entries are located, and
1615
       the low word contains the address of the lazy PLT entry
1616
       entry point, that must be within the memory region
1617
       assigned to that section.  */
1618
0
    lowword = entry->lzplt_entry + 4
1619
0
      + frvfdpic_plt_section (info)->output_offset
1620
0
      + frvfdpic_plt_section (info)->output_section->vma;
1621
0
    highword = _frvfdpic_osec_to_segment
1622
0
      (output_bfd, frvfdpic_plt_section (info)->output_section);
1623
0
  }
1624
0
      else
1625
0
  {
1626
    /* A function descriptor for a local function gets the index
1627
       of the section.  For a non-local function, it's
1628
       disregarded.  */
1629
0
    lowword = ad;
1630
0
    if (sec == NULL
1631
0
        || (entry->symndx == -1 && entry->d.h->dynindx != -1
1632
0
      && entry->d.h->dynindx == idx))
1633
0
      highword = 0;
1634
0
    else
1635
0
      highword = _frvfdpic_osec_to_segment
1636
0
        (output_bfd, sec->output_section);
1637
0
  }
1638
1639
0
      bfd_put_32 (output_bfd, lowword,
1640
0
      frvfdpic_got_section (info)->contents
1641
0
      + frvfdpic_got_initial_offset (info)
1642
0
      + entry->fd_entry);
1643
0
      bfd_put_32 (output_bfd, highword,
1644
0
      frvfdpic_got_section (info)->contents
1645
0
      + frvfdpic_got_initial_offset (info)
1646
0
      + entry->fd_entry + 4);
1647
0
    }
1648
1649
  /* Generate code for the PLT entry.  */
1650
0
  if (entry->plt_entry != (bfd_vma) -1)
1651
0
    {
1652
0
      bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
1653
0
  + entry->plt_entry;
1654
1655
0
      BFD_ASSERT (entry->fd_entry);
1656
1657
      /* Figure out what kind of PLT entry we need, depending on the
1658
   location of the function descriptor within the GOT.  */
1659
0
      if (entry->fd_entry >= -(1 << (12 - 1))
1660
0
    && entry->fd_entry < (1 << (12 - 1)))
1661
0
  {
1662
    /* lddi @(gr15, fd_entry), gr14 */
1663
0
    bfd_put_32 (output_bfd,
1664
0
          0x9cccf000 | (entry->fd_entry & ((1 << 12) - 1)),
1665
0
          plt_code);
1666
0
    plt_code += 4;
1667
0
  }
1668
0
      else
1669
0
  {
1670
0
    if (entry->fd_entry >= -(1 << (16 - 1))
1671
0
        && entry->fd_entry < (1 << (16 - 1)))
1672
0
      {
1673
        /* setlos lo(fd_entry), gr14 */
1674
0
        bfd_put_32 (output_bfd,
1675
0
        0x9cfc0000
1676
0
        | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1677
0
        plt_code);
1678
0
        plt_code += 4;
1679
0
      }
1680
0
    else
1681
0
      {
1682
        /* sethi.p hi(fd_entry), gr14
1683
     setlo lo(fd_entry), gr14 */
1684
0
        bfd_put_32 (output_bfd,
1685
0
        0x1cf80000
1686
0
        | ((entry->fd_entry >> 16)
1687
0
           & (((bfd_vma)1 << 16) - 1)),
1688
0
        plt_code);
1689
0
        plt_code += 4;
1690
0
        bfd_put_32 (output_bfd,
1691
0
        0x9cf40000
1692
0
        | (entry->fd_entry & (((bfd_vma)1 << 16) - 1)),
1693
0
        plt_code);
1694
0
        plt_code += 4;
1695
0
      }
1696
    /* ldd @(gr14,gr15),gr14 */
1697
0
    bfd_put_32 (output_bfd, 0x9c08e14f, plt_code);
1698
0
    plt_code += 4;
1699
0
  }
1700
      /* jmpl @(gr14,gr0) */
1701
0
      bfd_put_32 (output_bfd, 0x8030e000, plt_code);
1702
0
    }
1703
1704
  /* Generate code for the lazy PLT entry.  */
1705
0
  if (entry->lzplt_entry != (bfd_vma) -1)
1706
0
    {
1707
0
      bfd_byte *lzplt_code = frvfdpic_plt_section (info)->contents
1708
0
  + entry->lzplt_entry;
1709
0
      bfd_vma resolverStub_addr;
1710
1711
0
      bfd_put_32 (output_bfd, fd_lazy_rel_offset, lzplt_code);
1712
0
      lzplt_code += 4;
1713
1714
0
      resolverStub_addr = entry->lzplt_entry / FRVFDPIC_LZPLT_BLOCK_SIZE
1715
0
  * FRVFDPIC_LZPLT_BLOCK_SIZE + FRVFDPIC_LZPLT_RESOLV_LOC;
1716
0
      if (resolverStub_addr >= frvfdpic_plt_initial_offset (info))
1717
0
  resolverStub_addr = frvfdpic_plt_initial_offset (info) - 12;
1718
1719
0
      if (entry->lzplt_entry == resolverStub_addr)
1720
0
  {
1721
    /* This is a lazy PLT entry that includes a resolver call.  */
1722
    /* ldd @(gr15,gr0), gr4
1723
       jmpl @(gr4,gr0)  */
1724
0
    bfd_put_32 (output_bfd, 0x8808f140, lzplt_code);
1725
0
    bfd_put_32 (output_bfd, 0x80304000, lzplt_code + 4);
1726
0
  }
1727
0
      else
1728
0
  {
1729
    /* bra  resolverStub */
1730
0
    bfd_put_32 (output_bfd,
1731
0
          0xc01a0000
1732
0
          | (((resolverStub_addr - entry->lzplt_entry)
1733
0
        / 4) & (((bfd_vma)1 << 16) - 1)),
1734
0
          lzplt_code);
1735
0
  }
1736
0
    }
1737
1738
  /* Generate relocation for GOT entry holding the TLS offset.  */
1739
0
  if (entry->tlsoff_entry)
1740
0
    {
1741
0
      int idx = dynindx;
1742
0
      bfd_vma ad = addend;
1743
1744
0
      if (entry->symndx != -1
1745
0
    || FRVFDPIC_SYM_LOCAL (info, entry->d.h))
1746
0
  {
1747
    /* If the symbol is dynamic but binds locally, use
1748
       section+offset.  */
1749
0
    if (sec)
1750
0
      {
1751
0
        if (entry->symndx == -1)
1752
0
    ad += entry->d.h->root.u.def.value;
1753
0
        else
1754
0
    ad += sym->st_value;
1755
0
        ad += sec->output_offset;
1756
0
        if (sec->output_section
1757
0
      && elf_section_data (sec->output_section))
1758
0
    idx = elf_section_data (sec->output_section)->dynindx;
1759
0
        else
1760
0
    idx = 0;
1761
0
      }
1762
0
  }
1763
1764
      /* *ABS*+addend is special for TLS relocations, use only the
1765
   addend.  */
1766
0
      if (bfd_link_executable (info)
1767
0
    && idx == 0
1768
0
    && (bfd_is_abs_section (sec)
1769
0
        || bfd_is_und_section (sec)))
1770
0
  ;
1771
      /* If we're linking an executable, we can entirely omit the
1772
   dynamic relocation if the symbol is local to this module.  */
1773
0
      else if (bfd_link_executable (info)
1774
0
         && (entry->symndx != -1
1775
0
       || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1776
0
  {
1777
0
    if (sec)
1778
0
      ad += sec->output_section->vma - tls_biased_base (info);
1779
0
  }
1780
0
      else
1781
0
  {
1782
0
    if (idx == 0
1783
0
        && (bfd_is_abs_section (sec)
1784
0
      || bfd_is_und_section (sec)))
1785
0
      {
1786
0
        if (! elf_hash_table (info)->tls_sec)
1787
0
    {
1788
0
      (*info->callbacks->undefined_symbol)
1789
0
        (info, "TLS section", elf_hash_table (info)->dynobj,
1790
0
         frvfdpic_got_section (info), entry->tlsoff_entry, true);
1791
0
      return false;
1792
0
    }
1793
0
        idx = elf_section_data (elf_hash_table (info)->tls_sec)->dynindx;
1794
0
        ad += FRVFDPIC_TLS_BIAS;
1795
0
      }
1796
0
    _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1797
0
           _bfd_elf_section_offset
1798
0
           (output_bfd, info,
1799
0
            frvfdpic_got_section (info),
1800
0
            frvfdpic_got_initial_offset (info)
1801
0
            + entry->tlsoff_entry)
1802
0
           + frvfdpic_got_section (info)
1803
0
           ->output_section->vma
1804
0
           + frvfdpic_got_section (info)
1805
0
           ->output_offset,
1806
0
           R_FRV_TLSOFF, idx, ad, entry);
1807
0
  }
1808
1809
0
      bfd_put_32 (output_bfd, ad,
1810
0
      frvfdpic_got_section (info)->contents
1811
0
      + frvfdpic_got_initial_offset (info)
1812
0
      + entry->tlsoff_entry);
1813
0
    }
1814
1815
0
  if (entry->tlsdesc_entry)
1816
0
    {
1817
0
      int idx = dynindx;
1818
0
      bfd_vma ad = addend;
1819
1820
      /* If the symbol is dynamic but binds locally, use
1821
   section+offset.  */
1822
0
      if (sec && (entry->symndx != -1
1823
0
      || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1824
0
  {
1825
0
    if (entry->symndx == -1)
1826
0
      ad += entry->d.h->root.u.def.value;
1827
0
    else
1828
0
      ad += sym->st_value;
1829
0
    ad += sec->output_offset;
1830
0
    if (sec->output_section && elf_section_data (sec->output_section))
1831
0
      idx = elf_section_data (sec->output_section)->dynindx;
1832
0
    else
1833
0
      idx = 0;
1834
0
  }
1835
1836
      /* If we didn't set up a TLS offset entry, but we're linking an
1837
   executable and the symbol binds locally, we can use the
1838
   module offset in the TLS descriptor in relaxations.  */
1839
0
      if (bfd_link_executable (info) && ! entry->tlsoff_entry)
1840
0
  entry->tlsoff_entry = entry->tlsdesc_entry + 4;
1841
1842
0
      if (bfd_link_pde (info)
1843
0
    && ((idx == 0
1844
0
         && (bfd_is_abs_section (sec)
1845
0
       || bfd_is_und_section (sec)))
1846
0
        || entry->symndx != -1
1847
0
        || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1848
0
  {
1849
    /* *ABS*+addend is special for TLS relocations, use only the
1850
       addend for the TLS offset, and take the module id as
1851
       0.  */
1852
0
    if (idx == 0
1853
0
        && (bfd_is_abs_section (sec)
1854
0
      || bfd_is_und_section (sec)))
1855
0
      ;
1856
    /* For other TLS symbols that bind locally, add the section
1857
       TLS offset to the addend.  */
1858
0
    else if (sec)
1859
0
      ad += sec->output_section->vma - tls_biased_base (info);
1860
1861
0
    bfd_put_32 (output_bfd,
1862
0
          frvfdpic_plt_section (info)->output_section->vma
1863
0
          + frvfdpic_plt_section (info)->output_offset
1864
0
          + frvfdpic_plt_tls_ret_offset (info),
1865
0
          frvfdpic_got_section (info)->contents
1866
0
          + frvfdpic_got_initial_offset (info)
1867
0
          + entry->tlsdesc_entry);
1868
1869
0
    _frvfdpic_add_rofixup (output_bfd,
1870
0
         frvfdpic_gotfixup_section (info),
1871
0
         frvfdpic_got_section (info)
1872
0
         ->output_section->vma
1873
0
         + frvfdpic_got_section (info)
1874
0
         ->output_offset
1875
0
         + frvfdpic_got_initial_offset (info)
1876
0
         + entry->tlsdesc_entry, entry);
1877
1878
0
    BFD_ASSERT (frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs);
1879
1880
    /* We've used one of the reserved fixups, so discount it so
1881
       that we can check at the end that we've used them
1882
       all.  */
1883
0
    frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs--;
1884
1885
    /* While at that, make sure the ret instruction makes to the
1886
       right location in the PLT.  We could do it only when we
1887
       got to 0, but since the check at the end will only print
1888
       a warning, make sure we have the ret in place in case the
1889
       warning is missed.  */
1890
0
    bfd_put_32 (output_bfd, 0xc03a4000,
1891
0
          frvfdpic_plt_section (info)->contents
1892
0
          + frvfdpic_plt_tls_ret_offset (info));
1893
0
  }
1894
0
      else
1895
0
  {
1896
0
    if (idx == 0
1897
0
        && (bfd_is_abs_section (sec)
1898
0
      || bfd_is_und_section (sec)))
1899
0
      {
1900
0
        if (! elf_hash_table (info)->tls_sec)
1901
0
    {
1902
0
      (*info->callbacks->undefined_symbol)
1903
0
        (info, "TLS section", elf_hash_table (info)->dynobj,
1904
0
         frvfdpic_got_section (info), entry->tlsdesc_entry, true);
1905
0
      return false;
1906
0
    }
1907
0
        idx = elf_section_data (elf_hash_table (info)->tls_sec)->dynindx;
1908
0
        ad += FRVFDPIC_TLS_BIAS;
1909
0
      }
1910
1911
0
    _frvfdpic_add_dyn_reloc (output_bfd, frvfdpic_gotrel_section (info),
1912
0
           _bfd_elf_section_offset
1913
0
           (output_bfd, info,
1914
0
            frvfdpic_got_section (info),
1915
0
            frvfdpic_got_initial_offset (info)
1916
0
            + entry->tlsdesc_entry)
1917
0
           + frvfdpic_got_section (info)
1918
0
           ->output_section->vma
1919
0
           + frvfdpic_got_section (info)
1920
0
           ->output_offset,
1921
0
           R_FRV_TLSDESC_VALUE, idx, ad, entry);
1922
1923
0
    bfd_put_32 (output_bfd, 0,
1924
0
          frvfdpic_got_section (info)->contents
1925
0
          + frvfdpic_got_initial_offset (info)
1926
0
          + entry->tlsdesc_entry);
1927
0
  }
1928
1929
0
      bfd_put_32 (output_bfd, ad,
1930
0
      frvfdpic_got_section (info)->contents
1931
0
      + frvfdpic_got_initial_offset (info)
1932
0
      + entry->tlsdesc_entry + 4);
1933
0
    }
1934
1935
  /* Generate code for the get-TLS-offset PLT entry.  */
1936
0
  if (entry->tlsplt_entry != (bfd_vma) -1)
1937
0
    {
1938
0
      bfd_byte *plt_code = frvfdpic_plt_section (info)->contents
1939
0
  + entry->tlsplt_entry;
1940
1941
0
      if (bfd_link_executable (info)
1942
0
    && (entry->symndx != -1
1943
0
        || FRVFDPIC_SYM_LOCAL (info, entry->d.h)))
1944
0
  {
1945
0
    int idx = dynindx;
1946
0
    bfd_vma ad = addend;
1947
1948
    /* sec may be NULL when referencing an undefweak symbol
1949
       while linking a static executable.  */
1950
0
    if (!sec)
1951
0
      {
1952
0
        BFD_ASSERT (entry->symndx == -1
1953
0
        && entry->d.h->root.type == bfd_link_hash_undefweak);
1954
0
      }
1955
0
    else
1956
0
      {
1957
0
        if (entry->symndx == -1)
1958
0
    ad += entry->d.h->root.u.def.value;
1959
0
        else
1960
0
    ad += sym->st_value;
1961
0
        ad += sec->output_offset;
1962
0
        if (sec->output_section
1963
0
      && elf_section_data (sec->output_section))
1964
0
    idx = elf_section_data (sec->output_section)->dynindx;
1965
0
        else
1966
0
    idx = 0;
1967
0
      }
1968
1969
    /* *ABS*+addend is special for TLS relocations, use only the
1970
       addend for the TLS offset, and take the module id as
1971
       0.  */
1972
0
    if (idx == 0
1973
0
        && (bfd_is_abs_section (sec)
1974
0
      || bfd_is_und_section (sec)))
1975
0
      ;
1976
    /* For other TLS symbols that bind locally, add the section
1977
       TLS offset to the addend.  */
1978
0
    else if (sec)
1979
0
      ad += sec->output_section->vma - tls_biased_base (info);
1980
1981
0
    if ((bfd_signed_vma)ad >= -(1 << (16 - 1))
1982
0
        && (bfd_signed_vma)ad < (1 << (16 - 1)))
1983
0
      {
1984
        /* setlos lo(ad), gr9 */
1985
0
        bfd_put_32 (output_bfd,
1986
0
        0x92fc0000
1987
0
        | (ad
1988
0
           & (((bfd_vma)1 << 16) - 1)),
1989
0
        plt_code);
1990
0
        plt_code += 4;
1991
0
      }
1992
0
    else
1993
0
      {
1994
        /* sethi.p hi(ad), gr9
1995
     setlo lo(ad), gr9 */
1996
0
        bfd_put_32 (output_bfd,
1997
0
        0x12f80000
1998
0
        | ((ad >> 16)
1999
0
           & (((bfd_vma)1 << 16) - 1)),
2000
0
        plt_code);
2001
0
        plt_code += 4;
2002
0
        bfd_put_32 (output_bfd,
2003
0
        0x92f40000
2004
0
        | (ad
2005
0
           & (((bfd_vma)1 << 16) - 1)),
2006
0
        plt_code);
2007
0
        plt_code += 4;
2008
0
      }
2009
    /* ret */
2010
0
    bfd_put_32 (output_bfd, 0xc03a4000, plt_code);
2011
0
  }
2012
0
      else if (entry->tlsoff_entry)
2013
0
  {
2014
    /* Figure out what kind of PLT entry we need, depending on the
2015
       location of the TLS descriptor within the GOT.  */
2016
0
    if (entry->tlsoff_entry >= -(1 << (12 - 1))
2017
0
        && entry->tlsoff_entry < (1 << (12 - 1)))
2018
0
      {
2019
        /* ldi @(gr15, tlsoff_entry), gr9 */
2020
0
        bfd_put_32 (output_bfd,
2021
0
        0x92c8f000 | (entry->tlsoff_entry
2022
0
          & ((1 << 12) - 1)),
2023
0
        plt_code);
2024
0
        plt_code += 4;
2025
0
      }
2026
0
    else
2027
0
      {
2028
0
        if (entry->tlsoff_entry >= -(1 << (16 - 1))
2029
0
      && entry->tlsoff_entry < (1 << (16 - 1)))
2030
0
    {
2031
      /* setlos lo(tlsoff_entry), gr8 */
2032
0
      bfd_put_32 (output_bfd,
2033
0
            0x90fc0000
2034
0
            | (entry->tlsoff_entry
2035
0
         & (((bfd_vma)1 << 16) - 1)),
2036
0
            plt_code);
2037
0
      plt_code += 4;
2038
0
    }
2039
0
        else
2040
0
    {
2041
      /* sethi.p hi(tlsoff_entry), gr8
2042
         setlo lo(tlsoff_entry), gr8 */
2043
0
      bfd_put_32 (output_bfd,
2044
0
            0x10f80000
2045
0
            | ((entry->tlsoff_entry >> 16)
2046
0
         & (((bfd_vma)1 << 16) - 1)),
2047
0
            plt_code);
2048
0
      plt_code += 4;
2049
0
      bfd_put_32 (output_bfd,
2050
0
            0x90f40000
2051
0
            | (entry->tlsoff_entry
2052
0
         & (((bfd_vma)1 << 16) - 1)),
2053
0
            plt_code);
2054
0
      plt_code += 4;
2055
0
    }
2056
        /* ld @(gr15,gr8),gr9 */
2057
0
        bfd_put_32 (output_bfd, 0x9008f108, plt_code);
2058
0
        plt_code += 4;
2059
0
      }
2060
    /* ret */
2061
0
    bfd_put_32 (output_bfd, 0xc03a4000, plt_code);
2062
0
  }
2063
0
      else
2064
0
  {
2065
0
    BFD_ASSERT (entry->tlsdesc_entry);
2066
2067
    /* Figure out what kind of PLT entry we need, depending on the
2068
       location of the TLS descriptor within the GOT.  */
2069
0
    if (entry->tlsdesc_entry >= -(1 << (12 - 1))
2070
0
        && entry->tlsdesc_entry < (1 << (12 - 1)))
2071
0
      {
2072
        /* lddi @(gr15, tlsdesc_entry), gr8 */
2073
0
        bfd_put_32 (output_bfd,
2074
0
        0x90ccf000 | (entry->tlsdesc_entry
2075
0
          & ((1 << 12) - 1)),
2076
0
        plt_code);
2077
0
        plt_code += 4;
2078
0
      }
2079
0
    else
2080
0
      {
2081
0
        if (entry->tlsdesc_entry >= -(1 << (16 - 1))
2082
0
      && entry->tlsdesc_entry < (1 << (16 - 1)))
2083
0
    {
2084
      /* setlos lo(tlsdesc_entry), gr8 */
2085
0
      bfd_put_32 (output_bfd,
2086
0
            0x90fc0000
2087
0
            | (entry->tlsdesc_entry
2088
0
         & (((bfd_vma)1 << 16) - 1)),
2089
0
            plt_code);
2090
0
      plt_code += 4;
2091
0
    }
2092
0
        else
2093
0
    {
2094
      /* sethi.p hi(tlsdesc_entry), gr8
2095
         setlo lo(tlsdesc_entry), gr8 */
2096
0
      bfd_put_32 (output_bfd,
2097
0
            0x10f80000
2098
0
            | ((entry->tlsdesc_entry >> 16)
2099
0
         & (((bfd_vma)1 << 16) - 1)),
2100
0
            plt_code);
2101
0
      plt_code += 4;
2102
0
      bfd_put_32 (output_bfd,
2103
0
            0x90f40000
2104
0
            | (entry->tlsdesc_entry
2105
0
         & (((bfd_vma)1 << 16) - 1)),
2106
0
            plt_code);
2107
0
      plt_code += 4;
2108
0
    }
2109
        /* ldd @(gr15,gr8),gr8 */
2110
0
        bfd_put_32 (output_bfd, 0x9008f148, plt_code);
2111
0
        plt_code += 4;
2112
0
      }
2113
    /* jmpl @(gr8,gr0) */
2114
0
    bfd_put_32 (output_bfd, 0x80308000, plt_code);
2115
0
  }
2116
0
    }
2117
2118
0
  return true;
2119
0
}
2120
2121
/* Handle an FRV small data reloc.  */
2122
2123
static bfd_reloc_status_type
2124
elf32_frv_relocate_gprel12 (struct bfd_link_info *info,
2125
          bfd *input_bfd,
2126
          asection *input_section,
2127
          Elf_Internal_Rela *relocation,
2128
          bfd_byte *contents,
2129
          bfd_vma value)
2130
0
{
2131
0
  bfd_vma insn;
2132
0
  bfd_vma gp;
2133
0
  struct bfd_link_hash_entry *h;
2134
2135
0
  h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
2136
2137
0
  gp = (h->u.def.value
2138
0
  + h->u.def.section->output_section->vma
2139
0
  + h->u.def.section->output_offset);
2140
2141
0
  value -= input_section->output_section->vma;
2142
0
  value -= (gp - input_section->output_section->vma);
2143
2144
0
  insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2145
2146
0
  value += relocation->r_addend;
2147
2148
0
  if ((long) value > 0x7ff || (long) value < -0x800)
2149
0
    return bfd_reloc_overflow;
2150
2151
0
  bfd_put_32 (input_bfd,
2152
0
        (insn & 0xfffff000) | (value & 0xfff),
2153
0
        contents + relocation->r_offset);
2154
2155
0
  return bfd_reloc_ok;
2156
0
}
2157
2158
/* Handle an FRV small data reloc. for the u12 field.  */
2159
2160
static bfd_reloc_status_type
2161
elf32_frv_relocate_gprelu12 (struct bfd_link_info *info,
2162
           bfd *input_bfd,
2163
           asection *input_section,
2164
           Elf_Internal_Rela *relocation,
2165
           bfd_byte *contents,
2166
           bfd_vma value)
2167
0
{
2168
0
  bfd_vma insn;
2169
0
  bfd_vma gp;
2170
0
  struct bfd_link_hash_entry *h;
2171
0
  bfd_vma mask;
2172
2173
0
  h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
2174
2175
0
  gp = (h->u.def.value
2176
0
  + h->u.def.section->output_section->vma
2177
0
  + h->u.def.section->output_offset);
2178
2179
0
  value -= input_section->output_section->vma;
2180
0
  value -= (gp - input_section->output_section->vma);
2181
2182
0
  insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2183
2184
0
  value += relocation->r_addend;
2185
2186
0
  if ((long) value > 0x7ff || (long) value < -0x800)
2187
0
    return bfd_reloc_overflow;
2188
2189
  /* The high 6 bits go into bits 17-12. The low 6 bits go into bits 5-0.  */
2190
0
  mask = 0x3f03f;
2191
0
  insn = (insn & ~mask) | ((value & 0xfc0) << 12) | (value & 0x3f);
2192
2193
0
  bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2194
2195
0
  return bfd_reloc_ok;
2196
0
}
2197
2198
/* Handle an FRV ELF HI16 reloc.  */
2199
2200
static bfd_reloc_status_type
2201
elf32_frv_relocate_hi16 (bfd *input_bfd,
2202
       Elf_Internal_Rela *relhi,
2203
       bfd_byte *contents,
2204
       bfd_vma value)
2205
0
{
2206
0
  bfd_vma insn;
2207
2208
0
  insn = bfd_get_32 (input_bfd, contents + relhi->r_offset);
2209
2210
0
  value += relhi->r_addend;
2211
0
  value = ((value >> 16) & 0xffff);
2212
2213
0
  insn = (insn & 0xffff0000) | value;
2214
2215
0
  if ((long) value > 0xffff || (long) value < -0x10000)
2216
0
    return bfd_reloc_overflow;
2217
2218
0
  bfd_put_32 (input_bfd, insn, contents + relhi->r_offset);
2219
0
  return bfd_reloc_ok;
2220
2221
0
}
2222
static bfd_reloc_status_type
2223
elf32_frv_relocate_lo16 (bfd *input_bfd,
2224
       Elf_Internal_Rela *rello,
2225
       bfd_byte *contents,
2226
       bfd_vma value)
2227
0
{
2228
0
  bfd_vma insn;
2229
2230
0
  insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
2231
2232
0
  value += rello->r_addend;
2233
0
  value = value & 0xffff;
2234
2235
0
  insn = (insn & 0xffff0000) | value;
2236
2237
0
  if ((long) value > 0xffff || (long) value < -0x10000)
2238
0
    return bfd_reloc_overflow;
2239
2240
0
  bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
2241
0
  return bfd_reloc_ok;
2242
0
}
2243
2244
/* Perform the relocation for the CALL label24 instruction.  */
2245
2246
static bfd_reloc_status_type
2247
elf32_frv_relocate_label24 (bfd *input_bfd,
2248
          asection *input_section,
2249
          Elf_Internal_Rela *rello,
2250
          bfd_byte *contents,
2251
          bfd_vma value)
2252
0
{
2253
0
  bfd_vma insn;
2254
0
  bfd_vma label6;
2255
0
  bfd_vma label18;
2256
2257
  /* The format for the call instruction is:
2258
2259
    0 000000 0001111 000000000000000000
2260
      label6 opcode  label18
2261
2262
    The branch calculation is: pc + (4*label24)
2263
    where label24 is the concatenation of label6 and label18.  */
2264
2265
  /* Grab the instruction.  */
2266
0
  insn = bfd_get_32 (input_bfd, contents + rello->r_offset);
2267
2268
0
  value -= input_section->output_section->vma + input_section->output_offset;
2269
0
  value -= rello->r_offset;
2270
0
  value += rello->r_addend;
2271
2272
0
  value = value >> 2;
2273
2274
0
  label6  = value & 0xfc0000;
2275
0
  label6  = label6 << 7;
2276
2277
0
  label18 = value & 0x3ffff;
2278
2279
0
  insn = insn & 0x803c0000;
2280
0
  insn = insn | label6;
2281
0
  insn = insn | label18;
2282
2283
0
  bfd_put_32 (input_bfd, insn, contents + rello->r_offset);
2284
2285
0
  return bfd_reloc_ok;
2286
0
}
2287
2288
static bfd_reloc_status_type
2289
elf32_frv_relocate_gprelhi (struct bfd_link_info *info,
2290
          bfd *input_bfd,
2291
          asection *input_section,
2292
          Elf_Internal_Rela *relocation,
2293
          bfd_byte *contents,
2294
          bfd_vma value)
2295
0
{
2296
0
  bfd_vma insn;
2297
0
  bfd_vma gp;
2298
0
  struct bfd_link_hash_entry *h;
2299
2300
0
  h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
2301
2302
0
  gp = (h->u.def.value
2303
0
  + h->u.def.section->output_section->vma
2304
0
  + h->u.def.section->output_offset);
2305
2306
0
  value -= input_section->output_section->vma;
2307
0
  value -= (gp - input_section->output_section->vma);
2308
0
  value += relocation->r_addend;
2309
0
  value = ((value >> 16) & 0xffff);
2310
2311
0
  if ((long) value > 0xffff || (long) value < -0x10000)
2312
0
    return bfd_reloc_overflow;
2313
2314
0
  insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2315
0
  insn = (insn & 0xffff0000) | value;
2316
2317
0
  bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2318
0
  return bfd_reloc_ok;
2319
0
}
2320
2321
static bfd_reloc_status_type
2322
elf32_frv_relocate_gprello (struct bfd_link_info *info,
2323
          bfd *input_bfd,
2324
          asection *input_section,
2325
          Elf_Internal_Rela *relocation,
2326
          bfd_byte *contents,
2327
          bfd_vma value)
2328
0
{
2329
0
  bfd_vma insn;
2330
0
  bfd_vma gp;
2331
0
  struct bfd_link_hash_entry *h;
2332
2333
0
  h = bfd_link_hash_lookup (info->hash, "_gp", false, false, true);
2334
2335
0
  gp = (h->u.def.value
2336
0
  + h->u.def.section->output_section->vma
2337
0
  + h->u.def.section->output_offset);
2338
2339
0
  value -= input_section->output_section->vma;
2340
0
  value -= (gp - input_section->output_section->vma);
2341
0
  value += relocation->r_addend;
2342
0
  value = value & 0xffff;
2343
2344
0
  if ((long) value > 0xffff || (long) value < -0x10000)
2345
0
    return bfd_reloc_overflow;
2346
2347
0
  insn = bfd_get_32 (input_bfd, contents + relocation->r_offset);
2348
0
  insn = (insn & 0xffff0000) | value;
2349
2350
0
  bfd_put_32 (input_bfd, insn, contents + relocation->r_offset);
2351
2352
0
 return bfd_reloc_ok;
2353
0
}
2354
2355
static reloc_howto_type *
2356
frv_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
2357
           bfd_reloc_code_real_type code)
2358
0
{
2359
0
  switch (code)
2360
0
    {
2361
0
    default:
2362
0
      break;
2363
2364
0
    case BFD_RELOC_NONE:
2365
0
      return &elf32_frv_howto_table[ (int) R_FRV_NONE];
2366
2367
0
    case BFD_RELOC_32:
2368
0
      if (elf_elfheader (abfd)->e_type == ET_EXEC
2369
0
    || elf_elfheader (abfd)->e_type == ET_DYN)
2370
0
  return &elf32_frv_rel_32_howto;
2371
      /* Fall through.  */
2372
0
    case BFD_RELOC_CTOR:
2373
0
      return &elf32_frv_howto_table[ (int) R_FRV_32];
2374
2375
0
    case BFD_RELOC_FRV_LABEL16:
2376
0
      return &elf32_frv_howto_table[ (int) R_FRV_LABEL16];
2377
2378
0
    case BFD_RELOC_FRV_LABEL24:
2379
0
      return &elf32_frv_howto_table[ (int) R_FRV_LABEL24];
2380
2381
0
    case BFD_RELOC_FRV_LO16:
2382
0
      return &elf32_frv_howto_table[ (int) R_FRV_LO16];
2383
2384
0
    case BFD_RELOC_FRV_HI16:
2385
0
      return &elf32_frv_howto_table[ (int) R_FRV_HI16];
2386
2387
0
    case BFD_RELOC_FRV_GPREL12:
2388
0
      return &elf32_frv_howto_table[ (int) R_FRV_GPREL12];
2389
2390
0
    case BFD_RELOC_FRV_GPRELU12:
2391
0
      return &elf32_frv_howto_table[ (int) R_FRV_GPRELU12];
2392
2393
0
    case BFD_RELOC_FRV_GPREL32:
2394
0
      return &elf32_frv_howto_table[ (int) R_FRV_GPREL32];
2395
2396
0
    case BFD_RELOC_FRV_GPRELHI:
2397
0
      return &elf32_frv_howto_table[ (int) R_FRV_GPRELHI];
2398
2399
0
    case BFD_RELOC_FRV_GPRELLO:
2400
0
      return &elf32_frv_howto_table[ (int) R_FRV_GPRELLO];
2401
2402
0
    case BFD_RELOC_FRV_GOT12:
2403
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOT12];
2404
2405
0
    case BFD_RELOC_FRV_GOTHI:
2406
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOTHI];
2407
2408
0
    case BFD_RELOC_FRV_GOTLO:
2409
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOTLO];
2410
2411
0
    case BFD_RELOC_FRV_FUNCDESC:
2412
0
      if (elf_elfheader (abfd)->e_type == ET_EXEC
2413
0
    || elf_elfheader (abfd)->e_type == ET_DYN)
2414
0
  return &elf32_frv_rel_funcdesc_howto;
2415
0
      return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC];
2416
2417
0
    case BFD_RELOC_FRV_FUNCDESC_GOT12:
2418
0
      return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOT12];
2419
2420
0
    case BFD_RELOC_FRV_FUNCDESC_GOTHI:
2421
0
      return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTHI];
2422
2423
0
    case BFD_RELOC_FRV_FUNCDESC_GOTLO:
2424
0
      return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTLO];
2425
2426
0
    case BFD_RELOC_FRV_FUNCDESC_VALUE:
2427
0
      if (elf_elfheader (abfd)->e_type == ET_EXEC
2428
0
    || elf_elfheader (abfd)->e_type == ET_DYN)
2429
0
  return &elf32_frv_rel_funcdesc_value_howto;
2430
0
      return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_VALUE];
2431
2432
0
    case BFD_RELOC_FRV_FUNCDESC_GOTOFF12:
2433
0
      return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFF12];
2434
2435
0
    case BFD_RELOC_FRV_FUNCDESC_GOTOFFHI:
2436
0
      return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFHI];
2437
2438
0
    case BFD_RELOC_FRV_FUNCDESC_GOTOFFLO:
2439
0
      return &elf32_frv_howto_table[ (int) R_FRV_FUNCDESC_GOTOFFLO];
2440
2441
0
    case BFD_RELOC_FRV_GOTOFF12:
2442
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOTOFF12];
2443
2444
0
    case BFD_RELOC_FRV_GOTOFFHI:
2445
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFHI];
2446
2447
0
    case BFD_RELOC_FRV_GOTOFFLO:
2448
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOTOFFLO];
2449
2450
0
    case BFD_RELOC_FRV_GETTLSOFF:
2451
0
      return &elf32_frv_howto_table[ (int) R_FRV_GETTLSOFF];
2452
2453
0
    case BFD_RELOC_FRV_TLSDESC_VALUE:
2454
0
      if (elf_elfheader (abfd)->e_type == ET_EXEC
2455
0
    || elf_elfheader (abfd)->e_type == ET_DYN)
2456
0
  return &elf32_frv_rel_tlsdesc_value_howto;
2457
0
      return &elf32_frv_howto_table[ (int) R_FRV_TLSDESC_VALUE];
2458
2459
0
    case BFD_RELOC_FRV_GOTTLSDESC12:
2460
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESC12];
2461
2462
0
    case BFD_RELOC_FRV_GOTTLSDESCHI:
2463
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESCHI];
2464
2465
0
    case BFD_RELOC_FRV_GOTTLSDESCLO:
2466
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSDESCLO];
2467
2468
0
    case BFD_RELOC_FRV_TLSMOFF12:
2469
0
      return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFF12];
2470
2471
0
    case BFD_RELOC_FRV_TLSMOFFHI:
2472
0
      return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFFHI];
2473
2474
0
    case BFD_RELOC_FRV_TLSMOFFLO:
2475
0
      return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFFLO];
2476
2477
0
    case BFD_RELOC_FRV_GOTTLSOFF12:
2478
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFF12];
2479
2480
0
    case BFD_RELOC_FRV_GOTTLSOFFHI:
2481
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFFHI];
2482
2483
0
    case BFD_RELOC_FRV_GOTTLSOFFLO:
2484
0
      return &elf32_frv_howto_table[ (int) R_FRV_GOTTLSOFFLO];
2485
2486
0
    case BFD_RELOC_FRV_TLSOFF:
2487
0
      if (elf_elfheader (abfd)->e_type == ET_EXEC
2488
0
    || elf_elfheader (abfd)->e_type == ET_DYN)
2489
0
  return &elf32_frv_rel_tlsoff_howto;
2490
0
      return &elf32_frv_howto_table[ (int) R_FRV_TLSOFF];
2491
2492
0
    case BFD_RELOC_FRV_TLSDESC_RELAX:
2493
0
      return &elf32_frv_howto_table[ (int) R_FRV_TLSDESC_RELAX];
2494
2495
0
    case BFD_RELOC_FRV_GETTLSOFF_RELAX:
2496
0
      return &elf32_frv_howto_table[ (int) R_FRV_GETTLSOFF_RELAX];
2497
2498
0
    case BFD_RELOC_FRV_TLSOFF_RELAX:
2499
0
      return &elf32_frv_howto_table[ (int) R_FRV_TLSOFF_RELAX];
2500
2501
0
    case BFD_RELOC_FRV_TLSMOFF:
2502
0
      return &elf32_frv_howto_table[ (int) R_FRV_TLSMOFF];
2503
2504
0
    case BFD_RELOC_VTABLE_INHERIT:
2505
0
      return &elf32_frv_vtinherit_howto;
2506
2507
0
    case BFD_RELOC_VTABLE_ENTRY:
2508
0
      return &elf32_frv_vtentry_howto;
2509
0
    }
2510
2511
0
  return NULL;
2512
0
}
2513
2514
static reloc_howto_type *
2515
frv_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
2516
0
{
2517
0
  unsigned int i;
2518
2519
0
  for (i = 0;
2520
0
       i < sizeof (elf32_frv_howto_table) / sizeof (elf32_frv_howto_table[0]);
2521
0
       i++)
2522
0
    if (elf32_frv_howto_table[i].name != NULL
2523
0
  && strcasecmp (elf32_frv_howto_table[i].name, r_name) == 0)
2524
0
      return &elf32_frv_howto_table[i];
2525
2526
0
  if (strcasecmp (elf32_frv_vtinherit_howto.name, r_name) == 0)
2527
0
    return &elf32_frv_vtinherit_howto;
2528
0
  if (strcasecmp (elf32_frv_vtentry_howto.name, r_name) == 0)
2529
0
    return &elf32_frv_vtentry_howto;
2530
2531
0
  return NULL;
2532
0
}
2533
2534
/* Set the howto pointer for an FRV ELF reloc.  */
2535
2536
static bool
2537
frv_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
2538
      arelent *cache_ptr,
2539
      Elf_Internal_Rela *dst)
2540
0
{
2541
0
  unsigned int r_type;
2542
2543
0
  r_type = ELF32_R_TYPE (dst->r_info);
2544
0
  switch (r_type)
2545
0
    {
2546
0
    case R_FRV_GNU_VTINHERIT:
2547
0
      cache_ptr->howto = &elf32_frv_vtinherit_howto;
2548
0
      break;
2549
2550
0
    case R_FRV_GNU_VTENTRY:
2551
0
      cache_ptr->howto = &elf32_frv_vtentry_howto;
2552
0
      break;
2553
2554
0
    default:
2555
0
      if (r_type >= ARRAY_SIZE (elf32_frv_howto_table))
2556
0
  {
2557
    /* xgettext:c-format */
2558
0
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
2559
0
            abfd, r_type);
2560
0
    bfd_set_error (bfd_error_bad_value);
2561
0
    return false;
2562
0
  }
2563
0
      cache_ptr->howto = & elf32_frv_howto_table [r_type];
2564
0
      break;
2565
0
    }
2566
0
  return true;
2567
0
}
2568
2569
/* Set the howto pointer for an FRV ELF REL reloc.  */
2570
2571
static bool
2572
frvfdpic_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
2573
          arelent *cache_ptr, Elf_Internal_Rela *dst)
2574
0
{
2575
0
  unsigned int r_type;
2576
2577
0
  r_type = ELF32_R_TYPE (dst->r_info);
2578
0
  switch (r_type)
2579
0
    {
2580
0
    case R_FRV_32:
2581
0
      cache_ptr->howto = &elf32_frv_rel_32_howto;
2582
0
      break;
2583
2584
0
    case R_FRV_FUNCDESC:
2585
0
      cache_ptr->howto = &elf32_frv_rel_funcdesc_howto;
2586
0
      break;
2587
2588
0
    case R_FRV_FUNCDESC_VALUE:
2589
0
      cache_ptr->howto = &elf32_frv_rel_funcdesc_value_howto;
2590
0
      break;
2591
2592
0
    case R_FRV_TLSDESC_VALUE:
2593
0
      cache_ptr->howto = &elf32_frv_rel_tlsdesc_value_howto;
2594
0
      break;
2595
2596
0
    case R_FRV_TLSOFF:
2597
0
      cache_ptr->howto = &elf32_frv_rel_tlsoff_howto;
2598
0
      break;
2599
2600
0
    default:
2601
0
      cache_ptr->howto = NULL;
2602
0
      return false;
2603
0
    }
2604
0
  return true;
2605
0
}
2606

2607
/* Perform a single relocation.  By default we use the standard BFD
2608
   routines, but a few relocs, we have to do them ourselves.  */
2609
2610
static bfd_reloc_status_type
2611
frv_final_link_relocate (reloc_howto_type *howto,
2612
       bfd *input_bfd,
2613
       asection *input_section,
2614
       bfd_byte *contents,
2615
       Elf_Internal_Rela *rel,
2616
       bfd_vma relocation)
2617
0
{
2618
0
  return _bfd_final_link_relocate (howto, input_bfd, input_section,
2619
0
           contents, rel->r_offset, relocation,
2620
0
           rel->r_addend);
2621
0
}
2622
2623

2624
/* Relocate an FRV ELF section.
2625
2626
   The RELOCATE_SECTION function is called by the new ELF backend linker
2627
   to handle the relocations for a section.
2628
2629
   The relocs are always passed as Rela structures; if the section
2630
   actually uses Rel structures, the r_addend field will always be
2631
   zero.
2632
2633
   This function is responsible for adjusting the section contents as
2634
   necessary, and (if using Rela relocs and generating a relocatable
2635
   output file) adjusting the reloc addend as necessary.
2636
2637
   This function does not have to worry about setting the reloc
2638
   address or the reloc symbol index.
2639
2640
   LOCAL_SYMS is a pointer to the swapped in local symbols.
2641
2642
   LOCAL_SECTIONS is an array giving the section in the input file
2643
   corresponding to the st_shndx field of each local symbol.
2644
2645
   The global hash table entry for the global symbols can be found
2646
   via elf_sym_hashes (input_bfd).
2647
2648
   When generating relocatable output, this function must handle
2649
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
2650
   going to be the section symbol corresponding to the output
2651
   section, which means that the addend must be adjusted
2652
   accordingly.  */
2653
2654
static int
2655
elf32_frv_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
2656
          struct bfd_link_info *info,
2657
          bfd *input_bfd,
2658
          asection *input_section,
2659
          bfd_byte *contents,
2660
          Elf_Internal_Rela *relocs,
2661
          Elf_Internal_Sym *local_syms,
2662
          asection **local_sections)
2663
0
{
2664
0
  Elf_Internal_Shdr *symtab_hdr;
2665
0
  struct elf_link_hash_entry **sym_hashes;
2666
0
  Elf_Internal_Rela *rel;
2667
0
  Elf_Internal_Rela *relend;
2668
0
  unsigned isec_segment, got_segment, plt_segment, gprel_segment, tls_segment,
2669
0
    check_segment[2];
2670
0
  int silence_segment_error = !bfd_link_pic (info);
2671
0
  unsigned long insn;
2672
2673
0
  symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
2674
0
  sym_hashes = elf_sym_hashes (input_bfd);
2675
0
  relend     = relocs + input_section->reloc_count;
2676
2677
0
  isec_segment = _frvfdpic_osec_to_segment (output_bfd,
2678
0
              input_section->output_section);
2679
0
  if (IS_FDPIC (output_bfd) && frvfdpic_got_section (info))
2680
0
    got_segment = _frvfdpic_osec_to_segment (output_bfd,
2681
0
               frvfdpic_got_section (info)
2682
0
               ->output_section);
2683
0
  else
2684
0
    got_segment = -1;
2685
0
  if (IS_FDPIC (output_bfd) && frvfdpic_gotfixup_section (info))
2686
0
    gprel_segment = _frvfdpic_osec_to_segment (output_bfd,
2687
0
                 frvfdpic_gotfixup_section (info)
2688
0
                 ->output_section);
2689
0
  else
2690
0
    gprel_segment = -1;
2691
0
  if (IS_FDPIC (output_bfd) && frvfdpic_plt_section (info))
2692
0
    plt_segment = _frvfdpic_osec_to_segment (output_bfd,
2693
0
               frvfdpic_plt_section (info)
2694
0
               ->output_section);
2695
0
  else
2696
0
    plt_segment = -1;
2697
0
  if (elf_hash_table (info)->tls_sec)
2698
0
    tls_segment = _frvfdpic_osec_to_segment (output_bfd,
2699
0
               elf_hash_table (info)->tls_sec);
2700
0
  else
2701
0
    tls_segment = -1;
2702
2703
0
  for (rel = relocs; rel < relend; rel ++)
2704
0
    {
2705
0
      reloc_howto_type *howto;
2706
0
      unsigned long r_symndx;
2707
0
      Elf_Internal_Sym *sym;
2708
0
      asection *sec;
2709
0
      struct elf_link_hash_entry *h;
2710
0
      bfd_vma relocation;
2711
0
      bfd_reloc_status_type r;
2712
0
      const char *name;
2713
0
      int r_type;
2714
0
      asection *osec;
2715
0
      struct frvfdpic_relocs_info *picrel = NULL;
2716
0
      bfd_vma orig_addend = rel->r_addend;
2717
2718
0
      r_type = ELF32_R_TYPE (rel->r_info);
2719
2720
0
      if (   r_type == R_FRV_GNU_VTINHERIT
2721
0
    || r_type == R_FRV_GNU_VTENTRY)
2722
0
  continue;
2723
2724
0
      r_symndx = ELF32_R_SYM (rel->r_info);
2725
0
      howto  = elf32_frv_howto_table + ELF32_R_TYPE (rel->r_info);
2726
0
      h      = NULL;
2727
0
      sym    = NULL;
2728
0
      sec    = NULL;
2729
2730
0
      if (r_symndx < symtab_hdr->sh_info)
2731
0
  {
2732
0
    sym = local_syms + r_symndx;
2733
0
    osec = sec = local_sections [r_symndx];
2734
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2735
2736
0
    name = bfd_elf_string_from_elf_section
2737
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name);
2738
0
    if (name == NULL || name[0] == 0)
2739
0
      name = bfd_section_name (sec);
2740
0
  }
2741
0
      else
2742
0
  {
2743
0
    bool warned, ignored;
2744
0
    bool unresolved_reloc;
2745
2746
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2747
0
           r_symndx, symtab_hdr, sym_hashes,
2748
0
           h, sec, relocation,
2749
0
           unresolved_reloc, warned, ignored);
2750
0
    osec = sec;
2751
0
    name = h->root.root.string;
2752
0
  }
2753
2754
0
      if (sec != NULL && discarded_section (sec))
2755
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2756
0
           rel, 1, relend, R_FRV_NONE,
2757
0
           howto, 0, contents);
2758
2759
0
      if (bfd_link_relocatable (info))
2760
0
  continue;
2761
2762
0
      if (r_type != R_FRV_TLSMOFF
2763
0
    && h != NULL
2764
0
    && (h->root.type == bfd_link_hash_defined
2765
0
        || h->root.type == bfd_link_hash_defweak)
2766
0
    && !FRVFDPIC_SYM_LOCAL (info, h))
2767
0
  {
2768
0
    osec = sec = NULL;
2769
0
    relocation = 0;
2770
0
  }
2771
2772
0
      switch (r_type)
2773
0
  {
2774
0
  case R_FRV_LABEL24:
2775
0
  case R_FRV_32:
2776
0
    if (! IS_FDPIC (output_bfd))
2777
0
      goto non_fdpic;
2778
    /* Fall through.  */
2779
2780
0
  case R_FRV_GOT12:
2781
0
  case R_FRV_GOTHI:
2782
0
  case R_FRV_GOTLO:
2783
0
  case R_FRV_FUNCDESC_GOT12:
2784
0
  case R_FRV_FUNCDESC_GOTHI:
2785
0
  case R_FRV_FUNCDESC_GOTLO:
2786
0
  case R_FRV_GOTOFF12:
2787
0
  case R_FRV_GOTOFFHI:
2788
0
  case R_FRV_GOTOFFLO:
2789
0
  case R_FRV_FUNCDESC_GOTOFF12:
2790
0
  case R_FRV_FUNCDESC_GOTOFFHI:
2791
0
  case R_FRV_FUNCDESC_GOTOFFLO:
2792
0
  case R_FRV_FUNCDESC:
2793
0
  case R_FRV_FUNCDESC_VALUE:
2794
0
  case R_FRV_GETTLSOFF:
2795
0
  case R_FRV_TLSDESC_VALUE:
2796
0
  case R_FRV_GOTTLSDESC12:
2797
0
  case R_FRV_GOTTLSDESCHI:
2798
0
  case R_FRV_GOTTLSDESCLO:
2799
0
  case R_FRV_TLSMOFF12:
2800
0
  case R_FRV_TLSMOFFHI:
2801
0
  case R_FRV_TLSMOFFLO:
2802
0
  case R_FRV_GOTTLSOFF12:
2803
0
  case R_FRV_GOTTLSOFFHI:
2804
0
  case R_FRV_GOTTLSOFFLO:
2805
0
  case R_FRV_TLSOFF:
2806
0
  case R_FRV_TLSDESC_RELAX:
2807
0
  case R_FRV_GETTLSOFF_RELAX:
2808
0
  case R_FRV_TLSOFF_RELAX:
2809
0
  case R_FRV_TLSMOFF:
2810
0
    if ((input_section->flags & SEC_ALLOC) == 0)
2811
0
      break;
2812
2813
0
    if (h != NULL)
2814
0
      picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info
2815
0
                  (info), input_bfd, h,
2816
0
                  orig_addend, INSERT);
2817
0
    else
2818
      /* In order to find the entry we created before, we must
2819
         use the original addend, not the one that may have been
2820
         modified by _bfd_elf_rela_local_sym().  */
2821
0
      picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
2822
0
                 (info), input_bfd, r_symndx,
2823
0
                 orig_addend, INSERT);
2824
0
    if (! picrel)
2825
0
      return false;
2826
2827
0
    if (!_frvfdpic_emit_got_relocs_plt_entries (picrel, output_bfd, info,
2828
0
                  osec, sym,
2829
0
                  rel->r_addend))
2830
0
      {
2831
0
        info->callbacks->einfo
2832
    /* xgettext:c-format */
2833
0
    (_("%H: relocation to `%s+%v'"
2834
0
       " may have caused the error above\n"),
2835
0
     input_bfd, input_section, rel->r_offset, name, rel->r_addend);
2836
0
        return false;
2837
0
      }
2838
2839
0
    break;
2840
2841
0
  default:
2842
0
  non_fdpic:
2843
0
    picrel = NULL;
2844
0
    if (h
2845
0
        && ! FRVFDPIC_SYM_LOCAL (info, h)
2846
0
        && _bfd_elf_section_offset (output_bfd, info, input_section,
2847
0
            rel->r_offset) != (bfd_vma) -1)
2848
0
      {
2849
0
        info->callbacks->einfo
2850
0
    (_("%H: relocation references symbol"
2851
0
       " not defined in the module\n"),
2852
0
     input_bfd, input_section, rel->r_offset);
2853
0
        return false;
2854
0
      }
2855
0
    break;
2856
0
  }
2857
2858
0
      switch (r_type)
2859
0
  {
2860
0
  case R_FRV_GETTLSOFF:
2861
0
  case R_FRV_TLSDESC_VALUE:
2862
0
  case R_FRV_GOTTLSDESC12:
2863
0
  case R_FRV_GOTTLSDESCHI:
2864
0
  case R_FRV_GOTTLSDESCLO:
2865
0
  case R_FRV_TLSMOFF12:
2866
0
  case R_FRV_TLSMOFFHI:
2867
0
  case R_FRV_TLSMOFFLO:
2868
0
  case R_FRV_GOTTLSOFF12:
2869
0
  case R_FRV_GOTTLSOFFHI:
2870
0
  case R_FRV_GOTTLSOFFLO:
2871
0
  case R_FRV_TLSOFF:
2872
0
  case R_FRV_TLSDESC_RELAX:
2873
0
  case R_FRV_GETTLSOFF_RELAX:
2874
0
  case R_FRV_TLSOFF_RELAX:
2875
0
  case R_FRV_TLSMOFF:
2876
0
    if (sec && (bfd_is_abs_section (sec) || bfd_is_und_section (sec)))
2877
0
      relocation += tls_biased_base (info);
2878
0
    break;
2879
2880
0
  default:
2881
0
    break;
2882
0
  }
2883
2884
      /* Try to apply TLS relaxations.  */
2885
0
      if (1)
2886
0
  switch (r_type)
2887
0
    {
2888
2889
0
#define LOCAL_EXEC_P(info, picrel) \
2890
0
  (bfd_link_executable (info) \
2891
0
   && (picrel->symndx != -1 || FRVFDPIC_SYM_LOCAL ((info), (picrel)->d.h)))
2892
0
#define INITIAL_EXEC_P(info, picrel) \
2893
0
  ((bfd_link_executable (info)|| (info)->flags & DF_STATIC_TLS) \
2894
0
   && (picrel)->tlsoff_entry)
2895
2896
0
#define IN_RANGE_FOR_OFST12_P(value) \
2897
0
  ((bfd_vma)((value) + 2048) < (bfd_vma)4096)
2898
0
#define IN_RANGE_FOR_SETLOS_P(value) \
2899
0
  ((bfd_vma)((value) + 32768) < (bfd_vma)65536)
2900
0
#define TLSMOFF_IN_RANGE_FOR_SETLOS_P(value, info) \
2901
0
  (IN_RANGE_FOR_SETLOS_P ((value) - tls_biased_base (info)))
2902
2903
0
#define RELAX_GETTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
2904
0
  (LOCAL_EXEC_P ((info), (picrel)) \
2905
0
   && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
2906
0
#define RELAX_GETTLSOFF_INITIAL_EXEC_P(info, picrel) \
2907
0
  (INITIAL_EXEC_P ((info), (picrel)) \
2908
0
   && IN_RANGE_FOR_OFST12_P ((picrel)->tlsoff_entry))
2909
2910
0
#define RELAX_TLSDESC_LOCAL_EXEC_P(info, picrel, value) \
2911
0
  (LOCAL_EXEC_P ((info), (picrel)))
2912
0
#define RELAX_TLSDESC_INITIAL_EXEC_P(info, picrel) \
2913
0
  (INITIAL_EXEC_P ((info), (picrel)))
2914
2915
0
#define RELAX_GOTTLSOFF_LOCAL_EXEC_P(info, picrel, value) \
2916
0
  (LOCAL_EXEC_P ((info), (picrel)) \
2917
0
   && TLSMOFF_IN_RANGE_FOR_SETLOS_P((value), (info)))
2918
2919
0
    case R_FRV_GETTLSOFF:
2920
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2921
2922
      /* Is this a call instruction?  */
2923
0
      if ((insn & (unsigned long)0x01fc0000) != 0x003c0000)
2924
0
        {
2925
0
    info->callbacks->einfo
2926
0
      (_("%H: R_FRV_GETTLSOFF not applied to a call instruction\n"),
2927
0
       input_bfd, input_section, rel->r_offset);
2928
0
    return false;
2929
0
        }
2930
2931
0
      if (RELAX_GETTLSOFF_LOCAL_EXEC_P (info, picrel,
2932
0
                relocation + rel->r_addend))
2933
0
        {
2934
    /* Replace the call instruction (except the packing bit)
2935
       with setlos #tlsmofflo(symbol+offset), gr9.  */
2936
0
    insn &= (unsigned long)0x80000000;
2937
0
    insn |= (unsigned long)0x12fc0000;
2938
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2939
2940
0
    r_type = R_FRV_TLSMOFFLO;
2941
0
    howto  = elf32_frv_howto_table + r_type;
2942
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
2943
0
        }
2944
2945
0
      else if (RELAX_GETTLSOFF_INITIAL_EXEC_P (info, picrel))
2946
0
        {
2947
    /* Replace the call instruction (except the packing bit)
2948
       with ldi @(gr15, #gottlsoff12(symbol+addend)), gr9.  */
2949
0
    insn &= (unsigned long)0x80000000;
2950
0
    insn |= (unsigned long)0x12c8f000;
2951
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2952
2953
0
    r_type = R_FRV_GOTTLSOFF12;
2954
0
    howto  = elf32_frv_howto_table + r_type;
2955
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
2956
0
        }
2957
2958
0
      break;
2959
2960
0
    case R_FRV_GOTTLSDESC12:
2961
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
2962
2963
      /* Is this an lddi instruction?  */
2964
0
      if ((insn & (unsigned long)0x01fc0000) != 0x00cc0000)
2965
0
        {
2966
0
    info->callbacks->einfo
2967
0
      (_("%H: R_FRV_GOTTLSDESC12"
2968
0
         " not applied to an lddi instruction\n"),
2969
0
       input_bfd, input_section, rel->r_offset);
2970
0
    return false;
2971
0
        }
2972
2973
0
      if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
2974
0
              relocation + rel->r_addend)
2975
0
    && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
2976
0
              info))
2977
0
        {
2978
    /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
2979
       with setlos #tlsmofflo(symbol+offset), gr<C+1>.
2980
       Preserve the packing bit.  */
2981
0
    insn = (insn & (unsigned long)0x80000000)
2982
0
      | ((insn + (unsigned long)0x02000000)
2983
0
         & (unsigned long)0x7e000000);
2984
0
    insn |= (unsigned long)0x00fc0000;
2985
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
2986
2987
0
    r_type = R_FRV_TLSMOFFLO;
2988
0
    howto  = elf32_frv_howto_table + r_type;
2989
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
2990
0
        }
2991
2992
0
      else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
2993
0
             relocation + rel->r_addend))
2994
0
        {
2995
    /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
2996
       with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
2997
       Preserve the packing bit.  */
2998
0
    insn = (insn & (unsigned long)0x80000000)
2999
0
      | ((insn + (unsigned long)0x02000000)
3000
0
         & (unsigned long)0x7e000000);
3001
0
    insn |= (unsigned long)0x00f80000;
3002
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3003
3004
0
    r_type = R_FRV_TLSMOFFHI;
3005
0
    howto  = elf32_frv_howto_table + r_type;
3006
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3007
0
        }
3008
3009
0
      else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3010
0
        {
3011
    /* Replace lddi @(grB, #gottlsdesc12(symbol+offset), grC
3012
       with ldi @(grB, #gottlsoff12(symbol+offset),
3013
       gr<C+1>.  Preserve the packing bit.  If gottlsoff12
3014
       overflows, we'll error out, but that's sort-of ok,
3015
       since we'd started with gottlsdesc12, that's actually
3016
       more demanding.  Compiling with -fPIE instead of
3017
       -fpie would fix it; linking with --relax should fix
3018
       it as well.  */
3019
0
    insn = (insn & (unsigned long)0x80cbf000)
3020
0
      | ((insn + (unsigned long)0x02000000)
3021
0
         & (unsigned long)0x7e000000);
3022
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3023
3024
0
    r_type = R_FRV_GOTTLSOFF12;
3025
0
    howto  = elf32_frv_howto_table + r_type;
3026
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3027
0
        }
3028
3029
0
      break;
3030
3031
0
    case R_FRV_GOTTLSDESCHI:
3032
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3033
3034
      /* Is this a sethi instruction?  */
3035
0
      if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3036
0
        {
3037
0
    info->callbacks->einfo
3038
0
      (_("%H: R_FRV_GOTTLSDESCHI"
3039
0
         " not applied to a sethi instruction\n"),
3040
0
       input_bfd, input_section, rel->r_offset);
3041
0
    return false;
3042
0
        }
3043
3044
0
      if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3045
0
              relocation + rel->r_addend)
3046
0
    || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3047
0
        && IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry)))
3048
0
        {
3049
    /* Replace sethi with a nop.  Preserve the packing bit.  */
3050
0
    insn &= (unsigned long)0x80000000;
3051
0
    insn |= (unsigned long)0x00880000;
3052
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3053
3054
    /* Nothing to relocate.  */
3055
0
    continue;
3056
0
        }
3057
3058
0
      else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3059
0
        {
3060
    /* Simply decay GOTTLSDESC to GOTTLSOFF.  */
3061
0
    r_type = R_FRV_GOTTLSOFFHI;
3062
0
    howto  = elf32_frv_howto_table + r_type;
3063
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3064
0
        }
3065
3066
0
      break;
3067
3068
0
    case R_FRV_GOTTLSDESCLO:
3069
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3070
3071
      /* Is this a setlo or setlos instruction?  */
3072
0
      if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3073
0
        {
3074
0
    info->callbacks->einfo
3075
0
      (_("%H: R_FRV_GOTTLSDESCLO"
3076
0
         " not applied to a setlo or setlos instruction\n"),
3077
0
       input_bfd, input_section, rel->r_offset);
3078
0
    return false;
3079
0
        }
3080
3081
0
      if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3082
0
              relocation + rel->r_addend)
3083
0
    || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3084
0
        && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3085
0
        {
3086
    /* Replace setlo/setlos with a nop.  Preserve the
3087
       packing bit.  */
3088
0
    insn &= (unsigned long)0x80000000;
3089
0
    insn |= (unsigned long)0x00880000;
3090
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3091
3092
    /* Nothing to relocate.  */
3093
0
    continue;
3094
0
        }
3095
3096
0
      else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3097
0
        {
3098
    /* If the corresponding sethi (if it exists) decayed
3099
       to a nop, make sure this becomes (or already is) a
3100
       setlos, not setlo.  */
3101
0
    if (IN_RANGE_FOR_SETLOS_P (picrel->tlsoff_entry))
3102
0
      {
3103
0
        insn |= (unsigned long)0x00080000;
3104
0
        bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3105
0
      }
3106
3107
    /* Simply decay GOTTLSDESC to GOTTLSOFF.  */
3108
0
    r_type = R_FRV_GOTTLSOFFLO;
3109
0
    howto  = elf32_frv_howto_table + r_type;
3110
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3111
0
        }
3112
3113
0
      break;
3114
3115
0
    case R_FRV_TLSDESC_RELAX:
3116
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3117
3118
      /* Is this an ldd instruction?  */
3119
0
      if ((insn & (unsigned long)0x01fc0fc0) != 0x00080140)
3120
0
        {
3121
0
    info->callbacks->einfo
3122
0
      (_("%H: R_FRV_TLSDESC_RELAX"
3123
0
         " not applied to an ldd instruction\n"),
3124
0
       input_bfd, input_section, rel->r_offset);
3125
0
    return false;
3126
0
        }
3127
3128
0
      if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3129
0
              relocation + rel->r_addend)
3130
0
    && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3131
0
              info))
3132
0
        {
3133
    /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3134
       with setlos #tlsmofflo(symbol+offset), gr<C+1>.
3135
       Preserve the packing bit.  */
3136
0
    insn = (insn & (unsigned long)0x80000000)
3137
0
      | ((insn + (unsigned long)0x02000000)
3138
0
         & (unsigned long)0x7e000000);
3139
0
    insn |= (unsigned long)0x00fc0000;
3140
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3141
3142
0
    r_type = R_FRV_TLSMOFFLO;
3143
0
    howto  = elf32_frv_howto_table + r_type;
3144
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3145
0
        }
3146
3147
0
      else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3148
0
             relocation + rel->r_addend))
3149
0
        {
3150
    /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3151
       with sethi #tlsmoffhi(symbol+offset), gr<C+1>.
3152
       Preserve the packing bit.  */
3153
0
    insn = (insn & (unsigned long)0x80000000)
3154
0
      | ((insn + (unsigned long)0x02000000)
3155
0
         & (unsigned long)0x7e000000);
3156
0
    insn |= (unsigned long)0x00f80000;
3157
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3158
3159
0
    r_type = R_FRV_TLSMOFFHI;
3160
0
    howto  = elf32_frv_howto_table + r_type;
3161
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3162
0
        }
3163
3164
0
      else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3165
0
         && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
3166
0
        {
3167
    /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3168
       with ldi @(grB, #gottlsoff12(symbol+offset), gr<C+1>.
3169
       Preserve the packing bit.  */
3170
0
    insn = (insn & (unsigned long)0x8003f000)
3171
0
      | (unsigned long)0x00c80000
3172
0
      | ((insn + (unsigned long)0x02000000)
3173
0
         & (unsigned long)0x7e000000);
3174
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3175
3176
0
    r_type = R_FRV_GOTTLSOFF12;
3177
0
    howto  = elf32_frv_howto_table + r_type;
3178
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3179
0
        }
3180
3181
0
      else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3182
0
        {
3183
    /* Replace ldd #tlsdesc(symbol+offset)@(grB, grA), grC
3184
       with ld #tlsoff(symbol+offset)@(grB, grA), gr<C+1>.
3185
       Preserve the packing bit.  */
3186
0
    insn = (insn & (unsigned long)0x81ffffbf)
3187
0
      | ((insn + (unsigned long)0x02000000)
3188
0
         & (unsigned long)0x7e000000);
3189
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3190
3191
    /* #tlsoff(symbol+offset) is just a relaxation
3192
        annotation, so there's nothing left to
3193
        relocate.  */
3194
0
    continue;
3195
0
        }
3196
3197
0
      break;
3198
3199
0
    case R_FRV_GETTLSOFF_RELAX:
3200
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3201
3202
      /* Is this a calll or callil instruction?  */
3203
0
      if ((insn & (unsigned long)0x7ff80fc0) != 0x02300000)
3204
0
        {
3205
0
    info->callbacks->einfo
3206
0
      (_("%H: R_FRV_GETTLSOFF_RELAX"
3207
0
         " not applied to a calll instruction\n"),
3208
0
       input_bfd, input_section, rel->r_offset);
3209
0
    return false;
3210
0
        }
3211
3212
0
      if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3213
0
              relocation + rel->r_addend)
3214
0
    && TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3215
0
              info))
3216
0
        {
3217
    /* Replace calll with a nop.  Preserve the packing bit.  */
3218
0
    insn &= (unsigned long)0x80000000;
3219
0
    insn |= (unsigned long)0x00880000;
3220
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3221
3222
    /* Nothing to relocate.  */
3223
0
    continue;
3224
0
        }
3225
3226
0
      else if (RELAX_TLSDESC_LOCAL_EXEC_P (info, picrel,
3227
0
             relocation + rel->r_addend))
3228
0
        {
3229
    /* Replace calll with setlo #tlsmofflo(symbol+offset), gr9.
3230
       Preserve the packing bit.  */
3231
0
    insn &= (unsigned long)0x80000000;
3232
0
    insn |= (unsigned long)0x12f40000;
3233
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3234
3235
0
    r_type = R_FRV_TLSMOFFLO;
3236
0
    howto  = elf32_frv_howto_table + r_type;
3237
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3238
0
        }
3239
3240
0
      else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel))
3241
0
        {
3242
    /* Replace calll with a nop.  Preserve the packing bit.  */
3243
0
    insn &= (unsigned long)0x80000000;
3244
0
    insn |= (unsigned long)0x00880000;
3245
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3246
3247
    /* Nothing to relocate.  */
3248
0
    continue;
3249
0
        }
3250
3251
0
      break;
3252
3253
0
    case R_FRV_GOTTLSOFF12:
3254
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3255
3256
      /* Is this an ldi instruction?  */
3257
0
      if ((insn & (unsigned long)0x01fc0000) != 0x00c80000)
3258
0
        {
3259
0
    info->callbacks->einfo
3260
0
      (_("%H: R_FRV_GOTTLSOFF12"
3261
0
         " not applied to an ldi instruction\n"),
3262
0
       input_bfd, input_section, rel->r_offset);
3263
0
    return false;
3264
0
        }
3265
3266
0
      if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3267
0
                relocation + rel->r_addend))
3268
0
        {
3269
    /* Replace ldi @(grB, #gottlsoff12(symbol+offset), grC
3270
       with setlos #tlsmofflo(symbol+offset), grC.
3271
       Preserve the packing bit.  */
3272
0
    insn &= (unsigned long)0xfe000000;
3273
0
    insn |= (unsigned long)0x00fc0000;
3274
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3275
3276
0
    r_type = R_FRV_TLSMOFFLO;
3277
0
    howto  = elf32_frv_howto_table + r_type;
3278
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3279
0
        }
3280
3281
0
      break;
3282
3283
0
    case R_FRV_GOTTLSOFFHI:
3284
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3285
3286
      /* Is this a sethi instruction?  */
3287
0
      if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3288
0
        {
3289
0
    info->callbacks->einfo
3290
0
      (_("%H: R_FRV_GOTTLSOFFHI"
3291
0
         " not applied to a sethi instruction\n"),
3292
0
       input_bfd, input_section, rel->r_offset);
3293
0
    return false;
3294
0
        }
3295
3296
0
      if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3297
0
                relocation + rel->r_addend)
3298
0
    || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3299
0
        && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3300
0
        {
3301
    /* Replace sethi with a nop.  Preserve the packing bit.  */
3302
0
    insn &= (unsigned long)0x80000000;
3303
0
    insn |= (unsigned long)0x00880000;
3304
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3305
3306
    /* Nothing to relocate.  */
3307
0
    continue;
3308
0
        }
3309
3310
0
      break;
3311
3312
0
    case R_FRV_GOTTLSOFFLO:
3313
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3314
3315
      /* Is this a setlo or setlos instruction?  */
3316
0
      if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3317
0
        {
3318
0
    info->callbacks->einfo
3319
0
      (_("%H: R_FRV_GOTTLSOFFLO"
3320
0
         " not applied to a setlo or setlos instruction\n"),
3321
0
       input_bfd, input_section, rel->r_offset);
3322
0
    return false;
3323
0
        }
3324
3325
0
      if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3326
0
                relocation + rel->r_addend)
3327
0
    || (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3328
0
        && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry)))
3329
0
        {
3330
    /* Replace setlo/setlos with a nop.  Preserve the
3331
       packing bit.  */
3332
0
    insn &= (unsigned long)0x80000000;
3333
0
    insn |= (unsigned long)0x00880000;
3334
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3335
3336
    /* Nothing to relocate.  */
3337
0
    continue;
3338
0
        }
3339
3340
0
      break;
3341
3342
0
    case R_FRV_TLSOFF_RELAX:
3343
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3344
3345
      /* Is this an ld instruction?  */
3346
0
      if ((insn & (unsigned long)0x01fc0fc0) != 0x00080100)
3347
0
        {
3348
0
    info->callbacks->einfo
3349
0
      (_("%H: R_FRV_TLSOFF_RELAX"
3350
0
         " not applied to an ld instruction\n"),
3351
0
       input_bfd, input_section, rel->r_offset);
3352
0
    return false;
3353
0
        }
3354
3355
0
      if (RELAX_GOTTLSOFF_LOCAL_EXEC_P (info, picrel,
3356
0
                relocation + rel->r_addend))
3357
0
        {
3358
    /* Replace ld #gottlsoff(symbol+offset)@(grB, grA), grC
3359
       with setlos #tlsmofflo(symbol+offset), grC.
3360
       Preserve the packing bit.  */
3361
0
    insn &= (unsigned long)0xfe000000;
3362
0
    insn |= (unsigned long)0x00fc0000;
3363
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3364
3365
0
    r_type = R_FRV_TLSMOFFLO;
3366
0
    howto  = elf32_frv_howto_table + r_type;
3367
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3368
0
        }
3369
3370
0
      else if (RELAX_TLSDESC_INITIAL_EXEC_P (info, picrel)
3371
0
         && IN_RANGE_FOR_OFST12_P (picrel->tlsoff_entry))
3372
0
        {
3373
    /* Replace ld #tlsoff(symbol+offset)@(grB, grA), grC
3374
       with ldi @(grB, #gottlsoff12(symbol+offset), grC.
3375
       Preserve the packing bit.  */
3376
0
    insn = (insn & (unsigned long)0xfe03f000)
3377
0
      | (unsigned long)0x00c80000;
3378
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3379
3380
0
    r_type = R_FRV_GOTTLSOFF12;
3381
0
    howto  = elf32_frv_howto_table + r_type;
3382
0
    rel->r_info = ELF32_R_INFO (r_symndx, r_type);
3383
0
        }
3384
3385
0
      break;
3386
3387
0
    case R_FRV_TLSMOFFHI:
3388
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3389
3390
      /* Is this a sethi instruction?  */
3391
0
      if ((insn & (unsigned long)0x01ff0000) != 0x00f80000)
3392
0
        {
3393
0
    info->callbacks->einfo
3394
0
      (_("%H: R_FRV_TLSMOFFHI"
3395
0
         " not applied to a sethi instruction\n"),
3396
0
       input_bfd, input_section, rel->r_offset);
3397
0
    return false;
3398
0
        }
3399
3400
0
      if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3401
0
                 info))
3402
0
        {
3403
    /* Replace sethi with a nop.  Preserve the packing bit.  */
3404
0
    insn &= (unsigned long)0x80000000;
3405
0
    insn |= (unsigned long)0x00880000;
3406
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3407
3408
    /* Nothing to relocate.  */
3409
0
    continue;
3410
0
        }
3411
3412
0
      break;
3413
3414
0
    case R_FRV_TLSMOFFLO:
3415
0
      insn = bfd_get_32 (input_bfd, contents + rel->r_offset);
3416
3417
      /* Is this a setlo or setlos instruction?  */
3418
0
      if ((insn & (unsigned long)0x01f70000) != 0x00f40000)
3419
0
        {
3420
0
    info->callbacks->einfo
3421
0
      (_("R_FRV_TLSMOFFLO"
3422
0
         " not applied to a setlo or setlos instruction\n"),
3423
0
       input_bfd, input_section, rel->r_offset);
3424
0
    return false;
3425
0
        }
3426
3427
0
      if (TLSMOFF_IN_RANGE_FOR_SETLOS_P (relocation + rel->r_addend,
3428
0
                 info))
3429
        /* If the corresponding sethi (if it exists) decayed
3430
     to a nop, make sure this becomes (or already is) a
3431
     setlos, not setlo.  */
3432
0
        {
3433
0
    insn |= (unsigned long)0x00080000;
3434
0
    bfd_put_32 (input_bfd, insn, contents + rel->r_offset);
3435
0
        }
3436
3437
0
      break;
3438
3439
      /*
3440
        There's nothing to relax in these:
3441
    R_FRV_TLSDESC_VALUE
3442
    R_FRV_TLSOFF
3443
    R_FRV_TLSMOFF12
3444
    R_FRV_TLSMOFFHI
3445
    R_FRV_TLSMOFFLO
3446
    R_FRV_TLSMOFF
3447
      */
3448
3449
0
    default:
3450
0
      break;
3451
0
    }
3452
3453
0
      switch (r_type)
3454
0
  {
3455
0
  case R_FRV_LABEL24:
3456
0
    check_segment[0] = isec_segment;
3457
0
    if (! IS_FDPIC (output_bfd))
3458
0
      check_segment[1] = isec_segment;
3459
0
    else if (picrel->plt)
3460
0
      {
3461
0
        relocation = frvfdpic_plt_section (info)->output_section->vma
3462
0
    + frvfdpic_plt_section (info)->output_offset
3463
0
    + picrel->plt_entry;
3464
0
        check_segment[1] = plt_segment;
3465
0
      }
3466
    /* We don't want to warn on calls to undefined weak symbols,
3467
       as calls to them must be protected by non-NULL tests
3468
       anyway, and unprotected calls would invoke undefined
3469
       behavior.  */
3470
0
    else if (picrel->symndx == -1
3471
0
       && picrel->d.h->root.type == bfd_link_hash_undefweak)
3472
0
      check_segment[1] = check_segment[0];
3473
0
    else
3474
0
      check_segment[1] = sec
3475
0
        ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3476
0
        : (unsigned)-1;
3477
0
    break;
3478
3479
0
  case R_FRV_GOT12:
3480
0
  case R_FRV_GOTHI:
3481
0
  case R_FRV_GOTLO:
3482
0
    relocation = picrel->got_entry;
3483
0
    check_segment[0] = check_segment[1] = got_segment;
3484
0
    break;
3485
3486
0
  case R_FRV_FUNCDESC_GOT12:
3487
0
  case R_FRV_FUNCDESC_GOTHI:
3488
0
  case R_FRV_FUNCDESC_GOTLO:
3489
0
    relocation = picrel->fdgot_entry;
3490
0
    check_segment[0] = check_segment[1] = got_segment;
3491
0
    break;
3492
3493
0
  case R_FRV_GOTOFFHI:
3494
0
  case R_FRV_GOTOFF12:
3495
0
  case R_FRV_GOTOFFLO:
3496
0
    relocation -= frvfdpic_got_section (info)->output_section->vma
3497
0
      + frvfdpic_got_section (info)->output_offset
3498
0
      + frvfdpic_got_initial_offset (info);
3499
0
    check_segment[0] = got_segment;
3500
0
    check_segment[1] = sec
3501
0
      ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3502
0
      : (unsigned)-1;
3503
0
    break;
3504
3505
0
  case R_FRV_FUNCDESC_GOTOFF12:
3506
0
  case R_FRV_FUNCDESC_GOTOFFHI:
3507
0
  case R_FRV_FUNCDESC_GOTOFFLO:
3508
0
    relocation = picrel->fd_entry;
3509
0
    check_segment[0] = check_segment[1] = got_segment;
3510
0
    break;
3511
3512
0
  case R_FRV_FUNCDESC:
3513
0
    {
3514
0
      int dynindx;
3515
0
      bfd_vma addend = rel->r_addend;
3516
3517
0
      if (! (h && h->root.type == bfd_link_hash_undefweak
3518
0
       && FRVFDPIC_SYM_LOCAL (info, h)))
3519
0
        {
3520
    /* If the symbol is dynamic and there may be dynamic
3521
       symbol resolution because we are or are linked with a
3522
       shared library, emit a FUNCDESC relocation such that
3523
       the dynamic linker will allocate the function
3524
       descriptor.  If the symbol needs a non-local function
3525
       descriptor but binds locally (e.g., its visibility is
3526
       protected, emit a dynamic relocation decayed to
3527
       section+offset.  */
3528
0
    if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h)
3529
0
        && FRVFDPIC_SYM_LOCAL (info, h)
3530
0
        && !bfd_link_pde (info))
3531
0
      {
3532
0
        dynindx = elf_section_data (h->root.u.def.section
3533
0
            ->output_section)->dynindx;
3534
0
        addend += h->root.u.def.section->output_offset
3535
0
          + h->root.u.def.value;
3536
0
      }
3537
0
    else if (h && ! FRVFDPIC_FUNCDESC_LOCAL (info, h))
3538
0
      {
3539
0
        if (addend)
3540
0
          {
3541
0
      info->callbacks->einfo
3542
0
        (_("%H: %s references dynamic symbol"
3543
0
           " with nonzero addend\n"),
3544
0
         input_bfd, input_section, rel->r_offset,
3545
0
         "R_FRV_FUNCDESC");
3546
0
      return false;
3547
0
          }
3548
0
        dynindx = h->dynindx;
3549
0
      }
3550
0
    else
3551
0
      {
3552
        /* Otherwise, we know we have a private function
3553
           descriptor, so reference it directly.  */
3554
0
        BFD_ASSERT (picrel->privfd);
3555
0
        r_type = R_FRV_32;
3556
0
        dynindx = elf_section_data (frvfdpic_got_section (info)
3557
0
            ->output_section)->dynindx;
3558
0
        addend = frvfdpic_got_section (info)->output_offset
3559
0
          + frvfdpic_got_initial_offset (info)
3560
0
          + picrel->fd_entry;
3561
0
      }
3562
3563
    /* If there is room for dynamic symbol resolution, emit
3564
       the dynamic relocation.  However, if we're linking an
3565
       executable at a fixed location, we won't have emitted a
3566
       dynamic symbol entry for the got section, so idx will
3567
       be zero, which means we can and should compute the
3568
       address of the private descriptor ourselves.  */
3569
0
    if (bfd_link_pde (info)
3570
0
        && (!h || FRVFDPIC_FUNCDESC_LOCAL (info, h)))
3571
0
      {
3572
0
        addend += frvfdpic_got_section (info)->output_section->vma;
3573
0
        if ((bfd_section_flags (input_section->output_section)
3574
0
       & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3575
0
          {
3576
0
      bfd_vma offset;
3577
3578
0
      if (_frvfdpic_osec_readonly_p (output_bfd,
3579
0
                   input_section
3580
0
                   ->output_section))
3581
0
        {
3582
0
          info->callbacks->einfo
3583
0
            (_("%H: cannot emit fixups"
3584
0
         " in read-only section\n"),
3585
0
             input_bfd, input_section, rel->r_offset);
3586
0
          return false;
3587
0
        }
3588
3589
0
      offset = _bfd_elf_section_offset
3590
0
        (output_bfd, info,
3591
0
         input_section, rel->r_offset);
3592
3593
0
      if (offset != (bfd_vma)-1)
3594
0
        _frvfdpic_add_rofixup (output_bfd,
3595
0
             frvfdpic_gotfixup_section
3596
0
             (info),
3597
0
             offset + input_section
3598
0
             ->output_section->vma
3599
0
             + input_section->output_offset,
3600
0
             picrel);
3601
0
          }
3602
0
      }
3603
0
    else if ((bfd_section_flags (input_section->output_section)
3604
0
        & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3605
0
      {
3606
0
        bfd_vma offset;
3607
3608
0
        if (_frvfdpic_osec_readonly_p (output_bfd,
3609
0
               input_section
3610
0
               ->output_section))
3611
0
          {
3612
0
      info->callbacks->einfo
3613
0
        (_("%H: cannot emit dynamic relocations"
3614
0
           " in read-only section\n"),
3615
0
         input_bfd, input_section, rel->r_offset);
3616
0
      return false;
3617
0
          }
3618
3619
0
        offset = _bfd_elf_section_offset
3620
0
          (output_bfd, info,
3621
0
           input_section, rel->r_offset);
3622
3623
0
        if (offset != (bfd_vma)-1)
3624
0
          _frvfdpic_add_dyn_reloc (output_bfd,
3625
0
                 frvfdpic_gotrel_section (info),
3626
0
                 offset + input_section
3627
0
                 ->output_section->vma
3628
0
                 + input_section->output_offset,
3629
0
                 r_type, dynindx, addend, picrel);
3630
0
      }
3631
0
    else
3632
0
      addend += frvfdpic_got_section (info)->output_section->vma;
3633
0
        }
3634
3635
      /* We want the addend in-place because dynamic
3636
         relocations are REL.  Setting relocation to it should
3637
         arrange for it to be installed.  */
3638
0
      relocation = addend - rel->r_addend;
3639
0
    }
3640
0
    check_segment[0] = check_segment[1] = got_segment;
3641
0
    break;
3642
3643
0
  case R_FRV_32:
3644
0
    if (! IS_FDPIC (output_bfd))
3645
0
      {
3646
0
        check_segment[0] = check_segment[1] = -1;
3647
0
        break;
3648
0
      }
3649
    /* Fall through.  */
3650
0
  case R_FRV_FUNCDESC_VALUE:
3651
0
    {
3652
0
      int dynindx;
3653
0
      bfd_vma addend = rel->r_addend;
3654
3655
      /* If the symbol is dynamic but binds locally, use
3656
         section+offset.  */
3657
0
      if (h && ! FRVFDPIC_SYM_LOCAL (info, h))
3658
0
        {
3659
0
    if (addend && r_type == R_FRV_FUNCDESC_VALUE)
3660
0
      {
3661
0
        info->callbacks->einfo
3662
0
          (_("%H: %s references dynamic symbol"
3663
0
       " with nonzero addend\n"),
3664
0
           input_bfd, input_section, rel->r_offset,
3665
0
           "R_FRV_FUNCDESC_VALUE");
3666
0
        return false;
3667
0
      }
3668
0
    dynindx = h->dynindx;
3669
0
        }
3670
0
      else
3671
0
        {
3672
0
    if (h)
3673
0
      addend += h->root.u.def.value;
3674
0
    else
3675
0
      addend += sym->st_value;
3676
0
    if (osec)
3677
0
      addend += osec->output_offset;
3678
0
    if (osec && osec->output_section
3679
0
        && ! bfd_is_abs_section (osec->output_section)
3680
0
        && ! bfd_is_und_section (osec->output_section))
3681
0
      dynindx = elf_section_data (osec->output_section)->dynindx;
3682
0
    else
3683
0
      dynindx = 0;
3684
0
        }
3685
3686
      /* If we're linking an executable at a fixed address, we
3687
         can omit the dynamic relocation as long as the symbol
3688
         is defined in the current link unit (which is implied
3689
         by its output section not being NULL).  */
3690
0
      if (bfd_link_pde (info)
3691
0
    && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
3692
0
        {
3693
0
    if (osec)
3694
0
      addend += osec->output_section->vma;
3695
0
    if (IS_FDPIC (input_bfd)
3696
0
        && (bfd_section_flags (input_section->output_section)
3697
0
      & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3698
0
      {
3699
0
        if (_frvfdpic_osec_readonly_p (output_bfd,
3700
0
               input_section
3701
0
               ->output_section))
3702
0
          {
3703
0
      info->callbacks->einfo
3704
0
        (_("%H: cannot emit fixups in read-only section\n"),
3705
0
         input_bfd, input_section, rel->r_offset);
3706
0
      return false;
3707
0
          }
3708
0
        if (!h || h->root.type != bfd_link_hash_undefweak)
3709
0
          {
3710
0
      bfd_vma offset = _bfd_elf_section_offset
3711
0
        (output_bfd, info,
3712
0
         input_section, rel->r_offset);
3713
3714
0
      if (offset != (bfd_vma)-1)
3715
0
        {
3716
0
          _frvfdpic_add_rofixup (output_bfd,
3717
0
               frvfdpic_gotfixup_section
3718
0
               (info),
3719
0
               offset + input_section
3720
0
               ->output_section->vma
3721
0
               + input_section->output_offset,
3722
0
               picrel);
3723
0
          if (r_type == R_FRV_FUNCDESC_VALUE)
3724
0
            _frvfdpic_add_rofixup
3725
0
        (output_bfd,
3726
0
         frvfdpic_gotfixup_section (info),
3727
0
         offset
3728
0
         + input_section->output_section->vma
3729
0
         + input_section->output_offset + 4, picrel);
3730
0
        }
3731
0
          }
3732
0
      }
3733
0
        }
3734
0
      else
3735
0
        {
3736
0
    if ((bfd_section_flags (input_section->output_section)
3737
0
         & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
3738
0
      {
3739
0
        bfd_vma offset;
3740
3741
0
        if (_frvfdpic_osec_readonly_p (output_bfd,
3742
0
               input_section
3743
0
               ->output_section))
3744
0
          {
3745
0
      info->callbacks->einfo
3746
0
        (_("%H: cannot emit dynamic relocations"
3747
0
           " in read-only section\n"),
3748
0
         input_bfd, input_section, rel->r_offset);
3749
0
      return false;
3750
0
          }
3751
3752
0
        offset = _bfd_elf_section_offset
3753
0
          (output_bfd, info,
3754
0
           input_section, rel->r_offset);
3755
3756
0
        if (offset != (bfd_vma)-1)
3757
0
          _frvfdpic_add_dyn_reloc (output_bfd,
3758
0
                 frvfdpic_gotrel_section (info),
3759
0
                 offset + input_section
3760
0
                 ->output_section->vma
3761
0
                 + input_section->output_offset,
3762
0
                 r_type, dynindx, addend, picrel);
3763
0
      }
3764
0
    else if (osec)
3765
0
      addend += osec->output_section->vma;
3766
    /* We want the addend in-place because dynamic
3767
       relocations are REL.  Setting relocation to it
3768
       should arrange for it to be installed.  */
3769
0
    relocation = addend - rel->r_addend;
3770
0
        }
3771
3772
0
      if (r_type == R_FRV_FUNCDESC_VALUE)
3773
0
        {
3774
    /* If we've omitted the dynamic relocation, just emit
3775
       the fixed addresses of the symbol and of the local
3776
       GOT base offset.  */
3777
0
    if (bfd_link_pde (info)
3778
0
        && (!h || FRVFDPIC_SYM_LOCAL (info, h)))
3779
0
      bfd_put_32 (output_bfd,
3780
0
            frvfdpic_got_section (info)->output_section->vma
3781
0
            + frvfdpic_got_section (info)->output_offset
3782
0
            + frvfdpic_got_initial_offset (info),
3783
0
            contents + rel->r_offset + 4);
3784
0
    else
3785
      /* A function descriptor used for lazy or local
3786
         resolving is initialized such that its high word
3787
         contains the output section index in which the
3788
         PLT entries are located, and the low word
3789
         contains the offset of the lazy PLT entry entry
3790
         point into that section.  */
3791
0
      bfd_put_32 (output_bfd,
3792
0
            h && ! FRVFDPIC_SYM_LOCAL (info, h)
3793
0
            ? 0
3794
0
            : _frvfdpic_osec_to_segment (output_bfd,
3795
0
                 sec
3796
0
                 ->output_section),
3797
0
            contents + rel->r_offset + 4);
3798
0
        }
3799
0
    }
3800
0
    check_segment[0] = check_segment[1] = got_segment;
3801
0
    break;
3802
3803
0
  case R_FRV_GPREL12:
3804
0
  case R_FRV_GPRELU12:
3805
0
  case R_FRV_GPREL32:
3806
0
  case R_FRV_GPRELHI:
3807
0
  case R_FRV_GPRELLO:
3808
0
    check_segment[0] = gprel_segment;
3809
0
    check_segment[1] = sec
3810
0
      ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3811
0
      : (unsigned)-1;
3812
0
    break;
3813
3814
0
  case R_FRV_GETTLSOFF:
3815
0
    relocation = frvfdpic_plt_section (info)->output_section->vma
3816
0
      + frvfdpic_plt_section (info)->output_offset
3817
0
      + picrel->tlsplt_entry;
3818
0
    BFD_ASSERT (picrel->tlsplt_entry != (bfd_vma)-1
3819
0
          && picrel->tlsdesc_entry);
3820
0
    check_segment[0] = isec_segment;
3821
0
    check_segment[1] = plt_segment;
3822
0
    break;
3823
3824
0
  case R_FRV_GOTTLSDESC12:
3825
0
  case R_FRV_GOTTLSDESCHI:
3826
0
  case R_FRV_GOTTLSDESCLO:
3827
0
    BFD_ASSERT (picrel->tlsdesc_entry);
3828
0
    relocation = picrel->tlsdesc_entry;
3829
0
    check_segment[0] = tls_segment;
3830
0
    check_segment[1] = sec
3831
0
      && ! bfd_is_abs_section (sec)
3832
0
      && ! bfd_is_und_section (sec)
3833
0
      ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3834
0
      : tls_segment;
3835
0
    break;
3836
3837
0
  case R_FRV_TLSMOFF12:
3838
0
  case R_FRV_TLSMOFFHI:
3839
0
  case R_FRV_TLSMOFFLO:
3840
0
  case R_FRV_TLSMOFF:
3841
0
    check_segment[0] = tls_segment;
3842
0
    if (! sec)
3843
0
      check_segment[1] = -1;
3844
0
    else if (bfd_is_abs_section (sec)
3845
0
       || bfd_is_und_section (sec))
3846
0
      {
3847
0
        relocation = 0;
3848
0
        check_segment[1] = tls_segment;
3849
0
      }
3850
0
    else if (sec->output_section)
3851
0
      {
3852
0
        relocation -= tls_biased_base (info);
3853
0
        check_segment[1] =
3854
0
    _frvfdpic_osec_to_segment (output_bfd, sec->output_section);
3855
0
      }
3856
0
    else
3857
0
      check_segment[1] = -1;
3858
0
    break;
3859
3860
0
  case R_FRV_GOTTLSOFF12:
3861
0
  case R_FRV_GOTTLSOFFHI:
3862
0
  case R_FRV_GOTTLSOFFLO:
3863
0
    BFD_ASSERT (picrel->tlsoff_entry);
3864
0
    relocation = picrel->tlsoff_entry;
3865
0
    check_segment[0] = tls_segment;
3866
0
    check_segment[1] = sec
3867
0
      && ! bfd_is_abs_section (sec)
3868
0
      && ! bfd_is_und_section (sec)
3869
0
      ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3870
0
      : tls_segment;
3871
0
    break;
3872
3873
0
  case R_FRV_TLSDESC_VALUE:
3874
0
  case R_FRV_TLSOFF:
3875
    /* These shouldn't be present in input object files.  */
3876
0
    check_segment[0] = check_segment[1] = isec_segment;
3877
0
    break;
3878
3879
0
  case R_FRV_TLSDESC_RELAX:
3880
0
  case R_FRV_GETTLSOFF_RELAX:
3881
0
  case R_FRV_TLSOFF_RELAX:
3882
    /* These are just annotations for relaxation, nothing to do
3883
       here.  */
3884
0
    continue;
3885
3886
0
  default:
3887
0
    check_segment[0] = isec_segment;
3888
0
    check_segment[1] = sec
3889
0
      ? _frvfdpic_osec_to_segment (output_bfd, sec->output_section)
3890
0
      : (unsigned)-1;
3891
0
    break;
3892
0
  }
3893
3894
0
      if (check_segment[0] != check_segment[1] && IS_FDPIC (output_bfd))
3895
0
  {
3896
    /* If you take this out, remove the #error from fdpic-static-6.d
3897
       in the ld testsuite.  */
3898
    /* This helps catch problems in GCC while we can't do more
3899
       than static linking.  The idea is to test whether the
3900
       input file basename is crt0.o only once.  */
3901
0
    if (silence_segment_error == 1)
3902
0
      silence_segment_error =
3903
0
        (strlen (bfd_get_filename (input_bfd)) == 6
3904
0
         && filename_cmp (bfd_get_filename (input_bfd), "crt0.o") == 0)
3905
0
        || (strlen (bfd_get_filename (input_bfd)) > 6
3906
0
      && filename_cmp (bfd_get_filename (input_bfd)
3907
0
           + strlen (bfd_get_filename (input_bfd)) - 7,
3908
0
           "/crt0.o") == 0)
3909
0
        ? -1 : 0;
3910
0
    if (!silence_segment_error
3911
        /* We don't want duplicate errors for undefined
3912
     symbols.  */
3913
0
        && !(picrel && picrel->symndx == -1
3914
0
       && picrel->d.h->root.type == bfd_link_hash_undefined))
3915
0
      {
3916
0
        info->callbacks->einfo
3917
    /* xgettext:c-format */
3918
0
    (_("%H: reloc against `%s' references a different segment\n"),
3919
0
     input_bfd, input_section, rel->r_offset, name);
3920
0
      }
3921
0
    if (!silence_segment_error && bfd_link_pic (info))
3922
0
      return false;
3923
0
    elf_elfheader (output_bfd)->e_flags |= EF_FRV_PIC;
3924
0
  }
3925
3926
0
      switch (r_type)
3927
0
  {
3928
0
  case R_FRV_GOTOFFHI:
3929
0
  case R_FRV_TLSMOFFHI:
3930
    /* We need the addend to be applied before we shift the
3931
       value right.  */
3932
0
    relocation += rel->r_addend;
3933
    /* Fall through.  */
3934
0
  case R_FRV_GOTHI:
3935
0
  case R_FRV_FUNCDESC_GOTHI:
3936
0
  case R_FRV_FUNCDESC_GOTOFFHI:
3937
0
  case R_FRV_GOTTLSOFFHI:
3938
0
  case R_FRV_GOTTLSDESCHI:
3939
0
    relocation >>= 16;
3940
    /* Fall through.  */
3941
3942
0
  case R_FRV_GOTLO:
3943
0
  case R_FRV_FUNCDESC_GOTLO:
3944
0
  case R_FRV_GOTOFFLO:
3945
0
  case R_FRV_FUNCDESC_GOTOFFLO:
3946
0
  case R_FRV_GOTTLSOFFLO:
3947
0
  case R_FRV_GOTTLSDESCLO:
3948
0
  case R_FRV_TLSMOFFLO:
3949
0
    relocation &= 0xffff;
3950
0
    break;
3951
3952
0
  default:
3953
0
    break;
3954
0
  }
3955
3956
0
      switch (r_type)
3957
0
  {
3958
0
  case R_FRV_LABEL24:
3959
0
    if (! IS_FDPIC (output_bfd) || ! picrel->plt)
3960
0
      break;
3961
    /* Fall through.  */
3962
3963
    /* When referencing a GOT entry, a function descriptor or a
3964
       PLT, we don't want the addend to apply to the reference,
3965
       but rather to the referenced symbol.  The actual entry
3966
       will have already been created taking the addend into
3967
       account, so cancel it out here.  */
3968
0
  case R_FRV_GOT12:
3969
0
  case R_FRV_GOTHI:
3970
0
  case R_FRV_GOTLO:
3971
0
  case R_FRV_FUNCDESC_GOT12:
3972
0
  case R_FRV_FUNCDESC_GOTHI:
3973
0
  case R_FRV_FUNCDESC_GOTLO:
3974
0
  case R_FRV_FUNCDESC_GOTOFF12:
3975
0
  case R_FRV_FUNCDESC_GOTOFFHI:
3976
0
  case R_FRV_FUNCDESC_GOTOFFLO:
3977
0
  case R_FRV_GETTLSOFF:
3978
0
  case R_FRV_GOTTLSDESC12:
3979
0
  case R_FRV_GOTTLSDESCHI:
3980
0
  case R_FRV_GOTTLSDESCLO:
3981
0
  case R_FRV_GOTTLSOFF12:
3982
0
  case R_FRV_GOTTLSOFFHI:
3983
0
  case R_FRV_GOTTLSOFFLO:
3984
    /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF12
3985
       here, since we do want to apply the addend to the others.
3986
       Note that we've applied the addend to GOTOFFHI before we
3987
       shifted it right.  */
3988
0
  case R_FRV_GOTOFFHI:
3989
0
  case R_FRV_TLSMOFFHI:
3990
0
    relocation -= rel->r_addend;
3991
0
    break;
3992
3993
0
  default:
3994
0
    break;
3995
0
  }
3996
3997
0
     if (r_type == R_FRV_HI16)
3998
0
       r = elf32_frv_relocate_hi16 (input_bfd, rel, contents, relocation);
3999
4000
0
     else if (r_type == R_FRV_LO16)
4001
0
       r = elf32_frv_relocate_lo16 (input_bfd, rel, contents, relocation);
4002
4003
0
     else if (r_type == R_FRV_LABEL24 || r_type == R_FRV_GETTLSOFF)
4004
0
       r = elf32_frv_relocate_label24 (input_bfd, input_section, rel,
4005
0
               contents, relocation);
4006
4007
0
     else if (r_type == R_FRV_GPREL12)
4008
0
       r = elf32_frv_relocate_gprel12 (info, input_bfd, input_section, rel,
4009
0
               contents, relocation);
4010
4011
0
     else if (r_type == R_FRV_GPRELU12)
4012
0
       r = elf32_frv_relocate_gprelu12 (info, input_bfd, input_section, rel,
4013
0
          contents, relocation);
4014
4015
0
     else if (r_type == R_FRV_GPRELLO)
4016
0
       r = elf32_frv_relocate_gprello (info, input_bfd, input_section, rel,
4017
0
               contents, relocation);
4018
4019
0
     else if (r_type == R_FRV_GPRELHI)
4020
0
       r = elf32_frv_relocate_gprelhi (info, input_bfd, input_section, rel,
4021
0
               contents, relocation);
4022
4023
0
     else if (r_type == R_FRV_TLSOFF
4024
0
        || r_type == R_FRV_TLSDESC_VALUE)
4025
0
       r = bfd_reloc_notsupported;
4026
4027
0
     else
4028
0
       r = frv_final_link_relocate (howto, input_bfd, input_section, contents,
4029
0
            rel, relocation);
4030
4031
0
      if (r != bfd_reloc_ok)
4032
0
  {
4033
0
    const char * msg = (const char *) NULL;
4034
4035
0
    switch (r)
4036
0
      {
4037
0
      case bfd_reloc_overflow:
4038
0
        (*info->callbacks->reloc_overflow)
4039
0
    (info, (h ? &h->root : NULL), name, howto->name,
4040
0
     (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
4041
0
        break;
4042
4043
0
      case bfd_reloc_undefined:
4044
0
        (*info->callbacks->undefined_symbol)
4045
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
4046
0
        break;
4047
4048
0
      case bfd_reloc_outofrange:
4049
0
        msg = _("internal error: out of range error");
4050
0
        break;
4051
4052
0
      case bfd_reloc_notsupported:
4053
0
        msg = _("internal error: unsupported relocation error");
4054
0
        break;
4055
4056
0
      case bfd_reloc_dangerous:
4057
0
        msg = _("internal error: dangerous relocation");
4058
0
        break;
4059
4060
0
      default:
4061
0
        msg = _("internal error: unknown error");
4062
0
        break;
4063
0
      }
4064
4065
0
    if (msg)
4066
0
      {
4067
0
        info->callbacks->einfo
4068
    /* xgettext:c-format */
4069
0
    (_("%H: reloc against `%s': %s\n"),
4070
0
     input_bfd, input_section, rel->r_offset, name, msg);
4071
0
        return false;
4072
0
      }
4073
0
  }
4074
0
    }
4075
4076
0
  return true;
4077
0
}
4078

4079
/* Return the section that should be marked against GC for a given
4080
   relocation.  */
4081
4082
static asection *
4083
elf32_frv_gc_mark_hook (asection *sec,
4084
      struct bfd_link_info *info,
4085
      struct elf_reloc_cookie *cookie,
4086
      struct elf_link_hash_entry *h,
4087
      unsigned int symndx)
4088
0
{
4089
0
  if (h != NULL)
4090
0
    switch (ELF32_R_TYPE (cookie->rel->r_info))
4091
0
      {
4092
0
      case R_FRV_GNU_VTINHERIT:
4093
0
      case R_FRV_GNU_VTENTRY:
4094
0
  return NULL;
4095
0
      }
4096
4097
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
4098
0
}
4099

4100
/* Hook called by the linker routine which adds symbols from an object
4101
   file.  We use it to put .comm items in .scomm, and not .comm.  */
4102
4103
static bool
4104
elf32_frv_add_symbol_hook (bfd *abfd,
4105
         struct bfd_link_info *info,
4106
         Elf_Internal_Sym *sym,
4107
         const char **namep ATTRIBUTE_UNUSED,
4108
         flagword *flagsp ATTRIBUTE_UNUSED,
4109
         asection **secp,
4110
         bfd_vma *valp)
4111
0
{
4112
0
  if (sym->st_shndx == SHN_COMMON
4113
0
      && !bfd_link_relocatable (info)
4114
0
      && (int)sym->st_size <= (int)bfd_get_gp_size (abfd))
4115
0
    {
4116
      /* Common symbols less than or equal to -G nn bytes are
4117
   automatically put into .sbss.  */
4118
4119
0
      asection *scomm = bfd_get_section_by_name (abfd, ".scommon");
4120
4121
0
      if (scomm == NULL)
4122
0
  {
4123
0
    scomm = bfd_make_section_with_flags (abfd, ".scommon",
4124
0
                 (SEC_ALLOC
4125
0
            | SEC_IS_COMMON
4126
0
            | SEC_SMALL_DATA
4127
0
            | SEC_LINKER_CREATED));
4128
0
    if (scomm == NULL)
4129
0
      return false;
4130
0
  }
4131
4132
0
      *secp = scomm;
4133
0
      *valp = sym->st_size;
4134
0
    }
4135
4136
0
  return true;
4137
0
}
4138
4139
/* We need dynamic symbols for every section, since segments can
4140
   relocate independently.  */
4141
static bool
4142
_frvfdpic_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
4143
            struct bfd_link_info *info
4144
            ATTRIBUTE_UNUSED,
4145
            asection *p ATTRIBUTE_UNUSED)
4146
0
{
4147
0
  switch (elf_section_data (p)->this_hdr.sh_type)
4148
0
    {
4149
0
    case SHT_PROGBITS:
4150
0
    case SHT_NOBITS:
4151
      /* If sh_type is yet undecided, assume it could be
4152
   SHT_PROGBITS/SHT_NOBITS.  */
4153
0
    case SHT_NULL:
4154
0
      return false;
4155
4156
      /* There shouldn't be section relative relocations
4157
   against any other section.  */
4158
0
    default:
4159
0
      return true;
4160
0
    }
4161
0
}
4162
4163
/* Create  a .got section, as well as its additional info field.  This
4164
   is almost entirely copied from
4165
   elflink.c:_bfd_elf_create_got_section().  */
4166
4167
static bool
4168
_frv_create_got_section (bfd *abfd, struct bfd_link_info *info)
4169
0
{
4170
0
  flagword flags, pltflags;
4171
0
  asection *s;
4172
0
  struct elf_link_hash_entry *h;
4173
0
  struct bfd_link_hash_entry *bh;
4174
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4175
0
  int ptralign;
4176
0
  int offset;
4177
4178
  /* This function may be called more than once.  */
4179
0
  s = elf_hash_table (info)->sgot;
4180
0
  if (s != NULL)
4181
0
    return true;
4182
4183
  /* Machine specific: although pointers are 32-bits wide, we want the
4184
     GOT to be aligned to a 64-bit boundary, such that function
4185
     descriptors in it can be accessed with 64-bit loads and
4186
     stores.  */
4187
0
  ptralign = 3;
4188
4189
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4190
0
     | SEC_LINKER_CREATED);
4191
0
  pltflags = flags;
4192
4193
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
4194
0
  elf_hash_table (info)->sgot = s;
4195
0
  if (s == NULL
4196
0
      || !bfd_set_section_alignment (s, ptralign))
4197
0
    return false;
4198
4199
0
  if (bed->want_got_sym)
4200
0
    {
4201
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
4202
   (or .got.plt) section.  We don't do this in the linker script
4203
   because we don't want to define the symbol if we are not creating
4204
   a global offset table.  */
4205
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s, "_GLOBAL_OFFSET_TABLE_");
4206
0
      elf_hash_table (info)->hgot = h;
4207
0
      if (h == NULL)
4208
0
  return false;
4209
4210
      /* Machine-specific: we want the symbol for executables as
4211
   well.  */
4212
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
4213
0
  return false;
4214
0
    }
4215
4216
  /* The first bit of the global offset table is the header.  */
4217
0
  s->size += bed->got_header_size;
4218
4219
  /* This is the machine-specific part.  Create and initialize section
4220
     data for the got.  */
4221
0
  if (IS_FDPIC (abfd))
4222
0
    {
4223
0
      frvfdpic_relocs_info (info) = htab_try_create (1,
4224
0
                 frvfdpic_relocs_info_hash,
4225
0
                 frvfdpic_relocs_info_eq,
4226
0
                 (htab_del) NULL);
4227
0
      if (! frvfdpic_relocs_info (info))
4228
0
  return false;
4229
4230
0
      s = bfd_make_section_anyway_with_flags (abfd, ".rel.got",
4231
0
                (flags | SEC_READONLY));
4232
0
      elf_hash_table (info)->srelgot = s;
4233
0
      if (s == NULL
4234
0
    || !bfd_set_section_alignment (s, 2))
4235
0
  return false;
4236
4237
      /* Machine-specific.  */
4238
0
      s = bfd_make_section_anyway_with_flags (abfd, ".rofixup",
4239
0
                (flags | SEC_READONLY));
4240
0
      if (s == NULL
4241
0
    || !bfd_set_section_alignment (s, 2))
4242
0
  return false;
4243
4244
0
      frvfdpic_gotfixup_section (info) = s;
4245
0
      offset = -2048;
4246
0
      flags = BSF_GLOBAL;
4247
0
    }
4248
0
  else
4249
0
    {
4250
0
      offset = 2048;
4251
0
      flags = BSF_GLOBAL | BSF_WEAK;
4252
0
    }
4253
4254
  /* Define _gp in .rofixup, for FDPIC, or .got otherwise.  If it
4255
     turns out that we're linking with a different linker script, the
4256
     linker script will override it.  */
4257
0
  bh = NULL;
4258
0
  if (!(_bfd_generic_link_add_one_symbol
4259
0
  (info, abfd, "_gp", flags, s, offset, (const char *) NULL, false,
4260
0
   bed->collect, &bh)))
4261
0
    return false;
4262
0
  h = (struct elf_link_hash_entry *) bh;
4263
0
  h->def_regular = 1;
4264
0
  h->type = STT_OBJECT;
4265
  /* h->other = STV_HIDDEN; */ /* Should we?  */
4266
4267
  /* Machine-specific: we want the symbol for executables as well.  */
4268
0
  if (IS_FDPIC (abfd) && ! bfd_elf_link_record_dynamic_symbol (info, h))
4269
0
    return false;
4270
4271
0
  if (!IS_FDPIC (abfd))
4272
0
    return true;
4273
4274
  /* FDPIC supports Thread Local Storage, and this may require a
4275
     procedure linkage table for TLS PLT entries.  */
4276
4277
  /* This is mostly copied from
4278
     elflink.c:_bfd_elf_create_dynamic_sections().  */
4279
4280
0
  flags = pltflags;
4281
0
  pltflags |= SEC_CODE;
4282
0
  if (bed->plt_not_loaded)
4283
0
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
4284
0
  if (bed->plt_readonly)
4285
0
    pltflags |= SEC_READONLY;
4286
4287
0
  s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
4288
0
  if (s == NULL
4289
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
4290
0
    return false;
4291
  /* FRV-specific: remember it.  */
4292
0
  frvfdpic_plt_section (info) = s;
4293
4294
  /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
4295
     .plt section.  */
4296
0
  if (bed->want_plt_sym)
4297
0
    {
4298
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
4299
0
               "_PROCEDURE_LINKAGE_TABLE_");
4300
0
      elf_hash_table (info)->hplt = h;
4301
0
      if (h == NULL)
4302
0
  return false;
4303
0
    }
4304
4305
  /* FRV-specific: we want rel relocations for the plt.  */
4306
0
  s = bfd_make_section_anyway_with_flags (abfd, ".rel.plt",
4307
0
            flags | SEC_READONLY);
4308
0
  if (s == NULL
4309
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
4310
0
    return false;
4311
  /* FRV-specific: remember it.  */
4312
0
  frvfdpic_pltrel_section (info) = s;
4313
4314
0
  return true;
4315
0
}
4316
4317
/* Make sure the got and plt sections exist, and that our pointers in
4318
   the link hash table point to them.  */
4319
4320
static bool
4321
elf32_frvfdpic_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
4322
0
{
4323
  /* This is mostly copied from
4324
     elflink.c:_bfd_elf_create_dynamic_sections().  */
4325
0
  flagword flags;
4326
0
  asection *s;
4327
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
4328
4329
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
4330
0
     | SEC_LINKER_CREATED);
4331
4332
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
4333
     .rel[a].bss sections.  */
4334
4335
  /* FRV-specific: we want to create the GOT and the PLT in the FRV
4336
     way.  */
4337
0
  if (! _frv_create_got_section (abfd, info))
4338
0
    return false;
4339
4340
  /* FRV-specific: make sure we created everything we wanted.  */
4341
0
  BFD_ASSERT (frvfdpic_got_section (info) && frvfdpic_gotrel_section (info)
4342
0
        && frvfdpic_gotfixup_section (info)
4343
0
        && frvfdpic_plt_section (info)
4344
0
        && frvfdpic_pltrel_section (info));
4345
4346
0
  if (bed->want_dynbss)
4347
0
    {
4348
      /* The .dynbss section is a place to put symbols which are defined
4349
   by dynamic objects, are referenced by regular objects, and are
4350
   not functions.  We must allocate space for them in the process
4351
   image and use a R_*_COPY reloc to tell the dynamic linker to
4352
   initialize them at run time.  The linker script puts the .dynbss
4353
   section into the .bss section of the final image.  */
4354
0
      s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
4355
0
                SEC_ALLOC | SEC_LINKER_CREATED);
4356
0
      if (s == NULL)
4357
0
  return false;
4358
4359
      /* The .rel[a].bss section holds copy relocs.  This section is not
4360
     normally needed.  We need to create it here, though, so that the
4361
     linker will map it to an output section.  We can't just create it
4362
     only if we need it, because we will not know whether we need it
4363
     until we have seen all the input files, and the first time the
4364
     main linker code calls BFD after examining all the input files
4365
     (size_dynamic_sections) the input sections have already been
4366
     mapped to the output sections.  If the section turns out not to
4367
     be needed, we can discard it later.  We will never need this
4368
     section when generating a shared object, since they do not use
4369
     copy relocs.  */
4370
0
      if (! bfd_link_pic (info))
4371
0
  {
4372
0
    s = bfd_make_section_anyway_with_flags (abfd,
4373
0
              (bed->default_use_rela_p
4374
0
               ? ".rela.bss" : ".rel.bss"),
4375
0
              flags | SEC_READONLY);
4376
0
    if (s == NULL
4377
0
        || !bfd_set_section_alignment (s, bed->s->log_file_align))
4378
0
      return false;
4379
0
  }
4380
0
    }
4381
4382
0
  return true;
4383
0
}
4384
4385
/* Compute the total GOT and PLT size required by each symbol in each
4386
   range.  Symbols may require up to 4 words in the GOT: an entry
4387
   pointing to the symbol, an entry pointing to its function
4388
   descriptor, and a private function descriptors taking two
4389
   words.  */
4390
4391
static void
4392
_frvfdpic_count_nontls_entries (struct frvfdpic_relocs_info *entry,
4393
        struct _frvfdpic_dynamic_got_info *dinfo)
4394
0
{
4395
  /* Allocate space for a GOT entry pointing to the symbol.  */
4396
0
  if (entry->got12)
4397
0
    dinfo->got12 += 4;
4398
0
  else if (entry->gotlos)
4399
0
    dinfo->gotlos += 4;
4400
0
  else if (entry->gothilo)
4401
0
    dinfo->gothilo += 4;
4402
0
  else
4403
0
    entry->relocs32--;
4404
0
  entry->relocs32++;
4405
4406
  /* Allocate space for a GOT entry pointing to the function
4407
     descriptor.  */
4408
0
  if (entry->fdgot12)
4409
0
    dinfo->got12 += 4;
4410
0
  else if (entry->fdgotlos)
4411
0
    dinfo->gotlos += 4;
4412
0
  else if (entry->fdgothilo)
4413
0
    dinfo->gothilo += 4;
4414
0
  else
4415
0
    entry->relocsfd--;
4416
0
  entry->relocsfd++;
4417
4418
  /* Decide whether we need a PLT entry, a function descriptor in the
4419
     GOT, and a lazy PLT entry for this symbol.  */
4420
0
  entry->plt = entry->call
4421
0
    && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4422
0
    && elf_hash_table (dinfo->info)->dynamic_sections_created;
4423
0
  entry->privfd = entry->plt
4424
0
    || entry->fdgoff12 || entry->fdgofflos || entry->fdgoffhilo
4425
0
    || ((entry->fd || entry->fdgot12 || entry->fdgotlos || entry->fdgothilo)
4426
0
  && (entry->symndx != -1
4427
0
      || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
4428
0
  entry->lazyplt = entry->privfd
4429
0
    && entry->symndx == -1 && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4430
0
    && ! (dinfo->info->flags & DF_BIND_NOW)
4431
0
    && elf_hash_table (dinfo->info)->dynamic_sections_created;
4432
4433
  /* Allocate space for a function descriptor.  */
4434
0
  if (entry->fdgoff12)
4435
0
    dinfo->fd12 += 8;
4436
0
  else if (entry->fdgofflos)
4437
0
    dinfo->fdlos += 8;
4438
0
  else if (entry->privfd && entry->plt)
4439
0
    dinfo->fdplt += 8;
4440
0
  else if (entry->privfd)
4441
0
    dinfo->fdhilo += 8;
4442
0
  else
4443
0
    entry->relocsfdv--;
4444
0
  entry->relocsfdv++;
4445
4446
0
  if (entry->lazyplt)
4447
0
    dinfo->lzplt += 8;
4448
0
}
4449
4450
/* Compute the total GOT size required by each TLS symbol in each
4451
   range.  Symbols may require up to 5 words in the GOT: an entry
4452
   holding the TLS offset for the symbol, and an entry with a full TLS
4453
   descriptor taking 4 words.  */
4454
4455
static void
4456
_frvfdpic_count_tls_entries (struct frvfdpic_relocs_info *entry,
4457
           struct _frvfdpic_dynamic_got_info *dinfo,
4458
           bool subtract)
4459
0
{
4460
0
  const int l = subtract ? -1 : 1;
4461
4462
  /* Allocate space for a GOT entry with the TLS offset of the
4463
     symbol.  */
4464
0
  if (entry->tlsoff12)
4465
0
    dinfo->got12 += 4 * l;
4466
0
  else if (entry->tlsofflos)
4467
0
    dinfo->gotlos += 4 * l;
4468
0
  else if (entry->tlsoffhilo)
4469
0
    dinfo->gothilo += 4 * l;
4470
0
  else
4471
0
    entry->relocstlsoff -= l;
4472
0
  entry->relocstlsoff += l;
4473
4474
  /* If there's any TLSOFF relocation, mark the output file as not
4475
     suitable for dlopening.  This mark will remain even if we relax
4476
     all such relocations, but this is not a problem, since we'll only
4477
     do so for executables, and we definitely don't want anyone
4478
     dlopening executables.  */
4479
0
  if (entry->relocstlsoff)
4480
0
    dinfo->info->flags |= DF_STATIC_TLS;
4481
4482
  /* Allocate space for a TLS descriptor.  */
4483
0
  if (entry->tlsdesc12)
4484
0
    dinfo->tlsd12 += 8 * l;
4485
0
  else if (entry->tlsdesclos)
4486
0
    dinfo->tlsdlos += 8 * l;
4487
0
  else if (entry->tlsplt)
4488
0
    dinfo->tlsdplt += 8 * l;
4489
0
  else if (entry->tlsdeschilo)
4490
0
    dinfo->tlsdhilo += 8 * l;
4491
0
  else
4492
0
    entry->relocstlsd -= l;
4493
0
  entry->relocstlsd += l;
4494
0
}
4495
4496
/* Compute the number of dynamic relocations and fixups that a symbol
4497
   requires, and add (or subtract) from the grand and per-symbol
4498
   totals.  */
4499
4500
static void
4501
_frvfdpic_count_relocs_fixups (struct frvfdpic_relocs_info *entry,
4502
             struct _frvfdpic_dynamic_got_info *dinfo,
4503
             bool subtract)
4504
0
{
4505
0
  bfd_vma relocs = 0, fixups = 0, tlsrets = 0;
4506
4507
0
  if (!bfd_link_pde (dinfo->info))
4508
0
    {
4509
0
      relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv
4510
0
  + entry->relocstlsd;
4511
4512
      /* In the executable, TLS relocations to symbols that bind
4513
   locally (including those that resolve to global TLS offsets)
4514
   are resolved immediately, without any need for fixups or
4515
   dynamic relocations.  In shared libraries, however, we must
4516
   emit dynamic relocations even for local symbols, because we
4517
   don't know the module id the library is going to get at
4518
   run-time, nor its TLS base offset.  */
4519
0
      if (!bfd_link_executable (dinfo->info)
4520
0
    || (entry->symndx == -1
4521
0
        && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
4522
0
  relocs += entry->relocstlsoff;
4523
0
    }
4524
0
  else
4525
0
    {
4526
0
      if (entry->symndx != -1 || FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h))
4527
0
  {
4528
0
    if (entry->symndx != -1
4529
0
        || entry->d.h->root.type != bfd_link_hash_undefweak)
4530
0
      fixups += entry->relocs32 + 2 * entry->relocsfdv;
4531
0
    fixups += entry->relocstlsd;
4532
0
    tlsrets += entry->relocstlsd;
4533
0
  }
4534
0
      else
4535
0
  {
4536
0
    relocs += entry->relocs32 + entry->relocsfdv
4537
0
      + entry->relocstlsoff + entry->relocstlsd;
4538
0
  }
4539
4540
0
      if (entry->symndx != -1
4541
0
    || FRVFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
4542
0
  {
4543
0
    if (entry->symndx != -1
4544
0
        || entry->d.h->root.type != bfd_link_hash_undefweak)
4545
0
      fixups += entry->relocsfd;
4546
0
  }
4547
0
      else
4548
0
  relocs += entry->relocsfd;
4549
0
    }
4550
4551
0
  if (subtract)
4552
0
    {
4553
0
      relocs = - relocs;
4554
0
      fixups = - fixups;
4555
0
      tlsrets = - tlsrets;
4556
0
    }
4557
4558
0
  entry->dynrelocs += relocs;
4559
0
  entry->fixups += fixups;
4560
0
  dinfo->relocs += relocs;
4561
0
  dinfo->fixups += fixups;
4562
0
  dinfo->tls_ret_refs += tlsrets;
4563
0
}
4564
4565
/* Look for opportunities to relax TLS relocations.  We can assume
4566
   we're linking the main executable or a static-tls library, since
4567
   otherwise we wouldn't have got here.  When relaxing, we have to
4568
   first undo any previous accounting of TLS uses of fixups, dynamic
4569
   relocations, GOT and PLT entries.  */
4570
4571
static void
4572
_frvfdpic_relax_tls_entries (struct frvfdpic_relocs_info *entry,
4573
           struct _frvfdpic_dynamic_got_info *dinfo,
4574
           bool relaxing)
4575
0
{
4576
0
  bool changed = ! relaxing;
4577
4578
0
  BFD_ASSERT (bfd_link_executable (dinfo->info)
4579
0
        || (dinfo->info->flags & DF_STATIC_TLS));
4580
4581
0
  if (entry->tlsdesc12 || entry->tlsdesclos || entry->tlsdeschilo)
4582
0
    {
4583
0
      if (! changed)
4584
0
  {
4585
0
    _frvfdpic_count_relocs_fixups (entry, dinfo, true);
4586
0
    _frvfdpic_count_tls_entries (entry, dinfo, true);
4587
0
    changed = true;
4588
0
  }
4589
4590
      /* When linking an executable, we can always decay GOTTLSDESC to
4591
   TLSMOFF, if the symbol is local, or GOTTLSOFF, otherwise.
4592
   When linking a static-tls shared library, using TLSMOFF is
4593
   not an option, but we can still use GOTTLSOFF.  When decaying
4594
   to GOTTLSOFF, we must keep the GOT entry in range.  We know
4595
   it has to fit because we'll be trading the 4 words of hte TLS
4596
   descriptor for a single word in the same range.  */
4597
0
      if (! bfd_link_executable (dinfo->info)
4598
0
    || (entry->symndx == -1
4599
0
        && ! FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)))
4600
0
  {
4601
0
    entry->tlsoff12 |= entry->tlsdesc12;
4602
0
    entry->tlsofflos |= entry->tlsdesclos;
4603
0
    entry->tlsoffhilo |= entry->tlsdeschilo;
4604
0
  }
4605
4606
0
      entry->tlsdesc12 = entry->tlsdesclos = entry->tlsdeschilo = 0;
4607
0
    }
4608
4609
  /* We can only decay TLSOFFs or call #gettlsoff to TLSMOFF in the
4610
     main executable.  We have to check whether the symbol's TLSOFF is
4611
     in range for a setlos.  For symbols with a hash entry, we can
4612
     determine exactly what to do; for others locals, we don't have
4613
     addresses handy, so we use the size of the TLS section as an
4614
     approximation.  If we get it wrong, we'll retain a GOT entry
4615
     holding the TLS offset (without dynamic relocations or fixups),
4616
     but we'll still optimize away the loads from it.  Since TLS sizes
4617
     are generally very small, it's probably not worth attempting to
4618
     do better than this.  */
4619
0
  if ((entry->tlsplt
4620
0
       || entry->tlsoff12 || entry->tlsofflos || entry->tlsoffhilo)
4621
0
      && bfd_link_executable (dinfo->info) && relaxing
4622
0
      && ((entry->symndx == -1
4623
0
     && FRVFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
4624
     /* The above may hold for an undefweak TLS symbol, so make
4625
        sure we don't have this case before accessing def.value
4626
        and def.section.  */
4627
0
     && (entry->d.h->root.type == bfd_link_hash_undefweak
4628
0
         || (bfd_vma)(entry->d.h->root.u.def.value
4629
0
          + (entry->d.h->root.u.def.section
4630
0
             ->output_section->vma)
4631
0
          + entry->d.h->root.u.def.section->output_offset
4632
0
          + entry->addend
4633
0
          - tls_biased_base (dinfo->info)
4634
0
          + 32768) < (bfd_vma)65536))
4635
0
    || (entry->symndx != -1
4636
0
        && (elf_hash_table (dinfo->info)->tls_sec->size
4637
0
      + entry->addend < 32768 + FRVFDPIC_TLS_BIAS))))
4638
0
    {
4639
0
      if (! changed)
4640
0
  {
4641
0
    _frvfdpic_count_relocs_fixups (entry, dinfo, true);
4642
0
    _frvfdpic_count_tls_entries (entry, dinfo, true);
4643
0
    changed = true;
4644
0
  }
4645
4646
0
      entry->tlsplt =
4647
0
  entry->tlsoff12 = entry->tlsofflos = entry->tlsoffhilo = 0;
4648
0
    }
4649
4650
  /* We can decay `call #gettlsoff' to a ldi #tlsoff if we already
4651
     have a #gottlsoff12 relocation for this entry, or if we can fit
4652
     one more in the 12-bit (and 16-bit) ranges.  */
4653
0
  if (entry->tlsplt
4654
0
      && (entry->tlsoff12
4655
0
    || (relaxing
4656
0
        && dinfo->got12 + dinfo->fd12 + dinfo->tlsd12 <= 4096 - 12 - 4
4657
0
        && (dinfo->got12 + dinfo->fd12 + dinfo->tlsd12
4658
0
      + dinfo->gotlos + dinfo->fdlos + dinfo->tlsdlos
4659
0
      <= 65536 - 12 - 4))))
4660
0
    {
4661
0
      if (! changed)
4662
0
  {
4663
0
    _frvfdpic_count_relocs_fixups (entry, dinfo, true);
4664
0
    _frvfdpic_count_tls_entries (entry, dinfo, true);
4665
0
    changed = true;
4666
0
  }
4667
4668
0
      entry->tlsoff12 = 1;
4669
0
      entry->tlsplt = 0;
4670
0
    }
4671
4672
0
  if (changed)
4673
0
    {
4674
0
      _frvfdpic_count_tls_entries (entry, dinfo, false);
4675
0
      _frvfdpic_count_relocs_fixups (entry, dinfo, false);
4676
0
    }
4677
4678
0
  return;
4679
0
}
4680
4681
/* Compute the total GOT and PLT size required by each symbol in each range. *
4682
   Symbols may require up to 4 words in the GOT: an entry pointing to
4683
   the symbol, an entry pointing to its function descriptor, and a
4684
   private function descriptors taking two words.  */
4685
4686
static int
4687
_frvfdpic_count_got_plt_entries (void **entryp, void *dinfo_)
4688
0
{
4689
0
  struct frvfdpic_relocs_info *entry = *entryp;
4690
0
  struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
4691
4692
0
  _frvfdpic_count_nontls_entries (entry, dinfo);
4693
4694
0
  if (bfd_link_executable (dinfo->info)
4695
0
      || (dinfo->info->flags & DF_STATIC_TLS))
4696
0
    _frvfdpic_relax_tls_entries (entry, dinfo, false);
4697
0
  else
4698
0
    {
4699
0
      _frvfdpic_count_tls_entries (entry, dinfo, false);
4700
0
      _frvfdpic_count_relocs_fixups (entry, dinfo, false);
4701
0
    }
4702
4703
0
  return 1;
4704
0
}
4705
4706
/* Determine the positive and negative ranges to be used by each
4707
   offset range in the GOT.  FDCUR and CUR, that must be aligned to a
4708
   double-word boundary, are the minimum (negative) and maximum
4709
   (positive) GOT offsets already used by previous ranges, except for
4710
   an ODD entry that may have been left behind.  GOT and FD indicate
4711
   the size of GOT entries and function descriptors that must be
4712
   placed within the range from -WRAP to WRAP.  If there's room left,
4713
   up to FDPLT bytes should be reserved for additional function
4714
   descriptors.  */
4715
4716
inline static bfd_signed_vma
4717
_frvfdpic_compute_got_alloc_data (struct _frvfdpic_dynamic_got_alloc_data *gad,
4718
          bfd_signed_vma fdcur,
4719
          bfd_signed_vma odd,
4720
          bfd_signed_vma cur,
4721
          bfd_vma got,
4722
          bfd_vma fd,
4723
          bfd_vma fdplt,
4724
          bfd_vma tlsd,
4725
          bfd_vma tlsdplt,
4726
          bfd_vma wrap)
4727
0
{
4728
0
  bfd_signed_vma wrapmin = -wrap;
4729
0
  const bfd_vma tdescsz = 8;
4730
4731
  /* Start at the given initial points.  */
4732
0
  gad->fdcur = fdcur;
4733
0
  gad->cur = cur;
4734
4735
  /* If we had an incoming odd word and we have any got entries that
4736
     are going to use it, consume it, otherwise leave gad->odd at
4737
     zero.  We might force gad->odd to zero and return the incoming
4738
     odd such that it is used by the next range, but then GOT entries
4739
     might appear to be out of order and we wouldn't be able to
4740
     shorten the GOT by one word if it turns out to end with an
4741
     unpaired GOT entry.  */
4742
0
  if (odd && got)
4743
0
    {
4744
0
      gad->odd = odd;
4745
0
      got -= 4;
4746
0
      odd = 0;
4747
0
    }
4748
0
  else
4749
0
    gad->odd = 0;
4750
4751
  /* If we're left with an unpaired GOT entry, compute its location
4752
     such that we can return it.  Otherwise, if got doesn't require an
4753
     odd number of words here, either odd was already zero in the
4754
     block above, or it was set to zero because got was non-zero, or
4755
     got was already zero.  In the latter case, we want the value of
4756
     odd to carry over to the return statement, so we don't want to
4757
     reset odd unless the condition below is true.  */
4758
0
  if (got & 4)
4759
0
    {
4760
0
      odd = cur + got;
4761
0
      got += 4;
4762
0
    }
4763
4764
  /* Compute the tentative boundaries of this range.  */
4765
0
  gad->max = cur + got;
4766
0
  gad->min = fdcur - fd;
4767
0
  gad->fdplt = 0;
4768
4769
  /* If function descriptors took too much space, wrap some of them
4770
     around.  */
4771
0
  if (gad->min < wrapmin)
4772
0
    {
4773
0
      gad->max += wrapmin - gad->min;
4774
0
      gad->tmin = gad->min = wrapmin;
4775
0
    }
4776
4777
  /* If GOT entries took too much space, wrap some of them around.
4778
     This may well cause gad->min to become lower than wrapmin.  This
4779
     will cause a relocation overflow later on, so we don't have to
4780
     report it here . */
4781
0
  if ((bfd_vma) gad->max > wrap)
4782
0
    {
4783
0
      gad->min -= gad->max - wrap;
4784
0
      gad->max = wrap;
4785
0
    }
4786
4787
  /* Add TLS descriptors.  */
4788
0
  gad->tmax = gad->max + tlsd;
4789
0
  gad->tmin = gad->min;
4790
0
  gad->tlsdplt = 0;
4791
4792
  /* If TLS descriptors took too much space, wrap an integral number
4793
     of them around.  */
4794
0
  if ((bfd_vma) gad->tmax > wrap)
4795
0
    {
4796
0
      bfd_vma wrapsize = gad->tmax - wrap;
4797
4798
0
      wrapsize += tdescsz / 2;
4799
0
      wrapsize &= ~ tdescsz / 2;
4800
4801
0
      gad->tmin -= wrapsize;
4802
0
      gad->tmax -= wrapsize;
4803
0
    }
4804
4805
  /* If there is space left and we have function descriptors
4806
     referenced in PLT entries that could take advantage of shorter
4807
     offsets, place them now.  */
4808
0
  if (fdplt && gad->tmin > wrapmin)
4809
0
    {
4810
0
      bfd_vma fds;
4811
4812
0
      if ((bfd_vma) (gad->tmin - wrapmin) < fdplt)
4813
0
  fds = gad->tmin - wrapmin;
4814
0
      else
4815
0
  fds = fdplt;
4816
4817
0
      fdplt -= fds;
4818
0
      gad->min -= fds;
4819
0
      gad->tmin -= fds;
4820
0
      gad->fdplt += fds;
4821
0
    }
4822
4823
  /* If there is more space left, try to place some more function
4824
     descriptors for PLT entries.  */
4825
0
  if (fdplt && (bfd_vma) gad->tmax < wrap)
4826
0
    {
4827
0
      bfd_vma fds;
4828
4829
0
      if ((bfd_vma) (wrap - gad->tmax) < fdplt)
4830
0
  fds = wrap - gad->tmax;
4831
0
      else
4832
0
  fds = fdplt;
4833
4834
0
      fdplt -= fds;
4835
0
      gad->max += fds;
4836
0
      gad->tmax += fds;
4837
0
      gad->fdplt += fds;
4838
0
    }
4839
4840
  /* If there is space left and we have TLS descriptors referenced in
4841
     PLT entries that could take advantage of shorter offsets, place
4842
     them now.  */
4843
0
  if (tlsdplt && gad->tmin > wrapmin)
4844
0
    {
4845
0
      bfd_vma tlsds;
4846
4847
0
      if ((bfd_vma) (gad->tmin - wrapmin) < tlsdplt)
4848
0
  tlsds = (gad->tmin - wrapmin) & ~ (tdescsz / 2);
4849
0
      else
4850
0
  tlsds = tlsdplt;
4851
4852
0
      tlsdplt -= tlsds;
4853
0
      gad->tmin -= tlsds;
4854
0
      gad->tlsdplt += tlsds;
4855
0
    }
4856
4857
  /* If there is more space left, try to place some more TLS
4858
     descriptors for PLT entries.  Although we could try to fit an
4859
     additional TLS descriptor with half of it just before before the
4860
     wrap point and another right past the wrap point, this might
4861
     cause us to run out of space for the next region, so don't do
4862
     it.  */
4863
0
  if (tlsdplt && (bfd_vma) gad->tmax < wrap - tdescsz / 2)
4864
0
    {
4865
0
      bfd_vma tlsds;
4866
4867
0
      if ((bfd_vma) (wrap - gad->tmax) < tlsdplt)
4868
0
  tlsds = (wrap - gad->tmax) & ~ (tdescsz / 2);
4869
0
      else
4870
0
  tlsds = tlsdplt;
4871
4872
0
      tlsdplt -= tlsds;
4873
0
      gad->tmax += tlsds;
4874
0
      gad->tlsdplt += tlsds;
4875
0
    }
4876
4877
  /* If odd was initially computed as an offset past the wrap point,
4878
     wrap it around.  */
4879
0
  if (odd > gad->max)
4880
0
    odd = gad->min + odd - gad->max;
4881
4882
  /* _frvfdpic_get_got_entry() below will always wrap gad->cur if needed
4883
     before returning, so do it here too.  This guarantees that,
4884
     should cur and fdcur meet at the wrap point, they'll both be
4885
     equal to min.  */
4886
0
  if (gad->cur == gad->max)
4887
0
    gad->cur = gad->min;
4888
4889
  /* Ditto for _frvfdpic_get_tlsdesc_entry().  */
4890
0
  gad->tcur = gad->max;
4891
0
  if (gad->tcur == gad->tmax)
4892
0
    gad->tcur = gad->tmin;
4893
4894
0
  return odd;
4895
0
}
4896
4897
/* Compute the location of the next GOT entry, given the allocation
4898
   data for a range.  */
4899
4900
inline static bfd_signed_vma
4901
_frvfdpic_get_got_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
4902
0
{
4903
0
  bfd_signed_vma ret;
4904
4905
0
  if (gad->odd)
4906
0
    {
4907
      /* If there was an odd word left behind, use it.  */
4908
0
      ret = gad->odd;
4909
0
      gad->odd = 0;
4910
0
    }
4911
0
  else
4912
0
    {
4913
      /* Otherwise, use the word pointed to by cur, reserve the next
4914
   as an odd word, and skip to the next pair of words, possibly
4915
   wrapping around.  */
4916
0
      ret = gad->cur;
4917
0
      gad->odd = gad->cur + 4;
4918
0
      gad->cur += 8;
4919
0
      if (gad->cur == gad->max)
4920
0
  gad->cur = gad->min;
4921
0
    }
4922
4923
0
  return ret;
4924
0
}
4925
4926
/* Compute the location of the next function descriptor entry in the
4927
   GOT, given the allocation data for a range.  */
4928
4929
inline static bfd_signed_vma
4930
_frvfdpic_get_fd_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
4931
0
{
4932
  /* If we're at the bottom, wrap around, and only then allocate the
4933
     next pair of words.  */
4934
0
  if (gad->fdcur == gad->min)
4935
0
    gad->fdcur = gad->max;
4936
0
  return gad->fdcur -= 8;
4937
0
}
4938
4939
/* Compute the location of the next TLS descriptor entry in the GOT,
4940
   given the allocation data for a range.  */
4941
inline static bfd_signed_vma
4942
_frvfdpic_get_tlsdesc_entry (struct _frvfdpic_dynamic_got_alloc_data *gad)
4943
0
{
4944
0
  bfd_signed_vma ret;
4945
4946
0
  ret = gad->tcur;
4947
4948
0
  gad->tcur += 8;
4949
4950
  /* If we're at the top of the region, wrap around to the bottom.  */
4951
0
  if (gad->tcur == gad->tmax)
4952
0
    gad->tcur = gad->tmin;
4953
4954
0
  return ret;
4955
0
}
4956
4957
/* Assign GOT offsets for every GOT entry and function descriptor.
4958
   Doing everything in a single pass is tricky.  */
4959
4960
static int
4961
_frvfdpic_assign_got_entries (void **entryp, void *info_)
4962
0
{
4963
0
  struct frvfdpic_relocs_info *entry = *entryp;
4964
0
  struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
4965
4966
0
  if (entry->got12)
4967
0
    entry->got_entry = _frvfdpic_get_got_entry (&dinfo->got12);
4968
0
  else if (entry->gotlos)
4969
0
    entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
4970
0
  else if (entry->gothilo)
4971
0
    entry->got_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
4972
4973
0
  if (entry->fdgot12)
4974
0
    entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->got12);
4975
0
  else if (entry->fdgotlos)
4976
0
    entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
4977
0
  else if (entry->fdgothilo)
4978
0
    entry->fdgot_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
4979
4980
0
  if (entry->fdgoff12)
4981
0
    entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
4982
0
  else if (entry->plt && dinfo->got12.fdplt)
4983
0
    {
4984
0
      dinfo->got12.fdplt -= 8;
4985
0
      entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->got12);
4986
0
    }
4987
0
  else if (entry->fdgofflos)
4988
0
    entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
4989
0
  else if (entry->plt && dinfo->gotlos.fdplt)
4990
0
    {
4991
0
      dinfo->gotlos.fdplt -= 8;
4992
0
      entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gotlos);
4993
0
    }
4994
0
  else if (entry->plt)
4995
0
    {
4996
0
      dinfo->gothilo.fdplt -= 8;
4997
0
      entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
4998
0
    }
4999
0
  else if (entry->privfd)
5000
0
    entry->fd_entry = _frvfdpic_get_fd_entry (&dinfo->gothilo);
5001
5002
0
  if (entry->tlsoff12)
5003
0
    entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->got12);
5004
0
  else if (entry->tlsofflos)
5005
0
    entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gotlos);
5006
0
  else if (entry->tlsoffhilo)
5007
0
    entry->tlsoff_entry = _frvfdpic_get_got_entry (&dinfo->gothilo);
5008
5009
0
  if (entry->tlsdesc12)
5010
0
    entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
5011
0
  else if (entry->tlsplt && dinfo->got12.tlsdplt)
5012
0
    {
5013
0
      dinfo->got12.tlsdplt -= 8;
5014
0
      entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->got12);
5015
0
    }
5016
0
  else if (entry->tlsdesclos)
5017
0
    entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
5018
0
  else if (entry->tlsplt && dinfo->gotlos.tlsdplt)
5019
0
    {
5020
0
      dinfo->gotlos.tlsdplt -= 8;
5021
0
      entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gotlos);
5022
0
    }
5023
0
  else if (entry->tlsplt)
5024
0
    {
5025
0
      dinfo->gothilo.tlsdplt -= 8;
5026
0
      entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
5027
0
    }
5028
0
  else if (entry->tlsdeschilo)
5029
0
    entry->tlsdesc_entry = _frvfdpic_get_tlsdesc_entry (&dinfo->gothilo);
5030
5031
0
  return 1;
5032
0
}
5033
5034
/* Assign GOT offsets to private function descriptors used by PLT
5035
   entries (or referenced by 32-bit offsets), as well as PLT entries
5036
   and lazy PLT entries.  */
5037
5038
static int
5039
_frvfdpic_assign_plt_entries (void **entryp, void *info_)
5040
0
{
5041
0
  struct frvfdpic_relocs_info *entry = *entryp;
5042
0
  struct _frvfdpic_dynamic_got_plt_info *dinfo = info_;
5043
5044
0
  if (entry->privfd)
5045
0
    BFD_ASSERT (entry->fd_entry);
5046
5047
0
  if (entry->plt)
5048
0
    {
5049
0
      int size;
5050
5051
      /* We use the section's raw size to mark the location of the
5052
   next PLT entry.  */
5053
0
      entry->plt_entry = frvfdpic_plt_section (dinfo->g.info)->size;
5054
5055
      /* Figure out the length of this PLT entry based on the
5056
   addressing mode we need to reach the function descriptor.  */
5057
0
      BFD_ASSERT (entry->fd_entry);
5058
0
      if (entry->fd_entry >= -(1 << (12 - 1))
5059
0
    && entry->fd_entry < (1 << (12 - 1)))
5060
0
  size = 8;
5061
0
      else if (entry->fd_entry >= -(1 << (16 - 1))
5062
0
         && entry->fd_entry < (1 << (16 - 1)))
5063
0
  size = 12;
5064
0
      else
5065
0
  size = 16;
5066
5067
0
      frvfdpic_plt_section (dinfo->g.info)->size += size;
5068
0
    }
5069
5070
0
  if (entry->lazyplt)
5071
0
    {
5072
0
      entry->lzplt_entry = dinfo->g.lzplt;
5073
0
      dinfo->g.lzplt += 8;
5074
      /* If this entry is the one that gets the resolver stub, account
5075
   for the additional instruction.  */
5076
0
      if (entry->lzplt_entry % FRVFDPIC_LZPLT_BLOCK_SIZE
5077
0
    == FRVFDPIC_LZPLT_RESOLV_LOC)
5078
0
  dinfo->g.lzplt += 4;
5079
0
    }
5080
5081
0
  if (entry->tlsplt)
5082
0
    {
5083
0
      int size;
5084
5085
0
      entry->tlsplt_entry
5086
0
  = frvfdpic_plt_section (dinfo->g.info)->size;
5087
5088
0
      if (bfd_link_executable (dinfo->g.info)
5089
0
    && (entry->symndx != -1
5090
0
        || FRVFDPIC_SYM_LOCAL (dinfo->g.info, entry->d.h)))
5091
0
  {
5092
0
    if ((bfd_signed_vma)entry->addend >= -(1 << (16 - 1))
5093
        /* FIXME: here we use the size of the TLS section
5094
     as an upper bound for the value of the TLS
5095
     symbol, because we may not know the exact value
5096
     yet.  If we get it wrong, we'll just waste a
5097
     word in the PLT, and we should never get even
5098
     close to 32 KiB of TLS anyway.  */
5099
0
        && elf_hash_table (dinfo->g.info)->tls_sec
5100
0
        && (elf_hash_table (dinfo->g.info)->tls_sec->size
5101
0
      + (bfd_signed_vma)(entry->addend) <= (1 << (16 - 1))))
5102
0
      size = 8;
5103
0
    else
5104
0
      size = 12;
5105
0
  }
5106
0
      else if (entry->tlsoff_entry)
5107
0
  {
5108
0
    if (entry->tlsoff_entry >= -(1 << (12 - 1))
5109
0
        && entry->tlsoff_entry < (1 << (12 - 1)))
5110
0
      size = 8;
5111
0
    else if (entry->tlsoff_entry >= -(1 << (16 - 1))
5112
0
       && entry->tlsoff_entry < (1 << (16 - 1)))
5113
0
      size = 12;
5114
0
    else
5115
0
      size = 16;
5116
0
  }
5117
0
      else
5118
0
  {
5119
0
    BFD_ASSERT (entry->tlsdesc_entry);
5120
5121
0
    if (entry->tlsdesc_entry >= -(1 << (12 - 1))
5122
0
        && entry->tlsdesc_entry < (1 << (12 - 1)))
5123
0
      size = 8;
5124
0
    else if (entry->tlsdesc_entry >= -(1 << (16 - 1))
5125
0
       && entry->tlsdesc_entry < (1 << (16 - 1)))
5126
0
      size = 12;
5127
0
    else
5128
0
      size = 16;
5129
0
  }
5130
5131
0
      frvfdpic_plt_section (dinfo->g.info)->size += size;
5132
0
    }
5133
5134
0
  return 1;
5135
0
}
5136
5137
/* Cancel out any effects of calling _frvfdpic_assign_got_entries and
5138
   _frvfdpic_assign_plt_entries.  */
5139
5140
static int
5141
_frvfdpic_reset_got_plt_entries (void **entryp, void *ignore ATTRIBUTE_UNUSED)
5142
0
{
5143
0
  struct frvfdpic_relocs_info *entry = *entryp;
5144
5145
0
  entry->got_entry = 0;
5146
0
  entry->fdgot_entry = 0;
5147
0
  entry->fd_entry = 0;
5148
0
  entry->plt_entry = (bfd_vma)-1;
5149
0
  entry->lzplt_entry = (bfd_vma)-1;
5150
0
  entry->tlsoff_entry = 0;
5151
0
  entry->tlsdesc_entry = 0;
5152
0
  entry->tlsplt_entry = (bfd_vma)-1;
5153
5154
0
  return 1;
5155
0
}
5156
5157
/* Follow indirect and warning hash entries so that each got entry
5158
   points to the final symbol definition.  P must point to a pointer
5159
   to the hash table we're traversing.  Since this traversal may
5160
   modify the hash table, we set this pointer to NULL to indicate
5161
   we've made a potentially-destructive change to the hash table, so
5162
   the traversal must be restarted.  */
5163
static int
5164
_frvfdpic_resolve_final_relocs_info (void **entryp, void *p)
5165
0
{
5166
0
  struct frvfdpic_relocs_info *entry = *entryp;
5167
0
  htab_t *htab = p;
5168
5169
0
  if (entry->symndx == -1)
5170
0
    {
5171
0
      struct elf_link_hash_entry *h = entry->d.h;
5172
0
      struct frvfdpic_relocs_info *oentry;
5173
5174
0
      while (h->root.type == bfd_link_hash_indirect
5175
0
       || h->root.type == bfd_link_hash_warning)
5176
0
  h = (struct elf_link_hash_entry *)h->root.u.i.link;
5177
5178
0
      if (entry->d.h == h)
5179
0
  return 1;
5180
5181
0
      oentry = frvfdpic_relocs_info_for_global (*htab, 0, h, entry->addend,
5182
0
            NO_INSERT);
5183
5184
0
      if (oentry)
5185
0
  {
5186
    /* Merge the two entries.  */
5187
0
    frvfdpic_pic_merge_early_relocs_info (oentry, entry);
5188
0
    htab_clear_slot (*htab, entryp);
5189
0
    return 1;
5190
0
  }
5191
5192
0
      entry->d.h = h;
5193
5194
      /* If we can't find this entry with the new bfd hash, re-insert
5195
   it, and get the traversal restarted.  */
5196
0
      if (! htab_find (*htab, entry))
5197
0
  {
5198
0
    htab_clear_slot (*htab, entryp);
5199
0
    entryp = htab_find_slot (*htab, entry, INSERT);
5200
0
    if (! *entryp)
5201
0
      *entryp = entry;
5202
    /* Abort the traversal, since the whole table may have
5203
       moved, and leave it up to the parent to restart the
5204
       process.  */
5205
0
    *(htab_t *)p = NULL;
5206
0
    return 0;
5207
0
  }
5208
0
    }
5209
5210
0
  return 1;
5211
0
}
5212
5213
/* Compute the total size of the GOT, the PLT, the dynamic relocations
5214
   section and the rofixup section.  Assign locations for GOT and PLT
5215
   entries.  */
5216
5217
static bool
5218
_frvfdpic_size_got_plt (bfd *output_bfd,
5219
      struct _frvfdpic_dynamic_got_plt_info *gpinfop)
5220
0
{
5221
0
  bfd_signed_vma odd;
5222
0
  bfd_vma limit, tlslimit;
5223
0
  struct bfd_link_info *info = gpinfop->g.info;
5224
0
  bfd *dynobj = elf_hash_table (info)->dynobj;
5225
5226
0
  memcpy (frvfdpic_dynamic_got_plt_info (info), &gpinfop->g,
5227
0
    sizeof (gpinfop->g));
5228
5229
0
  odd = 12;
5230
  /* Compute the total size taken by entries in the 12-bit and 16-bit
5231
     ranges, to tell how many PLT function descriptors we can bring
5232
     into the 12-bit range without causing the 16-bit range to
5233
     overflow.  */
5234
0
  limit = odd + gpinfop->g.got12 + gpinfop->g.gotlos
5235
0
    + gpinfop->g.fd12 + gpinfop->g.fdlos
5236
0
    + gpinfop->g.tlsd12 + gpinfop->g.tlsdlos;
5237
0
  if (limit < (bfd_vma)1 << 16)
5238
0
    limit = ((bfd_vma)1 << 16) - limit;
5239
0
  else
5240
0
    limit = 0;
5241
0
  if (gpinfop->g.fdplt < limit)
5242
0
    {
5243
0
      tlslimit = (limit - gpinfop->g.fdplt) & ~ (bfd_vma) 8;
5244
0
      limit = gpinfop->g.fdplt;
5245
0
    }
5246
0
  else
5247
0
    tlslimit = 0;
5248
0
  if (gpinfop->g.tlsdplt < tlslimit)
5249
0
    tlslimit = gpinfop->g.tlsdplt;
5250
5251
  /* Determine the ranges of GOT offsets that we can use for each
5252
     range of addressing modes.  */
5253
0
  odd = _frvfdpic_compute_got_alloc_data (&gpinfop->got12,
5254
0
            0,
5255
0
            odd,
5256
0
            16,
5257
0
            gpinfop->g.got12,
5258
0
            gpinfop->g.fd12,
5259
0
            limit,
5260
0
            gpinfop->g.tlsd12,
5261
0
            tlslimit,
5262
0
            (bfd_vma)1 << (12-1));
5263
0
  odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gotlos,
5264
0
            gpinfop->got12.tmin,
5265
0
            odd,
5266
0
            gpinfop->got12.tmax,
5267
0
            gpinfop->g.gotlos,
5268
0
            gpinfop->g.fdlos,
5269
0
            gpinfop->g.fdplt
5270
0
            - gpinfop->got12.fdplt,
5271
0
            gpinfop->g.tlsdlos,
5272
0
            gpinfop->g.tlsdplt
5273
0
            - gpinfop->got12.tlsdplt,
5274
0
            (bfd_vma)1 << (16-1));
5275
0
  odd = _frvfdpic_compute_got_alloc_data (&gpinfop->gothilo,
5276
0
            gpinfop->gotlos.tmin,
5277
0
            odd,
5278
0
            gpinfop->gotlos.tmax,
5279
0
            gpinfop->g.gothilo,
5280
0
            gpinfop->g.fdhilo,
5281
0
            gpinfop->g.fdplt
5282
0
            - gpinfop->got12.fdplt
5283
0
            - gpinfop->gotlos.fdplt,
5284
0
            gpinfop->g.tlsdhilo,
5285
0
            gpinfop->g.tlsdplt
5286
0
            - gpinfop->got12.tlsdplt
5287
0
            - gpinfop->gotlos.tlsdplt,
5288
0
            (bfd_vma)1 << (32-1));
5289
5290
  /* Now assign (most) GOT offsets.  */
5291
0
  htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_got_entries,
5292
0
     gpinfop);
5293
5294
0
  frvfdpic_got_section (info)->size = gpinfop->gothilo.tmax
5295
0
    - gpinfop->gothilo.tmin
5296
    /* If an odd word is the last word of the GOT, we don't need this
5297
       word to be part of the GOT.  */
5298
0
    - (odd + 4 == gpinfop->gothilo.tmax ? 4 : 0);
5299
0
  if (frvfdpic_got_section (info)->size == 0)
5300
0
    frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
5301
0
  else if (frvfdpic_got_section (info)->size == 12
5302
0
     && ! elf_hash_table (info)->dynamic_sections_created)
5303
0
    {
5304
0
      frvfdpic_got_section (info)->flags |= SEC_EXCLUDE;
5305
0
      frvfdpic_got_section (info)->size = 0;
5306
0
    }
5307
  /* This will be non-NULL during relaxation.  The assumption is that
5308
     the size of one of these sections will never grow, only shrink,
5309
     so we can use the larger buffer we allocated before.  */
5310
0
  else if (frvfdpic_got_section (info)->contents == NULL)
5311
0
    {
5312
0
      frvfdpic_got_section (info)->contents =
5313
0
  (bfd_byte *) bfd_zalloc (dynobj,
5314
0
         frvfdpic_got_section (info)->size);
5315
0
      if (frvfdpic_got_section (info)->contents == NULL)
5316
0
  return false;
5317
0
      frvfdpic_got_section (info)->alloced = 1;
5318
0
    }
5319
5320
0
  if (frvfdpic_gotrel_section (info))
5321
    /* Subtract the number of lzplt entries, since those will generate
5322
       relocations in the pltrel section.  */
5323
0
    frvfdpic_gotrel_section (info)->size =
5324
0
      (gpinfop->g.relocs - gpinfop->g.lzplt / 8)
5325
0
      * get_elf_backend_data (output_bfd)->s->sizeof_rel;
5326
0
  else
5327
0
    BFD_ASSERT (gpinfop->g.relocs == 0);
5328
0
  if (frvfdpic_gotrel_section (info)->size == 0)
5329
0
    frvfdpic_gotrel_section (info)->flags |= SEC_EXCLUDE;
5330
0
  else if (frvfdpic_gotrel_section (info)->contents == NULL)
5331
0
    {
5332
0
      frvfdpic_gotrel_section (info)->contents =
5333
0
  (bfd_byte *) bfd_zalloc (dynobj,
5334
0
         frvfdpic_gotrel_section (info)->size);
5335
0
      if (frvfdpic_gotrel_section (info)->contents == NULL)
5336
0
  return false;
5337
0
      frvfdpic_gotrel_section (info)->alloced = 1;
5338
0
    }
5339
5340
0
  frvfdpic_gotfixup_section (info)->size = (gpinfop->g.fixups + 1) * 4;
5341
0
  if (frvfdpic_gotfixup_section (info)->size == 0)
5342
0
    frvfdpic_gotfixup_section (info)->flags |= SEC_EXCLUDE;
5343
0
  else if (frvfdpic_gotfixup_section (info)->contents == NULL)
5344
0
    {
5345
0
      frvfdpic_gotfixup_section (info)->contents =
5346
0
  (bfd_byte *) bfd_zalloc (dynobj,
5347
0
         frvfdpic_gotfixup_section (info)->size);
5348
0
      if (frvfdpic_gotfixup_section (info)->contents == NULL)
5349
0
  return false;
5350
0
      frvfdpic_gotfixup_section (info)->alloced = 1;
5351
0
    }
5352
5353
0
  if (frvfdpic_pltrel_section (info))
5354
0
    {
5355
0
      frvfdpic_pltrel_section (info)->size =
5356
0
  gpinfop->g.lzplt / 8
5357
0
  * get_elf_backend_data (output_bfd)->s->sizeof_rel;
5358
0
      if (frvfdpic_pltrel_section (info)->size == 0)
5359
0
  frvfdpic_pltrel_section (info)->flags |= SEC_EXCLUDE;
5360
0
      else if (frvfdpic_pltrel_section (info)->contents == NULL)
5361
0
  {
5362
0
    frvfdpic_pltrel_section (info)->contents =
5363
0
      (bfd_byte *) bfd_zalloc (dynobj,
5364
0
             frvfdpic_pltrel_section (info)->size);
5365
0
    if (frvfdpic_pltrel_section (info)->contents == NULL)
5366
0
      return false;
5367
0
    frvfdpic_pltrel_section (info)->alloced = 1;
5368
0
  }
5369
0
    }
5370
5371
  /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
5372
     such that there's room for the additional instruction needed to
5373
     call the resolver.  Since _frvfdpic_assign_got_entries didn't
5374
     account for them, our block size is 4 bytes smaller than the real
5375
     block size.  */
5376
0
  if (frvfdpic_plt_section (info))
5377
0
    {
5378
0
      frvfdpic_plt_section (info)->size = gpinfop->g.lzplt
5379
0
  + ((gpinfop->g.lzplt + (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) - 8)
5380
0
     / (FRVFDPIC_LZPLT_BLOCK_SIZE - 4) * 4);
5381
0
    }
5382
5383
  /* Reset it, such that _frvfdpic_assign_plt_entries() can use it to
5384
     actually assign lazy PLT entries addresses.  */
5385
0
  gpinfop->g.lzplt = 0;
5386
5387
  /* Save information that we're going to need to generate GOT and PLT
5388
     entries.  */
5389
0
  frvfdpic_got_initial_offset (info) = -gpinfop->gothilo.tmin;
5390
5391
0
  if (get_elf_backend_data (output_bfd)->want_got_sym)
5392
0
    elf_hash_table (info)->hgot->root.u.def.value
5393
0
      = frvfdpic_got_initial_offset (info);
5394
5395
0
  if (frvfdpic_plt_section (info))
5396
0
    frvfdpic_plt_initial_offset (info) =
5397
0
      frvfdpic_plt_section (info)->size;
5398
5399
  /* Allocate a ret statement at plt_initial_offset, to be used by
5400
     locally-resolved TLS descriptors.  */
5401
0
  if (gpinfop->g.tls_ret_refs)
5402
0
    frvfdpic_plt_section (info)->size += 4;
5403
5404
0
  htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_assign_plt_entries,
5405
0
     gpinfop);
5406
5407
  /* Allocate the PLT section contents only after
5408
     _frvfdpic_assign_plt_entries has a chance to add the size of the
5409
     non-lazy PLT entries.  */
5410
0
  if (frvfdpic_plt_section (info))
5411
0
    {
5412
0
      if (frvfdpic_plt_section (info)->size == 0)
5413
0
  frvfdpic_plt_section (info)->flags |= SEC_EXCLUDE;
5414
0
      else if (frvfdpic_plt_section (info)->contents == NULL)
5415
0
  {
5416
0
    frvfdpic_plt_section (info)->contents =
5417
0
      (bfd_byte *) bfd_zalloc (dynobj,
5418
0
             frvfdpic_plt_section (info)->size);
5419
0
    if (frvfdpic_plt_section (info)->contents == NULL)
5420
0
      return false;
5421
0
    frvfdpic_plt_section (info)->alloced = 1;
5422
0
  }
5423
0
    }
5424
5425
0
  return true;
5426
0
}
5427
5428
/* Set the sizes of the dynamic sections.  */
5429
5430
static bool
5431
elf32_frvfdpic_late_size_sections (bfd *output_bfd,
5432
           struct bfd_link_info *info)
5433
0
{
5434
0
  bfd *dynobj;
5435
0
  asection *s;
5436
0
  struct _frvfdpic_dynamic_got_plt_info gpinfo;
5437
5438
0
  dynobj = elf_hash_table (info)->dynobj;
5439
0
  if (dynobj == NULL)
5440
0
    return true;
5441
5442
0
  if (elf_hash_table (info)->dynamic_sections_created)
5443
0
    {
5444
      /* Set the contents of the .interp section to the interpreter.  */
5445
0
      if (bfd_link_executable (info) && !info->nointerp)
5446
0
  {
5447
0
    s = elf_hash_table (info)->interp;
5448
0
    BFD_ASSERT (s != NULL);
5449
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5450
0
    s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
5451
0
    s->alloced = 1;
5452
0
  }
5453
0
    }
5454
5455
0
  memset (&gpinfo, 0, sizeof (gpinfo));
5456
0
  gpinfo.g.info = info;
5457
5458
0
  for (;;)
5459
0
    {
5460
0
      htab_t relocs = frvfdpic_relocs_info (info);
5461
5462
0
      htab_traverse (relocs, _frvfdpic_resolve_final_relocs_info, &relocs);
5463
5464
0
      if (relocs == frvfdpic_relocs_info (info))
5465
0
  break;
5466
0
    }
5467
5468
0
  htab_traverse (frvfdpic_relocs_info (info), _frvfdpic_count_got_plt_entries,
5469
0
     &gpinfo.g);
5470
5471
  /* Allocate space to save the summary information, we're going to
5472
     use it if we're doing relaxations.  */
5473
0
  frvfdpic_dynamic_got_plt_info (info) = bfd_alloc (dynobj, sizeof (gpinfo.g));
5474
5475
0
  if (!_frvfdpic_size_got_plt (output_bfd, &gpinfo))
5476
0
    return false;
5477
5478
0
  return _bfd_elf_add_dynamic_tags (output_bfd, info, true);
5479
0
}
5480
5481
static bool
5482
elf32_frvfdpic_early_size_sections (bfd *output_bfd,
5483
            struct bfd_link_info *info)
5484
0
{
5485
0
  if (!bfd_link_relocatable (info)
5486
0
      && !bfd_elf_stack_segment_size (output_bfd, info,
5487
0
              "__stacksize", DEFAULT_STACK_SIZE))
5488
0
    return false;
5489
5490
0
  return true;
5491
0
}
5492
5493
/* Check whether any of the relocations was optimized away, and
5494
   subtract it from the relocation or fixup count.  */
5495
static bool
5496
_frvfdpic_check_discarded_relocs (bfd *abfd, asection *sec,
5497
          struct bfd_link_info *info,
5498
          bool *changed)
5499
0
{
5500
0
  Elf_Internal_Shdr *symtab_hdr;
5501
0
  struct elf_link_hash_entry **sym_hashes;
5502
0
  Elf_Internal_Rela *rel, *erel;
5503
5504
0
  if ((sec->flags & SEC_RELOC) == 0
5505
0
      || sec->reloc_count == 0)
5506
0
    return true;
5507
5508
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
5509
0
  sym_hashes = elf_sym_hashes (abfd);
5510
5511
0
  rel = elf_section_data (sec)->relocs;
5512
5513
  /* Now examine each relocation.  */
5514
0
  for (erel = rel + sec->reloc_count; rel < erel; rel++)
5515
0
    {
5516
0
      struct elf_link_hash_entry *h;
5517
0
      unsigned long r_symndx;
5518
0
      struct frvfdpic_relocs_info *picrel;
5519
0
      struct _frvfdpic_dynamic_got_info *dinfo;
5520
5521
0
      if (ELF32_R_TYPE (rel->r_info) != R_FRV_32
5522
0
    && ELF32_R_TYPE (rel->r_info) != R_FRV_FUNCDESC)
5523
0
  continue;
5524
5525
0
      if (_bfd_elf_section_offset (sec->output_section->owner,
5526
0
           info, sec, rel->r_offset)
5527
0
    != (bfd_vma)-1)
5528
0
  continue;
5529
5530
0
      r_symndx = ELF32_R_SYM (rel->r_info);
5531
0
      if (r_symndx < symtab_hdr->sh_info)
5532
0
  h = NULL;
5533
0
      else
5534
0
  {
5535
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5536
0
    while (h->root.type == bfd_link_hash_indirect
5537
0
     || h->root.type == bfd_link_hash_warning)
5538
0
      h = (struct elf_link_hash_entry *)h->root.u.i.link;
5539
0
  }
5540
5541
0
      if (h != NULL)
5542
0
  picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
5543
0
              abfd, h,
5544
0
              rel->r_addend, NO_INSERT);
5545
0
      else
5546
0
  picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info (info),
5547
0
             abfd, r_symndx,
5548
0
             rel->r_addend, NO_INSERT);
5549
5550
0
      if (! picrel)
5551
0
  return false;
5552
5553
0
      *changed = true;
5554
0
      dinfo = frvfdpic_dynamic_got_plt_info (info);
5555
5556
0
      _frvfdpic_count_relocs_fixups (picrel, dinfo, true);
5557
0
      if (ELF32_R_TYPE (rel->r_info) == R_FRV_32)
5558
0
  picrel->relocs32--;
5559
0
      else /* we know (ELF32_R_TYPE (rel->r_info) == R_FRV_FUNCDESC) */
5560
0
  picrel->relocsfd--;
5561
0
      _frvfdpic_count_relocs_fixups (picrel, dinfo, false);
5562
0
    }
5563
5564
0
  return true;
5565
0
}
5566
5567
static bool
5568
frvfdpic_elf_discard_info (bfd *ibfd,
5569
         struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
5570
         struct bfd_link_info *info)
5571
0
{
5572
0
  bool changed = false;
5573
0
  asection *s;
5574
0
  bfd *obfd = NULL;
5575
5576
  /* Account for relaxation of .eh_frame section.  */
5577
0
  for (s = ibfd->sections; s; s = s->next)
5578
0
    if (s->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
5579
0
      {
5580
0
  if (!_frvfdpic_check_discarded_relocs (ibfd, s, info, &changed))
5581
0
    return false;
5582
0
  obfd = s->output_section->owner;
5583
0
      }
5584
5585
0
  if (changed)
5586
0
    {
5587
0
      struct _frvfdpic_dynamic_got_plt_info gpinfo;
5588
5589
0
      memset (&gpinfo, 0, sizeof (gpinfo));
5590
0
      memcpy (&gpinfo.g, frvfdpic_dynamic_got_plt_info (info),
5591
0
        sizeof (gpinfo.g));
5592
5593
      /* Clear GOT and PLT assignments.  */
5594
0
      htab_traverse (frvfdpic_relocs_info (info),
5595
0
         _frvfdpic_reset_got_plt_entries,
5596
0
         NULL);
5597
5598
0
      if (!_frvfdpic_size_got_plt (obfd, &gpinfo))
5599
0
  return false;
5600
0
    }
5601
5602
0
  return true;
5603
0
}
5604
5605
/* Look for opportunities to relax TLS relocations.  We can assume
5606
   we're linking the main executable or a static-tls library, since
5607
   otherwise we wouldn't have got here.  */
5608
5609
static int
5610
_frvfdpic_relax_got_plt_entries (void **entryp, void *dinfo_)
5611
0
{
5612
0
  struct frvfdpic_relocs_info *entry = *entryp;
5613
0
  struct _frvfdpic_dynamic_got_info *dinfo = dinfo_;
5614
5615
0
  _frvfdpic_relax_tls_entries (entry, dinfo, true);
5616
5617
0
  return 1;
5618
0
}
5619
5620
static bool
5621
elf32_frvfdpic_relax_section (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
5622
            struct bfd_link_info *info, bool *again)
5623
0
{
5624
0
  struct _frvfdpic_dynamic_got_plt_info gpinfo;
5625
5626
0
  if (bfd_link_relocatable (info))
5627
0
    info->callbacks->fatal
5628
0
      (_("%P: --relax and -r may not be used together\n"));
5629
5630
  /* If we return early, we didn't change anything.  */
5631
0
  *again = false;
5632
5633
  /* We'll do our thing when requested to relax the GOT section.  */
5634
0
  if (sec != frvfdpic_got_section (info))
5635
0
    return true;
5636
5637
  /* We can only relax when linking the main executable or a library
5638
     that can't be dlopened.  */
5639
0
  if (! bfd_link_executable (info) && ! (info->flags & DF_STATIC_TLS))
5640
0
    return true;
5641
5642
  /* If there isn't a TLS section for this binary, we can't do
5643
     anything about its TLS relocations (it probably doesn't have
5644
     any.  */
5645
0
  if (elf_hash_table (info)->tls_sec == NULL)
5646
0
    return true;
5647
5648
0
  memset (&gpinfo, 0, sizeof (gpinfo));
5649
0
  memcpy (&gpinfo.g, frvfdpic_dynamic_got_plt_info (info), sizeof (gpinfo.g));
5650
5651
  /* Now look for opportunities to relax, adjusting the GOT usage
5652
     as needed.  */
5653
0
  htab_traverse (frvfdpic_relocs_info (info),
5654
0
     _frvfdpic_relax_got_plt_entries,
5655
0
     &gpinfo.g);
5656
5657
  /* If we changed anything, reset and re-assign GOT and PLT entries.  */
5658
0
  if (memcmp (frvfdpic_dynamic_got_plt_info (info),
5659
0
        &gpinfo.g, sizeof (gpinfo.g)) != 0)
5660
0
    {
5661
      /* Clear GOT and PLT assignments.  */
5662
0
      htab_traverse (frvfdpic_relocs_info (info),
5663
0
         _frvfdpic_reset_got_plt_entries,
5664
0
         NULL);
5665
5666
      /* The owner of the TLS section is the output bfd.  There should
5667
   be a better way to get to it.  */
5668
0
      if (!_frvfdpic_size_got_plt (elf_hash_table (info)->tls_sec->owner,
5669
0
           &gpinfo))
5670
0
  return false;
5671
5672
      /* Repeat until we don't make any further changes.  We could fail to
5673
   introduce changes in a round if, for example, the 12-bit range is
5674
   full, but we later release some space by getting rid of TLS
5675
   descriptors in it.  We have to repeat the whole process because
5676
   we might have changed the size of a section processed before this
5677
   one.  */
5678
0
      *again = true;
5679
0
    }
5680
5681
0
  return true;
5682
0
}
5683
5684
/* Fill in code and data in dynamic sections.  */
5685
5686
static bool
5687
elf32_frv_finish_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
5688
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
5689
           bfd_byte *buf ATTRIBUTE_UNUSED)
5690
0
{
5691
  /* Nothing to be done for non-FDPIC.  */
5692
0
  return true;
5693
0
}
5694
5695
static bool
5696
elf32_frvfdpic_finish_dynamic_sections (bfd *output_bfd,
5697
          struct bfd_link_info *info,
5698
          bfd_byte *buf ATTRIBUTE_UNUSED)
5699
0
{
5700
0
  bfd *dynobj;
5701
0
  asection *sdyn;
5702
5703
0
  dynobj = elf_hash_table (info)->dynobj;
5704
5705
0
  if (frvfdpic_dynamic_got_plt_info (info))
5706
0
    {
5707
0
      BFD_ASSERT (frvfdpic_dynamic_got_plt_info (info)->tls_ret_refs == 0);
5708
0
    }
5709
0
  if (frvfdpic_got_section (info))
5710
0
    {
5711
0
      BFD_ASSERT (frvfdpic_gotrel_section (info)->size
5712
0
      == (frvfdpic_gotrel_section (info)->reloc_count
5713
0
          * sizeof (Elf32_External_Rel)));
5714
5715
0
      if (frvfdpic_gotfixup_section (info))
5716
0
  {
5717
0
    struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
5718
0
    bfd_vma got_value = hgot->root.u.def.value
5719
0
      + hgot->root.u.def.section->output_section->vma
5720
0
      + hgot->root.u.def.section->output_offset;
5721
0
    struct bfd_link_hash_entry *hend;
5722
5723
0
    _frvfdpic_add_rofixup (output_bfd, frvfdpic_gotfixup_section (info),
5724
0
         got_value, 0);
5725
5726
0
    if (frvfdpic_gotfixup_section (info)->size
5727
0
        != (frvfdpic_gotfixup_section (info)->reloc_count * 4))
5728
0
      {
5729
0
      error:
5730
0
        info->callbacks->einfo
5731
0
    ("LINKER BUG: .rofixup section size mismatch\n");
5732
0
        return false;
5733
0
      }
5734
5735
0
    hend = bfd_link_hash_lookup (info->hash, "__ROFIXUP_END__",
5736
0
               false, false, true);
5737
0
    if (hend
5738
0
        && (hend->type == bfd_link_hash_defined
5739
0
      || hend->type == bfd_link_hash_defweak)
5740
0
        && hend->u.def.section->output_section != NULL)
5741
0
      {
5742
0
        bfd_vma value =
5743
0
    frvfdpic_gotfixup_section (info)->output_section->vma
5744
0
    + frvfdpic_gotfixup_section (info)->output_offset
5745
0
    + frvfdpic_gotfixup_section (info)->size
5746
0
    - hend->u.def.section->output_section->vma
5747
0
    - hend->u.def.section->output_offset;
5748
0
        BFD_ASSERT (hend->u.def.value == value);
5749
0
        if (hend->u.def.value != value)
5750
0
    goto error;
5751
0
      }
5752
0
  }
5753
0
    }
5754
0
  if (frvfdpic_pltrel_section (info))
5755
0
    {
5756
0
      BFD_ASSERT (frvfdpic_pltrel_section (info)->size
5757
0
      == (frvfdpic_pltrel_section (info)->reloc_count
5758
0
          * sizeof (Elf32_External_Rel)));
5759
0
    }
5760
5761
5762
0
  if (elf_hash_table (info)->dynamic_sections_created)
5763
0
    {
5764
0
      Elf32_External_Dyn * dyncon;
5765
0
      Elf32_External_Dyn * dynconend;
5766
5767
0
      sdyn = bfd_get_linker_section (dynobj, ".dynamic");
5768
5769
0
      BFD_ASSERT (sdyn != NULL);
5770
5771
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
5772
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
5773
5774
0
      for (; dyncon < dynconend; dyncon++)
5775
0
  {
5776
0
    Elf_Internal_Dyn dyn;
5777
5778
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
5779
5780
0
    switch (dyn.d_tag)
5781
0
      {
5782
0
      default:
5783
0
        break;
5784
5785
0
      case DT_PLTGOT:
5786
0
        dyn.d_un.d_ptr = frvfdpic_got_section (info)->output_section->vma
5787
0
    + frvfdpic_got_section (info)->output_offset
5788
0
    + frvfdpic_got_initial_offset (info);
5789
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5790
0
        break;
5791
5792
0
      case DT_JMPREL:
5793
0
        dyn.d_un.d_ptr = frvfdpic_pltrel_section (info)
5794
0
    ->output_section->vma
5795
0
    + frvfdpic_pltrel_section (info)->output_offset;
5796
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5797
0
        break;
5798
5799
0
      case DT_PLTRELSZ:
5800
0
        dyn.d_un.d_val = frvfdpic_pltrel_section (info)->size;
5801
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
5802
0
        break;
5803
0
      }
5804
0
  }
5805
0
    }
5806
5807
0
  return true;
5808
0
}
5809
5810
/* Adjust a symbol defined by a dynamic object and referenced by a
5811
   regular object.  */
5812
5813
static bool
5814
elf32_frvfdpic_adjust_dynamic_symbol
5815
(struct bfd_link_info *info ATTRIBUTE_UNUSED,
5816
 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
5817
0
{
5818
0
  bfd * dynobj;
5819
5820
0
  dynobj = elf_hash_table (info)->dynobj;
5821
5822
  /* Make sure we know what is going on here.  */
5823
0
  BFD_ASSERT (dynobj != NULL
5824
0
        && (h->is_weakalias
5825
0
      || (h->def_dynamic
5826
0
          && h->ref_regular
5827
0
          && !h->def_regular)));
5828
5829
  /* If this is a weak symbol, and there is a real definition, the
5830
     processor independent code will have arranged for us to see the
5831
     real definition first, and we can just use the same value.  */
5832
0
  if (h->is_weakalias)
5833
0
    {
5834
0
      struct elf_link_hash_entry *def = weakdef (h);
5835
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
5836
0
      h->root.u.def.section = def->root.u.def.section;
5837
0
      h->root.u.def.value = def->root.u.def.value;
5838
0
      return true;
5839
0
    }
5840
5841
0
  return true;
5842
0
}
5843
5844
/* Perform any actions needed for dynamic symbols.  */
5845
5846
static bool
5847
elf32_frvfdpic_finish_dynamic_symbol
5848
(bfd *output_bfd ATTRIBUTE_UNUSED,
5849
 struct bfd_link_info *info ATTRIBUTE_UNUSED,
5850
 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
5851
 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
5852
0
{
5853
0
  return true;
5854
0
}
5855
5856
/* Decide whether to attempt to turn absptr or lsda encodings in
5857
   shared libraries into pcrel within the given input section.  */
5858
5859
static bool
5860
frvfdpic_elf_use_relative_eh_frame
5861
(bfd *input_bfd ATTRIBUTE_UNUSED,
5862
 struct bfd_link_info *info ATTRIBUTE_UNUSED,
5863
 asection *eh_frame_section ATTRIBUTE_UNUSED)
5864
0
{
5865
  /* We can't use PC-relative encodings in FDPIC binaries, in general.  */
5866
0
  return false;
5867
0
}
5868
5869
/* Adjust the contents of an eh_frame_hdr section before they're output.  */
5870
5871
static bfd_byte
5872
frvfdpic_elf_encode_eh_address (bfd *abfd,
5873
        struct bfd_link_info *info,
5874
        asection *osec, bfd_vma offset,
5875
        asection *loc_sec, bfd_vma loc_offset,
5876
        bfd_vma *encoded)
5877
0
{
5878
0
  struct elf_link_hash_entry *h;
5879
5880
0
  h = elf_hash_table (info)->hgot;
5881
0
  BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
5882
5883
0
  if (! h || (_frvfdpic_osec_to_segment (abfd, osec)
5884
0
        == _frvfdpic_osec_to_segment (abfd, loc_sec->output_section)))
5885
0
    return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
5886
0
               loc_sec, loc_offset, encoded);
5887
5888
0
  BFD_ASSERT (_frvfdpic_osec_to_segment (abfd, osec)
5889
0
        == (_frvfdpic_osec_to_segment
5890
0
      (abfd, h->root.u.def.section->output_section)));
5891
5892
0
  *encoded = osec->vma + offset
5893
0
    - (h->root.u.def.value
5894
0
       + h->root.u.def.section->output_section->vma
5895
0
       + h->root.u.def.section->output_offset);
5896
5897
0
  return DW_EH_PE_datarel | DW_EH_PE_sdata4;
5898
0
}
5899
5900
/* Look through the relocs for a section during the first phase.
5901
5902
   Besides handling virtual table relocs for gc, we have to deal with
5903
   all sorts of PIC-related relocations.  We describe below the
5904
   general plan on how to handle such relocations, even though we only
5905
   collect information at this point, storing them in hash tables for
5906
   perusal of later passes.
5907
5908
   32 relocations are propagated to the linker output when creating
5909
   position-independent output.  LO16 and HI16 relocations are not
5910
   supposed to be encountered in this case.
5911
5912
   LABEL16 should always be resolvable by the linker, since it's only
5913
   used by branches.
5914
5915
   LABEL24, on the other hand, is used by calls.  If it turns out that
5916
   the target of a call is a dynamic symbol, a PLT entry must be
5917
   created for it, which triggers the creation of a private function
5918
   descriptor and, unless lazy binding is disabled, a lazy PLT entry.
5919
5920
   GPREL relocations require the referenced symbol to be in the same
5921
   segment as _gp, but this can only be checked later.
5922
5923
   All GOT, GOTOFF and FUNCDESC relocations require a .got section to
5924
   exist.  LABEL24 might as well, since it may require a PLT entry,
5925
   that will require a got.
5926
5927
   Non-FUNCDESC GOT relocations require a GOT entry to be created
5928
   regardless of whether the symbol is dynamic.  However, since a
5929
   global symbol that turns out to not be exported may have the same
5930
   address of a non-dynamic symbol, we don't assign GOT entries at
5931
   this point, such that we can share them in this case.  A relocation
5932
   for the GOT entry always has to be created, be it to offset a
5933
   private symbol by the section load address, be it to get the symbol
5934
   resolved dynamically.
5935
5936
   FUNCDESC GOT relocations require a GOT entry to be created, and
5937
   handled as if a FUNCDESC relocation was applied to the GOT entry in
5938
   an object file.
5939
5940
   FUNCDESC relocations referencing a symbol that turns out to NOT be
5941
   dynamic cause a private function descriptor to be created.  The
5942
   FUNCDESC relocation then decays to a 32 relocation that points at
5943
   the private descriptor.  If the symbol is dynamic, the FUNCDESC
5944
   relocation is propagated to the linker output, such that the
5945
   dynamic linker creates the canonical descriptor, pointing to the
5946
   dynamically-resolved definition of the function.
5947
5948
   Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
5949
   symbols that are assigned to the same segment as the GOT, but we
5950
   can only check this later, after we know the complete set of
5951
   symbols defined and/or exported.
5952
5953
   FUNCDESC GOTOFF relocations require a function descriptor to be
5954
   created and, unless lazy binding is disabled or the symbol is not
5955
   dynamic, a lazy PLT entry.  Since we can't tell at this point
5956
   whether a symbol is going to be dynamic, we have to decide later
5957
   whether to create a lazy PLT entry or bind the descriptor directly
5958
   to the private function.
5959
5960
   FUNCDESC_VALUE relocations are not supposed to be present in object
5961
   files, but they may very well be simply propagated to the linker
5962
   output, since they have no side effect.
5963
5964
5965
   A function descriptor always requires a FUNCDESC_VALUE relocation.
5966
   Whether it's in .plt.rel or not depends on whether lazy binding is
5967
   enabled and on whether the referenced symbol is dynamic.
5968
5969
   The existence of a lazy PLT requires the resolverStub lazy PLT
5970
   entry to be present.
5971
5972
5973
   As for assignment of GOT, PLT and lazy PLT entries, and private
5974
   descriptors, we might do them all sequentially, but we can do
5975
   better than that.  For example, we can place GOT entries and
5976
   private function descriptors referenced using 12-bit operands
5977
   closer to the PIC register value, such that these relocations don't
5978
   overflow.  Those that are only referenced with LO16 relocations
5979
   could come next, but we may as well place PLT-required function
5980
   descriptors in the 12-bit range to make them shorter.  Symbols
5981
   referenced with LO16/HI16 may come next, but we may place
5982
   additional function descriptors in the 16-bit range if we can
5983
   reliably tell that we've already placed entries that are ever
5984
   referenced with only LO16.  PLT entries are therefore generated as
5985
   small as possible, while not introducing relocation overflows in
5986
   GOT or FUNCDESC_GOTOFF relocations.  Lazy PLT entries could be
5987
   generated before or after PLT entries, but not intermingled with
5988
   them, such that we can have more lazy PLT entries in range for a
5989
   branch to the resolverStub.  The resolverStub should be emitted at
5990
   the most distant location from the first lazy PLT entry such that
5991
   it's still in range for a branch, or closer, if there isn't a need
5992
   for so many lazy PLT entries.  Additional lazy PLT entries may be
5993
   emitted after the resolverStub, as long as branches are still in
5994
   range.  If the branch goes out of range, longer lazy PLT entries
5995
   are emitted.
5996
5997
   We could further optimize PLT and lazy PLT entries by giving them
5998
   priority in assignment to closer-to-gr17 locations depending on the
5999
   number of occurrences of references to them (assuming a function
6000
   that's called more often is more important for performance, so its
6001
   PLT entry should be faster), or taking hints from the compiler.
6002
   Given infinite time and money... :-)  */
6003
6004
static bool
6005
elf32_frv_check_relocs (bfd *abfd,
6006
      struct bfd_link_info *info,
6007
      asection *sec,
6008
      const Elf_Internal_Rela *relocs)
6009
0
{
6010
0
  Elf_Internal_Shdr *symtab_hdr;
6011
0
  struct elf_link_hash_entry **sym_hashes;
6012
0
  const Elf_Internal_Rela *rel;
6013
0
  const Elf_Internal_Rela *rel_end;
6014
0
  bfd *dynobj;
6015
0
  struct frvfdpic_relocs_info *picrel;
6016
6017
0
  if (bfd_link_relocatable (info))
6018
0
    return true;
6019
6020
0
  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
6021
0
  sym_hashes = elf_sym_hashes (abfd);
6022
6023
0
  dynobj = elf_hash_table (info)->dynobj;
6024
0
  rel_end = relocs + sec->reloc_count;
6025
0
  for (rel = relocs; rel < rel_end; rel++)
6026
0
    {
6027
0
      struct elf_link_hash_entry *h;
6028
0
      unsigned long r_symndx;
6029
6030
0
      r_symndx = ELF32_R_SYM (rel->r_info);
6031
0
      if (r_symndx < symtab_hdr->sh_info)
6032
0
  h = NULL;
6033
0
      else
6034
0
  {
6035
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
6036
0
    while (h->root.type == bfd_link_hash_indirect
6037
0
     || h->root.type == bfd_link_hash_warning)
6038
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
6039
0
  }
6040
6041
0
      switch (ELF32_R_TYPE (rel->r_info))
6042
0
  {
6043
0
  case R_FRV_GETTLSOFF:
6044
0
  case R_FRV_TLSDESC_VALUE:
6045
0
  case R_FRV_GOTTLSDESC12:
6046
0
  case R_FRV_GOTTLSDESCHI:
6047
0
  case R_FRV_GOTTLSDESCLO:
6048
0
  case R_FRV_GOTTLSOFF12:
6049
0
  case R_FRV_GOTTLSOFFHI:
6050
0
  case R_FRV_GOTTLSOFFLO:
6051
0
  case R_FRV_TLSOFF:
6052
0
  case R_FRV_GOT12:
6053
0
  case R_FRV_GOTHI:
6054
0
  case R_FRV_GOTLO:
6055
0
  case R_FRV_FUNCDESC_GOT12:
6056
0
  case R_FRV_FUNCDESC_GOTHI:
6057
0
  case R_FRV_FUNCDESC_GOTLO:
6058
0
  case R_FRV_GOTOFF12:
6059
0
  case R_FRV_GOTOFFHI:
6060
0
  case R_FRV_GOTOFFLO:
6061
0
  case R_FRV_FUNCDESC_GOTOFF12:
6062
0
  case R_FRV_FUNCDESC_GOTOFFHI:
6063
0
  case R_FRV_FUNCDESC_GOTOFFLO:
6064
0
  case R_FRV_FUNCDESC:
6065
0
  case R_FRV_FUNCDESC_VALUE:
6066
0
  case R_FRV_TLSMOFF12:
6067
0
  case R_FRV_TLSMOFFHI:
6068
0
  case R_FRV_TLSMOFFLO:
6069
0
  case R_FRV_TLSMOFF:
6070
0
    if (! IS_FDPIC (abfd))
6071
0
      goto bad_reloc;
6072
    /* Fall through.  */
6073
0
  case R_FRV_GPREL12:
6074
0
  case R_FRV_GPRELU12:
6075
0
  case R_FRV_GPRELHI:
6076
0
  case R_FRV_GPRELLO:
6077
0
  case R_FRV_LABEL24:
6078
0
  case R_FRV_32:
6079
0
    if (! dynobj)
6080
0
      {
6081
0
        elf_hash_table (info)->dynobj = dynobj = abfd;
6082
0
        if (! _frv_create_got_section (abfd, info))
6083
0
    return false;
6084
0
      }
6085
0
    if (! IS_FDPIC (abfd))
6086
0
      {
6087
0
        picrel = NULL;
6088
0
        break;
6089
0
      }
6090
0
    if (h != NULL)
6091
0
      {
6092
0
        if (h->dynindx == -1)
6093
0
    switch (ELF_ST_VISIBILITY (h->other))
6094
0
      {
6095
0
      case STV_INTERNAL:
6096
0
      case STV_HIDDEN:
6097
0
        break;
6098
0
      default:
6099
0
        bfd_elf_link_record_dynamic_symbol (info, h);
6100
0
        break;
6101
0
      }
6102
0
        picrel
6103
0
    = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
6104
0
               abfd, h,
6105
0
               rel->r_addend, INSERT);
6106
0
      }
6107
0
    else
6108
0
      picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info
6109
0
                 (info), abfd, r_symndx,
6110
0
                 rel->r_addend, INSERT);
6111
0
    if (! picrel)
6112
0
      return false;
6113
0
    break;
6114
6115
0
  default:
6116
0
    picrel = NULL;
6117
0
    break;
6118
0
  }
6119
6120
0
      switch (ELF32_R_TYPE (rel->r_info))
6121
0
  {
6122
0
  case R_FRV_LABEL24:
6123
0
    if (IS_FDPIC (abfd))
6124
0
      picrel->call = 1;
6125
0
    break;
6126
6127
0
  case R_FRV_FUNCDESC_VALUE:
6128
0
    picrel->relocsfdv++;
6129
0
    if (bfd_section_flags (sec) & SEC_ALLOC)
6130
0
      picrel->relocs32--;
6131
    /* Fall through.  */
6132
6133
0
  case R_FRV_32:
6134
0
    if (! IS_FDPIC (abfd))
6135
0
      break;
6136
6137
0
    picrel->sym = 1;
6138
0
    if (bfd_section_flags (sec) & SEC_ALLOC)
6139
0
      picrel->relocs32++;
6140
0
    break;
6141
6142
0
  case R_FRV_GOT12:
6143
0
    picrel->got12 = 1;
6144
0
    break;
6145
6146
0
  case R_FRV_GOTHI:
6147
0
  case R_FRV_GOTLO:
6148
0
    picrel->gothilo = 1;
6149
0
    break;
6150
6151
0
  case R_FRV_FUNCDESC_GOT12:
6152
0
    picrel->fdgot12 = 1;
6153
0
    break;
6154
6155
0
  case R_FRV_FUNCDESC_GOTHI:
6156
0
  case R_FRV_FUNCDESC_GOTLO:
6157
0
    picrel->fdgothilo = 1;
6158
0
    break;
6159
6160
0
  case R_FRV_GOTOFF12:
6161
0
  case R_FRV_GOTOFFHI:
6162
0
  case R_FRV_GOTOFFLO:
6163
0
    picrel->gotoff = 1;
6164
0
    break;
6165
6166
0
  case R_FRV_FUNCDESC_GOTOFF12:
6167
0
    picrel->fdgoff12 = 1;
6168
0
    break;
6169
6170
0
  case R_FRV_FUNCDESC_GOTOFFHI:
6171
0
  case R_FRV_FUNCDESC_GOTOFFLO:
6172
0
    picrel->fdgoffhilo = 1;
6173
0
    break;
6174
6175
0
  case R_FRV_FUNCDESC:
6176
0
    picrel->fd = 1;
6177
0
    picrel->relocsfd++;
6178
0
    break;
6179
6180
0
  case R_FRV_GETTLSOFF:
6181
0
    picrel->tlsplt = 1;
6182
0
    break;
6183
6184
0
  case R_FRV_TLSDESC_VALUE:
6185
0
    picrel->relocstlsd++;
6186
0
    goto bad_reloc;
6187
6188
0
  case R_FRV_GOTTLSDESC12:
6189
0
    picrel->tlsdesc12 = 1;
6190
0
    break;
6191
6192
0
  case R_FRV_GOTTLSDESCHI:
6193
0
  case R_FRV_GOTTLSDESCLO:
6194
0
    picrel->tlsdeschilo = 1;
6195
0
    break;
6196
6197
0
  case R_FRV_TLSMOFF12:
6198
0
  case R_FRV_TLSMOFFHI:
6199
0
  case R_FRV_TLSMOFFLO:
6200
0
  case R_FRV_TLSMOFF:
6201
0
    break;
6202
6203
0
  case R_FRV_GOTTLSOFF12:
6204
0
    picrel->tlsoff12 = 1;
6205
0
    info->flags |= DF_STATIC_TLS;
6206
0
    break;
6207
6208
0
  case R_FRV_GOTTLSOFFHI:
6209
0
  case R_FRV_GOTTLSOFFLO:
6210
0
    picrel->tlsoffhilo = 1;
6211
0
    info->flags |= DF_STATIC_TLS;
6212
0
    break;
6213
6214
0
  case R_FRV_TLSOFF:
6215
0
    picrel->relocstlsoff++;
6216
0
    info->flags |= DF_STATIC_TLS;
6217
0
    goto bad_reloc;
6218
6219
  /* This relocation describes the C++ object vtable hierarchy.
6220
     Reconstruct it for later use during GC.  */
6221
0
  case R_FRV_GNU_VTINHERIT:
6222
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
6223
0
      return false;
6224
0
    break;
6225
6226
  /* This relocation describes which C++ vtable entries are actually
6227
     used.  Record for later use during GC.  */
6228
0
  case R_FRV_GNU_VTENTRY:
6229
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
6230
0
      return false;
6231
0
    break;
6232
6233
0
  case R_FRV_LABEL16:
6234
0
  case R_FRV_LO16:
6235
0
  case R_FRV_HI16:
6236
0
  case R_FRV_GPREL12:
6237
0
  case R_FRV_GPRELU12:
6238
0
  case R_FRV_GPREL32:
6239
0
  case R_FRV_GPRELHI:
6240
0
  case R_FRV_GPRELLO:
6241
0
  case R_FRV_TLSDESC_RELAX:
6242
0
  case R_FRV_GETTLSOFF_RELAX:
6243
0
  case R_FRV_TLSOFF_RELAX:
6244
0
    break;
6245
6246
0
  default:
6247
0
  bad_reloc:
6248
    /* xgettext:c-format */
6249
0
    _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
6250
0
            abfd, (unsigned int) ELF32_R_TYPE (rel->r_info));
6251
0
    return false;
6252
0
  }
6253
0
    }
6254
6255
0
  return true;
6256
0
}
6257
6258

6259
/* Return the machine subcode from the ELF e_flags header.  */
6260
6261
static int
6262
elf32_frv_machine (bfd *abfd)
6263
3
{
6264
3
  switch (elf_elfheader (abfd)->e_flags & EF_FRV_CPU_MASK)
6265
3
    {
6266
0
    default:        break;
6267
0
    case EF_FRV_CPU_FR550:  return bfd_mach_fr550;
6268
0
    case EF_FRV_CPU_FR500:  return bfd_mach_fr500;
6269
0
    case EF_FRV_CPU_FR450:  return bfd_mach_fr450;
6270
0
    case EF_FRV_CPU_FR405:  return bfd_mach_fr400;
6271
0
    case EF_FRV_CPU_FR400:  return bfd_mach_fr400;
6272
0
    case EF_FRV_CPU_FR300:  return bfd_mach_fr300;
6273
0
    case EF_FRV_CPU_SIMPLE: return bfd_mach_frvsimple;
6274
3
    case EF_FRV_CPU_TOMCAT: return bfd_mach_frvtomcat;
6275
3
    }
6276
6277
0
  return bfd_mach_frv;
6278
3
}
6279
6280
/* Set the right machine number for a FRV ELF file.  */
6281
6282
static bool
6283
elf32_frv_object_p (bfd *abfd)
6284
3
{
6285
3
  bfd_default_set_arch_mach (abfd, bfd_arch_frv, elf32_frv_machine (abfd));
6286
3
  return (((elf_elfheader (abfd)->e_flags & EF_FRV_FDPIC) != 0)
6287
3
    == (IS_FDPIC (abfd)));
6288
3
}
6289

6290
/* Function to set the ELF flag bits.  */
6291
6292
static bool
6293
frv_elf_set_private_flags (bfd *abfd, flagword flags)
6294
0
{
6295
0
  elf_elfheader (abfd)->e_flags = flags;
6296
0
  elf_flags_init (abfd) = true;
6297
0
  return true;
6298
0
}
6299
6300
/* Return true if the architecture described by elf header flag
6301
   EXTENSION is an extension of the architecture described by BASE.  */
6302
6303
static bool
6304
frv_elf_arch_extension_p (flagword base, flagword extension)
6305
0
{
6306
0
  if (base == extension)
6307
0
    return true;
6308
6309
  /* CPU_GENERIC code can be merged with code for a specific
6310
     architecture, in which case the result is marked as being
6311
     for the specific architecture.  Everything is therefore
6312
     an extension of CPU_GENERIC.  */
6313
0
  if (base == EF_FRV_CPU_GENERIC)
6314
0
    return true;
6315
6316
0
  if (extension == EF_FRV_CPU_FR450)
6317
0
    if (base == EF_FRV_CPU_FR400 || base == EF_FRV_CPU_FR405)
6318
0
      return true;
6319
6320
0
  if (extension == EF_FRV_CPU_FR405)
6321
0
    if (base == EF_FRV_CPU_FR400)
6322
0
      return true;
6323
6324
0
  return false;
6325
0
}
6326
6327
/* Merge backend specific data from an object file to the output
6328
   object file when linking.  */
6329
6330
static bool
6331
frv_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
6332
0
{
6333
0
  bfd *obfd = info->output_bfd;
6334
0
  flagword old_flags, old_partial;
6335
0
  flagword new_flags, new_partial;
6336
0
  bool error = false;
6337
0
  char new_opt[80];
6338
0
  char old_opt[80];
6339
6340
  /* FIXME: What should be checked when linking shared libraries?  */
6341
0
  if ((ibfd->flags & DYNAMIC) != 0)
6342
0
    return true;
6343
6344
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
6345
0
    return true;
6346
6347
0
  new_opt[0] = old_opt[0] = '\0';
6348
0
  new_flags = elf_elfheader (ibfd)->e_flags;
6349
0
  old_flags = elf_elfheader (obfd)->e_flags;
6350
6351
0
  if (new_flags & EF_FRV_FDPIC)
6352
0
    new_flags &= ~EF_FRV_PIC;
6353
6354
#ifdef DEBUG
6355
  _bfd_error_handler
6356
    ("old_flags = 0x%.8x, new_flags = 0x%.8x, init = %s, filename = %s",
6357
     old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
6358
     bfd_get_filename (ibfd));
6359
#endif
6360
6361
0
  if (!elf_flags_init (obfd))      /* First call, no flags set.  */
6362
0
    {
6363
0
      elf_flags_init (obfd) = true;
6364
0
      old_flags = new_flags;
6365
0
    }
6366
6367
0
  else if (new_flags == old_flags)   /* Compatible flags are ok.  */
6368
0
    ;
6369
6370
0
  else            /* Possibly incompatible flags.  */
6371
0
    {
6372
      /* Warn if different # of gprs are used.  Note, 0 means nothing is
6373
   said about the size of gprs.  */
6374
0
      new_partial = (new_flags & EF_FRV_GPR_MASK);
6375
0
      old_partial = (old_flags & EF_FRV_GPR_MASK);
6376
0
      if (new_partial == old_partial)
6377
0
  ;
6378
6379
0
      else if (new_partial == 0)
6380
0
  ;
6381
6382
0
      else if (old_partial == 0)
6383
0
  old_flags |= new_partial;
6384
6385
0
      else
6386
0
  {
6387
0
    switch (new_partial)
6388
0
      {
6389
0
      default:    strcat (new_opt, " -mgpr-??"); break;
6390
0
      case EF_FRV_GPR_32: strcat (new_opt, " -mgpr-32"); break;
6391
0
      case EF_FRV_GPR_64: strcat (new_opt, " -mgpr-64"); break;
6392
0
      }
6393
6394
0
    switch (old_partial)
6395
0
      {
6396
0
      default:    strcat (old_opt, " -mgpr-??"); break;
6397
0
      case EF_FRV_GPR_32: strcat (old_opt, " -mgpr-32"); break;
6398
0
      case EF_FRV_GPR_64: strcat (old_opt, " -mgpr-64"); break;
6399
0
      }
6400
0
  }
6401
6402
      /* Warn if different # of fprs are used.  Note, 0 means nothing is
6403
   said about the size of fprs.  */
6404
0
      new_partial = (new_flags & EF_FRV_FPR_MASK);
6405
0
      old_partial = (old_flags & EF_FRV_FPR_MASK);
6406
0
      if (new_partial == old_partial)
6407
0
  ;
6408
6409
0
      else if (new_partial == 0)
6410
0
  ;
6411
6412
0
      else if (old_partial == 0)
6413
0
  old_flags |= new_partial;
6414
6415
0
      else
6416
0
  {
6417
0
    switch (new_partial)
6418
0
      {
6419
0
      default:      strcat (new_opt, " -mfpr-?");      break;
6420
0
      case EF_FRV_FPR_32:   strcat (new_opt, " -mfpr-32");     break;
6421
0
      case EF_FRV_FPR_64:   strcat (new_opt, " -mfpr-64");     break;
6422
0
      case EF_FRV_FPR_NONE: strcat (new_opt, " -msoft-float"); break;
6423
0
      }
6424
6425
0
    switch (old_partial)
6426
0
      {
6427
0
      default:      strcat (old_opt, " -mfpr-?");      break;
6428
0
      case EF_FRV_FPR_32:   strcat (old_opt, " -mfpr-32");     break;
6429
0
      case EF_FRV_FPR_64:   strcat (old_opt, " -mfpr-64");     break;
6430
0
      case EF_FRV_FPR_NONE: strcat (old_opt, " -msoft-float"); break;
6431
0
      }
6432
0
  }
6433
6434
      /* Warn if different dword support was used.  Note, 0 means nothing is
6435
   said about the dword support.  */
6436
0
      new_partial = (new_flags & EF_FRV_DWORD_MASK);
6437
0
      old_partial = (old_flags & EF_FRV_DWORD_MASK);
6438
0
      if (new_partial == old_partial)
6439
0
  ;
6440
6441
0
      else if (new_partial == 0)
6442
0
  ;
6443
6444
0
      else if (old_partial == 0)
6445
0
  old_flags |= new_partial;
6446
6447
0
      else
6448
0
  {
6449
0
    switch (new_partial)
6450
0
      {
6451
0
      default:       strcat (new_opt, " -mdword-?");  break;
6452
0
      case EF_FRV_DWORD_YES: strcat (new_opt, " -mdword");    break;
6453
0
      case EF_FRV_DWORD_NO:  strcat (new_opt, " -mno-dword"); break;
6454
0
      }
6455
6456
0
    switch (old_partial)
6457
0
      {
6458
0
      default:       strcat (old_opt, " -mdword-?");  break;
6459
0
      case EF_FRV_DWORD_YES: strcat (old_opt, " -mdword");    break;
6460
0
      case EF_FRV_DWORD_NO:  strcat (old_opt, " -mno-dword"); break;
6461
0
      }
6462
0
  }
6463
6464
      /* Or in flags that accumulate (ie, if one module uses it, mark that the
6465
   feature is used.  */
6466
0
      old_flags |= new_flags & (EF_FRV_DOUBLE
6467
0
        | EF_FRV_MEDIA
6468
0
        | EF_FRV_MULADD
6469
0
        | EF_FRV_NON_PIC_RELOCS);
6470
6471
      /* If any module was compiled without -G0, clear the G0 bit.  */
6472
0
      old_flags = ((old_flags & ~ EF_FRV_G0)
6473
0
       | (old_flags & new_flags & EF_FRV_G0));
6474
6475
      /* If any module was compiled without -mnopack, clear the mnopack bit.  */
6476
0
      old_flags = ((old_flags & ~ EF_FRV_NOPACK)
6477
0
       | (old_flags & new_flags & EF_FRV_NOPACK));
6478
6479
      /* We don't have to do anything if the pic flags are the same, or the new
6480
   module(s) were compiled with -mlibrary-pic.  */
6481
0
      new_partial = (new_flags & EF_FRV_PIC_FLAGS);
6482
0
      old_partial = (old_flags & EF_FRV_PIC_FLAGS);
6483
0
      if ((new_partial == old_partial) || ((new_partial & EF_FRV_LIBPIC) != 0))
6484
0
  ;
6485
6486
      /* If the old module(s) were compiled with -mlibrary-pic, copy in the pic
6487
   flags if any from the new module.  */
6488
0
      else if ((old_partial & EF_FRV_LIBPIC) != 0)
6489
0
  old_flags = (old_flags & ~ EF_FRV_PIC_FLAGS) | new_partial;
6490
6491
      /* If we have mixtures of -fpic and -fPIC, or in both bits.  */
6492
0
      else if (new_partial != 0 && old_partial != 0)
6493
0
  old_flags |= new_partial;
6494
6495
      /* One module was compiled for pic and the other was not, see if we have
6496
   had any relocations that are not pic-safe.  */
6497
0
      else
6498
0
  {
6499
0
    if ((old_flags & EF_FRV_NON_PIC_RELOCS) == 0)
6500
0
      old_flags |= new_partial;
6501
0
    else
6502
0
      {
6503
0
        old_flags &= ~ EF_FRV_PIC_FLAGS;
6504
0
#ifndef FRV_NO_PIC_ERROR
6505
0
        error = true;
6506
0
        _bfd_error_handler
6507
    /* xgettext:c-format */
6508
0
    (_("%pB: compiled with %s and linked with modules"
6509
0
       " that use non-pic relocations"),
6510
0
     ibfd, (new_flags & EF_FRV_BIGPIC) ? "-fPIC" : "-fpic");
6511
0
#endif
6512
0
      }
6513
0
  }
6514
6515
      /* Warn if different cpu is used (allow a specific cpu to override
6516
   the generic cpu).  */
6517
0
      new_partial = (new_flags & EF_FRV_CPU_MASK);
6518
0
      old_partial = (old_flags & EF_FRV_CPU_MASK);
6519
0
      if (frv_elf_arch_extension_p (new_partial, old_partial))
6520
0
  ;
6521
6522
0
      else if (frv_elf_arch_extension_p (old_partial, new_partial))
6523
0
  old_flags = (old_flags & ~EF_FRV_CPU_MASK) | new_partial;
6524
6525
0
      else
6526
0
  {
6527
0
    switch (new_partial)
6528
0
      {
6529
0
      default:         strcat (new_opt, " -mcpu=?");      break;
6530
0
      case EF_FRV_CPU_GENERIC: strcat (new_opt, " -mcpu=frv");    break;
6531
0
      case EF_FRV_CPU_SIMPLE:  strcat (new_opt, " -mcpu=simple"); break;
6532
0
      case EF_FRV_CPU_FR550:   strcat (new_opt, " -mcpu=fr550");  break;
6533
0
      case EF_FRV_CPU_FR500:   strcat (new_opt, " -mcpu=fr500");  break;
6534
0
      case EF_FRV_CPU_FR450:   strcat (new_opt, " -mcpu=fr450");  break;
6535
0
      case EF_FRV_CPU_FR405:   strcat (new_opt, " -mcpu=fr405");  break;
6536
0
      case EF_FRV_CPU_FR400:   strcat (new_opt, " -mcpu=fr400");  break;
6537
0
      case EF_FRV_CPU_FR300:   strcat (new_opt, " -mcpu=fr300");  break;
6538
0
      case EF_FRV_CPU_TOMCAT:  strcat (new_opt, " -mcpu=tomcat"); break;
6539
0
      }
6540
6541
0
    switch (old_partial)
6542
0
      {
6543
0
      default:         strcat (old_opt, " -mcpu=?");      break;
6544
0
      case EF_FRV_CPU_GENERIC: strcat (old_opt, " -mcpu=frv");    break;
6545
0
      case EF_FRV_CPU_SIMPLE:  strcat (old_opt, " -mcpu=simple"); break;
6546
0
      case EF_FRV_CPU_FR550:   strcat (old_opt, " -mcpu=fr550");  break;
6547
0
      case EF_FRV_CPU_FR500:   strcat (old_opt, " -mcpu=fr500");  break;
6548
0
      case EF_FRV_CPU_FR450:   strcat (old_opt, " -mcpu=fr450");  break;
6549
0
      case EF_FRV_CPU_FR405:   strcat (old_opt, " -mcpu=fr405");  break;
6550
0
      case EF_FRV_CPU_FR400:   strcat (old_opt, " -mcpu=fr400");  break;
6551
0
      case EF_FRV_CPU_FR300:   strcat (old_opt, " -mcpu=fr300");  break;
6552
0
      case EF_FRV_CPU_TOMCAT:  strcat (old_opt, " -mcpu=tomcat"); break;
6553
0
      }
6554
0
  }
6555
6556
      /* Print out any mismatches from above.  */
6557
0
      if (new_opt[0])
6558
0
  {
6559
0
    error = true;
6560
0
    _bfd_error_handler
6561
      /* xgettext:c-format */
6562
0
      (_("%pB: compiled with %s and linked with modules compiled with %s"),
6563
0
       ibfd, new_opt, old_opt);
6564
0
  }
6565
6566
      /* Warn about any other mismatches */
6567
0
      new_partial = (new_flags & ~ EF_FRV_ALL_FLAGS);
6568
0
      old_partial = (old_flags & ~ EF_FRV_ALL_FLAGS);
6569
0
      if (new_partial != old_partial)
6570
0
  {
6571
0
    old_flags |= new_partial;
6572
0
    error = true;
6573
0
    _bfd_error_handler
6574
      /* xgettext:c-format */
6575
0
      (_("%pB: uses different unknown e_flags (%#x) fields"
6576
0
         " than previous modules (%#x)"),
6577
0
       ibfd, new_partial, old_partial);
6578
0
  }
6579
0
    }
6580
6581
  /* If the cpu is -mcpu=simple, then set the -mnopack bit.  */
6582
0
  if ((old_flags & EF_FRV_CPU_MASK) == EF_FRV_CPU_SIMPLE)
6583
0
    old_flags |= EF_FRV_NOPACK;
6584
6585
  /* Update the old flags now with changes made above.  */
6586
0
  old_partial = elf_elfheader (obfd)->e_flags & EF_FRV_CPU_MASK;
6587
0
  elf_elfheader (obfd)->e_flags = old_flags;
6588
0
  if (old_partial != (old_flags & EF_FRV_CPU_MASK))
6589
0
    bfd_default_set_arch_mach (obfd, bfd_arch_frv, elf32_frv_machine (obfd));
6590
6591
0
  if (((new_flags & EF_FRV_FDPIC) == 0)
6592
0
      != (! IS_FDPIC (ibfd)))
6593
0
    {
6594
0
      error = true;
6595
0
      if (IS_FDPIC (obfd))
6596
0
  _bfd_error_handler
6597
0
    (_("%pB: cannot link non-fdpic object file into fdpic executable"),
6598
0
     ibfd);
6599
0
      else
6600
0
  _bfd_error_handler
6601
0
    (_("%pB: cannot link fdpic object file into non-fdpic executable"),
6602
0
     ibfd);
6603
0
    }
6604
6605
0
  if (error)
6606
0
    bfd_set_error (bfd_error_bad_value);
6607
6608
0
  return !error;
6609
0
}
6610
6611

6612
static bool
6613
frv_elf_print_private_bfd_data (bfd *abfd, void * ptr)
6614
0
{
6615
0
  FILE *file = (FILE *) ptr;
6616
0
  flagword flags;
6617
6618
0
  BFD_ASSERT (abfd != NULL && ptr != NULL);
6619
6620
  /* Print normal ELF private data.  */
6621
0
  _bfd_elf_print_private_bfd_data (abfd, ptr);
6622
6623
0
  flags = elf_elfheader (abfd)->e_flags;
6624
0
  fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
6625
6626
0
  switch (flags & EF_FRV_CPU_MASK)
6627
0
    {
6628
0
    default:              break;
6629
0
    case EF_FRV_CPU_SIMPLE: fprintf (file, " -mcpu=simple");  break;
6630
0
    case EF_FRV_CPU_FR550:  fprintf (file, " -mcpu=fr550");  break;
6631
0
    case EF_FRV_CPU_FR500:  fprintf (file, " -mcpu=fr500");  break;
6632
0
    case EF_FRV_CPU_FR450:  fprintf (file, " -mcpu=fr450");  break;
6633
0
    case EF_FRV_CPU_FR405:  fprintf (file, " -mcpu=fr405");  break;
6634
0
    case EF_FRV_CPU_FR400:  fprintf (file, " -mcpu=fr400");  break;
6635
0
    case EF_FRV_CPU_FR300:  fprintf (file, " -mcpu=fr300");  break;
6636
0
    case EF_FRV_CPU_TOMCAT: fprintf (file, " -mcpu=tomcat");  break;
6637
0
    }
6638
6639
0
  switch (flags & EF_FRV_GPR_MASK)
6640
0
    {
6641
0
    default:              break;
6642
0
    case EF_FRV_GPR_32: fprintf (file, " -mgpr-32");    break;
6643
0
    case EF_FRV_GPR_64: fprintf (file, " -mgpr-64");    break;
6644
0
    }
6645
6646
0
  switch (flags & EF_FRV_FPR_MASK)
6647
0
    {
6648
0
    default:              break;
6649
0
    case EF_FRV_FPR_32:   fprintf (file, " -mfpr-32");    break;
6650
0
    case EF_FRV_FPR_64:   fprintf (file, " -mfpr-64");    break;
6651
0
    case EF_FRV_FPR_NONE: fprintf (file, " -msoft-float");  break;
6652
0
    }
6653
6654
0
  switch (flags & EF_FRV_DWORD_MASK)
6655
0
    {
6656
0
    default:              break;
6657
0
    case EF_FRV_DWORD_YES: fprintf (file, " -mdword");   break;
6658
0
    case EF_FRV_DWORD_NO:  fprintf (file, " -mno-dword"); break;
6659
0
    }
6660
6661
0
  if (flags & EF_FRV_DOUBLE)
6662
0
    fprintf (file, " -mdouble");
6663
6664
0
  if (flags & EF_FRV_MEDIA)
6665
0
    fprintf (file, " -mmedia");
6666
6667
0
  if (flags & EF_FRV_MULADD)
6668
0
    fprintf (file, " -mmuladd");
6669
6670
0
  if (flags & EF_FRV_PIC)
6671
0
    fprintf (file, " -fpic");
6672
6673
0
  if (flags & EF_FRV_BIGPIC)
6674
0
    fprintf (file, " -fPIC");
6675
6676
0
  if (flags & EF_FRV_LIBPIC)
6677
0
    fprintf (file, " -mlibrary-pic");
6678
6679
0
  if (flags & EF_FRV_FDPIC)
6680
0
    fprintf (file, " -mfdpic");
6681
6682
0
  if (flags & EF_FRV_NON_PIC_RELOCS)
6683
0
    fprintf (file, " non-pic relocations");
6684
6685
0
  if (flags & EF_FRV_G0)
6686
0
    fprintf (file, " -G0");
6687
6688
0
  fputc ('\n', file);
6689
0
  return true;
6690
0
}
6691
6692

6693
/* Support for core dump NOTE sections.  */
6694
6695
static bool
6696
elf32_frv_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6697
0
{
6698
0
  int offset;
6699
0
  unsigned int raw_size;
6700
6701
0
  switch (note->descsz)
6702
0
    {
6703
0
      default:
6704
0
  return false;
6705
6706
      /* The Linux/FRV elf_prstatus struct is 268 bytes long.  The other
6707
   hardcoded offsets and sizes listed below (and contained within
6708
   this lexical block) refer to fields in the target's elf_prstatus
6709
   struct.  */
6710
0
      case 268:
6711
  /* `pr_cursig' is at offset 12.  */
6712
0
  elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
6713
6714
  /* `pr_pid' is at offset 24.  */
6715
0
  elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
6716
6717
  /* `pr_reg' is at offset 72.  */
6718
0
  offset = 72;
6719
6720
  /* Most grok_prstatus implementations set `raw_size' to the size
6721
     of the pr_reg field.  For Linux/FRV, we set `raw_size' to be
6722
     the size of `pr_reg' plus the size of `pr_exec_fdpic_loadmap'
6723
     and `pr_interp_fdpic_loadmap', both of which (by design)
6724
     immediately follow `pr_reg'.  This will allow these fields to
6725
     be viewed by GDB as registers.
6726
6727
     `pr_reg' is 184 bytes long.  `pr_exec_fdpic_loadmap' and
6728
     `pr_interp_fdpic_loadmap' are 4 bytes each.  */
6729
0
  raw_size = 184 + 4 + 4;
6730
6731
0
  break;
6732
0
    }
6733
6734
  /* Make a ".reg/999" section.  */
6735
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg", raw_size,
6736
0
            note->descpos + offset);
6737
0
}
6738
6739
static bool
6740
elf32_frv_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6741
0
{
6742
0
  switch (note->descsz)
6743
0
    {
6744
0
      default:
6745
0
  return false;
6746
6747
      /* The Linux/FRV elf_prpsinfo struct is 124 bytes long.  */
6748
0
      case 124:
6749
6750
  /* `pr_fname' is found at offset 28 and is 16 bytes long.  */
6751
0
  elf_tdata (abfd)->core->program
6752
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
6753
6754
  /* `pr_psargs' is found at offset 44 and is 80 bytes long.  */
6755
0
  elf_tdata (abfd)->core->command
6756
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
6757
0
    }
6758
6759
  /* Note that for some reason, a spurious space is tacked
6760
     onto the end of the args in some (at least one anyway)
6761
     implementations, so strip it off if it exists.  */
6762
6763
0
  {
6764
0
    char *command = elf_tdata (abfd)->core->command;
6765
0
    int n = strlen (command);
6766
6767
0
    if (0 < n && command[n - 1] == ' ')
6768
0
      command[n - 1] = '\0';
6769
0
  }
6770
6771
  return true;
6772
0
}
6773
#define ELF_ARCH    bfd_arch_frv
6774
#define ELF_MACHINE_CODE  EM_CYGNUS_FRV
6775
#define ELF_MAXPAGESIZE   0x1000
6776
6777
#define TARGET_BIG_SYM    frv_elf32_vec
6778
#define TARGET_BIG_NAME   "elf32-frv"
6779
6780
#define elf_info_to_howto     frv_info_to_howto_rela
6781
#define elf_backend_relocate_section    elf32_frv_relocate_section
6782
#define elf_backend_gc_mark_hook    elf32_frv_gc_mark_hook
6783
#define elf_backend_check_relocs    elf32_frv_check_relocs
6784
#define elf_backend_object_p      elf32_frv_object_p
6785
#define elf_backend_add_symbol_hook   elf32_frv_add_symbol_hook
6786
6787
#define elf_backend_stack_align     8
6788
#define elf_backend_can_gc_sections   1
6789
#define elf_backend_rela_normal     1
6790
6791
#define bfd_elf32_bfd_reloc_type_lookup   frv_reloc_type_lookup
6792
#define bfd_elf32_bfd_reloc_name_lookup frv_reloc_name_lookup
6793
#define bfd_elf32_bfd_set_private_flags   frv_elf_set_private_flags
6794
#define bfd_elf32_bfd_merge_private_bfd_data  frv_elf_merge_private_bfd_data
6795
#define bfd_elf32_bfd_print_private_bfd_data  frv_elf_print_private_bfd_data
6796
6797
#define elf_backend_want_got_sym  1
6798
#define elf_backend_got_header_size 0
6799
#define elf_backend_want_got_plt  0
6800
#define elf_backend_plt_readonly  1
6801
#define elf_backend_want_plt_sym  0
6802
#define elf_backend_plt_header_size 0
6803
6804
#define elf_backend_finish_dynamic_sections \
6805
    elf32_frv_finish_dynamic_sections
6806
6807
#define elf_backend_grok_prstatus elf32_frv_grok_prstatus
6808
#define elf_backend_grok_psinfo   elf32_frv_grok_psinfo
6809
6810
#define elf_backend_linux_prpsinfo32_ugid16 true
6811
6812
#include "elf32-target.h"
6813
6814
#undef ELF_TARGET_ID
6815
#define ELF_TARGET_ID   FRV_ELF_DATA
6816
#undef ELF_MAXPAGESIZE
6817
#define ELF_MAXPAGESIZE   0x4000
6818
6819
#undef TARGET_BIG_SYM
6820
#define TARGET_BIG_SYM    frv_elf32_fdpic_vec
6821
#undef TARGET_BIG_NAME
6822
#define TARGET_BIG_NAME   "elf32-frvfdpic"
6823
#undef  elf32_bed
6824
#define elf32_bed   elf32_frvfdpic_bed
6825
6826
#undef elf_info_to_howto_rel
6827
#define elf_info_to_howto_rel frvfdpic_info_to_howto_rel
6828
6829
#undef bfd_elf32_bfd_link_hash_table_create
6830
#define bfd_elf32_bfd_link_hash_table_create \
6831
    frvfdpic_elf_link_hash_table_create
6832
#undef elf_backend_early_size_sections
6833
#define elf_backend_early_size_sections \
6834
    elf32_frvfdpic_early_size_sections
6835
6836
#undef elf_backend_create_dynamic_sections
6837
#define elf_backend_create_dynamic_sections \
6838
    elf32_frvfdpic_create_dynamic_sections
6839
#undef elf_backend_adjust_dynamic_symbol
6840
#define elf_backend_adjust_dynamic_symbol \
6841
    elf32_frvfdpic_adjust_dynamic_symbol
6842
#undef elf_backend_late_size_sections
6843
#define elf_backend_late_size_sections \
6844
    elf32_frvfdpic_late_size_sections
6845
#undef bfd_elf32_bfd_relax_section
6846
#define bfd_elf32_bfd_relax_section \
6847
  elf32_frvfdpic_relax_section
6848
#undef elf_backend_finish_dynamic_symbol
6849
#define elf_backend_finish_dynamic_symbol \
6850
    elf32_frvfdpic_finish_dynamic_symbol
6851
#undef elf_backend_finish_dynamic_sections
6852
#define elf_backend_finish_dynamic_sections \
6853
    elf32_frvfdpic_finish_dynamic_sections
6854
6855
#undef elf_backend_discard_info
6856
#define elf_backend_discard_info \
6857
    frvfdpic_elf_discard_info
6858
#undef elf_backend_can_make_relative_eh_frame
6859
#define elf_backend_can_make_relative_eh_frame \
6860
    frvfdpic_elf_use_relative_eh_frame
6861
#undef elf_backend_can_make_lsda_relative_eh_frame
6862
#define elf_backend_can_make_lsda_relative_eh_frame \
6863
    frvfdpic_elf_use_relative_eh_frame
6864
#undef elf_backend_encode_eh_address
6865
#define elf_backend_encode_eh_address \
6866
    frvfdpic_elf_encode_eh_address
6867
6868
#undef elf_backend_may_use_rel_p
6869
#define elf_backend_may_use_rel_p 1
6870
#undef elf_backend_may_use_rela_p
6871
#define elf_backend_may_use_rela_p  1
6872
/* We use REL for dynamic relocations only.  */
6873
#undef elf_backend_default_use_rela_p
6874
#define elf_backend_default_use_rela_p  1
6875
6876
#undef elf_backend_omit_section_dynsym
6877
#define elf_backend_omit_section_dynsym _frvfdpic_link_omit_section_dynsym
6878
6879
#include "elf32-target.h"