Coverage Report

Created: 2026-05-11 07:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-bfin.c
Line
Count
Source
1
/* ADI Blackfin BFD support for 32-bit ELF.
2
   Copyright (C) 2005-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/bfin.h"
26
#include "dwarf2.h"
27
#include "hashtab.h"
28
#include "elf32-bfin.h"
29
30
/* FUNCTION : bfin_pltpc_reloc
31
   ABSTRACT : TODO : figure out how to handle pltpc relocs.  */
32
static bfd_reloc_status_type
33
bfin_pltpc_reloc (
34
     bfd *abfd ATTRIBUTE_UNUSED,
35
     arelent *reloc_entry ATTRIBUTE_UNUSED,
36
     asymbol *symbol ATTRIBUTE_UNUSED,
37
     void * data ATTRIBUTE_UNUSED,
38
     asection *input_section ATTRIBUTE_UNUSED,
39
     bfd *output_bfd ATTRIBUTE_UNUSED,
40
     char **error_message ATTRIBUTE_UNUSED)
41
0
{
42
0
  bfd_reloc_status_type flag = bfd_reloc_ok;
43
0
  return flag;
44
0
}
45

46
47
static bfd_reloc_status_type
48
bfin_pcrel24_reloc (bfd *abfd,
49
        arelent *reloc_entry,
50
        asymbol *symbol,
51
        void * data,
52
        asection *input_section,
53
        bfd *output_bfd,
54
        char **error_message ATTRIBUTE_UNUSED)
55
4
{
56
4
  bfd_vma relocation;
57
4
  bfd_size_type addr = reloc_entry->address;
58
4
  bfd_vma output_base = 0;
59
4
  reloc_howto_type *howto = reloc_entry->howto;
60
4
  asection *output_section;
61
4
  bool relocatable = (output_bfd != NULL);
62
63
4
  if (!bfd_reloc_offset_in_range (howto, abfd, input_section, addr - 2))
64
3
    return bfd_reloc_outofrange;
65
66
1
  if (bfd_is_und_section (symbol->section)
67
0
      && (symbol->flags & BSF_WEAK) == 0
68
0
      && !relocatable)
69
0
    return bfd_reloc_undefined;
70
71
1
  if (bfd_is_com_section (symbol->section))
72
0
    relocation = 0;
73
1
  else
74
1
    relocation = symbol->value;
75
76
1
  output_section = symbol->section->output_section;
77
78
1
  if (relocatable)
79
0
    output_base = 0;
80
1
  else
81
1
    output_base = output_section->vma;
82
83
1
  if (!relocatable || !strcmp (symbol->name, symbol->section->name))
84
1
    relocation += output_base + symbol->section->output_offset;
85
86
1
  if (!relocatable && !strcmp (symbol->name, symbol->section->name))
87
1
    relocation += reloc_entry->addend;
88
89
1
  relocation -= input_section->output_section->vma + input_section->output_offset;
90
1
  relocation -= reloc_entry->address;
91
92
1
  if (howto->complain_on_overflow != complain_overflow_dont)
93
1
    {
94
1
      bfd_reloc_status_type status;
95
1
      status = bfd_check_overflow (howto->complain_on_overflow,
96
1
           howto->bitsize,
97
1
           howto->rightshift,
98
1
           bfd_arch_bits_per_address(abfd),
99
1
           relocation);
100
1
      if (status != bfd_reloc_ok)
101
0
  return status;
102
1
    }
103
104
  /* if rightshift is 1 and the number odd, return error.  */
105
1
  if (howto->rightshift && (relocation & 0x01))
106
1
    {
107
1
      _bfd_error_handler (_("relocation should be even number"));
108
1
      return bfd_reloc_overflow;
109
1
    }
110
111
0
  relocation >>= (bfd_vma) howto->rightshift;
112
  /* Shift everything up to where it's going to be used.  */
113
114
0
  relocation <<= (bfd_vma) howto->bitpos;
115
116
0
  if (relocatable)
117
0
    {
118
0
      reloc_entry->address += input_section->output_offset;
119
0
      reloc_entry->addend += symbol->section->output_offset;
120
0
    }
121
122
0
  {
123
0
    short x;
124
125
    /* We are getting reloc_entry->address 2 byte off from
126
       the start of instruction. Assuming absolute postion
127
       of the reloc data. But, following code had been written assuming
128
       reloc address is starting at begining of instruction.
129
       To compensate that I have increased the value of
130
       relocation by 1 (effectively 2) and used the addr -2 instead of addr.  */
131
132
0
    relocation += 1;
133
0
    x = bfd_get_16 (abfd, (bfd_byte *) data + addr - 2);
134
0
    x = (x & 0xff00) | ((relocation >> 16) & 0xff);
135
0
    bfd_put_16 (abfd, x, (unsigned char *) data + addr - 2);
136
137
0
    x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
138
0
    x = relocation & 0xFFFF;
139
0
    bfd_put_16 (abfd, x, (unsigned char *) data + addr );
140
0
  }
141
0
  return bfd_reloc_ok;
142
1
}
143
144
static bfd_reloc_status_type
145
bfin_imm16_reloc (bfd *abfd,
146
      arelent *reloc_entry,
147
      asymbol *symbol,
148
      void * data,
149
      asection *input_section,
150
      bfd *output_bfd,
151
      char **error_message ATTRIBUTE_UNUSED)
152
4
{
153
4
  bfd_vma relocation, x;
154
4
  bfd_size_type reloc_addr = reloc_entry->address;
155
4
  bfd_vma output_base = 0;
156
4
  reloc_howto_type *howto = reloc_entry->howto;
157
4
  asection *output_section;
158
4
  bool relocatable = (output_bfd != NULL);
159
160
  /* Is the address of the relocation really within the section?  */
161
4
  if (!bfd_reloc_offset_in_range (howto, abfd, input_section, reloc_addr))
162
1
    return bfd_reloc_outofrange;
163
164
3
  if (bfd_is_und_section (symbol->section)
165
0
      && (symbol->flags & BSF_WEAK) == 0
166
0
      && !relocatable)
167
0
    return bfd_reloc_undefined;
168
169
3
  output_section = symbol->section->output_section;
170
3
  relocation = symbol->value;
171
172
  /* Convert input-section-relative symbol value to absolute.  */
173
3
  if (relocatable)
174
0
    output_base = 0;
175
3
  else
176
3
    output_base = output_section->vma;
177
178
3
  if (!relocatable || !strcmp (symbol->name, symbol->section->name))
179
3
    relocation += output_base + symbol->section->output_offset;
180
181
  /* Add in supplied addend.  */
182
3
  relocation += reloc_entry->addend;
183
184
3
  if (relocatable)
185
0
    {
186
0
      reloc_entry->address += input_section->output_offset;
187
0
      reloc_entry->addend += symbol->section->output_offset;
188
0
    }
189
3
  else
190
3
    {
191
3
      reloc_entry->addend = 0;
192
3
    }
193
194
3
  if (howto->complain_on_overflow != complain_overflow_dont)
195
3
    {
196
3
      bfd_reloc_status_type flag;
197
3
      flag = bfd_check_overflow (howto->complain_on_overflow,
198
3
         howto->bitsize,
199
3
         howto->rightshift,
200
3
         bfd_arch_bits_per_address(abfd),
201
3
         relocation);
202
3
      if (flag != bfd_reloc_ok)
203
1
  return flag;
204
3
    }
205
206
  /* Here the variable relocation holds the final address of the
207
     symbol we are relocating against, plus any addend.  */
208
209
2
  relocation >>= (bfd_vma) howto->rightshift;
210
2
  x = relocation;
211
2
  bfd_put_16 (abfd, x, (unsigned char *) data + reloc_addr);
212
2
  return bfd_reloc_ok;
213
3
}
214
215
216
static bfd_reloc_status_type
217
bfin_byte4_reloc (bfd *abfd,
218
      arelent *reloc_entry,
219
      asymbol *symbol,
220
      void * data,
221
      asection *input_section,
222
      bfd *output_bfd,
223
      char **error_message ATTRIBUTE_UNUSED)
224
0
{
225
0
  bfd_vma relocation, x;
226
0
  bfd_size_type addr = reloc_entry->address;
227
0
  bfd_vma output_base = 0;
228
0
  asection *output_section;
229
0
  bool relocatable = (output_bfd != NULL);
230
231
  /* Is the address of the relocation really within the section?  */
232
0
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd, input_section,
233
0
          addr))
234
0
    return bfd_reloc_outofrange;
235
236
0
  if (bfd_is_und_section (symbol->section)
237
0
      && (symbol->flags & BSF_WEAK) == 0
238
0
      && !relocatable)
239
0
    return bfd_reloc_undefined;
240
241
0
  output_section = symbol->section->output_section;
242
0
  relocation = symbol->value;
243
  /* Convert input-section-relative symbol value to absolute.  */
244
0
  if (relocatable)
245
0
    output_base = 0;
246
0
  else
247
0
    output_base = output_section->vma;
248
249
0
  if ((symbol->name
250
0
       && symbol->section->name
251
0
       && !strcmp (symbol->name, symbol->section->name))
252
0
      || !relocatable)
253
0
    {
254
0
      relocation += output_base + symbol->section->output_offset;
255
0
    }
256
257
0
  relocation += reloc_entry->addend;
258
259
0
  if (relocatable)
260
0
    {
261
      /* This output will be relocatable ... like ld -r. */
262
0
      reloc_entry->address += input_section->output_offset;
263
0
      reloc_entry->addend += symbol->section->output_offset;
264
0
    }
265
0
  else
266
0
    {
267
0
      reloc_entry->addend = 0;
268
0
    }
269
270
  /* Here the variable relocation holds the final address of the
271
     symbol we are relocating against, plus any addend.  */
272
0
  x = relocation & 0xFFFF0000;
273
0
  x >>=16;
274
0
  bfd_put_16 (abfd, x, (unsigned char *) data + addr + 2);
275
276
0
  x = relocation & 0x0000FFFF;
277
0
  bfd_put_16 (abfd, x, (unsigned char *) data + addr);
278
0
  return bfd_reloc_ok;
279
0
}
280
281
/* bfin_bfd_reloc handles the blackfin arithmetic relocations.
282
   Use this instead of bfd_perform_relocation.  */
283
static bfd_reloc_status_type
284
bfin_bfd_reloc (bfd *abfd,
285
    arelent *reloc_entry,
286
    asymbol *symbol,
287
    void * data,
288
    asection *input_section,
289
    bfd *output_bfd,
290
    char **error_message ATTRIBUTE_UNUSED)
291
4
{
292
4
  bfd_vma relocation;
293
4
  bfd_size_type addr = reloc_entry->address;
294
4
  bfd_vma output_base = 0;
295
4
  reloc_howto_type *howto = reloc_entry->howto;
296
4
  asection *output_section;
297
4
  bool relocatable = (output_bfd != NULL);
298
299
  /* Is the address of the relocation really within the section?  */
300
4
  if (!bfd_reloc_offset_in_range (howto, abfd, input_section, addr))
301
2
    return bfd_reloc_outofrange;
302
303
2
  if (bfd_is_und_section (symbol->section)
304
0
      && (symbol->flags & BSF_WEAK) == 0
305
0
      && !relocatable)
306
0
    return bfd_reloc_undefined;
307
308
  /* Get symbol value.  (Common symbols are special.)  */
309
2
  if (bfd_is_com_section (symbol->section))
310
0
    relocation = 0;
311
2
  else
312
2
    relocation = symbol->value;
313
314
2
  output_section = symbol->section->output_section;
315
316
  /* Convert input-section-relative symbol value to absolute.  */
317
2
  if (relocatable)
318
0
    output_base = 0;
319
2
  else
320
2
    output_base = output_section->vma;
321
322
2
  if (!relocatable || !strcmp (symbol->name, symbol->section->name))
323
2
    relocation += output_base + symbol->section->output_offset;
324
325
2
  if (!relocatable && !strcmp (symbol->name, symbol->section->name))
326
1
    {
327
      /* Add in supplied addend.  */
328
1
      relocation += reloc_entry->addend;
329
1
    }
330
331
  /* Here the variable relocation holds the final address of the
332
     symbol we are relocating against, plus any addend.  */
333
334
2
  if (howto->pc_relative)
335
2
    {
336
2
      relocation -= input_section->output_section->vma + input_section->output_offset;
337
338
2
      if (howto->pcrel_offset)
339
2
  relocation -= reloc_entry->address;
340
2
    }
341
342
2
  if (relocatable)
343
0
    {
344
0
      reloc_entry->address += input_section->output_offset;
345
0
      reloc_entry->addend += symbol->section->output_offset;
346
0
    }
347
348
2
  if (howto->complain_on_overflow != complain_overflow_dont)
349
2
    {
350
2
      bfd_reloc_status_type status;
351
352
2
      status = bfd_check_overflow (howto->complain_on_overflow,
353
2
          howto->bitsize,
354
2
          howto->rightshift,
355
2
          bfd_arch_bits_per_address(abfd),
356
2
          relocation);
357
2
      if (status != bfd_reloc_ok)
358
1
  return status;
359
2
    }
360
361
  /* If rightshift is 1 and the number odd, return error.  */
362
1
  if (howto->rightshift && (relocation & 0x01))
363
0
    {
364
0
      _bfd_error_handler (_("relocation should be even number"));
365
0
      return bfd_reloc_overflow;
366
0
    }
367
368
1
  relocation >>= (bfd_vma) howto->rightshift;
369
370
  /* Shift everything up to where it's going to be used.  */
371
372
1
  relocation <<= (bfd_vma) howto->bitpos;
373
374
1
#define DOIT(x)               \
375
1
  x = ( (x & ~howto->dst_mask) | (relocation & howto->dst_mask))
376
377
  /* handle 8 and 16 bit relocations here. */
378
1
  switch (bfd_get_reloc_size (howto))
379
1
    {
380
0
    case 1:
381
0
      {
382
0
  char x = bfd_get_8 (abfd, (char *) data + addr);
383
0
  DOIT (x);
384
0
  bfd_put_8 (abfd, x, (unsigned char *) data + addr);
385
0
      }
386
0
      break;
387
388
1
    case 2:
389
1
      {
390
1
  unsigned short x = bfd_get_16 (abfd, (bfd_byte *) data + addr);
391
1
  DOIT (x);
392
1
  bfd_put_16 (abfd, (bfd_vma) x, (unsigned char *) data + addr);
393
1
      }
394
1
      break;
395
396
0
    default:
397
0
      return bfd_reloc_other;
398
1
    }
399
400
1
  return bfd_reloc_ok;
401
1
}
402
403
/* HOWTO Table for blackfin.
404
   Blackfin relocations are fairly complicated.
405
   Some of the salient features are
406
   a. Even numbered offsets. A number of (not all) relocations are
407
      even numbered. This means that the rightmost bit is not stored.
408
      Needs to right shift by 1 and check to see if value is not odd
409
   b. A relocation can be an expression. An expression takes on
410
      a variety of relocations arranged in a stack.
411
   As a result, we cannot use the standard generic function as special
412
   function. We will have our own, which is very similar to the standard
413
   generic function except that it understands how to get the value from
414
   the relocation stack. .  */
415
416
#define BFIN_RELOC_MIN 0
417
310
#define BFIN_RELOC_MAX 0x21
418
19
#define BFIN_GNUEXT_RELOC_MIN 0x40
419
8
#define BFIN_GNUEXT_RELOC_MAX 0x43
420
#define BFIN_ARELOC_MIN 0xE0
421
#define BFIN_ARELOC_MAX 0xF3
422
423
static reloc_howto_type bfin_howto_table [] =
424
{
425
  /* This reloc does nothing. .  */
426
  HOWTO (R_BFIN_UNUSED0,  /* type.  */
427
   0,     /* rightshift.  */
428
   0,     /* size.  */
429
   0,     /* bitsize.  */
430
   false,     /* pc_relative.  */
431
   0,     /* bitpos.  */
432
   complain_overflow_dont, /* complain_on_overflow.  */
433
   bfd_elf_generic_reloc, /* special_function.  */
434
   "R_BFIN_UNUSED0",  /* name.  */
435
   false,     /* partial_inplace.  */
436
   0,     /* src_mask.  */
437
   0,     /* dst_mask.  */
438
   false),    /* pcrel_offset.  */
439
440
  HOWTO (R_BFIN_PCREL5M2, /* type.  */
441
   1,     /* rightshift.  */
442
   2,     /* size.  */
443
   4,     /* bitsize.  */
444
   true,      /* pc_relative.  */
445
   0,     /* bitpos.  */
446
   complain_overflow_unsigned, /* complain_on_overflow.  */
447
   bfin_bfd_reloc,  /* special_function.  */
448
   "R_BFIN_PCREL5M2", /* name.  */
449
   false,     /* partial_inplace.  */
450
   0,     /* src_mask.  */
451
   0x0000000F,    /* dst_mask.  */
452
   false),    /* pcrel_offset.  */
453
454
  HOWTO (R_BFIN_UNUSED1,  /* type.  */
455
   0,     /* rightshift.  */
456
   0,     /* size.  */
457
   0,     /* bitsize.  */
458
   false,     /* pc_relative.  */
459
   0,     /* bitpos.  */
460
   complain_overflow_dont, /* complain_on_overflow.  */
461
   bfd_elf_generic_reloc, /* special_function.  */
462
   "R_BFIN_UNUSED1",  /* name.  */
463
   false,     /* partial_inplace.  */
464
   0,     /* src_mask.  */
465
   0,     /* dst_mask.  */
466
   false),    /* pcrel_offset.  */
467
468
  HOWTO (R_BFIN_PCREL10,  /* type.  */
469
   1,     /* rightshift.  */
470
   2,     /* size.  */
471
   10,      /* bitsize.  */
472
   true,      /* pc_relative.  */
473
   0,     /* bitpos.  */
474
   complain_overflow_signed, /* complain_on_overflow.  */
475
   bfin_bfd_reloc,  /* special_function.  */
476
   "R_BFIN_PCREL10",  /* name.  */
477
   false,     /* partial_inplace.  */
478
   0,     /* src_mask.  */
479
   0x000003FF,    /* dst_mask.  */
480
   true),     /* pcrel_offset.  */
481
482
  HOWTO (R_BFIN_PCREL12_JUMP, /* type.  */
483
   1,     /* rightshift.  */
484
        /* the offset is actually 13 bit
485
           aligned on a word boundary so
486
           only 12 bits have to be used.
487
           Right shift the rightmost bit..  */
488
   2,     /* size.  */
489
   12,      /* bitsize.  */
490
   true,      /* pc_relative.  */
491
   0,     /* bitpos.  */
492
   complain_overflow_signed, /* complain_on_overflow.  */
493
   bfin_bfd_reloc,  /* special_function.  */
494
   "R_BFIN_PCREL12_JUMP", /* name.  */
495
   false,     /* partial_inplace.  */
496
   0,     /* src_mask.  */
497
   0x0FFF,    /* dst_mask.  */
498
   true),     /* pcrel_offset.  */
499
500
  HOWTO (R_BFIN_RIMM16,   /* type.  */
501
   0,     /* rightshift.  */
502
   2,     /* size.  */
503
   16,      /* bitsize.  */
504
   false,     /* pc_relative.  */
505
   0,     /* bitpos.  */
506
   complain_overflow_signed, /* complain_on_overflow.  */
507
   bfin_imm16_reloc,  /* special_function.  */
508
   "R_BFIN_RIMM16", /* name.  */
509
   false,     /* partial_inplace.  */
510
   0,     /* src_mask.  */
511
   0x0000FFFF,    /* dst_mask.  */
512
   true),     /* pcrel_offset.  */
513
514
  HOWTO (R_BFIN_LUIMM16,  /* type.  */
515
   0,     /* rightshift.  */
516
   2,     /* size.  */
517
   16,      /* bitsize.  */
518
   false,     /* pc_relative.  */
519
   0,     /* bitpos.  */
520
   complain_overflow_dont, /* complain_on_overflow.  */
521
   bfin_imm16_reloc,  /* special_function.  */
522
   "R_BFIN_LUIMM16",  /* name.  */
523
   false,     /* partial_inplace.  */
524
   0,     /* src_mask.  */
525
   0x0000FFFF,    /* dst_mask.  */
526
   true),     /* pcrel_offset.  */
527
528
  HOWTO (R_BFIN_HUIMM16,  /* type.  */
529
   16,      /* rightshift.  */
530
   2,     /* size.  */
531
   16,      /* bitsize.  */
532
   false,     /* pc_relative.  */
533
   0,     /* bitpos.  */
534
   complain_overflow_unsigned, /* complain_on_overflow.  */
535
   bfin_imm16_reloc,  /* special_function.  */
536
   "R_BFIN_HUIMM16",  /* name.  */
537
   false,     /* partial_inplace.  */
538
   0,     /* src_mask.  */
539
   0x0000FFFF,    /* dst_mask.  */
540
   true),     /* pcrel_offset.  */
541
542
  HOWTO (R_BFIN_PCREL12_JUMP_S, /* type.  */
543
   1,     /* rightshift.  */
544
   2,     /* size.  */
545
   12,      /* bitsize.  */
546
   true,      /* pc_relative.  */
547
   0,     /* bitpos.  */
548
   complain_overflow_signed, /* complain_on_overflow.  */
549
   bfin_bfd_reloc,  /* special_function.  */
550
   "R_BFIN_PCREL12_JUMP_S", /* name.  */
551
   false,     /* partial_inplace.  */
552
   0,     /* src_mask.  */
553
   0x00000FFF,    /* dst_mask.  */
554
   true),     /* pcrel_offset.  */
555
556
  HOWTO (R_BFIN_PCREL24_JUMP_X, /* type.  */
557
   1,     /* rightshift.  */
558
   4,     /* size.  */
559
   24,      /* bitsize.  */
560
   true,      /* pc_relative.  */
561
   0,     /* bitpos.  */
562
   complain_overflow_signed, /* complain_on_overflow.  */
563
   bfin_pcrel24_reloc,  /* special_function.  */
564
  "R_BFIN_PCREL24_JUMP_X", /* name.  */
565
   false,     /* partial_inplace.  */
566
   0,     /* src_mask.  */
567
   0x00FFFFFF,    /* dst_mask.  */
568
   true),     /* pcrel_offset.  */
569
570
  HOWTO (R_BFIN_PCREL24,  /* type.  */
571
   1,     /* rightshift.  */
572
   4,     /* size.  */
573
   24,      /* bitsize.  */
574
   true,      /* pc_relative.  */
575
   0,     /* bitpos.  */
576
   complain_overflow_signed, /* complain_on_overflow.  */
577
   bfin_pcrel24_reloc,  /* special_function.  */
578
   "R_BFIN_PCREL24",  /* name.  */
579
   false,     /* partial_inplace.  */
580
   0,     /* src_mask.  */
581
   0x00FFFFFF,    /* dst_mask.  */
582
   true),     /* pcrel_offset.  */
583
584
  HOWTO (R_BFIN_UNUSEDB,  /* type.  */
585
   0,     /* rightshift.  */
586
   0,     /* size.  */
587
   0,     /* bitsize.  */
588
   false,     /* pc_relative.  */
589
   0,     /* bitpos.  */
590
   complain_overflow_dont, /* complain_on_overflow.  */
591
   bfd_elf_generic_reloc, /* special_function.  */
592
   "R_BFIN_UNUSEDB",  /* name.  */
593
   false,     /* partial_inplace.  */
594
   0,     /* src_mask.  */
595
   0,     /* dst_mask.  */
596
   false),    /* pcrel_offset.  */
597
598
  HOWTO (R_BFIN_UNUSEDC,  /* type.  */
599
   0,     /* rightshift.  */
600
   0,     /* size.  */
601
   0,     /* bitsize.  */
602
   false,     /* pc_relative.  */
603
   0,     /* bitpos.  */
604
   complain_overflow_dont, /* complain_on_overflow.  */
605
   bfd_elf_generic_reloc, /* special_function.  */
606
   "R_BFIN_UNUSEDC",  /* name.  */
607
   false,     /* partial_inplace.  */
608
   0,     /* src_mask.  */
609
   0,     /* dst_mask.  */
610
   false),    /* pcrel_offset.  */
611
612
  HOWTO (R_BFIN_PCREL24_JUMP_L, /* type.  */
613
   1,     /* rightshift.  */
614
   4,     /* size.  */
615
   24,      /* bitsize.  */
616
   true,      /* pc_relative.  */
617
   0,     /* bitpos.  */
618
   complain_overflow_signed, /* complain_on_overflow.  */
619
   bfin_pcrel24_reloc,  /* special_function.  */
620
   "R_BFIN_PCREL24_JUMP_L", /* name.  */
621
   false,     /* partial_inplace.  */
622
   0,     /* src_mask.  */
623
   0x00FFFFFF,    /* dst_mask.  */
624
   true),     /* pcrel_offset.  */
625
626
  HOWTO (R_BFIN_PCREL24_CALL_X, /* type.  */
627
   1,     /* rightshift.  */
628
   4,     /* size.  */
629
   24,      /* bitsize.  */
630
   true,      /* pc_relative.  */
631
   0,     /* bitpos.  */
632
   complain_overflow_signed, /* complain_on_overflow.  */
633
   bfin_pcrel24_reloc,  /* special_function.  */
634
   "R_BFIN_PCREL24_CALL_X", /* name.  */
635
   false,     /* partial_inplace.  */
636
   0,     /* src_mask.  */
637
   0x00FFFFFF,    /* dst_mask.  */
638
   true),     /* pcrel_offset.  */
639
640
  HOWTO (R_BFIN_VAR_EQ_SYMB,  /* type.  */
641
   0,     /* rightshift.  */
642
   4,     /* size.  */
643
   32,      /* bitsize.  */
644
   false,     /* pc_relative.  */
645
   0,     /* bitpos.  */
646
   complain_overflow_bitfield, /* complain_on_overflow.  */
647
   bfin_bfd_reloc,  /* special_function.  */
648
   "R_BFIN_VAR_EQ_SYMB",  /* name.  */
649
   false,     /* partial_inplace.  */
650
   0,     /* src_mask.  */
651
   0,     /* dst_mask.  */
652
   false),    /* pcrel_offset.  */
653
654
  HOWTO (R_BFIN_BYTE_DATA,  /* type.  */
655
   0,     /* rightshift.  */
656
   1,     /* size.  */
657
   8,     /* bitsize.  */
658
   false,     /* pc_relative.  */
659
   0,     /* bitpos.  */
660
   complain_overflow_unsigned, /* complain_on_overflow.  */
661
   bfin_bfd_reloc,  /* special_function.  */
662
   "R_BFIN_BYTE_DATA",  /* name.  */
663
   false,     /* partial_inplace.  */
664
   0,     /* src_mask.  */
665
   0xFF,      /* dst_mask.  */
666
   true),     /* pcrel_offset.  */
667
668
  HOWTO (R_BFIN_BYTE2_DATA, /* type.  */
669
   0,     /* rightshift.  */
670
   2,     /* size.  */
671
   16,      /* bitsize.  */
672
   false,     /* pc_relative.  */
673
   0,     /* bitpos.  */
674
   complain_overflow_signed, /* complain_on_overflow.  */
675
   bfin_bfd_reloc,  /* special_function.  */
676
   "R_BFIN_BYTE2_DATA", /* name.  */
677
   false,     /* partial_inplace.  */
678
   0,     /* src_mask.  */
679
   0xFFFF,    /* dst_mask.  */
680
   true),     /* pcrel_offset.  */
681
682
  HOWTO (R_BFIN_BYTE4_DATA, /* type.  */
683
   0,     /* rightshift.  */
684
   4,     /* size.  */
685
   32,      /* bitsize.  */
686
   false,     /* pc_relative.  */
687
   0,     /* bitpos.  */
688
   complain_overflow_unsigned, /* complain_on_overflow.  */
689
   bfin_byte4_reloc,  /* special_function.  */
690
   "R_BFIN_BYTE4_DATA", /* name.  */
691
   false,     /* partial_inplace.  */
692
   0,     /* src_mask.  */
693
   0xFFFFFFFF,    /* dst_mask.  */
694
   true),     /* pcrel_offset.  */
695
696
  HOWTO (R_BFIN_PCREL11,  /* type.  */
697
   1,     /* rightshift.  */
698
   2,     /* size.  */
699
   10,      /* bitsize.  */
700
   true,      /* pc_relative.  */
701
   0,     /* bitpos.  */
702
   complain_overflow_unsigned, /* complain_on_overflow.  */
703
   bfin_bfd_reloc,  /* special_function.  */
704
   "R_BFIN_PCREL11",  /* name.  */
705
   false,     /* partial_inplace.  */
706
   0,     /* src_mask.  */
707
   0x000003FF,    /* dst_mask.  */
708
   false),    /* pcrel_offset.  */
709
710
711
  /* A 18-bit signed operand with the GOT offset for the address of
712
     the symbol.  */
713
  HOWTO (R_BFIN_GOT17M4,  /* type */
714
   2,     /* rightshift */
715
   2,     /* size */
716
   16,      /* bitsize */
717
   false,     /* pc_relative */
718
   0,     /* bitpos */
719
   complain_overflow_signed, /* complain_on_overflow */
720
   bfd_elf_generic_reloc, /* special_function */
721
   "R_BFIN_GOT17M4",  /* name */
722
   false,     /* partial_inplace */
723
   0xffff,    /* src_mask */
724
   0xffff,    /* dst_mask */
725
   false),    /* pcrel_offset */
726
727
  /* The upper 16 bits of the GOT offset for the address of the
728
     symbol.  */
729
  HOWTO (R_BFIN_GOTHI,    /* type */
730
   0,     /* rightshift */
731
   2,     /* size */
732
   16,      /* bitsize */
733
   false,     /* pc_relative */
734
   0,     /* bitpos */
735
   complain_overflow_dont, /* complain_on_overflow */
736
   bfd_elf_generic_reloc, /* special_function */
737
   "R_BFIN_GOTHI",    /* name */
738
   false,     /* partial_inplace */
739
   0xffff,      /* src_mask */
740
   0xffff,    /* dst_mask */
741
   false),    /* pcrel_offset */
742
743
  /* The lower 16 bits of the GOT offset for the address of the
744
     symbol.  */
745
  HOWTO (R_BFIN_GOTLO,    /* type */
746
   0,     /* rightshift */
747
   2,     /* size */
748
   16,      /* bitsize */
749
   false,     /* pc_relative */
750
   0,     /* bitpos */
751
   complain_overflow_dont, /* complain_on_overflow */
752
   bfd_elf_generic_reloc, /* special_function */
753
   "R_BFIN_GOTLO",    /* name */
754
   false,     /* partial_inplace */
755
   0xffff,    /* src_mask */
756
   0xffff,    /* dst_mask */
757
   false),    /* pcrel_offset */
758
759
  /* The 32-bit address of the canonical descriptor of a function.  */
760
  HOWTO (R_BFIN_FUNCDESC, /* type */
761
   0,     /* rightshift */
762
   4,     /* size */
763
   32,      /* bitsize */
764
   false,     /* pc_relative */
765
   0,     /* bitpos */
766
   complain_overflow_bitfield, /* complain_on_overflow */
767
   bfd_elf_generic_reloc, /* special_function */
768
   "R_BFIN_FUNCDESC", /* name */
769
   false,     /* partial_inplace */
770
   0xffffffff,    /* src_mask */
771
   0xffffffff,    /* dst_mask */
772
   false),    /* pcrel_offset */
773
774
  /* A 12-bit signed operand with the GOT offset for the address of
775
     canonical descriptor of a function.  */
776
  HOWTO (R_BFIN_FUNCDESC_GOT17M4, /* type */
777
   2,     /* rightshift */
778
   2,     /* size */
779
   16,      /* bitsize */
780
   false,     /* pc_relative */
781
   0,     /* bitpos */
782
   complain_overflow_signed, /* complain_on_overflow */
783
   bfd_elf_generic_reloc, /* special_function */
784
   "R_BFIN_FUNCDESC_GOT17M4", /* name */
785
   false,     /* partial_inplace */
786
   0xffff,    /* src_mask */
787
   0xffff,    /* dst_mask */
788
   false),    /* pcrel_offset */
789
790
  /* The upper 16 bits of the GOT offset for the address of the
791
     canonical descriptor of a function.  */
792
  HOWTO (R_BFIN_FUNCDESC_GOTHI, /* type */
793
   0,     /* rightshift */
794
   2,     /* size */
795
   16,      /* bitsize */
796
   false,     /* pc_relative */
797
   0,     /* bitpos */
798
   complain_overflow_dont, /* complain_on_overflow */
799
   bfd_elf_generic_reloc, /* special_function */
800
   "R_BFIN_FUNCDESC_GOTHI", /* name */
801
   false,     /* partial_inplace */
802
   0xffff,    /* src_mask */
803
   0xffff,    /* dst_mask */
804
   false),    /* pcrel_offset */
805
806
  /* The lower 16 bits of the GOT offset for the address of the
807
     canonical descriptor of a function.  */
808
  HOWTO (R_BFIN_FUNCDESC_GOTLO, /* type */
809
   0,     /* rightshift */
810
   2,     /* size */
811
   16,      /* bitsize */
812
   false,     /* pc_relative */
813
   0,     /* bitpos */
814
   complain_overflow_dont, /* complain_on_overflow */
815
   bfd_elf_generic_reloc, /* special_function */
816
   "R_BFIN_FUNCDESC_GOTLO", /* name */
817
   false,     /* partial_inplace */
818
   0xffff,    /* src_mask */
819
   0xffff,    /* dst_mask */
820
   false),    /* pcrel_offset */
821
822
  /* The 32-bit address of the canonical descriptor of a function.  */
823
  HOWTO (R_BFIN_FUNCDESC_VALUE, /* type */
824
   0,     /* rightshift */
825
   4,     /* size */
826
   64,      /* bitsize */
827
   false,     /* pc_relative */
828
   0,     /* bitpos */
829
   complain_overflow_bitfield, /* complain_on_overflow */
830
   bfd_elf_generic_reloc, /* special_function */
831
   "R_BFIN_FUNCDESC_VALUE", /* name */
832
   false,     /* partial_inplace */
833
   0xffffffff,    /* src_mask */
834
   0xffffffff,    /* dst_mask */
835
   false),    /* pcrel_offset */
836
837
  /* A 12-bit signed operand with the GOT offset for the address of
838
     canonical descriptor of a function.  */
839
  HOWTO (R_BFIN_FUNCDESC_GOTOFF17M4, /* type */
840
   2,     /* rightshift */
841
   2,     /* size */
842
   16,      /* bitsize */
843
   false,     /* pc_relative */
844
   0,     /* bitpos */
845
   complain_overflow_signed, /* complain_on_overflow */
846
   bfd_elf_generic_reloc, /* special_function */
847
   "R_BFIN_FUNCDESC_GOTOFF17M4", /* name */
848
   false,     /* partial_inplace */
849
   0xffff,    /* src_mask */
850
   0xffff,    /* dst_mask */
851
   false),    /* pcrel_offset */
852
853
  /* The upper 16 bits of the GOT offset for the address of the
854
     canonical descriptor of a function.  */
855
  HOWTO (R_BFIN_FUNCDESC_GOTOFFHI, /* type */
856
   0,     /* rightshift */
857
   2,     /* size */
858
   16,      /* bitsize */
859
   false,     /* pc_relative */
860
   0,     /* bitpos */
861
   complain_overflow_dont, /* complain_on_overflow */
862
   bfd_elf_generic_reloc, /* special_function */
863
   "R_BFIN_FUNCDESC_GOTOFFHI", /* name */
864
   false,     /* partial_inplace */
865
   0xffff,    /* src_mask */
866
   0xffff,    /* dst_mask */
867
   false),    /* pcrel_offset */
868
869
  /* The lower 16 bits of the GOT offset for the address of the
870
     canonical descriptor of a function.  */
871
  HOWTO (R_BFIN_FUNCDESC_GOTOFFLO, /* type */
872
   0,     /* rightshift */
873
   2,     /* size */
874
   16,      /* bitsize */
875
   false,     /* pc_relative */
876
   0,     /* bitpos */
877
   complain_overflow_dont, /* complain_on_overflow */
878
   bfd_elf_generic_reloc, /* special_function */
879
   "R_BFIN_FUNCDESC_GOTOFFLO", /* name */
880
   false,     /* partial_inplace */
881
   0xffff,    /* src_mask */
882
   0xffff,    /* dst_mask */
883
   false),    /* pcrel_offset */
884
885
  /* A 12-bit signed operand with the GOT offset for the address of
886
     the symbol.  */
887
  HOWTO (R_BFIN_GOTOFF17M4, /* type */
888
   2,     /* rightshift */
889
   2,     /* size */
890
   16,      /* bitsize */
891
   false,     /* pc_relative */
892
   0,     /* bitpos */
893
   complain_overflow_signed, /* complain_on_overflow */
894
   bfd_elf_generic_reloc, /* special_function */
895
   "R_BFIN_GOTOFF17M4", /* name */
896
   false,     /* partial_inplace */
897
   0xffff,    /* src_mask */
898
   0xffff,    /* dst_mask */
899
   false),    /* pcrel_offset */
900
901
  /* The upper 16 bits of the GOT offset for the address of the
902
     symbol.  */
903
  HOWTO (R_BFIN_GOTOFFHI,  /* type */
904
   0,     /* rightshift */
905
   2,     /* size */
906
   16,      /* bitsize */
907
   false,     /* pc_relative */
908
   0,     /* bitpos */
909
   complain_overflow_dont, /* complain_on_overflow */
910
   bfd_elf_generic_reloc, /* special_function */
911
   "R_BFIN_GOTOFFHI", /* name */
912
   false,     /* partial_inplace */
913
   0xffff,    /* src_mask */
914
   0xffff,    /* dst_mask */
915
   false),    /* pcrel_offset */
916
917
  /* The lower 16 bits of the GOT offset for the address of the
918
     symbol.  */
919
  HOWTO (R_BFIN_GOTOFFLO, /* type */
920
   0,     /* rightshift */
921
   2,     /* size */
922
   16,      /* bitsize */
923
   false,     /* pc_relative */
924
   0,     /* bitpos */
925
   complain_overflow_dont, /* complain_on_overflow */
926
   bfd_elf_generic_reloc, /* special_function */
927
   "R_BFIN_GOTOFFLO", /* name */
928
   false,     /* partial_inplace */
929
   0xffff,    /* src_mask */
930
   0xffff,    /* dst_mask */
931
   false),    /* pcrel_offset */
932
};
933
934
static reloc_howto_type bfin_gnuext_howto_table [] =
935
{
936
  HOWTO (R_BFIN_PLTPC,    /* type.  */
937
   0,     /* rightshift.  */
938
   2,     /* size.  */
939
   16,      /* bitsize.  */
940
   false,     /* pc_relative.  */
941
   0,     /* bitpos.  */
942
   complain_overflow_bitfield, /* complain_on_overflow.  */
943
   bfin_pltpc_reloc,  /* special_function.  */
944
   "R_BFIN_PLTPC",  /* name.  */
945
   false,     /* partial_inplace.  */
946
   0xffff,    /* src_mask.  */
947
   0xffff,    /* dst_mask.  */
948
   false),    /* pcrel_offset.  */
949
950
  HOWTO (R_BFIN_GOT,    /* type.  */
951
   0,     /* rightshift.  */
952
   2,     /* size.  */
953
   16,      /* bitsize.  */
954
   false,     /* pc_relative.  */
955
   0,     /* bitpos.  */
956
   complain_overflow_bitfield, /* complain_on_overflow.  */
957
   bfd_elf_generic_reloc, /* special_function.  */
958
   "R_BFIN_GOT",    /* name.  */
959
   false,     /* partial_inplace.  */
960
   0x7fff,    /* src_mask.  */
961
   0x7fff,    /* dst_mask.  */
962
   false),    /* pcrel_offset.  */
963
964
/* GNU extension to record C++ vtable hierarchy.  */
965
  HOWTO (R_BFIN_GNU_VTINHERIT,  /* type.  */
966
   0,     /* rightshift.  */
967
   4,     /* size.  */
968
   0,     /* bitsize.  */
969
   false,     /* pc_relative.  */
970
   0,     /* bitpos.  */
971
   complain_overflow_dont, /* complain_on_overflow.  */
972
   NULL,      /* special_function.  */
973
   "R_BFIN_GNU_VTINHERIT", /* name.  */
974
   false,     /* partial_inplace.  */
975
   0,     /* src_mask.  */
976
   0,     /* dst_mask.  */
977
   false),    /* pcrel_offset.  */
978
979
/* GNU extension to record C++ vtable member usage.  */
980
  HOWTO (R_BFIN_GNU_VTENTRY,  /* type.  */
981
   0,     /* rightshift.  */
982
   4,     /* size.  */
983
   0,     /* bitsize.  */
984
   false,     /* pc_relative.  */
985
   0,     /* bitpos.  */
986
   complain_overflow_dont, /* complain_on_overflow.  */
987
   _bfd_elf_rel_vtable_reloc_fn, /* special_function.  */
988
   "R_BFIN_GNU_VTENTRY",  /* name.  */
989
   false,     /* partial_inplace.  */
990
   0,     /* src_mask.  */
991
   0,     /* dst_mask.  */
992
   false)     /* pcrel_offset.  */
993
};
994
995
struct bfin_reloc_map
996
{
997
  bfd_reloc_code_real_type  bfd_reloc_val;
998
  unsigned int      bfin_reloc_val;
999
};
1000
1001
static const struct bfin_reloc_map bfin_reloc_map [] =
1002
{
1003
  { BFD_RELOC_NONE,     R_BFIN_UNUSED0 },
1004
  { BFD_RELOC_BFIN_5_PCREL,   R_BFIN_PCREL5M2 },
1005
  { BFD_RELOC_NONE,     R_BFIN_UNUSED1 },
1006
  { BFD_RELOC_BFIN_10_PCREL,    R_BFIN_PCREL10 },
1007
  { BFD_RELOC_BFIN_12_PCREL_JUMP, R_BFIN_PCREL12_JUMP },
1008
  { BFD_RELOC_BFIN_16_IMM,    R_BFIN_RIMM16 },
1009
  { BFD_RELOC_BFIN_16_LOW,    R_BFIN_LUIMM16 },
1010
  { BFD_RELOC_BFIN_16_HIGH,   R_BFIN_HUIMM16 },
1011
  { BFD_RELOC_BFIN_12_PCREL_JUMP_S, R_BFIN_PCREL12_JUMP_S },
1012
  { BFD_RELOC_24_PCREL,     R_BFIN_PCREL24 },
1013
  { BFD_RELOC_24_PCREL,     R_BFIN_PCREL24 },
1014
  { BFD_RELOC_BFIN_24_PCREL_JUMP_L, R_BFIN_PCREL24_JUMP_L },
1015
  { BFD_RELOC_NONE,     R_BFIN_UNUSEDB },
1016
  { BFD_RELOC_NONE,     R_BFIN_UNUSEDC },
1017
  { BFD_RELOC_BFIN_24_PCREL_CALL_X, R_BFIN_PCREL24_CALL_X },
1018
  { BFD_RELOC_8,      R_BFIN_BYTE_DATA },
1019
  { BFD_RELOC_16,     R_BFIN_BYTE2_DATA },
1020
  { BFD_RELOC_32,     R_BFIN_BYTE4_DATA },
1021
  { BFD_RELOC_BFIN_11_PCREL,    R_BFIN_PCREL11 },
1022
  { BFD_RELOC_BFIN_GOT,     R_BFIN_GOT },
1023
  { BFD_RELOC_BFIN_PLTPC,   R_BFIN_PLTPC },
1024
1025
  { BFD_RELOC_BFIN_GOT17M4,      R_BFIN_GOT17M4 },
1026
  { BFD_RELOC_BFIN_GOTHI,      R_BFIN_GOTHI },
1027
  { BFD_RELOC_BFIN_GOTLO,      R_BFIN_GOTLO },
1028
  { BFD_RELOC_BFIN_FUNCDESC,   R_BFIN_FUNCDESC },
1029
  { BFD_RELOC_BFIN_FUNCDESC_GOT17M4, R_BFIN_FUNCDESC_GOT17M4 },
1030
  { BFD_RELOC_BFIN_FUNCDESC_GOTHI, R_BFIN_FUNCDESC_GOTHI },
1031
  { BFD_RELOC_BFIN_FUNCDESC_GOTLO, R_BFIN_FUNCDESC_GOTLO },
1032
  { BFD_RELOC_BFIN_FUNCDESC_VALUE, R_BFIN_FUNCDESC_VALUE },
1033
  { BFD_RELOC_BFIN_FUNCDESC_GOTOFF17M4, R_BFIN_FUNCDESC_GOTOFF17M4 },
1034
  { BFD_RELOC_BFIN_FUNCDESC_GOTOFFHI, R_BFIN_FUNCDESC_GOTOFFHI },
1035
  { BFD_RELOC_BFIN_FUNCDESC_GOTOFFLO, R_BFIN_FUNCDESC_GOTOFFLO },
1036
  { BFD_RELOC_BFIN_GOTOFF17M4,   R_BFIN_GOTOFF17M4 },
1037
  { BFD_RELOC_HI16_GOTOFF,    R_BFIN_GOTOFFHI },
1038
  { BFD_RELOC_LO16_GOTOFF,    R_BFIN_GOTOFFLO },
1039
1040
  { BFD_RELOC_VTABLE_INHERIT,   R_BFIN_GNU_VTINHERIT },
1041
  { BFD_RELOC_VTABLE_ENTRY,   R_BFIN_GNU_VTENTRY },
1042
};
1043
1044
1045
static bool
1046
bfin_info_to_howto (bfd *abfd,
1047
        arelent *cache_ptr,
1048
        Elf_Internal_Rela *dst)
1049
310
{
1050
310
  unsigned int r_type;
1051
1052
310
  r_type = ELF32_R_TYPE (dst->r_info);
1053
1054
310
  if (r_type <= BFIN_RELOC_MAX)
1055
301
    cache_ptr->howto = &bfin_howto_table [r_type];
1056
1057
9
  else if (r_type >= BFIN_GNUEXT_RELOC_MIN && r_type <= BFIN_GNUEXT_RELOC_MAX)
1058
1
    cache_ptr->howto = &bfin_gnuext_howto_table [r_type - BFIN_GNUEXT_RELOC_MIN];
1059
1060
8
  else
1061
8
    {
1062
      /* xgettext:c-format */
1063
8
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
1064
8
        abfd, r_type);
1065
8
      bfd_set_error (bfd_error_bad_value);
1066
8
      return false;
1067
8
    }
1068
1069
302
  return true;
1070
310
}
1071
1072
/* Given a BFD reloc type, return the howto.  */
1073
static reloc_howto_type *
1074
bfin_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
1075
          bfd_reloc_code_real_type code)
1076
0
{
1077
0
  unsigned int i;
1078
0
  unsigned int r_type = (unsigned int) -1;
1079
1080
0
  for (i = sizeof (bfin_reloc_map) / sizeof (bfin_reloc_map[0]); i--;)
1081
0
    if (bfin_reloc_map[i].bfd_reloc_val == code)
1082
0
      r_type = bfin_reloc_map[i].bfin_reloc_val;
1083
1084
0
  if (r_type <= BFIN_RELOC_MAX)
1085
0
    return &bfin_howto_table [r_type];
1086
1087
0
  else if (r_type >= BFIN_GNUEXT_RELOC_MIN && r_type <= BFIN_GNUEXT_RELOC_MAX)
1088
0
   return &bfin_gnuext_howto_table [r_type - BFIN_GNUEXT_RELOC_MIN];
1089
1090
0
  return (reloc_howto_type *) NULL;
1091
0
}
1092
1093
static reloc_howto_type *
1094
bfin_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
1095
          const char *r_name)
1096
0
{
1097
0
  unsigned int i;
1098
1099
0
  for (i = 0;
1100
0
       i < (sizeof (bfin_howto_table)
1101
0
      / sizeof (bfin_howto_table[0]));
1102
0
       i++)
1103
0
    if (bfin_howto_table[i].name != NULL
1104
0
  && strcasecmp (bfin_howto_table[i].name, r_name) == 0)
1105
0
      return &bfin_howto_table[i];
1106
1107
0
  for (i = 0;
1108
0
       i < (sizeof (bfin_gnuext_howto_table)
1109
0
      / sizeof (bfin_gnuext_howto_table[0]));
1110
0
       i++)
1111
0
    if (bfin_gnuext_howto_table[i].name != NULL
1112
0
  && strcasecmp (bfin_gnuext_howto_table[i].name, r_name) == 0)
1113
0
      return &bfin_gnuext_howto_table[i];
1114
1115
0
  return NULL;
1116
0
}
1117
1118
/* Given a bfin relocation type, return the howto.  */
1119
static reloc_howto_type *
1120
bfin_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
1121
      unsigned int r_type)
1122
0
{
1123
0
  if (r_type <= BFIN_RELOC_MAX)
1124
0
    return &bfin_howto_table [r_type];
1125
1126
0
  else if (r_type >= BFIN_GNUEXT_RELOC_MIN && r_type <= BFIN_GNUEXT_RELOC_MAX)
1127
0
   return &bfin_gnuext_howto_table [r_type - BFIN_GNUEXT_RELOC_MIN];
1128
1129
0
  return (reloc_howto_type *) NULL;
1130
0
}
1131
1132
/* Set by ld emulation if --code-in-l1.  */
1133
bool elf32_bfin_code_in_l1 = 0;
1134
1135
/* Set by ld emulation if --data-in-l1.  */
1136
bool elf32_bfin_data_in_l1 = 0;
1137
1138
static bool
1139
elf32_bfin_final_write_processing (bfd *abfd)
1140
0
{
1141
0
  if (elf32_bfin_code_in_l1)
1142
0
    elf_elfheader (abfd)->e_flags |= EF_BFIN_CODE_IN_L1;
1143
0
  if (elf32_bfin_data_in_l1)
1144
0
    elf_elfheader (abfd)->e_flags |= EF_BFIN_DATA_IN_L1;
1145
0
  return _bfd_elf_final_write_processing (abfd);
1146
0
}
1147
1148
/* Return TRUE if the name is a local label.
1149
   bfin local labels begin with L$.  */
1150
static bool
1151
bfin_is_local_label_name (bfd *abfd, const char *label)
1152
0
{
1153
0
  if (label[0] == 'L' && label[1] == '$' )
1154
0
    return true;
1155
1156
0
  return _bfd_elf_is_local_label_name (abfd, label);
1157
0
}
1158

1159
/* Look through the relocs for a section during the first phase, and
1160
   allocate space in the global offset table or procedure linkage
1161
   table.  */
1162
1163
static bool
1164
bfin_check_relocs (bfd * abfd,
1165
       struct bfd_link_info *info,
1166
       asection *sec,
1167
       const Elf_Internal_Rela *relocs)
1168
0
{
1169
0
  bfd *dynobj;
1170
0
  Elf_Internal_Shdr *symtab_hdr;
1171
0
  struct elf_link_hash_entry **sym_hashes;
1172
0
  bfd_signed_vma *local_got_refcounts;
1173
0
  const Elf_Internal_Rela *rel;
1174
0
  const Elf_Internal_Rela *rel_end;
1175
0
  asection *sgot;
1176
0
  asection *srelgot;
1177
1178
0
  if (bfd_link_relocatable (info))
1179
0
    return true;
1180
1181
0
  dynobj = elf_hash_table (info)->dynobj;
1182
0
  symtab_hdr = &elf_symtab_hdr (abfd);
1183
0
  sym_hashes = elf_sym_hashes (abfd);
1184
0
  local_got_refcounts = elf_local_got_refcounts (abfd);
1185
1186
0
  sgot = NULL;
1187
0
  srelgot = NULL;
1188
1189
0
  rel_end = relocs + sec->reloc_count;
1190
0
  for (rel = relocs; rel < rel_end; rel++)
1191
0
    {
1192
0
      unsigned long r_symndx;
1193
0
      struct elf_link_hash_entry *h;
1194
1195
0
      r_symndx = ELF32_R_SYM (rel->r_info);
1196
0
      if (r_symndx < symtab_hdr->sh_info)
1197
0
  h = NULL;
1198
0
      else
1199
0
  {
1200
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1201
0
    while (h->root.type == bfd_link_hash_indirect
1202
0
     || h->root.type == bfd_link_hash_warning)
1203
0
      h = (struct elf_link_hash_entry *)h->root.u.i.link;
1204
0
  }
1205
1206
0
      switch (ELF32_R_TYPE (rel->r_info))
1207
0
  {
1208
       /* This relocation describes the C++ object vtable hierarchy.
1209
     Reconstruct it for later use during GC.  */
1210
0
  case R_BFIN_GNU_VTINHERIT:
1211
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1212
0
      return false;
1213
0
    break;
1214
1215
  /* This relocation describes which C++ vtable entries
1216
     are actually used.  Record for later use during GC.  */
1217
0
  case R_BFIN_GNU_VTENTRY:
1218
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1219
0
      return false;
1220
0
    break;
1221
1222
0
  case R_BFIN_GOT:
1223
0
    if (h != NULL
1224
0
        && strcmp (h->root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1225
0
      break;
1226
    /* Fall through.  */
1227
1228
0
    if (dynobj == NULL)
1229
0
      {
1230
        /* Create the .got section.  */
1231
0
        elf_hash_table (info)->dynobj = dynobj = abfd;
1232
0
        if (!_bfd_elf_create_got_section (dynobj, info))
1233
0
    return false;
1234
0
      }
1235
1236
0
    sgot = elf_hash_table (info)->sgot;
1237
0
    srelgot = elf_hash_table (info)->srelgot;
1238
0
    BFD_ASSERT (sgot != NULL);
1239
1240
0
    if (h != NULL)
1241
0
      {
1242
0
        if (h->got.refcount == 0)
1243
0
    {
1244
      /* Make sure this symbol is output as a dynamic symbol.  */
1245
0
      if (h->dynindx == -1 && !h->forced_local)
1246
0
        {
1247
0
          if (!bfd_elf_link_record_dynamic_symbol (info, h))
1248
0
      return false;
1249
0
        }
1250
1251
      /* Allocate space in the .got section.  */
1252
0
      sgot->size += 4;
1253
      /* Allocate relocation space.  */
1254
0
      srelgot->size += sizeof (Elf32_External_Rela);
1255
0
    }
1256
0
        h->got.refcount++;
1257
0
      }
1258
0
    else
1259
0
      {
1260
        /* This is a global offset table entry for a local symbol.  */
1261
0
        if (local_got_refcounts == NULL)
1262
0
    {
1263
0
      bfd_size_type size;
1264
1265
0
      size = symtab_hdr->sh_info;
1266
0
      size *= sizeof (bfd_signed_vma);
1267
0
      local_got_refcounts = ((bfd_signed_vma *)
1268
0
           bfd_zalloc (abfd, size));
1269
0
      if (local_got_refcounts == NULL)
1270
0
        return false;
1271
0
      elf_local_got_refcounts (abfd) = local_got_refcounts;
1272
0
    }
1273
0
        if (local_got_refcounts[r_symndx] == 0)
1274
0
    {
1275
0
      sgot->size += 4;
1276
0
      if (bfd_link_pic (info))
1277
0
        {
1278
          /* If we are generating a shared object, we need to
1279
       output a R_68K_RELATIVE reloc so that the dynamic
1280
       linker can adjust this GOT entry.  */
1281
0
          srelgot->size += sizeof (Elf32_External_Rela);
1282
0
        }
1283
0
    }
1284
0
        local_got_refcounts[r_symndx]++;
1285
0
      }
1286
0
    break;
1287
1288
0
  default:
1289
0
    break;
1290
0
  }
1291
0
    }
1292
1293
0
  return true;
1294
0
}
1295
1296
static enum elf_reloc_type_class
1297
elf32_bfin_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
1298
           const asection *rel_sec ATTRIBUTE_UNUSED,
1299
           const Elf_Internal_Rela * rela)
1300
0
{
1301
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
1302
0
    {
1303
0
    default:
1304
0
      return reloc_class_normal;
1305
0
    }
1306
0
}
1307

1308
static bfd_reloc_status_type
1309
bfin_final_link_relocate (Elf_Internal_Rela *rel, reloc_howto_type *howto,
1310
        bfd *input_bfd, asection *input_section,
1311
        bfd_byte *contents, bfd_vma address,
1312
        bfd_vma value, bfd_vma addend)
1313
0
{
1314
0
  int r_type = ELF32_R_TYPE (rel->r_info);
1315
1316
0
  if (r_type == R_BFIN_PCREL24 || r_type == R_BFIN_PCREL24_JUMP_L)
1317
0
    {
1318
0
      bfd_reloc_status_type r = bfd_reloc_ok;
1319
0
      bfd_vma x;
1320
1321
0
      if (!bfd_reloc_offset_in_range (howto, input_bfd, input_section,
1322
0
              address - 2))
1323
0
    return bfd_reloc_outofrange;
1324
1325
0
      value += addend;
1326
1327
      /* Perform usual pc-relative correction.  */
1328
0
      value -= input_section->output_section->vma + input_section->output_offset;
1329
0
      value -= address;
1330
1331
      /* We are getting reloc_entry->address 2 byte off from
1332
   the start of instruction. Assuming absolute postion
1333
   of the reloc data. But, following code had been written assuming
1334
   reloc address is starting at begining of instruction.
1335
   To compensate that I have increased the value of
1336
   relocation by 1 (effectively 2) and used the addr -2 instead of addr.  */
1337
1338
0
      value += 2;
1339
0
      address -= 2;
1340
1341
0
      if ((value & 0xFF000000) != 0
1342
0
    && (value & 0xFF000000) != 0xFF000000)
1343
0
  r = bfd_reloc_overflow;
1344
1345
0
      value >>= 1;
1346
1347
0
      x = bfd_get_16 (input_bfd, contents + address);
1348
0
      x = (x & 0xff00) | ((value >> 16) & 0xff);
1349
0
      bfd_put_16 (input_bfd, x, contents + address);
1350
1351
0
      x = bfd_get_16 (input_bfd, contents + address + 2);
1352
0
      x = value & 0xFFFF;
1353
0
      bfd_put_16 (input_bfd, x, contents + address + 2);
1354
0
      return r;
1355
0
    }
1356
1357
0
  return _bfd_final_link_relocate (howto, input_bfd, input_section, contents,
1358
0
           rel->r_offset, value, addend);
1359
1360
0
}
1361
1362
static int
1363
bfin_relocate_section (bfd * output_bfd,
1364
           struct bfd_link_info *info,
1365
           bfd * input_bfd,
1366
           asection * input_section,
1367
           bfd_byte * contents,
1368
           Elf_Internal_Rela * relocs,
1369
           Elf_Internal_Sym * local_syms,
1370
           asection ** local_sections)
1371
0
{
1372
0
  Elf_Internal_Shdr *symtab_hdr;
1373
0
  struct elf_link_hash_entry **sym_hashes;
1374
0
  bfd_vma *local_got_offsets;
1375
0
  asection *sgot;
1376
0
  Elf_Internal_Rela *rel;
1377
0
  Elf_Internal_Rela *relend;
1378
1379
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
1380
0
  sym_hashes = elf_sym_hashes (input_bfd);
1381
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
1382
1383
0
  sgot = NULL;
1384
1385
0
  rel = relocs;
1386
0
  relend = relocs + input_section->reloc_count;
1387
0
  for (; rel < relend; rel++)
1388
0
    {
1389
0
      int r_type;
1390
0
      reloc_howto_type *howto;
1391
0
      unsigned long r_symndx;
1392
0
      struct elf_link_hash_entry *h;
1393
0
      Elf_Internal_Sym *sym;
1394
0
      asection *sec;
1395
0
      bfd_vma relocation = 0;
1396
0
      bool unresolved_reloc;
1397
0
      bfd_reloc_status_type r;
1398
0
      bfd_vma address;
1399
1400
0
      r_type = ELF32_R_TYPE (rel->r_info);
1401
0
      if (r_type < 0 || r_type >= 243)
1402
0
  {
1403
0
    bfd_set_error (bfd_error_bad_value);
1404
0
    return false;
1405
0
  }
1406
1407
0
      if (r_type == R_BFIN_GNU_VTENTRY
1408
0
    || r_type == R_BFIN_GNU_VTINHERIT)
1409
0
  continue;
1410
1411
0
      howto = bfin_reloc_type_lookup (input_bfd, r_type);
1412
0
      if (howto == NULL)
1413
0
  {
1414
0
    bfd_set_error (bfd_error_bad_value);
1415
0
    return false;
1416
0
  }
1417
0
      r_symndx = ELF32_R_SYM (rel->r_info);
1418
1419
0
      h = NULL;
1420
0
      sym = NULL;
1421
0
      sec = NULL;
1422
0
      unresolved_reloc = false;
1423
1424
0
      if (r_symndx < symtab_hdr->sh_info)
1425
0
  {
1426
0
    sym = local_syms + r_symndx;
1427
0
    sec = local_sections[r_symndx];
1428
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1429
0
  }
1430
0
      else
1431
0
  {
1432
0
    bool warned, ignored;
1433
1434
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1435
0
           r_symndx, symtab_hdr, sym_hashes,
1436
0
           h, sec, relocation,
1437
0
           unresolved_reloc, warned, ignored);
1438
0
  }
1439
1440
0
      if (sec != NULL && discarded_section (sec))
1441
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1442
0
           rel, 1, relend, R_BFIN_UNUSED0,
1443
0
           howto, 0, contents);
1444
1445
0
      if (bfd_link_relocatable (info))
1446
0
  continue;
1447
1448
0
      address = rel->r_offset;
1449
1450
      /* Then, process normally.  */
1451
0
      switch (r_type)
1452
0
  {
1453
0
  case R_BFIN_GNU_VTINHERIT:
1454
0
  case R_BFIN_GNU_VTENTRY:
1455
0
    return bfd_reloc_ok;
1456
1457
0
  case R_BFIN_GOT:
1458
    /* Relocation is to the address of the entry for this symbol
1459
       in the global offset table.  */
1460
0
    if (h != NULL
1461
0
        && strcmp (h->root.root.string, "__GLOBAL_OFFSET_TABLE_") == 0)
1462
0
      goto do_default;
1463
1464
    /* Relocation is the offset of the entry for this symbol in
1465
       the global offset table.  */
1466
0
    sgot = elf_hash_table (info)->sgot;
1467
0
    if (sgot == NULL)
1468
0
      {
1469
0
        r = bfd_reloc_undefined;
1470
0
        goto report_error;
1471
0
      }
1472
1473
0
    {
1474
0
      bfd_vma off;
1475
1476
0
      if (h != NULL)
1477
0
        {
1478
0
    bool dyn;
1479
1480
0
    off = h->got.offset;
1481
0
    BFD_ASSERT (off != (bfd_vma) - 1);
1482
0
    dyn = elf_hash_table (info)->dynamic_sections_created;
1483
1484
0
    if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
1485
0
                  bfd_link_pic (info),
1486
0
                  h)
1487
0
        || (bfd_link_pic (info)
1488
0
      && (info->symbolic
1489
0
          || h->dynindx == -1
1490
0
          || h->forced_local)
1491
0
      && h->def_regular))
1492
0
      {
1493
        /* This is actually a static link, or it is a
1494
           -Bsymbolic link and the symbol is defined
1495
           locally, or the symbol was forced to be local
1496
           because of a version file..  We must initialize
1497
           this entry in the global offset table.  Since
1498
           the offset must always be a multiple of 4, we
1499
           use the least significant bit to record whether
1500
           we have initialized it already.
1501
1502
           When doing a dynamic link, we create a .rela.got
1503
           relocation entry to initialize the value.  This
1504
           is done in the finish_dynamic_symbol routine.  */
1505
0
        if ((off & 1) != 0)
1506
0
          off &= ~1;
1507
0
        else
1508
0
          {
1509
0
      bfd_put_32 (output_bfd, relocation,
1510
0
            sgot->contents + off);
1511
0
      h->got.offset |= 1;
1512
0
          }
1513
0
      }
1514
0
    else
1515
0
      unresolved_reloc = false;
1516
0
        }
1517
0
      else
1518
0
        {
1519
0
    BFD_ASSERT (local_got_offsets != NULL);
1520
0
    off = local_got_offsets[r_symndx];
1521
0
    BFD_ASSERT (off != (bfd_vma) - 1);
1522
1523
    /* The offset must always be a multiple of 4.  We use
1524
       the least significant bit to record whether we have
1525
       already generated the necessary reloc.  */
1526
0
    if ((off & 1) != 0)
1527
0
      off &= ~1;
1528
0
    else
1529
0
      {
1530
0
        bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1531
1532
0
        if (bfd_link_pic (info))
1533
0
          {
1534
0
      asection *s;
1535
0
      Elf_Internal_Rela outrel;
1536
0
      bfd_byte *loc;
1537
1538
0
      s = elf_hash_table (info)->srelgot;
1539
0
      BFD_ASSERT (s != NULL);
1540
1541
0
      outrel.r_offset = (sgot->output_section->vma
1542
0
             + sgot->output_offset + off);
1543
0
      outrel.r_info =
1544
0
        ELF32_R_INFO (0, R_BFIN_PCREL24);
1545
0
      outrel.r_addend = relocation;
1546
0
      loc = s->contents;
1547
0
      loc +=
1548
0
        s->reloc_count++ * sizeof (Elf32_External_Rela);
1549
0
      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1550
0
          }
1551
1552
0
        local_got_offsets[r_symndx] |= 1;
1553
0
      }
1554
0
        }
1555
1556
0
      relocation = sgot->output_offset + off;
1557
0
      rel->r_addend = 0;
1558
      /* bfin : preg = [preg + 17bitdiv4offset] relocation is div by 4.  */
1559
0
      relocation /= 4;
1560
0
    }
1561
    /* Fall through.  */
1562
1563
0
  default:
1564
0
  do_default:
1565
0
    r = bfin_final_link_relocate (rel, howto, input_bfd, input_section,
1566
0
          contents, address,
1567
0
          relocation, rel->r_addend);
1568
0
    break;
1569
0
  }
1570
1571
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
1572
   because such sections are not SEC_ALLOC and thus ld.so will
1573
   not process them.  */
1574
0
      if (unresolved_reloc
1575
0
    && !((input_section->flags & SEC_DEBUGGING) != 0 && h->def_dynamic)
1576
0
    && _bfd_elf_section_offset (output_bfd, info, input_section,
1577
0
              rel->r_offset) != (bfd_vma) -1)
1578
0
  r = bfd_reloc_undefined;
1579
1580
0
    report_error:
1581
0
      if (r != bfd_reloc_ok)
1582
0
  {
1583
0
    const char *name;
1584
1585
0
    if (h != NULL)
1586
0
      name = h->root.root.string;
1587
0
    else
1588
0
      {
1589
0
        name = bfd_elf_string_from_elf_section (input_bfd,
1590
0
                  symtab_hdr->sh_link,
1591
0
                  sym->st_name);
1592
0
        if (name == NULL)
1593
0
    return false;
1594
0
        if (*name == '\0')
1595
0
    name = bfd_section_name (sec);
1596
0
      }
1597
1598
0
    if (r == bfd_reloc_overflow)
1599
0
      (*info->callbacks->reloc_overflow)
1600
0
        (info, (h ? &h->root : NULL), name, howto->name,
1601
0
         (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1602
0
    else if (r == bfd_reloc_undefined)
1603
0
      {
1604
0
        _bfd_error_handler
1605
    /* xgettext:c-format */
1606
0
    (_("%pB(%pA+%#" PRIx64 "): "
1607
0
       "unresolvable relocation against symbol `%s'"),
1608
0
     input_bfd, input_section, (uint64_t) rel->r_offset, name);
1609
0
        return false;
1610
0
      }
1611
0
    else
1612
0
      {
1613
0
        _bfd_error_handler
1614
    /* xgettext:c-format */
1615
0
    (_("%pB(%pA+%#" PRIx64 "): reloc against `%s': error %d"),
1616
0
     input_bfd, input_section, (uint64_t) rel->r_offset,
1617
0
     name, (int) r);
1618
0
        return false;
1619
0
      }
1620
0
  }
1621
0
    }
1622
1623
0
  return true;
1624
0
}
1625
1626
static asection *
1627
bfin_gc_mark_hook (asection *sec,
1628
       struct bfd_link_info *info,
1629
       struct elf_reloc_cookie *cookie,
1630
       struct elf_link_hash_entry *h,
1631
       unsigned int symndx)
1632
0
{
1633
0
  if (h != NULL)
1634
0
    switch (ELF32_R_TYPE (cookie->rel->r_info))
1635
0
      {
1636
0
      case R_BFIN_GNU_VTINHERIT:
1637
0
      case R_BFIN_GNU_VTENTRY:
1638
0
  return NULL;
1639
0
      }
1640
1641
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
1642
0
}
1643

1644
extern const bfd_target bfin_elf32_fdpic_vec;
1645
1.27k
#define IS_FDPIC(bfd) ((bfd)->xvec == &bfin_elf32_fdpic_vec)
1646
1647
/* An extension of the elf hash table data structure,
1648
   containing some additional Blackfin-specific data.  */
1649
struct bfinfdpic_elf_link_hash_table
1650
{
1651
  struct elf_link_hash_table elf;
1652
1653
  /* A pointer to the .rofixup section.  */
1654
  asection *sgotfixup;
1655
  /* GOT base offset.  */
1656
  bfd_vma got0;
1657
  /* Location of the first non-lazy PLT entry, i.e., the number of
1658
     bytes taken by lazy PLT entries.  */
1659
  bfd_vma plt0;
1660
  /* A hash table holding information about which symbols were
1661
     referenced with which PIC-related relocations.  */
1662
  struct htab *relocs_info;
1663
  /* Summary reloc information collected by
1664
     _bfinfdpic_count_got_plt_entries.  */
1665
  struct _bfinfdpic_dynamic_got_info *g;
1666
};
1667
1668
/* Get the Blackfin ELF linker hash table from a link_info structure.  */
1669
1670
#define bfinfdpic_hash_table(p) \
1671
0
  ((is_elf_hash_table ((p)->hash)          \
1672
0
    && elf_hash_table_id (elf_hash_table (p)) == BFIN_ELF_DATA)   \
1673
0
   ? (struct bfinfdpic_elf_link_hash_table *) (p)->hash : NULL)
1674
1675
#define bfinfdpic_got_section(info) \
1676
0
  (bfinfdpic_hash_table (info)->elf.sgot)
1677
#define bfinfdpic_gotrel_section(info) \
1678
0
  (bfinfdpic_hash_table (info)->elf.srelgot)
1679
#define bfinfdpic_gotfixup_section(info) \
1680
0
  (bfinfdpic_hash_table (info)->sgotfixup)
1681
#define bfinfdpic_plt_section(info) \
1682
0
  (bfinfdpic_hash_table (info)->elf.splt)
1683
#define bfinfdpic_pltrel_section(info) \
1684
0
  (bfinfdpic_hash_table (info)->elf.srelplt)
1685
#define bfinfdpic_relocs_info(info) \
1686
0
  (bfinfdpic_hash_table (info)->relocs_info)
1687
#define bfinfdpic_got_initial_offset(info) \
1688
0
  (bfinfdpic_hash_table (info)->got0)
1689
#define bfinfdpic_plt_initial_offset(info) \
1690
0
  (bfinfdpic_hash_table (info)->plt0)
1691
#define bfinfdpic_dynamic_got_plt_info(info) \
1692
0
  (bfinfdpic_hash_table (info)->g)
1693
1694
/* The name of the dynamic interpreter.  This is put in the .interp
1695
   section.  */
1696
1697
0
#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
1698
1699
0
#define DEFAULT_STACK_SIZE 0x20000
1700
1701
/* This structure is used to collect the number of entries present in
1702
   each addressable range of the got.  */
1703
struct _bfinfdpic_dynamic_got_info
1704
{
1705
  /* Several bits of information about the current link.  */
1706
  struct bfd_link_info *info;
1707
  /* Total size needed for GOT entries within the 18- or 32-bit
1708
     ranges.  */
1709
  bfd_vma got17m4, gothilo;
1710
  /* Total size needed for function descriptor entries within the 18-
1711
     or 32-bit ranges.  */
1712
  bfd_vma fd17m4, fdhilo;
1713
  /* Total size needed function descriptor entries referenced in PLT
1714
     entries, that would be profitable to place in offsets close to
1715
     the PIC register.  */
1716
  bfd_vma fdplt;
1717
  /* Total size needed by lazy PLT entries.  */
1718
  bfd_vma lzplt;
1719
  /* Number of relocations carried over from input object files.  */
1720
  unsigned long relocs;
1721
  /* Number of fixups introduced by relocations in input object files.  */
1722
  unsigned long fixups;
1723
};
1724
1725
/* Create a Blackfin ELF linker hash table.  */
1726
1727
static struct bfd_link_hash_table *
1728
bfinfdpic_elf_link_hash_table_create (bfd *abfd)
1729
0
{
1730
0
  struct bfinfdpic_elf_link_hash_table *ret;
1731
0
  size_t amt = sizeof (struct bfinfdpic_elf_link_hash_table);
1732
1733
0
  ret = bfd_zmalloc (amt);
1734
0
  if (ret == NULL)
1735
0
    return NULL;
1736
1737
0
  if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
1738
0
              _bfd_elf_link_hash_newfunc,
1739
0
              sizeof (struct elf_link_hash_entry)))
1740
0
    {
1741
0
      free (ret);
1742
0
      return NULL;
1743
0
    }
1744
1745
0
  return &ret->elf.root;
1746
0
}
1747
1748
/* Decide whether a reference to a symbol can be resolved locally or
1749
   not.  If the symbol is protected, we want the local address, but
1750
   its function descriptor must be assigned by the dynamic linker.  */
1751
#define BFINFDPIC_SYM_LOCAL(INFO, H) \
1752
0
  (_bfd_elf_symbol_refs_local_p ((H), (INFO), 1) \
1753
0
   || ! elf_hash_table (INFO)->dynamic_sections_created)
1754
#define BFINFDPIC_FUNCDESC_LOCAL(INFO, H) \
1755
0
  ((H)->dynindx == -1 || ! elf_hash_table (INFO)->dynamic_sections_created)
1756
1757
/* This structure collects information on what kind of GOT, PLT or
1758
   function descriptors are required by relocations that reference a
1759
   certain symbol.  */
1760
struct bfinfdpic_relocs_info
1761
{
1762
  /* The index of the symbol, as stored in the relocation r_info, if
1763
     we have a local symbol; -1 otherwise.  */
1764
  long symndx;
1765
  union
1766
  {
1767
    /* The input bfd in which the symbol is defined, if it's a local
1768
       symbol.  */
1769
    bfd *abfd;
1770
    /* If symndx == -1, the hash table entry corresponding to a global
1771
       symbol (even if it turns out to bind locally, in which case it
1772
       should ideally be replaced with section's symndx + addend).  */
1773
    struct elf_link_hash_entry *h;
1774
  } d;
1775
  /* The addend of the relocation that references the symbol.  */
1776
  bfd_vma addend;
1777
1778
  /* The fields above are used to identify an entry.  The fields below
1779
     contain information on how an entry is used and, later on, which
1780
     locations it was assigned.  */
1781
  /* The following 2 fields record whether the symbol+addend above was
1782
     ever referenced with a GOT relocation.  The 17M4 suffix indicates a
1783
     GOT17M4 relocation; hilo is used for GOTLO/GOTHI pairs.  */
1784
  unsigned got17m4;
1785
  unsigned gothilo;
1786
  /* Whether a FUNCDESC relocation references symbol+addend.  */
1787
  unsigned fd;
1788
  /* Whether a FUNCDESC_GOT relocation references symbol+addend.  */
1789
  unsigned fdgot17m4;
1790
  unsigned fdgothilo;
1791
  /* Whether a FUNCDESC_GOTOFF relocation references symbol+addend.  */
1792
  unsigned fdgoff17m4;
1793
  unsigned fdgoffhilo;
1794
  /* Whether symbol+addend is referenced with GOTOFF17M4, GOTOFFLO or
1795
     GOTOFFHI relocations.  The addend doesn't really matter, since we
1796
     envision that this will only be used to check whether the symbol
1797
     is mapped to the same segment as the got.  */
1798
  unsigned gotoff;
1799
  /* Whether symbol+addend is referenced by a LABEL24 relocation.  */
1800
  unsigned call;
1801
  /* Whether symbol+addend is referenced by a 32 or FUNCDESC_VALUE
1802
     relocation.  */
1803
  unsigned sym;
1804
  /* Whether we need a PLT entry for a symbol.  Should be implied by
1805
     something like:
1806
     (call && symndx == -1 && ! BFINFDPIC_SYM_LOCAL (info, d.h))  */
1807
  unsigned plt:1;
1808
  /* Whether a function descriptor should be created in this link unit
1809
     for symbol+addend.  Should be implied by something like:
1810
     (plt || fdgotoff17m4 || fdgotofflohi
1811
      || ((fd || fdgot17m4 || fdgothilo)
1812
    && (symndx != -1 || BFINFDPIC_FUNCDESC_LOCAL (info, d.h))))  */
1813
  unsigned privfd:1;
1814
  /* Whether a lazy PLT entry is needed for this symbol+addend.
1815
     Should be implied by something like:
1816
     (privfd && symndx == -1 && ! BFINFDPIC_SYM_LOCAL (info, d.h)
1817
      && ! (info->flags & DF_BIND_NOW))  */
1818
  unsigned lazyplt:1;
1819
  /* Whether we've already emitted GOT relocations and PLT entries as
1820
     needed for this symbol.  */
1821
  unsigned done:1;
1822
1823
  /* The number of R_BFIN_BYTE4_DATA, R_BFIN_FUNCDESC and R_BFIN_FUNCDESC_VALUE
1824
     relocations referencing the symbol.  */
1825
  unsigned relocs32, relocsfd, relocsfdv;
1826
1827
  /* The number of .rofixups entries and dynamic relocations allocated
1828
     for this symbol, minus any that might have already been used.  */
1829
  unsigned fixups, dynrelocs;
1830
1831
  /* The offsets of the GOT entries assigned to symbol+addend, to the
1832
     function descriptor's address, and to a function descriptor,
1833
     respectively.  Should be zero if unassigned.  The offsets are
1834
     counted from the value that will be assigned to the PIC register,
1835
     not from the beginning of the .got section.  */
1836
  bfd_signed_vma got_entry, fdgot_entry, fd_entry;
1837
  /* The offsets of the PLT entries assigned to symbol+addend,
1838
     non-lazy and lazy, respectively.  If unassigned, should be
1839
     (bfd_vma)-1.  */
1840
  bfd_vma plt_entry, lzplt_entry;
1841
};
1842
1843
/* Compute a hash with the key fields of an bfinfdpic_relocs_info entry.  */
1844
static hashval_t
1845
bfinfdpic_relocs_info_hash (const void *entry_)
1846
0
{
1847
0
  const struct bfinfdpic_relocs_info *entry = entry_;
1848
1849
0
  return (entry->symndx == -1
1850
0
    ? (long) entry->d.h->root.root.hash
1851
0
    : entry->symndx + (long) entry->d.abfd->id * 257) + entry->addend;
1852
0
}
1853
1854
/* Test whether the key fields of two bfinfdpic_relocs_info entries are
1855
   identical.  */
1856
static int
1857
bfinfdpic_relocs_info_eq (const void *entry1, const void *entry2)
1858
0
{
1859
0
  const struct bfinfdpic_relocs_info *e1 = entry1;
1860
0
  const struct bfinfdpic_relocs_info *e2 = entry2;
1861
1862
0
  return e1->symndx == e2->symndx && e1->addend == e2->addend
1863
0
    && (e1->symndx == -1 ? e1->d.h == e2->d.h : e1->d.abfd == e2->d.abfd);
1864
0
}
1865
1866
/* Find or create an entry in a hash table HT that matches the key
1867
   fields of the given ENTRY.  If it's not found, memory for a new
1868
   entry is allocated in ABFD's obstack.  */
1869
static struct bfinfdpic_relocs_info *
1870
bfinfdpic_relocs_info_find (struct htab *ht,
1871
         bfd *abfd,
1872
         const struct bfinfdpic_relocs_info *entry,
1873
         enum insert_option insert)
1874
0
{
1875
0
  struct bfinfdpic_relocs_info **loc;
1876
1877
0
  if (!ht)
1878
0
    return NULL;
1879
1880
0
  loc = (struct bfinfdpic_relocs_info **) htab_find_slot (ht, entry, insert);
1881
1882
0
  if (! loc)
1883
0
    return NULL;
1884
1885
0
  if (*loc)
1886
0
    return *loc;
1887
1888
0
  *loc = bfd_zalloc (abfd, sizeof (**loc));
1889
1890
0
  if (! *loc)
1891
0
    return *loc;
1892
1893
0
  (*loc)->symndx = entry->symndx;
1894
0
  (*loc)->d = entry->d;
1895
0
  (*loc)->addend = entry->addend;
1896
0
  (*loc)->plt_entry = (bfd_vma)-1;
1897
0
  (*loc)->lzplt_entry = (bfd_vma)-1;
1898
1899
0
  return *loc;
1900
0
}
1901
1902
/* Obtain the address of the entry in HT associated with H's symbol +
1903
   addend, creating a new entry if none existed.  ABFD is only used
1904
   for memory allocation purposes.  */
1905
inline static struct bfinfdpic_relocs_info *
1906
bfinfdpic_relocs_info_for_global (struct htab *ht,
1907
          bfd *abfd,
1908
          struct elf_link_hash_entry *h,
1909
          bfd_vma addend,
1910
          enum insert_option insert)
1911
0
{
1912
0
  struct bfinfdpic_relocs_info entry;
1913
1914
0
  entry.symndx = -1;
1915
0
  entry.d.h = h;
1916
0
  entry.addend = addend;
1917
1918
0
  return bfinfdpic_relocs_info_find (ht, abfd, &entry, insert);
1919
0
}
1920
1921
/* Obtain the address of the entry in HT associated with the SYMNDXth
1922
   local symbol of the input bfd ABFD, plus the addend, creating a new
1923
   entry if none existed.  */
1924
inline static struct bfinfdpic_relocs_info *
1925
bfinfdpic_relocs_info_for_local (struct htab *ht,
1926
        bfd *abfd,
1927
        long symndx,
1928
        bfd_vma addend,
1929
        enum insert_option insert)
1930
0
{
1931
0
  struct bfinfdpic_relocs_info entry;
1932
1933
0
  entry.symndx = symndx;
1934
0
  entry.d.abfd = abfd;
1935
0
  entry.addend = addend;
1936
1937
0
  return bfinfdpic_relocs_info_find (ht, abfd, &entry, insert);
1938
0
}
1939
1940
/* Merge fields set by check_relocs() of two entries that end up being
1941
   mapped to the same (presumably global) symbol.  */
1942
1943
inline static void
1944
bfinfdpic_pic_merge_early_relocs_info (struct bfinfdpic_relocs_info *e2,
1945
               struct bfinfdpic_relocs_info const *e1)
1946
0
{
1947
0
  e2->got17m4 |= e1->got17m4;
1948
0
  e2->gothilo |= e1->gothilo;
1949
0
  e2->fd |= e1->fd;
1950
0
  e2->fdgot17m4 |= e1->fdgot17m4;
1951
0
  e2->fdgothilo |= e1->fdgothilo;
1952
0
  e2->fdgoff17m4 |= e1->fdgoff17m4;
1953
0
  e2->fdgoffhilo |= e1->fdgoffhilo;
1954
0
  e2->gotoff |= e1->gotoff;
1955
0
  e2->call |= e1->call;
1956
0
  e2->sym |= e1->sym;
1957
0
}
1958
1959
/* Every block of 65535 lazy PLT entries shares a single call to the
1960
   resolver, inserted in the 32768th lazy PLT entry (i.e., entry #
1961
   32767, counting from 0).  All other lazy PLT entries branch to it
1962
   in a single instruction.  */
1963
1964
0
#define LZPLT_RESOLVER_EXTRA 10
1965
0
#define LZPLT_NORMAL_SIZE 6
1966
0
#define LZPLT_ENTRIES 1362
1967
1968
0
#define BFINFDPIC_LZPLT_BLOCK_SIZE ((bfd_vma) LZPLT_NORMAL_SIZE * LZPLT_ENTRIES + LZPLT_RESOLVER_EXTRA)
1969
0
#define BFINFDPIC_LZPLT_RESOLV_LOC (LZPLT_NORMAL_SIZE * LZPLT_ENTRIES / 2)
1970
1971
/* Add a dynamic relocation to the SRELOC section.  */
1972
1973
inline static bfd_vma
1974
_bfinfdpic_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
1975
       int reloc_type, long dynindx, bfd_vma addend,
1976
       struct bfinfdpic_relocs_info *entry)
1977
0
{
1978
0
  Elf_Internal_Rela outrel;
1979
0
  bfd_vma reloc_offset;
1980
1981
0
  outrel.r_offset = offset;
1982
0
  outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
1983
0
  outrel.r_addend = addend;
1984
1985
0
  reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rel);
1986
0
  BFD_ASSERT (reloc_offset < sreloc->size);
1987
0
  bfd_elf32_swap_reloc_out (output_bfd, &outrel,
1988
0
          sreloc->contents + reloc_offset);
1989
0
  sreloc->reloc_count++;
1990
1991
  /* If the entry's index is zero, this relocation was probably to a
1992
     linkonce section that got discarded.  We reserved a dynamic
1993
     relocation, but it was for another entry than the one we got at
1994
     the time of emitting the relocation.  Unfortunately there's no
1995
     simple way for us to catch this situation, since the relocation
1996
     is cleared right before calling relocate_section, at which point
1997
     we no longer know what the relocation used to point to.  */
1998
0
  if (entry->symndx)
1999
0
    {
2000
0
      BFD_ASSERT (entry->dynrelocs > 0);
2001
0
      entry->dynrelocs--;
2002
0
    }
2003
2004
0
  return reloc_offset;
2005
0
}
2006
2007
/* Add a fixup to the ROFIXUP section.  */
2008
2009
static bfd_vma
2010
_bfinfdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
2011
      struct bfinfdpic_relocs_info *entry)
2012
0
{
2013
0
  bfd_vma fixup_offset;
2014
2015
0
  if (rofixup->flags & SEC_EXCLUDE)
2016
0
    return -1;
2017
2018
0
  fixup_offset = rofixup->reloc_count * 4;
2019
0
  if (rofixup->contents)
2020
0
    {
2021
0
      BFD_ASSERT (fixup_offset < rofixup->size);
2022
0
      bfd_put_32 (output_bfd, offset, rofixup->contents + fixup_offset);
2023
0
    }
2024
0
  rofixup->reloc_count++;
2025
2026
0
  if (entry && entry->symndx)
2027
0
    {
2028
      /* See discussion about symndx == 0 in _bfinfdpic_add_dyn_reloc
2029
   above.  */
2030
0
      BFD_ASSERT (entry->fixups > 0);
2031
0
      entry->fixups--;
2032
0
    }
2033
2034
0
  return fixup_offset;
2035
0
}
2036
2037
/* Find the segment number in which OSEC, and output section, is
2038
   located.  */
2039
2040
static unsigned
2041
_bfinfdpic_osec_to_segment (bfd *output_bfd, asection *osec)
2042
0
{
2043
0
  Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
2044
2045
0
  return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
2046
0
}
2047
2048
inline static bool
2049
_bfinfdpic_osec_readonly_p (bfd *output_bfd, asection *osec)
2050
0
{
2051
0
  unsigned seg = _bfinfdpic_osec_to_segment (output_bfd, osec);
2052
2053
0
  return ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W);
2054
0
}
2055
2056
/* Generate relocations for GOT entries, function descriptors, and
2057
   code for PLT and lazy PLT entries.  */
2058
2059
inline static bool
2060
_bfinfdpic_emit_got_relocs_plt_entries (struct bfinfdpic_relocs_info *entry,
2061
          bfd *output_bfd,
2062
          struct bfd_link_info *info,
2063
          asection *sec,
2064
          Elf_Internal_Sym *sym,
2065
          bfd_vma addend)
2066
0
{
2067
0
  bfd_vma fd_lazy_rel_offset = (bfd_vma) -1;
2068
0
  int dynindx = -1;
2069
2070
0
  if (entry->done)
2071
0
    return true;
2072
0
  entry->done = 1;
2073
2074
0
  if (entry->got_entry || entry->fdgot_entry || entry->fd_entry)
2075
0
    {
2076
      /* If the symbol is dynamic, consider it for dynamic
2077
   relocations, otherwise decay to section + offset.  */
2078
0
      if (entry->symndx == -1 && entry->d.h->dynindx != -1)
2079
0
  dynindx = entry->d.h->dynindx;
2080
0
      else
2081
0
  {
2082
0
    if (sec
2083
0
        && sec->output_section
2084
0
        && ! bfd_is_abs_section (sec->output_section)
2085
0
        && ! bfd_is_und_section (sec->output_section))
2086
0
      dynindx = elf_section_data (sec->output_section)->dynindx;
2087
0
    else
2088
0
      dynindx = 0;
2089
0
  }
2090
0
    }
2091
2092
  /* Generate relocation for GOT entry pointing to the symbol.  */
2093
0
  if (entry->got_entry)
2094
0
    {
2095
0
      int idx = dynindx;
2096
0
      bfd_vma ad = addend;
2097
2098
      /* If the symbol is dynamic but binds locally, use
2099
   section+offset.  */
2100
0
      if (sec && (entry->symndx != -1
2101
0
      || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
2102
0
  {
2103
0
    if (entry->symndx == -1)
2104
0
      ad += entry->d.h->root.u.def.value;
2105
0
    else
2106
0
      ad += sym->st_value;
2107
0
    ad += sec->output_offset;
2108
0
    if (sec->output_section && elf_section_data (sec->output_section))
2109
0
      idx = elf_section_data (sec->output_section)->dynindx;
2110
0
    else
2111
0
      idx = 0;
2112
0
  }
2113
2114
      /* If we're linking an executable at a fixed address, we can
2115
   omit the dynamic relocation as long as the symbol is local to
2116
   this module.  */
2117
0
      if (bfd_link_pde (info)
2118
0
    && (entry->symndx != -1
2119
0
        || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
2120
0
  {
2121
0
    if (sec)
2122
0
      ad += sec->output_section->vma;
2123
0
    if (entry->symndx != -1
2124
0
        || entry->d.h->root.type != bfd_link_hash_undefweak)
2125
0
      _bfinfdpic_add_rofixup (output_bfd,
2126
0
           bfinfdpic_gotfixup_section (info),
2127
0
           bfinfdpic_got_section (info)->output_section
2128
0
           ->vma
2129
0
           + bfinfdpic_got_section (info)->output_offset
2130
0
           + bfinfdpic_got_initial_offset (info)
2131
0
           + entry->got_entry, entry);
2132
0
  }
2133
0
      else
2134
0
  _bfinfdpic_add_dyn_reloc (output_bfd, bfinfdpic_gotrel_section (info),
2135
0
         _bfd_elf_section_offset
2136
0
         (output_bfd, info,
2137
0
          bfinfdpic_got_section (info),
2138
0
          bfinfdpic_got_initial_offset (info)
2139
0
          + entry->got_entry)
2140
0
         + bfinfdpic_got_section (info)
2141
0
         ->output_section->vma
2142
0
         + bfinfdpic_got_section (info)->output_offset,
2143
0
         R_BFIN_BYTE4_DATA, idx, ad, entry);
2144
2145
0
      bfd_put_32 (output_bfd, ad,
2146
0
      bfinfdpic_got_section (info)->contents
2147
0
      + bfinfdpic_got_initial_offset (info)
2148
0
      + entry->got_entry);
2149
0
    }
2150
2151
  /* Generate relocation for GOT entry pointing to a canonical
2152
     function descriptor.  */
2153
0
  if (entry->fdgot_entry)
2154
0
    {
2155
0
      int reloc, idx;
2156
0
      bfd_vma ad = 0;
2157
2158
0
      if (! (entry->symndx == -1
2159
0
       && entry->d.h->root.type == bfd_link_hash_undefweak
2160
0
       && BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
2161
0
  {
2162
    /* If the symbol is dynamic and there may be dynamic symbol
2163
       resolution because we are, or are linked with, a shared
2164
       library, emit a FUNCDESC relocation such that the dynamic
2165
       linker will allocate the function descriptor.  If the
2166
       symbol needs a non-local function descriptor but binds
2167
       locally (e.g., its visibility is protected, emit a
2168
       dynamic relocation decayed to section+offset.  */
2169
0
    if (entry->symndx == -1
2170
0
        && ! BFINFDPIC_FUNCDESC_LOCAL (info, entry->d.h)
2171
0
        && BFINFDPIC_SYM_LOCAL (info, entry->d.h)
2172
0
        && !bfd_link_pde (info))
2173
0
      {
2174
0
        reloc = R_BFIN_FUNCDESC;
2175
0
        idx = elf_section_data (entry->d.h->root.u.def.section
2176
0
              ->output_section)->dynindx;
2177
0
        ad = entry->d.h->root.u.def.section->output_offset
2178
0
    + entry->d.h->root.u.def.value;
2179
0
      }
2180
0
    else if (entry->symndx == -1
2181
0
       && ! BFINFDPIC_FUNCDESC_LOCAL (info, entry->d.h))
2182
0
      {
2183
0
        reloc = R_BFIN_FUNCDESC;
2184
0
        idx = dynindx;
2185
0
        ad = addend;
2186
0
        if (ad)
2187
0
    return false;
2188
0
      }
2189
0
    else
2190
0
      {
2191
        /* Otherwise, we know we have a private function descriptor,
2192
     so reference it directly.  */
2193
0
        if (elf_hash_table (info)->dynamic_sections_created)
2194
0
    BFD_ASSERT (entry->privfd);
2195
0
        reloc = R_BFIN_BYTE4_DATA;
2196
0
        idx = elf_section_data (bfinfdpic_got_section (info)
2197
0
              ->output_section)->dynindx;
2198
0
        ad = bfinfdpic_got_section (info)->output_offset
2199
0
    + bfinfdpic_got_initial_offset (info) + entry->fd_entry;
2200
0
      }
2201
2202
    /* If there is room for dynamic symbol resolution, emit the
2203
       dynamic relocation.  However, if we're linking an
2204
       executable at a fixed location, we won't have emitted a
2205
       dynamic symbol entry for the got section, so idx will be
2206
       zero, which means we can and should compute the address
2207
       of the private descriptor ourselves.  */
2208
0
    if (bfd_link_pde (info)
2209
0
        && (entry->symndx != -1
2210
0
      || BFINFDPIC_FUNCDESC_LOCAL (info, entry->d.h)))
2211
0
      {
2212
0
        ad += bfinfdpic_got_section (info)->output_section->vma;
2213
0
        _bfinfdpic_add_rofixup (output_bfd,
2214
0
             bfinfdpic_gotfixup_section (info),
2215
0
             bfinfdpic_got_section (info)
2216
0
             ->output_section->vma
2217
0
             + bfinfdpic_got_section (info)
2218
0
             ->output_offset
2219
0
             + bfinfdpic_got_initial_offset (info)
2220
0
             + entry->fdgot_entry, entry);
2221
0
      }
2222
0
    else
2223
0
      _bfinfdpic_add_dyn_reloc (output_bfd,
2224
0
             bfinfdpic_gotrel_section (info),
2225
0
             _bfd_elf_section_offset
2226
0
             (output_bfd, info,
2227
0
              bfinfdpic_got_section (info),
2228
0
              bfinfdpic_got_initial_offset (info)
2229
0
              + entry->fdgot_entry)
2230
0
             + bfinfdpic_got_section (info)
2231
0
             ->output_section->vma
2232
0
             + bfinfdpic_got_section (info)
2233
0
             ->output_offset,
2234
0
             reloc, idx, ad, entry);
2235
0
  }
2236
2237
0
      bfd_put_32 (output_bfd, ad,
2238
0
      bfinfdpic_got_section (info)->contents
2239
0
      + bfinfdpic_got_initial_offset (info)
2240
0
      + entry->fdgot_entry);
2241
0
    }
2242
2243
  /* Generate relocation to fill in a private function descriptor in
2244
     the GOT.  */
2245
0
  if (entry->fd_entry)
2246
0
    {
2247
0
      int idx = dynindx;
2248
0
      bfd_vma ad = addend;
2249
0
      bfd_vma ofst;
2250
0
      long lowword, highword;
2251
2252
      /* If the symbol is dynamic but binds locally, use
2253
   section+offset.  */
2254
0
      if (sec && (entry->symndx != -1
2255
0
      || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
2256
0
  {
2257
0
    if (entry->symndx == -1)
2258
0
      ad += entry->d.h->root.u.def.value;
2259
0
    else
2260
0
      ad += sym->st_value;
2261
0
    ad += sec->output_offset;
2262
0
    if (sec->output_section && elf_section_data (sec->output_section))
2263
0
      idx = elf_section_data (sec->output_section)->dynindx;
2264
0
    else
2265
0
      idx = 0;
2266
0
  }
2267
2268
      /* If we're linking an executable at a fixed address, we can
2269
   omit the dynamic relocation as long as the symbol is local to
2270
   this module.  */
2271
0
      if (bfd_link_pde (info)
2272
0
    && (entry->symndx != -1 || BFINFDPIC_SYM_LOCAL (info, entry->d.h)))
2273
0
  {
2274
0
    if (sec)
2275
0
      ad += sec->output_section->vma;
2276
0
    ofst = 0;
2277
0
    if (entry->symndx != -1
2278
0
        || entry->d.h->root.type != bfd_link_hash_undefweak)
2279
0
      {
2280
0
        _bfinfdpic_add_rofixup (output_bfd,
2281
0
             bfinfdpic_gotfixup_section (info),
2282
0
             bfinfdpic_got_section (info)
2283
0
             ->output_section->vma
2284
0
             + bfinfdpic_got_section (info)
2285
0
             ->output_offset
2286
0
             + bfinfdpic_got_initial_offset (info)
2287
0
             + entry->fd_entry, entry);
2288
0
        _bfinfdpic_add_rofixup (output_bfd,
2289
0
             bfinfdpic_gotfixup_section (info),
2290
0
             bfinfdpic_got_section (info)
2291
0
             ->output_section->vma
2292
0
             + bfinfdpic_got_section (info)
2293
0
             ->output_offset
2294
0
             + bfinfdpic_got_initial_offset (info)
2295
0
             + entry->fd_entry + 4, entry);
2296
0
      }
2297
0
  }
2298
0
      else
2299
0
  {
2300
0
    ofst
2301
0
      = _bfinfdpic_add_dyn_reloc (output_bfd,
2302
0
          entry->lazyplt
2303
0
          ? bfinfdpic_pltrel_section (info)
2304
0
          : bfinfdpic_gotrel_section (info),
2305
0
          _bfd_elf_section_offset
2306
0
          (output_bfd, info,
2307
0
           bfinfdpic_got_section (info),
2308
0
           bfinfdpic_got_initial_offset (info)
2309
0
           + entry->fd_entry)
2310
0
          + bfinfdpic_got_section (info)
2311
0
          ->output_section->vma
2312
0
          + bfinfdpic_got_section (info)
2313
0
          ->output_offset,
2314
0
          R_BFIN_FUNCDESC_VALUE, idx, ad, entry);
2315
0
  }
2316
2317
      /* If we've omitted the dynamic relocation, just emit the fixed
2318
   addresses of the symbol and of the local GOT base offset.  */
2319
0
      if (bfd_link_pde (info)
2320
0
    && sec
2321
0
    && sec->output_section)
2322
0
  {
2323
0
    lowword = ad;
2324
0
    highword = bfinfdpic_got_section (info)->output_section->vma
2325
0
      + bfinfdpic_got_section (info)->output_offset
2326
0
      + bfinfdpic_got_initial_offset (info);
2327
0
  }
2328
0
      else if (entry->lazyplt)
2329
0
  {
2330
0
    if (ad)
2331
0
      return false;
2332
2333
0
    fd_lazy_rel_offset = ofst;
2334
2335
    /* A function descriptor used for lazy or local resolving is
2336
       initialized such that its high word contains the output
2337
       section index in which the PLT entries are located, and
2338
       the low word contains the address of the lazy PLT entry
2339
       entry point, that must be within the memory region
2340
       assigned to that section.  */
2341
0
    lowword = entry->lzplt_entry + 4
2342
0
      + bfinfdpic_plt_section (info)->output_offset
2343
0
      + bfinfdpic_plt_section (info)->output_section->vma;
2344
0
    highword = _bfinfdpic_osec_to_segment
2345
0
      (output_bfd, bfinfdpic_plt_section (info)->output_section);
2346
0
  }
2347
0
      else
2348
0
  {
2349
    /* A function descriptor for a local function gets the index
2350
       of the section.  For a non-local function, it's
2351
       disregarded.  */
2352
0
    lowword = ad;
2353
0
    if (sec == NULL
2354
0
        || (entry->symndx == -1 && entry->d.h->dynindx != -1
2355
0
      && entry->d.h->dynindx == idx))
2356
0
      highword = 0;
2357
0
    else
2358
0
      highword = _bfinfdpic_osec_to_segment
2359
0
        (output_bfd, sec->output_section);
2360
0
  }
2361
2362
0
      bfd_put_32 (output_bfd, lowword,
2363
0
      bfinfdpic_got_section (info)->contents
2364
0
      + bfinfdpic_got_initial_offset (info)
2365
0
      + entry->fd_entry);
2366
0
      bfd_put_32 (output_bfd, highword,
2367
0
      bfinfdpic_got_section (info)->contents
2368
0
      + bfinfdpic_got_initial_offset (info)
2369
0
      + entry->fd_entry + 4);
2370
0
    }
2371
2372
  /* Generate code for the PLT entry.  */
2373
0
  if (entry->plt_entry != (bfd_vma) -1)
2374
0
    {
2375
0
      bfd_byte *plt_code = bfinfdpic_plt_section (info)->contents
2376
0
  + entry->plt_entry;
2377
2378
0
      BFD_ASSERT (entry->fd_entry);
2379
2380
      /* Figure out what kind of PLT entry we need, depending on the
2381
   location of the function descriptor within the GOT.  */
2382
0
      if (entry->fd_entry >= -(1 << (18 - 1))
2383
0
    && entry->fd_entry + 4 < (1 << (18 - 1)))
2384
0
  {
2385
    /* P1 = [P3 + fd_entry]; P3 = [P3 + fd_entry + 4] */
2386
0
    bfd_put_32 (output_bfd,
2387
0
          0xe519 | ((entry->fd_entry << 14) & 0xFFFF0000),
2388
0
          plt_code);
2389
0
    bfd_put_32 (output_bfd,
2390
0
          0xe51b | (((entry->fd_entry + 4) << 14) & 0xFFFF0000),
2391
0
          plt_code + 4);
2392
0
    plt_code += 8;
2393
0
  }
2394
0
      else
2395
0
  {
2396
    /* P1.L = fd_entry; P1.H = fd_entry;
2397
       P3 = P3 + P1;
2398
       P1 = [P3];
2399
       P3 = [P3 + 4];  */
2400
0
    bfd_put_32 (output_bfd,
2401
0
          0xe109 | (entry->fd_entry << 16),
2402
0
          plt_code);
2403
0
    bfd_put_32 (output_bfd,
2404
0
          0xe149 | (entry->fd_entry & 0xFFFF0000),
2405
0
          plt_code + 4);
2406
0
    bfd_put_16 (output_bfd, 0x5ad9, plt_code + 8);
2407
0
    bfd_put_16 (output_bfd, 0x9159, plt_code + 10);
2408
0
    bfd_put_16 (output_bfd, 0xac5b, plt_code + 12);
2409
0
    plt_code += 14;
2410
0
  }
2411
      /* JUMP (P1) */
2412
0
      bfd_put_16 (output_bfd, 0x0051, plt_code);
2413
0
    }
2414
2415
  /* Generate code for the lazy PLT entry.  */
2416
0
  if (entry->lzplt_entry != (bfd_vma) -1)
2417
0
    {
2418
0
      bfd_byte *lzplt_code = bfinfdpic_plt_section (info)->contents
2419
0
  + entry->lzplt_entry;
2420
0
      bfd_vma resolverStub_addr;
2421
2422
0
      bfd_put_32 (output_bfd, fd_lazy_rel_offset, lzplt_code);
2423
0
      lzplt_code += 4;
2424
2425
0
      resolverStub_addr = entry->lzplt_entry / BFINFDPIC_LZPLT_BLOCK_SIZE
2426
0
  * BFINFDPIC_LZPLT_BLOCK_SIZE + BFINFDPIC_LZPLT_RESOLV_LOC;
2427
0
      if (resolverStub_addr >= bfinfdpic_plt_initial_offset (info))
2428
0
  resolverStub_addr = bfinfdpic_plt_initial_offset (info) - LZPLT_NORMAL_SIZE - LZPLT_RESOLVER_EXTRA;
2429
2430
0
      if (entry->lzplt_entry == resolverStub_addr)
2431
0
  {
2432
    /* This is a lazy PLT entry that includes a resolver call.
2433
       P2 = [P3];
2434
       R3 = [P3 + 4];
2435
       JUMP (P2);  */
2436
0
    bfd_put_32 (output_bfd,
2437
0
          0xa05b915a,
2438
0
          lzplt_code);
2439
0
    bfd_put_16 (output_bfd, 0x0052, lzplt_code + 4);
2440
0
  }
2441
0
      else
2442
0
  {
2443
    /* JUMP.S  resolverStub */
2444
0
    bfd_put_16 (output_bfd,
2445
0
          0x2000
2446
0
          | (((resolverStub_addr - entry->lzplt_entry)
2447
0
        / 2) & (((bfd_vma)1 << 12) - 1)),
2448
0
          lzplt_code);
2449
0
  }
2450
0
    }
2451
2452
0
  return true;
2453
0
}
2454

2455
/* Relocate an Blackfin ELF section.
2456
2457
   The RELOCATE_SECTION function is called by the new ELF backend linker
2458
   to handle the relocations for a section.
2459
2460
   The relocs are always passed as Rela structures; if the section
2461
   actually uses Rel structures, the r_addend field will always be
2462
   zero.
2463
2464
   This function is responsible for adjusting the section contents as
2465
   necessary, and (if using Rela relocs and generating a relocatable
2466
   output file) adjusting the reloc addend as necessary.
2467
2468
   This function does not have to worry about setting the reloc
2469
   address or the reloc symbol index.
2470
2471
   LOCAL_SYMS is a pointer to the swapped in local symbols.
2472
2473
   LOCAL_SECTIONS is an array giving the section in the input file
2474
   corresponding to the st_shndx field of each local symbol.
2475
2476
   The global hash table entry for the global symbols can be found
2477
   via elf_sym_hashes (input_bfd).
2478
2479
   When generating relocatable output, this function must handle
2480
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
2481
   going to be the section symbol corresponding to the output
2482
   section, which means that the addend must be adjusted
2483
   accordingly.  */
2484
2485
static int
2486
bfinfdpic_relocate_section (bfd * output_bfd,
2487
          struct bfd_link_info *info,
2488
          bfd * input_bfd,
2489
          asection * input_section,
2490
          bfd_byte * contents,
2491
          Elf_Internal_Rela * relocs,
2492
          Elf_Internal_Sym * local_syms,
2493
          asection ** local_sections)
2494
0
{
2495
0
  Elf_Internal_Shdr *symtab_hdr;
2496
0
  struct elf_link_hash_entry **sym_hashes;
2497
0
  Elf_Internal_Rela *rel;
2498
0
  Elf_Internal_Rela *relend;
2499
0
  unsigned isec_segment, got_segment, plt_segment,
2500
0
    check_segment[2];
2501
0
  int silence_segment_error = !bfd_link_pic (info);
2502
2503
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
2504
0
  sym_hashes = elf_sym_hashes (input_bfd);
2505
0
  relend     = relocs + input_section->reloc_count;
2506
2507
0
  isec_segment = _bfinfdpic_osec_to_segment (output_bfd,
2508
0
               input_section->output_section);
2509
0
  if (IS_FDPIC (output_bfd) && bfinfdpic_got_section (info))
2510
0
    got_segment = _bfinfdpic_osec_to_segment (output_bfd,
2511
0
                bfinfdpic_got_section (info)
2512
0
                ->output_section);
2513
0
  else
2514
0
    got_segment = -1;
2515
0
  if (IS_FDPIC (output_bfd) && elf_hash_table (info)->dynamic_sections_created)
2516
0
    plt_segment = _bfinfdpic_osec_to_segment (output_bfd,
2517
0
                bfinfdpic_plt_section (info)
2518
0
                ->output_section);
2519
0
  else
2520
0
    plt_segment = -1;
2521
2522
0
  for (rel = relocs; rel < relend; rel ++)
2523
0
    {
2524
0
      reloc_howto_type *howto;
2525
0
      unsigned long r_symndx;
2526
0
      Elf_Internal_Sym *sym;
2527
0
      asection *sec;
2528
0
      struct elf_link_hash_entry *h;
2529
0
      bfd_vma relocation;
2530
0
      bfd_reloc_status_type r;
2531
0
      const char * name = NULL;
2532
0
      int r_type;
2533
0
      asection *osec;
2534
0
      struct bfinfdpic_relocs_info *picrel;
2535
0
      bfd_vma orig_addend = rel->r_addend;
2536
2537
0
      r_type = ELF32_R_TYPE (rel->r_info);
2538
2539
0
      if (r_type == R_BFIN_GNU_VTINHERIT
2540
0
    || r_type == R_BFIN_GNU_VTENTRY)
2541
0
  continue;
2542
2543
0
      r_symndx = ELF32_R_SYM (rel->r_info);
2544
0
      howto = bfin_reloc_type_lookup (input_bfd, r_type);
2545
0
      if (howto == NULL)
2546
0
  {
2547
0
    bfd_set_error (bfd_error_bad_value);
2548
0
    return false;
2549
0
  }
2550
2551
0
      h      = NULL;
2552
0
      sym    = NULL;
2553
0
      sec    = NULL;
2554
0
      picrel = NULL;
2555
2556
0
      if (r_symndx < symtab_hdr->sh_info)
2557
0
  {
2558
0
    sym = local_syms + r_symndx;
2559
0
    osec = sec = local_sections [r_symndx];
2560
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
2561
2562
0
    name = bfd_elf_string_from_elf_section
2563
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name);
2564
0
    name = name == NULL ? bfd_section_name (sec) : name;
2565
0
  }
2566
0
      else
2567
0
  {
2568
0
    bool warned, ignored;
2569
0
    bool unresolved_reloc;
2570
2571
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2572
0
           r_symndx, symtab_hdr, sym_hashes,
2573
0
           h, sec, relocation,
2574
0
           unresolved_reloc, warned, ignored);
2575
0
    osec = sec;
2576
0
  }
2577
2578
0
      if (sec != NULL && discarded_section (sec))
2579
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2580
0
           rel, 1, relend, R_BFIN_UNUSED0,
2581
0
           howto, 0, contents);
2582
2583
0
      if (bfd_link_relocatable (info))
2584
0
  continue;
2585
2586
0
      if (h != NULL
2587
0
    && (h->root.type == bfd_link_hash_defined
2588
0
        || h->root.type == bfd_link_hash_defweak)
2589
0
    && !BFINFDPIC_SYM_LOCAL (info, h))
2590
0
  {
2591
0
    osec = sec = NULL;
2592
0
    relocation = 0;
2593
0
  }
2594
2595
0
      switch (r_type)
2596
0
  {
2597
0
  case R_BFIN_PCREL24:
2598
0
  case R_BFIN_PCREL24_JUMP_L:
2599
0
  case R_BFIN_BYTE4_DATA:
2600
0
    if (! IS_FDPIC (output_bfd))
2601
0
      goto non_fdpic;
2602
    /* Fall through.  */
2603
2604
0
  case R_BFIN_GOT17M4:
2605
0
  case R_BFIN_GOTHI:
2606
0
  case R_BFIN_GOTLO:
2607
0
  case R_BFIN_FUNCDESC_GOT17M4:
2608
0
  case R_BFIN_FUNCDESC_GOTHI:
2609
0
  case R_BFIN_FUNCDESC_GOTLO:
2610
0
  case R_BFIN_GOTOFF17M4:
2611
0
  case R_BFIN_GOTOFFHI:
2612
0
  case R_BFIN_GOTOFFLO:
2613
0
  case R_BFIN_FUNCDESC_GOTOFF17M4:
2614
0
  case R_BFIN_FUNCDESC_GOTOFFHI:
2615
0
  case R_BFIN_FUNCDESC_GOTOFFLO:
2616
0
  case R_BFIN_FUNCDESC:
2617
0
  case R_BFIN_FUNCDESC_VALUE:
2618
0
    if ((input_section->flags & SEC_ALLOC) == 0)
2619
0
      break;
2620
2621
0
    if (h != NULL)
2622
0
      picrel = bfinfdpic_relocs_info_for_global (bfinfdpic_relocs_info
2623
0
                   (info), input_bfd, h,
2624
0
                   orig_addend, INSERT);
2625
0
    else
2626
      /* In order to find the entry we created before, we must
2627
         use the original addend, not the one that may have been
2628
         modified by _bfd_elf_rela_local_sym().  */
2629
0
      picrel = bfinfdpic_relocs_info_for_local (bfinfdpic_relocs_info
2630
0
                  (info), input_bfd, r_symndx,
2631
0
                  orig_addend, INSERT);
2632
0
    if (! picrel)
2633
0
      return false;
2634
2635
0
    if (!_bfinfdpic_emit_got_relocs_plt_entries (picrel, output_bfd, info,
2636
0
                   osec, sym,
2637
0
                   rel->r_addend))
2638
0
      {
2639
0
        _bfd_error_handler
2640
    /* xgettext:c-format */
2641
0
    (_("%pB: relocation at `%pA+%#" PRIx64 "' "
2642
0
       "references symbol `%s' with nonzero addend"),
2643
0
     input_bfd, input_section, (uint64_t) rel->r_offset, name);
2644
0
        return false;
2645
2646
0
      }
2647
2648
0
    break;
2649
2650
0
  default:
2651
0
  non_fdpic:
2652
0
    picrel = NULL;
2653
0
    if (h && ! BFINFDPIC_SYM_LOCAL (info, h)
2654
0
        && _bfd_elf_section_offset (output_bfd, info, input_section,
2655
0
            rel->r_offset) != (bfd_vma) -1)
2656
0
      {
2657
0
        info->callbacks->warning
2658
0
    (info, _("relocation references symbol not defined in the module"),
2659
0
     name, input_bfd, input_section, rel->r_offset);
2660
0
        return false;
2661
0
      }
2662
0
    break;
2663
0
  }
2664
2665
0
      switch (r_type)
2666
0
  {
2667
0
  case R_BFIN_PCREL24:
2668
0
  case R_BFIN_PCREL24_JUMP_L:
2669
0
    check_segment[0] = isec_segment;
2670
0
    if (! IS_FDPIC (output_bfd))
2671
0
      check_segment[1] = isec_segment;
2672
0
    else if (picrel->plt)
2673
0
      {
2674
0
        relocation = bfinfdpic_plt_section (info)->output_section->vma
2675
0
    + bfinfdpic_plt_section (info)->output_offset
2676
0
    + picrel->plt_entry;
2677
0
        check_segment[1] = plt_segment;
2678
0
      }
2679
    /* We don't want to warn on calls to undefined weak symbols,
2680
       as calls to them must be protected by non-NULL tests
2681
       anyway, and unprotected calls would invoke undefined
2682
       behavior.  */
2683
0
    else if (picrel->symndx == -1
2684
0
       && picrel->d.h->root.type == bfd_link_hash_undefweak)
2685
0
      check_segment[1] = check_segment[0];
2686
0
    else
2687
0
      check_segment[1] = sec
2688
0
        ? _bfinfdpic_osec_to_segment (output_bfd, sec->output_section)
2689
0
        : (unsigned)-1;
2690
0
    break;
2691
2692
0
  case R_BFIN_GOT17M4:
2693
0
  case R_BFIN_GOTHI:
2694
0
  case R_BFIN_GOTLO:
2695
0
    relocation = picrel->got_entry;
2696
0
    check_segment[0] = check_segment[1] = got_segment;
2697
0
    break;
2698
2699
0
  case R_BFIN_FUNCDESC_GOT17M4:
2700
0
  case R_BFIN_FUNCDESC_GOTHI:
2701
0
  case R_BFIN_FUNCDESC_GOTLO:
2702
0
    relocation = picrel->fdgot_entry;
2703
0
    check_segment[0] = check_segment[1] = got_segment;
2704
0
    break;
2705
2706
0
  case R_BFIN_GOTOFFHI:
2707
0
  case R_BFIN_GOTOFF17M4:
2708
0
  case R_BFIN_GOTOFFLO:
2709
0
    relocation -= bfinfdpic_got_section (info)->output_section->vma
2710
0
      + bfinfdpic_got_section (info)->output_offset
2711
0
      + bfinfdpic_got_initial_offset (info);
2712
0
    check_segment[0] = got_segment;
2713
0
    check_segment[1] = sec
2714
0
      ? _bfinfdpic_osec_to_segment (output_bfd, sec->output_section)
2715
0
      : (unsigned)-1;
2716
0
    break;
2717
2718
0
  case R_BFIN_FUNCDESC_GOTOFF17M4:
2719
0
  case R_BFIN_FUNCDESC_GOTOFFHI:
2720
0
  case R_BFIN_FUNCDESC_GOTOFFLO:
2721
0
    relocation = picrel->fd_entry;
2722
0
    check_segment[0] = check_segment[1] = got_segment;
2723
0
    break;
2724
2725
0
  case R_BFIN_FUNCDESC:
2726
0
    if ((input_section->flags & SEC_ALLOC) != 0)
2727
0
      {
2728
0
        int dynindx;
2729
0
        bfd_vma addend = rel->r_addend;
2730
2731
0
        if (! (h && h->root.type == bfd_link_hash_undefweak
2732
0
         && BFINFDPIC_SYM_LOCAL (info, h)))
2733
0
    {
2734
      /* If the symbol is dynamic and there may be dynamic
2735
         symbol resolution because we are or are linked with a
2736
         shared library, emit a FUNCDESC relocation such that
2737
         the dynamic linker will allocate the function
2738
         descriptor.  If the symbol needs a non-local function
2739
         descriptor but binds locally (e.g., its visibility is
2740
         protected, emit a dynamic relocation decayed to
2741
         section+offset.  */
2742
0
      if (h && ! BFINFDPIC_FUNCDESC_LOCAL (info, h)
2743
0
          && BFINFDPIC_SYM_LOCAL (info, h)
2744
0
          && !bfd_link_pde (info))
2745
0
        {
2746
0
          dynindx = elf_section_data (h->root.u.def.section
2747
0
              ->output_section)->dynindx;
2748
0
          addend += h->root.u.def.section->output_offset
2749
0
      + h->root.u.def.value;
2750
0
        }
2751
0
      else if (h && ! BFINFDPIC_FUNCDESC_LOCAL (info, h))
2752
0
        {
2753
0
          if (addend)
2754
0
      {
2755
0
        info->callbacks->warning
2756
0
          (info, _("R_BFIN_FUNCDESC references dynamic symbol with nonzero addend"),
2757
0
           name, input_bfd, input_section, rel->r_offset);
2758
0
        return false;
2759
0
      }
2760
0
          dynindx = h->dynindx;
2761
0
        }
2762
0
      else
2763
0
        {
2764
          /* Otherwise, we know we have a private function
2765
       descriptor, so reference it directly.  */
2766
0
          BFD_ASSERT (picrel->privfd);
2767
0
          r_type = R_BFIN_BYTE4_DATA;
2768
0
          dynindx = elf_section_data (bfinfdpic_got_section (info)
2769
0
              ->output_section)->dynindx;
2770
0
          addend = bfinfdpic_got_section (info)->output_offset
2771
0
      + bfinfdpic_got_initial_offset (info)
2772
0
      + picrel->fd_entry;
2773
0
        }
2774
2775
      /* If there is room for dynamic symbol resolution, emit
2776
         the dynamic relocation.  However, if we're linking an
2777
         executable at a fixed location, we won't have emitted a
2778
         dynamic symbol entry for the got section, so idx will
2779
         be zero, which means we can and should compute the
2780
         address of the private descriptor ourselves.  */
2781
0
      if (bfd_link_pde (info)
2782
0
          && (!h || BFINFDPIC_FUNCDESC_LOCAL (info, h)))
2783
0
        {
2784
0
          bfd_vma offset;
2785
2786
0
          addend += bfinfdpic_got_section (info)->output_section->vma;
2787
0
          if ((bfd_section_flags (input_section->output_section)
2788
0
         & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2789
0
      {
2790
0
        if (_bfinfdpic_osec_readonly_p (output_bfd,
2791
0
                input_section
2792
0
                ->output_section))
2793
0
          {
2794
0
            info->callbacks->warning
2795
0
        (info,
2796
0
         _("cannot emit fixups in read-only section"),
2797
0
         name, input_bfd, input_section, rel->r_offset);
2798
0
            return false;
2799
0
          }
2800
2801
0
        offset = _bfd_elf_section_offset
2802
0
          (output_bfd, info,
2803
0
           input_section, rel->r_offset);
2804
2805
0
        if (offset != (bfd_vma)-1)
2806
0
          _bfinfdpic_add_rofixup (output_bfd,
2807
0
                bfinfdpic_gotfixup_section
2808
0
                (info),
2809
0
                offset + input_section
2810
0
                ->output_section->vma
2811
0
                + input_section->output_offset,
2812
0
                picrel);
2813
0
      }
2814
0
        }
2815
0
      else if ((bfd_section_flags (input_section->output_section)
2816
0
          & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2817
0
        {
2818
0
          bfd_vma offset;
2819
2820
0
          if (_bfinfdpic_osec_readonly_p (output_bfd,
2821
0
                  input_section
2822
0
                  ->output_section))
2823
0
      {
2824
0
        info->callbacks->warning
2825
0
          (info,
2826
0
           _("cannot emit dynamic relocations in read-only section"),
2827
0
           name, input_bfd, input_section, rel->r_offset);
2828
0
        return false;
2829
0
      }
2830
0
          offset = _bfd_elf_section_offset (output_bfd, info,
2831
0
              input_section, rel->r_offset);
2832
2833
0
          if (offset != (bfd_vma)-1)
2834
0
      _bfinfdpic_add_dyn_reloc (output_bfd,
2835
0
              bfinfdpic_gotrel_section (info),
2836
0
              offset + input_section
2837
0
              ->output_section->vma
2838
0
              + input_section->output_offset,
2839
0
              r_type,
2840
0
              dynindx, addend, picrel);
2841
0
        }
2842
0
      else
2843
0
        addend += bfinfdpic_got_section (info)->output_section->vma;
2844
0
    }
2845
2846
        /* We want the addend in-place because dynamic
2847
     relocations are REL.  Setting relocation to it should
2848
     arrange for it to be installed.  */
2849
0
        relocation = addend - rel->r_addend;
2850
0
    }
2851
0
    check_segment[0] = check_segment[1] = got_segment;
2852
0
    break;
2853
2854
0
  case R_BFIN_BYTE4_DATA:
2855
0
    if (! IS_FDPIC (output_bfd))
2856
0
      {
2857
0
        check_segment[0] = check_segment[1] = -1;
2858
0
        break;
2859
0
      }
2860
    /* Fall through.  */
2861
0
  case R_BFIN_FUNCDESC_VALUE:
2862
0
    {
2863
0
      int dynindx;
2864
0
      bfd_vma addend = rel->r_addend;
2865
0
      bfd_vma offset;
2866
0
      offset = _bfd_elf_section_offset (output_bfd, info,
2867
0
                input_section, rel->r_offset);
2868
2869
      /* If the symbol is dynamic but binds locally, use
2870
         section+offset.  */
2871
0
      if (h && ! BFINFDPIC_SYM_LOCAL (info, h))
2872
0
        {
2873
0
    if (addend && r_type == R_BFIN_FUNCDESC_VALUE)
2874
0
      {
2875
0
        info->callbacks->warning
2876
0
          (info, _("R_BFIN_FUNCDESC_VALUE references dynamic symbol with nonzero addend"),
2877
0
           name, input_bfd, input_section, rel->r_offset);
2878
0
        return false;
2879
0
      }
2880
0
    dynindx = h->dynindx;
2881
0
        }
2882
0
      else
2883
0
        {
2884
0
    if (h)
2885
0
      addend += h->root.u.def.value;
2886
0
    else
2887
0
      addend += sym->st_value;
2888
0
    if (osec)
2889
0
      addend += osec->output_offset;
2890
0
    if (osec && osec->output_section
2891
0
        && ! bfd_is_abs_section (osec->output_section)
2892
0
        && ! bfd_is_und_section (osec->output_section))
2893
0
      dynindx = elf_section_data (osec->output_section)->dynindx;
2894
0
    else
2895
0
      dynindx = 0;
2896
0
        }
2897
2898
      /* If we're linking an executable at a fixed address, we
2899
         can omit the dynamic relocation as long as the symbol
2900
         is defined in the current link unit (which is implied
2901
         by its output section not being NULL).  */
2902
0
      if (bfd_link_pde (info)
2903
0
    && (!h || BFINFDPIC_SYM_LOCAL (info, h)))
2904
0
        {
2905
0
    if (osec)
2906
0
      addend += osec->output_section->vma;
2907
0
    if (IS_FDPIC (input_bfd)
2908
0
        && (bfd_section_flags (input_section->output_section)
2909
0
      & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2910
0
      {
2911
0
        if (_bfinfdpic_osec_readonly_p (output_bfd,
2912
0
               input_section
2913
0
               ->output_section))
2914
0
          {
2915
0
      info->callbacks->warning
2916
0
        (info,
2917
0
         _("cannot emit fixups in read-only section"),
2918
0
         name, input_bfd, input_section, rel->r_offset);
2919
0
      return false;
2920
0
          }
2921
0
        if (!h || h->root.type != bfd_link_hash_undefweak)
2922
0
          {
2923
0
      if (offset != (bfd_vma)-1)
2924
0
        {
2925
0
          _bfinfdpic_add_rofixup (output_bfd,
2926
0
                bfinfdpic_gotfixup_section
2927
0
                (info),
2928
0
                offset + input_section
2929
0
                ->output_section->vma
2930
0
                + input_section->output_offset,
2931
0
                picrel);
2932
2933
0
          if (r_type == R_BFIN_FUNCDESC_VALUE)
2934
0
            _bfinfdpic_add_rofixup
2935
0
        (output_bfd,
2936
0
         bfinfdpic_gotfixup_section (info),
2937
0
         offset + input_section->output_section->vma
2938
0
         + input_section->output_offset + 4, picrel);
2939
0
        }
2940
0
          }
2941
0
      }
2942
0
        }
2943
0
      else
2944
0
        {
2945
0
    if ((bfd_section_flags (input_section->output_section)
2946
0
         & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
2947
0
      {
2948
0
        if (_bfinfdpic_osec_readonly_p (output_bfd,
2949
0
               input_section
2950
0
               ->output_section))
2951
0
          {
2952
0
      info->callbacks->warning
2953
0
        (info,
2954
0
         _("cannot emit dynamic relocations in read-only section"),
2955
0
         name, input_bfd, input_section, rel->r_offset);
2956
0
      return false;
2957
0
          }
2958
2959
0
        if (offset != (bfd_vma)-1)
2960
0
          _bfinfdpic_add_dyn_reloc (output_bfd,
2961
0
            bfinfdpic_gotrel_section (info),
2962
0
            offset
2963
0
            + input_section->output_section->vma
2964
0
            + input_section->output_offset,
2965
0
            r_type, dynindx, addend, picrel);
2966
0
      }
2967
0
    else if (osec)
2968
0
      addend += osec->output_section->vma;
2969
    /* We want the addend in-place because dynamic
2970
       relocations are REL.  Setting relocation to it
2971
       should arrange for it to be installed.  */
2972
0
    relocation = addend - rel->r_addend;
2973
0
        }
2974
2975
0
      if (r_type == R_BFIN_FUNCDESC_VALUE)
2976
0
        {
2977
    /* If we've omitted the dynamic relocation, just emit
2978
       the fixed addresses of the symbol and of the local
2979
       GOT base offset.  */
2980
0
    if (bfd_link_pde (info)
2981
0
        && (!h || BFINFDPIC_SYM_LOCAL (info, h)))
2982
0
      bfd_put_32 (output_bfd,
2983
0
            bfinfdpic_got_section (info)->output_section->vma
2984
0
            + bfinfdpic_got_section (info)->output_offset
2985
0
            + bfinfdpic_got_initial_offset (info),
2986
0
            contents + rel->r_offset + 4);
2987
0
    else
2988
      /* A function descriptor used for lazy or local
2989
         resolving is initialized such that its high word
2990
         contains the output section index in which the
2991
         PLT entries are located, and the low word
2992
         contains the offset of the lazy PLT entry entry
2993
         point into that section.  */
2994
0
      bfd_put_32 (output_bfd,
2995
0
            h && ! BFINFDPIC_SYM_LOCAL (info, h)
2996
0
            ? 0
2997
0
            : _bfinfdpic_osec_to_segment (output_bfd,
2998
0
                  sec
2999
0
                  ->output_section),
3000
0
            contents + rel->r_offset + 4);
3001
0
        }
3002
0
    }
3003
0
    check_segment[0] = check_segment[1] = got_segment;
3004
0
    break;
3005
3006
0
  default:
3007
0
    check_segment[0] = isec_segment;
3008
0
    check_segment[1] = sec
3009
0
      ? _bfinfdpic_osec_to_segment (output_bfd, sec->output_section)
3010
0
      : (unsigned)-1;
3011
0
    break;
3012
0
  }
3013
3014
0
      if (check_segment[0] != check_segment[1] && IS_FDPIC (output_bfd))
3015
0
  {
3016
0
#if 1 /* If you take this out, remove the #error from fdpic-static-6.d
3017
   in the ld testsuite.  */
3018
    /* This helps catch problems in GCC while we can't do more
3019
       than static linking.  The idea is to test whether the
3020
       input file basename is crt0.o only once.  */
3021
0
    if (silence_segment_error == 1)
3022
0
      silence_segment_error =
3023
0
        (strlen (bfd_get_filename (input_bfd)) == 6
3024
0
         && filename_cmp (bfd_get_filename (input_bfd), "crt0.o") == 0)
3025
0
        || (strlen (bfd_get_filename (input_bfd)) > 6
3026
0
      && filename_cmp (bfd_get_filename (input_bfd)
3027
0
           + strlen (bfd_get_filename (input_bfd)) - 7,
3028
0
           "/crt0.o") == 0)
3029
0
        ? -1 : 0;
3030
0
#endif
3031
0
    if (!silence_segment_error
3032
        /* We don't want duplicate errors for undefined
3033
     symbols.  */
3034
0
        && !(picrel && picrel->symndx == -1
3035
0
       && picrel->d.h->root.type == bfd_link_hash_undefined))
3036
0
      info->callbacks->warning
3037
0
        (info,
3038
0
         bfd_link_pic (info)
3039
0
         ? _("relocations between different segments are not supported")
3040
0
         : _("warning: relocation references a different segment"),
3041
0
         name, input_bfd, input_section, rel->r_offset);
3042
0
    if (!silence_segment_error && bfd_link_pic (info))
3043
0
      return false;
3044
0
    elf_elfheader (output_bfd)->e_flags |= EF_BFIN_PIC;
3045
0
  }
3046
3047
0
      switch (r_type)
3048
0
  {
3049
0
  case R_BFIN_GOTOFFHI:
3050
    /* We need the addend to be applied before we shift the
3051
       value right.  */
3052
0
    relocation += rel->r_addend;
3053
    /* Fall through.  */
3054
0
  case R_BFIN_GOTHI:
3055
0
  case R_BFIN_FUNCDESC_GOTHI:
3056
0
  case R_BFIN_FUNCDESC_GOTOFFHI:
3057
0
    relocation >>= 16;
3058
    /* Fall through.  */
3059
3060
0
  case R_BFIN_GOTLO:
3061
0
  case R_BFIN_FUNCDESC_GOTLO:
3062
0
  case R_BFIN_GOTOFFLO:
3063
0
  case R_BFIN_FUNCDESC_GOTOFFLO:
3064
0
    relocation &= 0xffff;
3065
0
    break;
3066
3067
0
  default:
3068
0
    break;
3069
0
  }
3070
3071
0
      switch (r_type)
3072
0
  {
3073
0
  case R_BFIN_PCREL24:
3074
0
  case R_BFIN_PCREL24_JUMP_L:
3075
0
    if (! IS_FDPIC (output_bfd) || ! picrel->plt)
3076
0
      break;
3077
    /* Fall through.  */
3078
3079
    /* When referencing a GOT entry, a function descriptor or a
3080
       PLT, we don't want the addend to apply to the reference,
3081
       but rather to the referenced symbol.  The actual entry
3082
       will have already been created taking the addend into
3083
       account, so cancel it out here.  */
3084
0
  case R_BFIN_GOT17M4:
3085
0
  case R_BFIN_GOTHI:
3086
0
  case R_BFIN_GOTLO:
3087
0
  case R_BFIN_FUNCDESC_GOT17M4:
3088
0
  case R_BFIN_FUNCDESC_GOTHI:
3089
0
  case R_BFIN_FUNCDESC_GOTLO:
3090
0
  case R_BFIN_FUNCDESC_GOTOFF17M4:
3091
0
  case R_BFIN_FUNCDESC_GOTOFFHI:
3092
0
  case R_BFIN_FUNCDESC_GOTOFFLO:
3093
    /* Note that we only want GOTOFFHI, not GOTOFFLO or GOTOFF17M4
3094
       here, since we do want to apply the addend to the others.
3095
       Note that we've applied the addend to GOTOFFHI before we
3096
       shifted it right.  */
3097
0
  case R_BFIN_GOTOFFHI:
3098
0
    relocation -= rel->r_addend;
3099
0
    break;
3100
3101
0
  default:
3102
0
    break;
3103
0
  }
3104
3105
0
      r = bfin_final_link_relocate (rel, howto, input_bfd, input_section,
3106
0
            contents, rel->r_offset,
3107
0
            relocation, rel->r_addend);
3108
3109
0
      if (r != bfd_reloc_ok)
3110
0
  {
3111
0
    const char * msg = (const char *) NULL;
3112
3113
0
    switch (r)
3114
0
      {
3115
0
      case bfd_reloc_overflow:
3116
0
        (*info->callbacks->reloc_overflow)
3117
0
    (info, (h ? &h->root : NULL), name, howto->name,
3118
0
     (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
3119
0
        break;
3120
3121
0
      case bfd_reloc_undefined:
3122
0
        (*info->callbacks->undefined_symbol)
3123
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
3124
0
        break;
3125
3126
0
      case bfd_reloc_outofrange:
3127
0
        msg = _("internal error: out of range error");
3128
0
        break;
3129
3130
0
      case bfd_reloc_notsupported:
3131
0
        msg = _("internal error: unsupported relocation error");
3132
0
        break;
3133
3134
0
      case bfd_reloc_dangerous:
3135
0
        msg = _("internal error: dangerous relocation");
3136
0
        break;
3137
3138
0
      default:
3139
0
        msg = _("internal error: unknown error");
3140
0
        break;
3141
0
      }
3142
3143
0
    if (msg)
3144
0
      (*info->callbacks->warning) (info, msg, name, input_bfd,
3145
0
           input_section, rel->r_offset);
3146
0
  }
3147
0
    }
3148
3149
0
  return true;
3150
0
}
3151
3152
/* We need dynamic symbols for every section, since segments can
3153
   relocate independently.  */
3154
static bool
3155
_bfinfdpic_link_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
3156
            struct bfd_link_info *info ATTRIBUTE_UNUSED,
3157
            asection *p)
3158
0
{
3159
0
  switch (elf_section_data (p)->this_hdr.sh_type)
3160
0
    {
3161
0
    case SHT_PROGBITS:
3162
0
    case SHT_NOBITS:
3163
      /* If sh_type is yet undecided, assume it could be
3164
   SHT_PROGBITS/SHT_NOBITS.  */
3165
0
    case SHT_NULL:
3166
0
      return false;
3167
3168
      /* There shouldn't be section relative relocations
3169
   against any other section.  */
3170
0
    default:
3171
0
      return true;
3172
0
    }
3173
0
}
3174
3175
/* Create  a .got section, as well as its additional info field.  This
3176
   is almost entirely copied from
3177
   elflink.c:_bfd_elf_create_got_section().  */
3178
3179
static bool
3180
_bfin_create_got_section (bfd *abfd, struct bfd_link_info *info)
3181
0
{
3182
0
  flagword flags, pltflags;
3183
0
  asection *s;
3184
0
  struct elf_link_hash_entry *h;
3185
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
3186
0
  int ptralign;
3187
3188
  /* This function may be called more than once.  */
3189
0
  s = elf_hash_table (info)->sgot;
3190
0
  if (s != NULL)
3191
0
    return true;
3192
3193
  /* Machine specific: although pointers are 32-bits wide, we want the
3194
     GOT to be aligned to a 64-bit boundary, such that function
3195
     descriptors in it can be accessed with 64-bit loads and
3196
     stores.  */
3197
0
  ptralign = 3;
3198
3199
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3200
0
     | SEC_LINKER_CREATED);
3201
0
  pltflags = flags;
3202
3203
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3204
0
  elf_hash_table (info)->sgot = s;
3205
0
  if (s == NULL
3206
0
      || !bfd_set_section_alignment (s, ptralign))
3207
0
    return false;
3208
3209
0
  if (bed->want_got_sym)
3210
0
    {
3211
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
3212
   (or .got.plt) section.  We don't do this in the linker script
3213
   because we don't want to define the symbol if we are not creating
3214
   a global offset table.  */
3215
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s, "__GLOBAL_OFFSET_TABLE_");
3216
0
      elf_hash_table (info)->hgot = h;
3217
0
      if (h == NULL)
3218
0
  return false;
3219
3220
      /* Machine-specific: we want the symbol for executables as
3221
   well.  */
3222
0
      if (! bfd_elf_link_record_dynamic_symbol (info, h))
3223
0
  return false;
3224
0
    }
3225
3226
  /* The first bit of the global offset table is the header.  */
3227
0
  s->size += bed->got_header_size;
3228
3229
  /* This is the machine-specific part.  Create and initialize section
3230
     data for the got.  */
3231
0
  if (IS_FDPIC (abfd))
3232
0
    {
3233
0
      bfinfdpic_relocs_info (info) = htab_try_create (1,
3234
0
                  bfinfdpic_relocs_info_hash,
3235
0
                  bfinfdpic_relocs_info_eq,
3236
0
                  (htab_del) NULL);
3237
0
      if (! bfinfdpic_relocs_info (info))
3238
0
  return false;
3239
3240
0
      s = bfd_make_section_anyway_with_flags (abfd, ".rel.got",
3241
0
                (flags | SEC_READONLY));
3242
0
      if (s == NULL
3243
0
    || !bfd_set_section_alignment (s, 2))
3244
0
  return false;
3245
3246
0
      bfinfdpic_gotrel_section (info) = s;
3247
3248
      /* Machine-specific.  */
3249
0
      s = bfd_make_section_anyway_with_flags (abfd, ".rofixup",
3250
0
                (flags | SEC_READONLY));
3251
0
      if (s == NULL
3252
0
    || !bfd_set_section_alignment (s, 2))
3253
0
  return false;
3254
3255
0
      bfinfdpic_gotfixup_section (info) = s;
3256
0
    }
3257
3258
0
  pltflags |= SEC_CODE;
3259
0
  if (bed->plt_not_loaded)
3260
0
    pltflags &= ~ (SEC_CODE | SEC_LOAD | SEC_HAS_CONTENTS);
3261
0
  if (bed->plt_readonly)
3262
0
    pltflags |= SEC_READONLY;
3263
3264
0
  s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
3265
0
  if (s == NULL
3266
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
3267
0
    return false;
3268
  /* Blackfin-specific: remember it.  */
3269
0
  bfinfdpic_plt_section (info) = s;
3270
3271
0
  if (bed->want_plt_sym)
3272
0
    {
3273
      /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
3274
   .plt section.  */
3275
0
      struct bfd_link_hash_entry *bh = NULL;
3276
3277
0
      if (! (_bfd_generic_link_add_one_symbol
3278
0
       (info, abfd, "__PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s, 0, NULL,
3279
0
        false, get_elf_backend_data (abfd)->collect, &bh)))
3280
0
  return false;
3281
0
      h = (struct elf_link_hash_entry *) bh;
3282
0
      h->def_regular = 1;
3283
0
      h->type = STT_OBJECT;
3284
3285
0
      if (! bfd_link_executable (info)
3286
0
    && ! bfd_elf_link_record_dynamic_symbol (info, h))
3287
0
  return false;
3288
0
    }
3289
3290
  /* Blackfin-specific: we want rel relocations for the plt.  */
3291
0
  s = bfd_make_section_anyway_with_flags (abfd, ".rel.plt",
3292
0
            flags | SEC_READONLY);
3293
0
  if (s == NULL
3294
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
3295
0
    return false;
3296
  /* Blackfin-specific: remember it.  */
3297
0
  bfinfdpic_pltrel_section (info) = s;
3298
3299
0
  return true;
3300
0
}
3301
3302
/* Make sure the got and plt sections exist, and that our pointers in
3303
   the link hash table point to them.  */
3304
3305
static bool
3306
elf32_bfinfdpic_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
3307
0
{
3308
  /* This is mostly copied from
3309
     elflink.c:_bfd_elf_create_dynamic_sections().  */
3310
0
  flagword flags;
3311
0
  asection *s;
3312
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
3313
3314
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
3315
0
     | SEC_LINKER_CREATED);
3316
3317
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
3318
     .rel[a].bss sections.  */
3319
3320
  /* Blackfin-specific: we want to create the GOT in the Blackfin way.  */
3321
0
  if (! _bfin_create_got_section (abfd, info))
3322
0
    return false;
3323
3324
  /* Blackfin-specific: make sure we created everything we wanted.  */
3325
0
  BFD_ASSERT (bfinfdpic_got_section (info) && bfinfdpic_gotrel_section (info)
3326
        /* && bfinfdpic_gotfixup_section (info) */
3327
0
        && bfinfdpic_plt_section (info)
3328
0
        && bfinfdpic_pltrel_section (info));
3329
3330
0
  if (bed->want_dynbss)
3331
0
    {
3332
      /* The .dynbss section is a place to put symbols which are defined
3333
   by dynamic objects, are referenced by regular objects, and are
3334
   not functions.  We must allocate space for them in the process
3335
   image and use a R_*_COPY reloc to tell the dynamic linker to
3336
   initialize them at run time.  The linker script puts the .dynbss
3337
   section into the .bss section of the final image.  */
3338
0
      s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
3339
0
                SEC_ALLOC | SEC_LINKER_CREATED);
3340
0
      if (s == NULL)
3341
0
  return false;
3342
3343
      /* The .rel[a].bss section holds copy relocs.  This section is not
3344
   normally needed.  We need to create it here, though, so that the
3345
   linker will map it to an output section.  We can't just create it
3346
   only if we need it, because we will not know whether we need it
3347
   until we have seen all the input files, and the first time the
3348
   main linker code calls BFD after examining all the input files
3349
   (size_dynamic_sections) the input sections have already been
3350
   mapped to the output sections.  If the section turns out not to
3351
   be needed, we can discard it later.  We will never need this
3352
   section when generating a shared object, since they do not use
3353
   copy relocs.  */
3354
0
      if (! bfd_link_pic (info))
3355
0
  {
3356
0
    s = bfd_make_section_anyway_with_flags (abfd,
3357
0
              ".rela.bss",
3358
0
              flags | SEC_READONLY);
3359
0
    if (s == NULL
3360
0
        || !bfd_set_section_alignment (s, bed->s->log_file_align))
3361
0
      return false;
3362
0
  }
3363
0
    }
3364
3365
0
  return true;
3366
0
}
3367
3368
/* Compute the total GOT size required by each symbol in each range.
3369
   Symbols may require up to 4 words in the GOT: an entry pointing to
3370
   the symbol, an entry pointing to its function descriptor, and a
3371
   private function descriptors taking two words.  */
3372
3373
static void
3374
_bfinfdpic_count_nontls_entries (struct bfinfdpic_relocs_info *entry,
3375
         struct _bfinfdpic_dynamic_got_info *dinfo)
3376
0
{
3377
  /* Allocate space for a GOT entry pointing to the symbol.  */
3378
0
  if (entry->got17m4)
3379
0
    dinfo->got17m4 += 4;
3380
0
  else if (entry->gothilo)
3381
0
    dinfo->gothilo += 4;
3382
0
  else
3383
0
    entry->relocs32--;
3384
0
  entry->relocs32++;
3385
3386
  /* Allocate space for a GOT entry pointing to the function
3387
     descriptor.  */
3388
0
  if (entry->fdgot17m4)
3389
0
    dinfo->got17m4 += 4;
3390
0
  else if (entry->fdgothilo)
3391
0
    dinfo->gothilo += 4;
3392
0
  else
3393
0
    entry->relocsfd--;
3394
0
  entry->relocsfd++;
3395
3396
  /* Decide whether we need a PLT entry, a function descriptor in the
3397
     GOT, and a lazy PLT entry for this symbol.  */
3398
0
  entry->plt = entry->call
3399
0
    && entry->symndx == -1 && ! BFINFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
3400
0
    && elf_hash_table (dinfo->info)->dynamic_sections_created;
3401
0
  entry->privfd = entry->plt
3402
0
    || entry->fdgoff17m4 || entry->fdgoffhilo
3403
0
    || ((entry->fd || entry->fdgot17m4 || entry->fdgothilo)
3404
0
  && (entry->symndx != -1
3405
0
      || BFINFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h)));
3406
0
  entry->lazyplt = entry->privfd
3407
0
    && entry->symndx == -1 && ! BFINFDPIC_SYM_LOCAL (dinfo->info, entry->d.h)
3408
0
    && ! (dinfo->info->flags & DF_BIND_NOW)
3409
0
    && elf_hash_table (dinfo->info)->dynamic_sections_created;
3410
3411
  /* Allocate space for a function descriptor.  */
3412
0
  if (entry->fdgoff17m4)
3413
0
    dinfo->fd17m4 += 8;
3414
0
  else if (entry->privfd && entry->plt)
3415
0
    dinfo->fdplt += 8;
3416
0
  else if (entry->privfd)
3417
0
    dinfo->fdhilo += 8;
3418
0
  else
3419
0
    entry->relocsfdv--;
3420
0
  entry->relocsfdv++;
3421
3422
0
  if (entry->lazyplt)
3423
0
    dinfo->lzplt += LZPLT_NORMAL_SIZE;
3424
0
}
3425
3426
/* Compute the number of dynamic relocations and fixups that a symbol
3427
   requires, and add (or subtract) from the grand and per-symbol
3428
   totals.  */
3429
3430
static void
3431
_bfinfdpic_count_relocs_fixups (struct bfinfdpic_relocs_info *entry,
3432
        struct _bfinfdpic_dynamic_got_info *dinfo,
3433
        bool subtract)
3434
0
{
3435
0
  bfd_vma relocs = 0, fixups = 0;
3436
3437
0
  if (!bfd_link_pde (dinfo->info))
3438
0
    relocs = entry->relocs32 + entry->relocsfd + entry->relocsfdv;
3439
0
  else
3440
0
    {
3441
0
      if (entry->symndx != -1 || BFINFDPIC_SYM_LOCAL (dinfo->info, entry->d.h))
3442
0
  {
3443
0
    if (entry->symndx != -1
3444
0
        || entry->d.h->root.type != bfd_link_hash_undefweak)
3445
0
      fixups += entry->relocs32 + 2 * entry->relocsfdv;
3446
0
  }
3447
0
      else
3448
0
  relocs += entry->relocs32 + entry->relocsfdv;
3449
3450
0
      if (entry->symndx != -1
3451
0
    || BFINFDPIC_FUNCDESC_LOCAL (dinfo->info, entry->d.h))
3452
0
  {
3453
0
    if (entry->symndx != -1
3454
0
        || entry->d.h->root.type != bfd_link_hash_undefweak)
3455
0
      fixups += entry->relocsfd;
3456
0
  }
3457
0
      else
3458
0
  relocs += entry->relocsfd;
3459
0
    }
3460
3461
0
  if (subtract)
3462
0
    {
3463
0
      relocs = - relocs;
3464
0
      fixups = - fixups;
3465
0
    }
3466
3467
0
  entry->dynrelocs += relocs;
3468
0
  entry->fixups += fixups;
3469
0
  dinfo->relocs += relocs;
3470
0
  dinfo->fixups += fixups;
3471
0
}
3472
3473
/* Compute the total GOT and PLT size required by each symbol in each range. *
3474
   Symbols may require up to 4 words in the GOT: an entry pointing to
3475
   the symbol, an entry pointing to its function descriptor, and a
3476
   private function descriptors taking two words.  */
3477
3478
static int
3479
_bfinfdpic_count_got_plt_entries (void **entryp, void *dinfo_)
3480
0
{
3481
0
  struct bfinfdpic_relocs_info *entry = *entryp;
3482
0
  struct _bfinfdpic_dynamic_got_info *dinfo = dinfo_;
3483
3484
0
  _bfinfdpic_count_nontls_entries (entry, dinfo);
3485
3486
0
  _bfinfdpic_count_relocs_fixups (entry, dinfo, false);
3487
3488
0
  return 1;
3489
0
}
3490
3491
/* This structure is used to assign offsets to got entries, function
3492
   descriptors, plt entries and lazy plt entries.  */
3493
3494
struct _bfinfdpic_dynamic_got_plt_info
3495
{
3496
  /* Summary information collected with _bfinfdpic_count_got_plt_entries.  */
3497
  struct _bfinfdpic_dynamic_got_info g;
3498
3499
  /* For each addressable range, we record a MAX (positive) and MIN
3500
     (negative) value.  CUR is used to assign got entries, and it's
3501
     incremented from an initial positive value to MAX, then from MIN
3502
     to FDCUR (unless FDCUR wraps around first).  FDCUR is used to
3503
     assign function descriptors, and it's decreased from an initial
3504
     non-positive value to MIN, then from MAX down to CUR (unless CUR
3505
     wraps around first).  All of MIN, MAX, CUR and FDCUR always point
3506
     to even words.  ODD, if non-zero, indicates an odd word to be
3507
     used for the next got entry, otherwise CUR is used and
3508
     incremented by a pair of words, wrapping around when it reaches
3509
     MAX.  FDCUR is decremented (and wrapped) before the next function
3510
     descriptor is chosen.  FDPLT indicates the number of remaining
3511
     slots that can be used for function descriptors used only by PLT
3512
     entries.  */
3513
  struct _bfinfdpic_dynamic_got_alloc_data
3514
  {
3515
    bfd_signed_vma max, cur, odd, fdcur, min;
3516
    bfd_vma fdplt;
3517
  } got17m4, gothilo;
3518
};
3519
3520
/* Determine the positive and negative ranges to be used by each
3521
   offset range in the GOT.  FDCUR and CUR, that must be aligned to a
3522
   double-word boundary, are the minimum (negative) and maximum
3523
   (positive) GOT offsets already used by previous ranges, except for
3524
   an ODD entry that may have been left behind.  GOT and FD indicate
3525
   the size of GOT entries and function descriptors that must be
3526
   placed within the range from -WRAP to WRAP.  If there's room left,
3527
   up to FDPLT bytes should be reserved for additional function
3528
   descriptors.  */
3529
3530
inline static bfd_signed_vma
3531
_bfinfdpic_compute_got_alloc_data (struct _bfinfdpic_dynamic_got_alloc_data *gad,
3532
           bfd_signed_vma fdcur,
3533
           bfd_signed_vma odd,
3534
           bfd_signed_vma cur,
3535
           bfd_vma got,
3536
           bfd_vma fd,
3537
           bfd_vma fdplt,
3538
           bfd_vma wrap)
3539
0
{
3540
0
  bfd_signed_vma wrapmin = -wrap;
3541
3542
  /* Start at the given initial points.  */
3543
0
  gad->fdcur = fdcur;
3544
0
  gad->cur = cur;
3545
3546
  /* If we had an incoming odd word and we have any got entries that
3547
     are going to use it, consume it, otherwise leave gad->odd at
3548
     zero.  We might force gad->odd to zero and return the incoming
3549
     odd such that it is used by the next range, but then GOT entries
3550
     might appear to be out of order and we wouldn't be able to
3551
     shorten the GOT by one word if it turns out to end with an
3552
     unpaired GOT entry.  */
3553
0
  if (odd && got)
3554
0
    {
3555
0
      gad->odd = odd;
3556
0
      got -= 4;
3557
0
      odd = 0;
3558
0
    }
3559
0
  else
3560
0
    gad->odd = 0;
3561
3562
  /* If we're left with an unpaired GOT entry, compute its location
3563
     such that we can return it.  Otherwise, if got doesn't require an
3564
     odd number of words here, either odd was already zero in the
3565
     block above, or it was set to zero because got was non-zero, or
3566
     got was already zero.  In the latter case, we want the value of
3567
     odd to carry over to the return statement, so we don't want to
3568
     reset odd unless the condition below is true.  */
3569
0
  if (got & 4)
3570
0
    {
3571
0
      odd = cur + got;
3572
0
      got += 4;
3573
0
    }
3574
3575
  /* Compute the tentative boundaries of this range.  */
3576
0
  gad->max = cur + got;
3577
0
  gad->min = fdcur - fd;
3578
0
  gad->fdplt = 0;
3579
3580
  /* If function descriptors took too much space, wrap some of them
3581
     around.  */
3582
0
  if (gad->min < wrapmin)
3583
0
    {
3584
0
      gad->max += wrapmin - gad->min;
3585
0
      gad->min = wrapmin;
3586
0
    }
3587
  /* If there is space left and we have function descriptors
3588
     referenced in PLT entries that could take advantage of shorter
3589
     offsets, place them here.  */
3590
0
  else if (fdplt && gad->min > wrapmin)
3591
0
    {
3592
0
      bfd_vma fds;
3593
0
      if ((bfd_vma) (gad->min - wrapmin) < fdplt)
3594
0
  fds = gad->min - wrapmin;
3595
0
      else
3596
0
  fds = fdplt;
3597
3598
0
      fdplt -= fds;
3599
0
      gad->min -= fds;
3600
0
      gad->fdplt += fds;
3601
0
    }
3602
3603
  /* If GOT entries took too much space, wrap some of them around.
3604
     This may well cause gad->min to become lower than wrapmin.  This
3605
     will cause a relocation overflow later on, so we don't have to
3606
     report it here . */
3607
0
  if ((bfd_vma) gad->max > wrap)
3608
0
    {
3609
0
      gad->min -= gad->max - wrap;
3610
0
      gad->max = wrap;
3611
0
    }
3612
  /* If there is more space left, try to place some more function
3613
     descriptors for PLT entries.  */
3614
0
  else if (fdplt && (bfd_vma) gad->max < wrap)
3615
0
    {
3616
0
      bfd_vma fds;
3617
0
      if ((bfd_vma) (wrap - gad->max) < fdplt)
3618
0
  fds = wrap - gad->max;
3619
0
      else
3620
0
  fds = fdplt;
3621
3622
0
      fdplt -= fds;
3623
0
      gad->max += fds;
3624
0
      gad->fdplt += fds;
3625
0
    }
3626
3627
  /* If odd was initially computed as an offset past the wrap point,
3628
     wrap it around.  */
3629
0
  if (odd > gad->max)
3630
0
    odd = gad->min + odd - gad->max;
3631
3632
  /* _bfinfdpic_get_got_entry() below will always wrap gad->cur if needed
3633
     before returning, so do it here too.  This guarantees that,
3634
     should cur and fdcur meet at the wrap point, they'll both be
3635
     equal to min.  */
3636
0
  if (gad->cur == gad->max)
3637
0
    gad->cur = gad->min;
3638
3639
0
  return odd;
3640
0
}
3641
3642
/* Compute the location of the next GOT entry, given the allocation
3643
   data for a range.  */
3644
3645
inline static bfd_signed_vma
3646
_bfinfdpic_get_got_entry (struct _bfinfdpic_dynamic_got_alloc_data *gad)
3647
0
{
3648
0
  bfd_signed_vma ret;
3649
3650
0
  if (gad->odd)
3651
0
    {
3652
      /* If there was an odd word left behind, use it.  */
3653
0
      ret = gad->odd;
3654
0
      gad->odd = 0;
3655
0
    }
3656
0
  else
3657
0
    {
3658
      /* Otherwise, use the word pointed to by cur, reserve the next
3659
   as an odd word, and skip to the next pair of words, possibly
3660
   wrapping around.  */
3661
0
      ret = gad->cur;
3662
0
      gad->odd = gad->cur + 4;
3663
0
      gad->cur += 8;
3664
0
      if (gad->cur == gad->max)
3665
0
  gad->cur = gad->min;
3666
0
    }
3667
3668
0
  return ret;
3669
0
}
3670
3671
/* Compute the location of the next function descriptor entry in the
3672
   GOT, given the allocation data for a range.  */
3673
3674
inline static bfd_signed_vma
3675
_bfinfdpic_get_fd_entry (struct _bfinfdpic_dynamic_got_alloc_data *gad)
3676
0
{
3677
  /* If we're at the bottom, wrap around, and only then allocate the
3678
     next pair of words.  */
3679
0
  if (gad->fdcur == gad->min)
3680
0
    gad->fdcur = gad->max;
3681
0
  return gad->fdcur -= 8;
3682
0
}
3683
3684
/* Assign GOT offsets for every GOT entry and function descriptor.
3685
   Doing everything in a single pass is tricky.  */
3686
3687
static int
3688
_bfinfdpic_assign_got_entries (void **entryp, void *info_)
3689
0
{
3690
0
  struct bfinfdpic_relocs_info *entry = *entryp;
3691
0
  struct _bfinfdpic_dynamic_got_plt_info *dinfo = info_;
3692
3693
0
  if (entry->got17m4)
3694
0
    entry->got_entry = _bfinfdpic_get_got_entry (&dinfo->got17m4);
3695
0
  else if (entry->gothilo)
3696
0
    entry->got_entry = _bfinfdpic_get_got_entry (&dinfo->gothilo);
3697
3698
0
  if (entry->fdgot17m4)
3699
0
    entry->fdgot_entry = _bfinfdpic_get_got_entry (&dinfo->got17m4);
3700
0
  else if (entry->fdgothilo)
3701
0
    entry->fdgot_entry = _bfinfdpic_get_got_entry (&dinfo->gothilo);
3702
3703
0
  if (entry->fdgoff17m4)
3704
0
    entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->got17m4);
3705
0
  else if (entry->plt && dinfo->got17m4.fdplt)
3706
0
    {
3707
0
      dinfo->got17m4.fdplt -= 8;
3708
0
      entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->got17m4);
3709
0
    }
3710
0
  else if (entry->plt)
3711
0
    {
3712
0
      dinfo->gothilo.fdplt -= 8;
3713
0
      entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->gothilo);
3714
0
    }
3715
0
  else if (entry->privfd)
3716
0
    entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->gothilo);
3717
3718
0
  return 1;
3719
0
}
3720
3721
/* Assign GOT offsets to private function descriptors used by PLT
3722
   entries (or referenced by 32-bit offsets), as well as PLT entries
3723
   and lazy PLT entries.  */
3724
3725
static int
3726
_bfinfdpic_assign_plt_entries (void **entryp, void *info_)
3727
0
{
3728
0
  struct bfinfdpic_relocs_info *entry = *entryp;
3729
0
  struct _bfinfdpic_dynamic_got_plt_info *dinfo = info_;
3730
3731
  /* If this symbol requires a local function descriptor, allocate
3732
     one.  */
3733
0
  if (entry->privfd && entry->fd_entry == 0)
3734
0
    {
3735
0
      if (dinfo->got17m4.fdplt)
3736
0
  {
3737
0
    entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->got17m4);
3738
0
    dinfo->got17m4.fdplt -= 8;
3739
0
  }
3740
0
      else
3741
0
  {
3742
0
    BFD_ASSERT (dinfo->gothilo.fdplt);
3743
0
    entry->fd_entry = _bfinfdpic_get_fd_entry (&dinfo->gothilo);
3744
0
    dinfo->gothilo.fdplt -= 8;
3745
0
  }
3746
0
    }
3747
3748
0
  if (entry->plt)
3749
0
    {
3750
0
      int size;
3751
3752
      /* We use the section's raw size to mark the location of the
3753
   next PLT entry.  */
3754
0
      entry->plt_entry = bfinfdpic_plt_section (dinfo->g.info)->size;
3755
3756
      /* Figure out the length of this PLT entry based on the
3757
   addressing mode we need to reach the function descriptor.  */
3758
0
      BFD_ASSERT (entry->fd_entry);
3759
0
      if (entry->fd_entry >= -(1 << (18 - 1))
3760
0
    && entry->fd_entry + 4 < (1 << (18 - 1)))
3761
0
  size = 10;
3762
0
      else
3763
0
  size = 16;
3764
3765
0
      bfinfdpic_plt_section (dinfo->g.info)->size += size;
3766
0
    }
3767
3768
0
  if (entry->lazyplt)
3769
0
    {
3770
0
      entry->lzplt_entry = dinfo->g.lzplt;
3771
0
      dinfo->g.lzplt += LZPLT_NORMAL_SIZE;
3772
      /* If this entry is the one that gets the resolver stub, account
3773
   for the additional instruction.  */
3774
0
      if (entry->lzplt_entry % BFINFDPIC_LZPLT_BLOCK_SIZE
3775
0
    == BFINFDPIC_LZPLT_RESOLV_LOC)
3776
0
  dinfo->g.lzplt += LZPLT_RESOLVER_EXTRA;
3777
0
    }
3778
3779
0
  return 1;
3780
0
}
3781
3782
/* Cancel out any effects of calling _bfinfdpic_assign_got_entries and
3783
   _bfinfdpic_assign_plt_entries.  */
3784
3785
static int
3786
_bfinfdpic_reset_got_plt_entries (void **entryp, void *ignore ATTRIBUTE_UNUSED)
3787
0
{
3788
0
  struct bfinfdpic_relocs_info *entry = *entryp;
3789
3790
0
  entry->got_entry = 0;
3791
0
  entry->fdgot_entry = 0;
3792
0
  entry->fd_entry = 0;
3793
0
  entry->plt_entry = (bfd_vma)-1;
3794
0
  entry->lzplt_entry = (bfd_vma)-1;
3795
3796
0
  return 1;
3797
0
}
3798
3799
/* Follow indirect and warning hash entries so that each got entry
3800
   points to the final symbol definition.  P must point to a pointer
3801
   to the hash table we're traversing.  Since this traversal may
3802
   modify the hash table, we set this pointer to NULL to indicate
3803
   we've made a potentially-destructive change to the hash table, so
3804
   the traversal must be restarted.  */
3805
static int
3806
_bfinfdpic_resolve_final_relocs_info (void **entryp, void *p)
3807
0
{
3808
0
  struct bfinfdpic_relocs_info *entry = *entryp;
3809
0
  htab_t *htab = p;
3810
3811
0
  if (entry->symndx == -1)
3812
0
    {
3813
0
      struct elf_link_hash_entry *h = entry->d.h;
3814
0
      struct bfinfdpic_relocs_info *oentry;
3815
3816
0
      while (h->root.type == bfd_link_hash_indirect
3817
0
       || h->root.type == bfd_link_hash_warning)
3818
0
  h = (struct elf_link_hash_entry *)h->root.u.i.link;
3819
3820
0
      if (entry->d.h == h)
3821
0
  return 1;
3822
3823
0
      oentry = bfinfdpic_relocs_info_for_global (*htab, 0, h, entry->addend,
3824
0
            NO_INSERT);
3825
3826
0
      if (oentry)
3827
0
  {
3828
    /* Merge the two entries.  */
3829
0
    bfinfdpic_pic_merge_early_relocs_info (oentry, entry);
3830
0
    htab_clear_slot (*htab, entryp);
3831
0
    return 1;
3832
0
  }
3833
3834
0
      entry->d.h = h;
3835
3836
      /* If we can't find this entry with the new bfd hash, re-insert
3837
   it, and get the traversal restarted.  */
3838
0
      if (! htab_find (*htab, entry))
3839
0
  {
3840
0
    htab_clear_slot (*htab, entryp);
3841
0
    entryp = htab_find_slot (*htab, entry, INSERT);
3842
0
    if (! *entryp)
3843
0
      *entryp = entry;
3844
    /* Abort the traversal, since the whole table may have
3845
       moved, and leave it up to the parent to restart the
3846
       process.  */
3847
0
    *(htab_t *)p = NULL;
3848
0
    return 0;
3849
0
  }
3850
0
    }
3851
3852
0
  return 1;
3853
0
}
3854
3855
/* Compute the total size of the GOT, the PLT, the dynamic relocations
3856
   section and the rofixup section.  Assign locations for GOT and PLT
3857
   entries.  */
3858
3859
static bool
3860
_bfinfdpic_size_got_plt (bfd *output_bfd,
3861
       struct _bfinfdpic_dynamic_got_plt_info *gpinfop)
3862
0
{
3863
0
  bfd_signed_vma odd;
3864
0
  bfd_vma limit;
3865
0
  struct bfd_link_info *info = gpinfop->g.info;
3866
0
  bfd *dynobj = elf_hash_table (info)->dynobj;
3867
3868
0
  memcpy (bfinfdpic_dynamic_got_plt_info (info), &gpinfop->g,
3869
0
    sizeof (gpinfop->g));
3870
3871
0
  odd = 12;
3872
  /* Compute the total size taken by entries in the 18-bit range,
3873
     to tell how many PLT function descriptors we can bring into it
3874
     without causing it to overflow.  */
3875
0
  limit = odd + gpinfop->g.got17m4 + gpinfop->g.fd17m4;
3876
0
  if (limit < (bfd_vma)1 << 18)
3877
0
    limit = ((bfd_vma)1 << 18) - limit;
3878
0
  else
3879
0
    limit = 0;
3880
0
  if (gpinfop->g.fdplt < limit)
3881
0
    limit = gpinfop->g.fdplt;
3882
3883
  /* Determine the ranges of GOT offsets that we can use for each
3884
     range of addressing modes.  */
3885
0
  odd = _bfinfdpic_compute_got_alloc_data (&gpinfop->got17m4,
3886
0
            0,
3887
0
            odd,
3888
0
            16,
3889
0
            gpinfop->g.got17m4,
3890
0
            gpinfop->g.fd17m4,
3891
0
            limit,
3892
0
            (bfd_vma)1 << (18-1));
3893
0
  odd = _bfinfdpic_compute_got_alloc_data (&gpinfop->gothilo,
3894
0
            gpinfop->got17m4.min,
3895
0
            odd,
3896
0
            gpinfop->got17m4.max,
3897
0
            gpinfop->g.gothilo,
3898
0
            gpinfop->g.fdhilo,
3899
0
            gpinfop->g.fdplt - gpinfop->got17m4.fdplt,
3900
0
            (bfd_vma)1 << (32-1));
3901
3902
  /* Now assign (most) GOT offsets.  */
3903
0
  htab_traverse (bfinfdpic_relocs_info (info), _bfinfdpic_assign_got_entries,
3904
0
     gpinfop);
3905
3906
0
  bfinfdpic_got_section (info)->size = gpinfop->gothilo.max
3907
0
    - gpinfop->gothilo.min
3908
    /* If an odd word is the last word of the GOT, we don't need this
3909
       word to be part of the GOT.  */
3910
0
    - (odd + 4 == gpinfop->gothilo.max ? 4 : 0);
3911
0
  if (bfinfdpic_got_section (info)->size == 0)
3912
0
    bfinfdpic_got_section (info)->flags |= SEC_EXCLUDE;
3913
0
  else if (bfinfdpic_got_section (info)->size == 12
3914
0
     && ! elf_hash_table (info)->dynamic_sections_created)
3915
0
    {
3916
0
      bfinfdpic_got_section (info)->flags |= SEC_EXCLUDE;
3917
0
      bfinfdpic_got_section (info)->size = 0;
3918
0
    }
3919
0
  else
3920
0
    {
3921
0
      bfinfdpic_got_section (info)->contents =
3922
0
  (bfd_byte *) bfd_zalloc (dynobj,
3923
0
         bfinfdpic_got_section (info)->size);
3924
0
      if (bfinfdpic_got_section (info)->contents == NULL)
3925
0
  return false;
3926
0
      bfinfdpic_got_section (info)->alloced = 1;
3927
0
    }
3928
3929
0
  if (elf_hash_table (info)->dynamic_sections_created)
3930
    /* Subtract the number of lzplt entries, since those will generate
3931
       relocations in the pltrel section.  */
3932
0
    bfinfdpic_gotrel_section (info)->size =
3933
0
      (gpinfop->g.relocs - gpinfop->g.lzplt / LZPLT_NORMAL_SIZE)
3934
0
      * get_elf_backend_data (output_bfd)->s->sizeof_rel;
3935
0
  else
3936
0
    BFD_ASSERT (gpinfop->g.relocs == 0);
3937
0
  if (bfinfdpic_gotrel_section (info)->size == 0)
3938
0
    bfinfdpic_gotrel_section (info)->flags |= SEC_EXCLUDE;
3939
0
  else
3940
0
    {
3941
0
      bfinfdpic_gotrel_section (info)->contents =
3942
0
  (bfd_byte *) bfd_zalloc (dynobj,
3943
0
         bfinfdpic_gotrel_section (info)->size);
3944
0
      if (bfinfdpic_gotrel_section (info)->contents == NULL)
3945
0
  return false;
3946
0
      bfinfdpic_gotrel_section (info)->alloced = 1;
3947
0
    }
3948
3949
0
  bfinfdpic_gotfixup_section (info)->size = (gpinfop->g.fixups + 1) * 4;
3950
0
  if (bfinfdpic_gotfixup_section (info)->size == 0)
3951
0
    bfinfdpic_gotfixup_section (info)->flags |= SEC_EXCLUDE;
3952
0
  else
3953
0
    {
3954
0
      bfinfdpic_gotfixup_section (info)->contents =
3955
0
  (bfd_byte *) bfd_zalloc (dynobj,
3956
0
         bfinfdpic_gotfixup_section (info)->size);
3957
0
      if (bfinfdpic_gotfixup_section (info)->contents == NULL)
3958
0
  return false;
3959
0
      bfinfdpic_gotfixup_section (info)->alloced = 1;
3960
0
    }
3961
3962
0
  if (elf_hash_table (info)->dynamic_sections_created)
3963
0
    bfinfdpic_pltrel_section (info)->size =
3964
0
      gpinfop->g.lzplt / LZPLT_NORMAL_SIZE * get_elf_backend_data (output_bfd)->s->sizeof_rel;
3965
0
  if (bfinfdpic_pltrel_section (info)->size == 0)
3966
0
    bfinfdpic_pltrel_section (info)->flags |= SEC_EXCLUDE;
3967
0
  else
3968
0
    {
3969
0
      bfinfdpic_pltrel_section (info)->contents =
3970
0
  (bfd_byte *) bfd_zalloc (dynobj,
3971
0
         bfinfdpic_pltrel_section (info)->size);
3972
0
      if (bfinfdpic_pltrel_section (info)->contents == NULL)
3973
0
  return false;
3974
0
      bfinfdpic_pltrel_section (info)->alloced = 1;
3975
0
    }
3976
3977
  /* Add 4 bytes for every block of at most 65535 lazy PLT entries,
3978
     such that there's room for the additional instruction needed to
3979
     call the resolver.  Since _bfinfdpic_assign_got_entries didn't
3980
     account for them, our block size is 4 bytes smaller than the real
3981
     block size.  */
3982
0
  if (elf_hash_table (info)->dynamic_sections_created)
3983
0
    {
3984
0
      bfinfdpic_plt_section (info)->size = gpinfop->g.lzplt
3985
0
  + ((gpinfop->g.lzplt + (BFINFDPIC_LZPLT_BLOCK_SIZE - 4) - LZPLT_NORMAL_SIZE)
3986
0
     / (BFINFDPIC_LZPLT_BLOCK_SIZE - 4) * LZPLT_RESOLVER_EXTRA);
3987
0
    }
3988
3989
  /* Reset it, such that _bfinfdpic_assign_plt_entries() can use it to
3990
     actually assign lazy PLT entries addresses.  */
3991
0
  gpinfop->g.lzplt = 0;
3992
3993
  /* Save information that we're going to need to generate GOT and PLT
3994
     entries.  */
3995
0
  bfinfdpic_got_initial_offset (info) = -gpinfop->gothilo.min;
3996
3997
0
  if (get_elf_backend_data (output_bfd)->want_got_sym)
3998
0
    elf_hash_table (info)->hgot->root.u.def.value
3999
0
      = bfinfdpic_got_initial_offset (info);
4000
4001
0
  if (elf_hash_table (info)->dynamic_sections_created)
4002
0
    bfinfdpic_plt_initial_offset (info) =
4003
0
      bfinfdpic_plt_section (info)->size;
4004
4005
0
  htab_traverse (bfinfdpic_relocs_info (info), _bfinfdpic_assign_plt_entries,
4006
0
     gpinfop);
4007
4008
  /* Allocate the PLT section contents only after
4009
     _bfinfdpic_assign_plt_entries has a chance to add the size of the
4010
     non-lazy PLT entries.  */
4011
0
  if (bfinfdpic_plt_section (info)->size == 0)
4012
0
    bfinfdpic_plt_section (info)->flags |= SEC_EXCLUDE;
4013
0
  else
4014
0
    {
4015
0
      bfinfdpic_plt_section (info)->contents =
4016
0
  (bfd_byte *) bfd_zalloc (dynobj,
4017
0
         bfinfdpic_plt_section (info)->size);
4018
0
      if (bfinfdpic_plt_section (info)->contents == NULL)
4019
0
  return false;
4020
0
      bfinfdpic_plt_section (info)->alloced = 1;
4021
0
    }
4022
4023
0
  return true;
4024
0
}
4025
4026
/* Set the sizes of the dynamic sections.  */
4027
4028
static bool
4029
elf32_bfinfdpic_late_size_sections (bfd *output_bfd,
4030
            struct bfd_link_info *info)
4031
0
{
4032
0
  struct elf_link_hash_table *htab;
4033
0
  bfd *dynobj;
4034
0
  asection *s;
4035
0
  struct _bfinfdpic_dynamic_got_plt_info gpinfo;
4036
4037
0
  htab = elf_hash_table (info);
4038
0
  dynobj = htab->dynobj;
4039
0
  if (dynobj == NULL)
4040
0
    return true;
4041
4042
0
  if (htab->dynamic_sections_created)
4043
0
    {
4044
      /* Set the contents of the .interp section to the interpreter.  */
4045
0
      if (bfd_link_executable (info) && !info->nointerp)
4046
0
  {
4047
0
    s = htab->interp;
4048
0
    BFD_ASSERT (s != NULL);
4049
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4050
0
    s->contents = (bfd_byte *) ELF_DYNAMIC_INTERPRETER;
4051
0
    s->alloced = 1;
4052
0
  }
4053
0
    }
4054
4055
0
  memset (&gpinfo, 0, sizeof (gpinfo));
4056
0
  gpinfo.g.info = info;
4057
4058
0
  for (;;)
4059
0
    {
4060
0
      htab_t relocs = bfinfdpic_relocs_info (info);
4061
4062
0
      htab_traverse (relocs, _bfinfdpic_resolve_final_relocs_info, &relocs);
4063
4064
0
      if (relocs == bfinfdpic_relocs_info (info))
4065
0
  break;
4066
0
    }
4067
4068
0
  htab_traverse (bfinfdpic_relocs_info (info), _bfinfdpic_count_got_plt_entries,
4069
0
     &gpinfo.g);
4070
4071
  /* Allocate space to save the summary information, we're going to
4072
     use it if we're doing relaxations.  */
4073
0
  bfinfdpic_dynamic_got_plt_info (info) = bfd_alloc (dynobj, sizeof (gpinfo.g));
4074
4075
0
  if (!_bfinfdpic_size_got_plt (output_bfd, &gpinfo))
4076
0
      return false;
4077
4078
0
  s = bfd_get_linker_section (dynobj, ".dynbss");
4079
0
  if (s && s->size == 0)
4080
0
    s->flags |= SEC_EXCLUDE;
4081
4082
0
  s = bfd_get_linker_section (dynobj, ".rela.bss");
4083
0
  if (s && s->size == 0)
4084
0
    s->flags |= SEC_EXCLUDE;
4085
4086
0
  return _bfd_elf_add_dynamic_tags (output_bfd, info, true);
4087
0
}
4088
4089
static bool
4090
elf32_bfinfdpic_early_size_sections (bfd *output_bfd,
4091
             struct bfd_link_info *info)
4092
0
{
4093
0
  if (!bfd_link_relocatable (info)
4094
0
      && !bfd_elf_stack_segment_size (output_bfd, info,
4095
0
              "__stacksize", DEFAULT_STACK_SIZE))
4096
0
    return false;
4097
4098
0
  return true;
4099
0
}
4100
4101
/* Check whether any of the relocations was optimized away, and
4102
   subtract it from the relocation or fixup count.  */
4103
static bool
4104
_bfinfdpic_check_discarded_relocs (bfd *abfd, asection *sec,
4105
           struct bfd_link_info *info,
4106
           bool *changed)
4107
0
{
4108
0
  Elf_Internal_Shdr *symtab_hdr;
4109
0
  struct elf_link_hash_entry **sym_hashes;
4110
0
  Elf_Internal_Rela *rel, *erel;
4111
4112
0
  if ((sec->flags & SEC_RELOC) == 0
4113
0
      || sec->reloc_count == 0)
4114
0
    return true;
4115
4116
0
  symtab_hdr = &elf_symtab_hdr (abfd);
4117
0
  sym_hashes = elf_sym_hashes (abfd);
4118
4119
0
  rel = elf_section_data (sec)->relocs;
4120
4121
  /* Now examine each relocation.  */
4122
0
  for (erel = rel + sec->reloc_count; rel < erel; rel++)
4123
0
    {
4124
0
      struct elf_link_hash_entry *h;
4125
0
      unsigned long r_symndx;
4126
0
      struct bfinfdpic_relocs_info *picrel;
4127
0
      struct _bfinfdpic_dynamic_got_info *dinfo;
4128
4129
0
      if (ELF32_R_TYPE (rel->r_info) != R_BFIN_BYTE4_DATA
4130
0
    && ELF32_R_TYPE (rel->r_info) != R_BFIN_FUNCDESC)
4131
0
  continue;
4132
4133
0
      if (_bfd_elf_section_offset (sec->output_section->owner,
4134
0
           info, sec, rel->r_offset)
4135
0
    != (bfd_vma)-1)
4136
0
  continue;
4137
4138
0
      r_symndx = ELF32_R_SYM (rel->r_info);
4139
0
      if (r_symndx < symtab_hdr->sh_info)
4140
0
  h = NULL;
4141
0
      else
4142
0
  {
4143
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4144
0
    while (h->root.type == bfd_link_hash_indirect
4145
0
     || h->root.type == bfd_link_hash_warning)
4146
0
      h = (struct elf_link_hash_entry *)h->root.u.i.link;
4147
0
  }
4148
4149
0
      if (h != NULL)
4150
0
  picrel = bfinfdpic_relocs_info_for_global (bfinfdpic_relocs_info (info),
4151
0
              abfd, h,
4152
0
              rel->r_addend, NO_INSERT);
4153
0
      else
4154
0
  picrel = bfinfdpic_relocs_info_for_local (bfinfdpic_relocs_info (info),
4155
0
             abfd, r_symndx,
4156
0
             rel->r_addend, NO_INSERT);
4157
4158
0
      if (! picrel)
4159
0
  return false;
4160
4161
0
      *changed = true;
4162
0
      dinfo = bfinfdpic_dynamic_got_plt_info (info);
4163
4164
0
      _bfinfdpic_count_relocs_fixups (picrel, dinfo, true);
4165
0
      if (ELF32_R_TYPE (rel->r_info) == R_BFIN_BYTE4_DATA)
4166
0
  picrel->relocs32--;
4167
0
      else /* we know (ELF32_R_TYPE (rel->r_info) == R_BFIN_FUNCDESC) */
4168
0
  picrel->relocsfd--;
4169
0
      _bfinfdpic_count_relocs_fixups (picrel, dinfo, false);
4170
0
    }
4171
4172
0
  return true;
4173
0
}
4174
4175
static bool
4176
bfinfdpic_elf_discard_info (bfd *ibfd,
4177
         struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
4178
         struct bfd_link_info *info)
4179
0
{
4180
0
  bool changed = false;
4181
0
  asection *s;
4182
0
  bfd *obfd = NULL;
4183
4184
  /* Account for relaxation of .eh_frame section.  */
4185
0
  for (s = ibfd->sections; s; s = s->next)
4186
0
    if (s->sec_info_type == SEC_INFO_TYPE_EH_FRAME)
4187
0
      {
4188
0
  if (!_bfinfdpic_check_discarded_relocs (ibfd, s, info, &changed))
4189
0
    return false;
4190
0
  obfd = s->output_section->owner;
4191
0
      }
4192
4193
0
  if (changed)
4194
0
    {
4195
0
      struct _bfinfdpic_dynamic_got_plt_info gpinfo;
4196
4197
0
      memset (&gpinfo, 0, sizeof (gpinfo));
4198
0
      memcpy (&gpinfo.g, bfinfdpic_dynamic_got_plt_info (info),
4199
0
        sizeof (gpinfo.g));
4200
4201
      /* Clear GOT and PLT assignments.  */
4202
0
      htab_traverse (bfinfdpic_relocs_info (info),
4203
0
         _bfinfdpic_reset_got_plt_entries,
4204
0
         NULL);
4205
4206
0
      if (!_bfinfdpic_size_got_plt (obfd, &gpinfo))
4207
0
  return false;
4208
0
    }
4209
4210
0
  return true;
4211
0
}
4212
4213
static bool
4214
elf32_bfinfdpic_finish_dynamic_sections (bfd *output_bfd,
4215
           struct bfd_link_info *info,
4216
           bfd_byte *buf ATTRIBUTE_UNUSED)
4217
0
{
4218
0
  bfd *dynobj;
4219
0
  asection *sdyn;
4220
4221
0
  dynobj = elf_hash_table (info)->dynobj;
4222
4223
0
  if (bfinfdpic_got_section (info))
4224
0
    {
4225
0
      BFD_ASSERT (bfinfdpic_gotrel_section (info)->size
4226
      /* PR 17334: It appears that the GOT section can end up
4227
         being bigger than the number of relocs.  Presumably
4228
         because some relocs have been deleted.  A test case has
4229
         yet to be generated for verify this, but in the meantime
4230
         the test below has been changed from == to >= so that
4231
         applications can continue to be built.  */
4232
0
      >= (bfinfdpic_gotrel_section (info)->reloc_count
4233
0
          * sizeof (Elf32_External_Rel)));
4234
4235
0
      if (bfinfdpic_gotfixup_section (info))
4236
0
  {
4237
0
    struct elf_link_hash_entry *hgot = elf_hash_table (info)->hgot;
4238
0
    bfd_vma got_value = hgot->root.u.def.value
4239
0
      + hgot->root.u.def.section->output_section->vma
4240
0
      + hgot->root.u.def.section->output_offset;
4241
4242
0
    _bfinfdpic_add_rofixup (output_bfd, bfinfdpic_gotfixup_section (info),
4243
0
         got_value, 0);
4244
4245
0
    if (bfinfdpic_gotfixup_section (info)->size
4246
0
        != (bfinfdpic_gotfixup_section (info)->reloc_count * 4))
4247
0
      {
4248
0
        _bfd_error_handler
4249
0
    ("LINKER BUG: .rofixup section size mismatch");
4250
0
        return false;
4251
0
      }
4252
0
  }
4253
0
    }
4254
0
  if (elf_hash_table (info)->dynamic_sections_created)
4255
0
    {
4256
0
      BFD_ASSERT (bfinfdpic_pltrel_section (info)->size
4257
0
      == (bfinfdpic_pltrel_section (info)->reloc_count
4258
0
          * sizeof (Elf32_External_Rel)));
4259
0
    }
4260
4261
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4262
4263
0
  if (elf_hash_table (info)->dynamic_sections_created)
4264
0
    {
4265
0
      Elf32_External_Dyn * dyncon;
4266
0
      Elf32_External_Dyn * dynconend;
4267
4268
0
      BFD_ASSERT (sdyn != NULL);
4269
4270
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
4271
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4272
4273
0
      for (; dyncon < dynconend; dyncon++)
4274
0
  {
4275
0
    Elf_Internal_Dyn dyn;
4276
4277
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4278
4279
0
    switch (dyn.d_tag)
4280
0
      {
4281
0
      default:
4282
0
        break;
4283
4284
0
      case DT_PLTGOT:
4285
0
        dyn.d_un.d_ptr = bfinfdpic_got_section (info)->output_section->vma
4286
0
    + bfinfdpic_got_section (info)->output_offset
4287
0
    + bfinfdpic_got_initial_offset (info);
4288
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4289
0
        break;
4290
4291
0
      case DT_JMPREL:
4292
0
        dyn.d_un.d_ptr = bfinfdpic_pltrel_section (info)
4293
0
    ->output_section->vma
4294
0
    + bfinfdpic_pltrel_section (info)->output_offset;
4295
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4296
0
        break;
4297
4298
0
      case DT_PLTRELSZ:
4299
0
        dyn.d_un.d_val = bfinfdpic_pltrel_section (info)->size;
4300
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
4301
0
        break;
4302
0
      }
4303
0
  }
4304
0
    }
4305
4306
0
  return true;
4307
0
}
4308
4309
/* Adjust a symbol defined by a dynamic object and referenced by a
4310
   regular object.  */
4311
4312
static bool
4313
elf32_bfinfdpic_adjust_dynamic_symbol (struct bfd_link_info *info,
4314
               struct elf_link_hash_entry *h)
4315
0
{
4316
0
  bfd * dynobj;
4317
4318
0
  dynobj = elf_hash_table (info)->dynobj;
4319
4320
  /* Make sure we know what is going on here.  */
4321
0
  BFD_ASSERT (dynobj != NULL
4322
0
        && (h->is_weakalias
4323
0
      || (h->def_dynamic
4324
0
          && h->ref_regular
4325
0
          && !h->def_regular)));
4326
4327
  /* If this is a weak symbol, and there is a real definition, the
4328
     processor independent code will have arranged for us to see the
4329
     real definition first, and we can just use the same value.  */
4330
0
  if (h->is_weakalias)
4331
0
    {
4332
0
      struct elf_link_hash_entry *def = weakdef (h);
4333
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4334
0
      h->root.u.def.section = def->root.u.def.section;
4335
0
      h->root.u.def.value = def->root.u.def.value;
4336
0
    }
4337
4338
0
  return true;
4339
0
}
4340
4341
/* Perform any actions needed for dynamic symbols.  */
4342
4343
static bool
4344
elf32_bfinfdpic_finish_dynamic_symbol
4345
(bfd *output_bfd ATTRIBUTE_UNUSED,
4346
 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4347
 struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
4348
 Elf_Internal_Sym *sym ATTRIBUTE_UNUSED)
4349
0
{
4350
0
  return true;
4351
0
}
4352
4353
/* Decide whether to attempt to turn absptr or lsda encodings in
4354
   shared libraries into pcrel within the given input section.  */
4355
4356
static bool
4357
bfinfdpic_elf_use_relative_eh_frame
4358
(bfd *input_bfd ATTRIBUTE_UNUSED,
4359
 struct bfd_link_info *info ATTRIBUTE_UNUSED,
4360
 asection *eh_frame_section ATTRIBUTE_UNUSED)
4361
0
{
4362
  /* We can't use PC-relative encodings in FDPIC binaries, in general.  */
4363
0
  return false;
4364
0
}
4365
4366
/* Adjust the contents of an eh_frame_hdr section before they're output.  */
4367
4368
static bfd_byte
4369
bfinfdpic_elf_encode_eh_address (bfd *abfd,
4370
        struct bfd_link_info *info,
4371
        asection *osec, bfd_vma offset,
4372
        asection *loc_sec, bfd_vma loc_offset,
4373
        bfd_vma *encoded)
4374
0
{
4375
0
  struct elf_link_hash_entry *h;
4376
4377
0
  h = elf_hash_table (info)->hgot;
4378
0
  BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
4379
4380
0
  if (! h || (_bfinfdpic_osec_to_segment (abfd, osec)
4381
0
        == _bfinfdpic_osec_to_segment (abfd, loc_sec->output_section)))
4382
0
    return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
4383
0
               loc_sec, loc_offset, encoded);
4384
4385
0
  BFD_ASSERT (_bfinfdpic_osec_to_segment (abfd, osec)
4386
0
        == (_bfinfdpic_osec_to_segment
4387
0
      (abfd, h->root.u.def.section->output_section)));
4388
4389
0
  *encoded = osec->vma + offset
4390
0
    - (h->root.u.def.value
4391
0
       + h->root.u.def.section->output_section->vma
4392
0
       + h->root.u.def.section->output_offset);
4393
4394
0
  return DW_EH_PE_datarel | DW_EH_PE_sdata4;
4395
0
}
4396
4397
4398
4399
/* Look through the relocs for a section during the first phase.
4400
4401
   Besides handling virtual table relocs for gc, we have to deal with
4402
   all sorts of PIC-related relocations.  We describe below the
4403
   general plan on how to handle such relocations, even though we only
4404
   collect information at this point, storing them in hash tables for
4405
   perusal of later passes.
4406
4407
   32 relocations are propagated to the linker output when creating
4408
   position-independent output.  LO16 and HI16 relocations are not
4409
   supposed to be encountered in this case.
4410
4411
   LABEL16 should always be resolvable by the linker, since it's only
4412
   used by branches.
4413
4414
   LABEL24, on the other hand, is used by calls.  If it turns out that
4415
   the target of a call is a dynamic symbol, a PLT entry must be
4416
   created for it, which triggers the creation of a private function
4417
   descriptor and, unless lazy binding is disabled, a lazy PLT entry.
4418
4419
   GPREL relocations require the referenced symbol to be in the same
4420
   segment as _gp, but this can only be checked later.
4421
4422
   All GOT, GOTOFF and FUNCDESC relocations require a .got section to
4423
   exist.  LABEL24 might as well, since it may require a PLT entry,
4424
   that will require a got.
4425
4426
   Non-FUNCDESC GOT relocations require a GOT entry to be created
4427
   regardless of whether the symbol is dynamic.  However, since a
4428
   global symbol that turns out to not be exported may have the same
4429
   address of a non-dynamic symbol, we don't assign GOT entries at
4430
   this point, such that we can share them in this case.  A relocation
4431
   for the GOT entry always has to be created, be it to offset a
4432
   private symbol by the section load address, be it to get the symbol
4433
   resolved dynamically.
4434
4435
   FUNCDESC GOT relocations require a GOT entry to be created, and
4436
   handled as if a FUNCDESC relocation was applied to the GOT entry in
4437
   an object file.
4438
4439
   FUNCDESC relocations referencing a symbol that turns out to NOT be
4440
   dynamic cause a private function descriptor to be created.  The
4441
   FUNCDESC relocation then decays to a 32 relocation that points at
4442
   the private descriptor.  If the symbol is dynamic, the FUNCDESC
4443
   relocation is propagated to the linker output, such that the
4444
   dynamic linker creates the canonical descriptor, pointing to the
4445
   dynamically-resolved definition of the function.
4446
4447
   Non-FUNCDESC GOTOFF relocations must always refer to non-dynamic
4448
   symbols that are assigned to the same segment as the GOT, but we
4449
   can only check this later, after we know the complete set of
4450
   symbols defined and/or exported.
4451
4452
   FUNCDESC GOTOFF relocations require a function descriptor to be
4453
   created and, unless lazy binding is disabled or the symbol is not
4454
   dynamic, a lazy PLT entry.  Since we can't tell at this point
4455
   whether a symbol is going to be dynamic, we have to decide later
4456
   whether to create a lazy PLT entry or bind the descriptor directly
4457
   to the private function.
4458
4459
   FUNCDESC_VALUE relocations are not supposed to be present in object
4460
   files, but they may very well be simply propagated to the linker
4461
   output, since they have no side effect.
4462
4463
4464
   A function descriptor always requires a FUNCDESC_VALUE relocation.
4465
   Whether it's in .plt.rel or not depends on whether lazy binding is
4466
   enabled and on whether the referenced symbol is dynamic.
4467
4468
   The existence of a lazy PLT requires the resolverStub lazy PLT
4469
   entry to be present.
4470
4471
4472
   As for assignment of GOT, PLT and lazy PLT entries, and private
4473
   descriptors, we might do them all sequentially, but we can do
4474
   better than that.  For example, we can place GOT entries and
4475
   private function descriptors referenced using 12-bit operands
4476
   closer to the PIC register value, such that these relocations don't
4477
   overflow.  Those that are only referenced with LO16 relocations
4478
   could come next, but we may as well place PLT-required function
4479
   descriptors in the 12-bit range to make them shorter.  Symbols
4480
   referenced with LO16/HI16 may come next, but we may place
4481
   additional function descriptors in the 16-bit range if we can
4482
   reliably tell that we've already placed entries that are ever
4483
   referenced with only LO16.  PLT entries are therefore generated as
4484
   small as possible, while not introducing relocation overflows in
4485
   GOT or FUNCDESC_GOTOFF relocations.  Lazy PLT entries could be
4486
   generated before or after PLT entries, but not intermingled with
4487
   them, such that we can have more lazy PLT entries in range for a
4488
   branch to the resolverStub.  The resolverStub should be emitted at
4489
   the most distant location from the first lazy PLT entry such that
4490
   it's still in range for a branch, or closer, if there isn't a need
4491
   for so many lazy PLT entries.  Additional lazy PLT entries may be
4492
   emitted after the resolverStub, as long as branches are still in
4493
   range.  If the branch goes out of range, longer lazy PLT entries
4494
   are emitted.
4495
4496
   We could further optimize PLT and lazy PLT entries by giving them
4497
   priority in assignment to closer-to-gr17 locations depending on the
4498
   number of occurrences of references to them (assuming a function
4499
   that's called more often is more important for performance, so its
4500
   PLT entry should be faster), or taking hints from the compiler.
4501
   Given infinite time and money... :-)  */
4502
4503
static bool
4504
bfinfdpic_check_relocs (bfd *abfd, struct bfd_link_info *info,
4505
      asection *sec, const Elf_Internal_Rela *relocs)
4506
0
{
4507
0
  Elf_Internal_Shdr *symtab_hdr;
4508
0
  struct elf_link_hash_entry **sym_hashes;
4509
0
  const Elf_Internal_Rela *rel;
4510
0
  const Elf_Internal_Rela *rel_end;
4511
0
  bfd *dynobj;
4512
0
  struct bfinfdpic_relocs_info *picrel;
4513
4514
0
  if (bfd_link_relocatable (info))
4515
0
    return true;
4516
4517
0
  symtab_hdr = &elf_symtab_hdr (abfd);
4518
0
  sym_hashes = elf_sym_hashes (abfd);
4519
4520
0
  dynobj = elf_hash_table (info)->dynobj;
4521
0
  rel_end = relocs + sec->reloc_count;
4522
0
  for (rel = relocs; rel < rel_end; rel++)
4523
0
    {
4524
0
      struct elf_link_hash_entry *h;
4525
0
      unsigned long r_symndx;
4526
4527
0
      r_symndx = ELF32_R_SYM (rel->r_info);
4528
0
      if (r_symndx < symtab_hdr->sh_info)
4529
0
  h = NULL;
4530
0
      else
4531
0
  {
4532
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
4533
0
    while (h->root.type == bfd_link_hash_indirect
4534
0
     || h->root.type == bfd_link_hash_warning)
4535
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
4536
0
  }
4537
4538
0
      switch (ELF32_R_TYPE (rel->r_info))
4539
0
  {
4540
0
  case R_BFIN_GOT17M4:
4541
0
  case R_BFIN_GOTHI:
4542
0
  case R_BFIN_GOTLO:
4543
0
  case R_BFIN_FUNCDESC_GOT17M4:
4544
0
  case R_BFIN_FUNCDESC_GOTHI:
4545
0
  case R_BFIN_FUNCDESC_GOTLO:
4546
0
  case R_BFIN_GOTOFF17M4:
4547
0
  case R_BFIN_GOTOFFHI:
4548
0
  case R_BFIN_GOTOFFLO:
4549
0
  case R_BFIN_FUNCDESC_GOTOFF17M4:
4550
0
  case R_BFIN_FUNCDESC_GOTOFFHI:
4551
0
  case R_BFIN_FUNCDESC_GOTOFFLO:
4552
0
  case R_BFIN_FUNCDESC:
4553
0
  case R_BFIN_FUNCDESC_VALUE:
4554
0
    if (! IS_FDPIC (abfd))
4555
0
      goto bad_reloc;
4556
    /* Fall through.  */
4557
0
  case R_BFIN_PCREL24:
4558
0
  case R_BFIN_PCREL24_JUMP_L:
4559
0
  case R_BFIN_BYTE4_DATA:
4560
0
    if (IS_FDPIC (abfd) && ! dynobj)
4561
0
      {
4562
0
        elf_hash_table (info)->dynobj = dynobj = abfd;
4563
0
        if (! _bfin_create_got_section (abfd, info))
4564
0
    return false;
4565
0
      }
4566
0
    if (! IS_FDPIC (abfd))
4567
0
      {
4568
0
        picrel = NULL;
4569
0
        break;
4570
0
      }
4571
0
    if (h != NULL)
4572
0
      {
4573
0
        if (h->dynindx == -1)
4574
0
    switch (ELF_ST_VISIBILITY (h->other))
4575
0
      {
4576
0
      case STV_INTERNAL:
4577
0
      case STV_HIDDEN:
4578
0
        break;
4579
0
      default:
4580
0
        bfd_elf_link_record_dynamic_symbol (info, h);
4581
0
        break;
4582
0
      }
4583
0
        picrel
4584
0
    = bfinfdpic_relocs_info_for_global (bfinfdpic_relocs_info (info),
4585
0
               abfd, h,
4586
0
               rel->r_addend, INSERT);
4587
0
      }
4588
0
    else
4589
0
      picrel = bfinfdpic_relocs_info_for_local (bfinfdpic_relocs_info
4590
0
                 (info), abfd, r_symndx,
4591
0
                 rel->r_addend, INSERT);
4592
0
    if (! picrel)
4593
0
      return false;
4594
0
    break;
4595
4596
0
  default:
4597
0
    picrel = NULL;
4598
0
    break;
4599
0
  }
4600
4601
0
      switch (ELF32_R_TYPE (rel->r_info))
4602
0
  {
4603
0
  case R_BFIN_PCREL24:
4604
0
  case R_BFIN_PCREL24_JUMP_L:
4605
0
    if (IS_FDPIC (abfd))
4606
0
      picrel->call++;
4607
0
    break;
4608
4609
0
  case R_BFIN_FUNCDESC_VALUE:
4610
0
    picrel->relocsfdv++;
4611
0
    if (bfd_section_flags (sec) & SEC_ALLOC)
4612
0
      picrel->relocs32--;
4613
    /* Fall through.  */
4614
4615
0
  case R_BFIN_BYTE4_DATA:
4616
0
    if (! IS_FDPIC (abfd))
4617
0
      break;
4618
4619
0
    picrel->sym++;
4620
0
    if (bfd_section_flags (sec) & SEC_ALLOC)
4621
0
      picrel->relocs32++;
4622
0
    break;
4623
4624
0
  case R_BFIN_GOT17M4:
4625
0
    picrel->got17m4++;
4626
0
    break;
4627
4628
0
  case R_BFIN_GOTHI:
4629
0
  case R_BFIN_GOTLO:
4630
0
    picrel->gothilo++;
4631
0
    break;
4632
4633
0
  case R_BFIN_FUNCDESC_GOT17M4:
4634
0
    picrel->fdgot17m4++;
4635
0
    break;
4636
4637
0
  case R_BFIN_FUNCDESC_GOTHI:
4638
0
  case R_BFIN_FUNCDESC_GOTLO:
4639
0
    picrel->fdgothilo++;
4640
0
    break;
4641
4642
0
  case R_BFIN_GOTOFF17M4:
4643
0
  case R_BFIN_GOTOFFHI:
4644
0
  case R_BFIN_GOTOFFLO:
4645
0
    picrel->gotoff++;
4646
0
    break;
4647
4648
0
  case R_BFIN_FUNCDESC_GOTOFF17M4:
4649
0
    picrel->fdgoff17m4++;
4650
0
    break;
4651
4652
0
  case R_BFIN_FUNCDESC_GOTOFFHI:
4653
0
  case R_BFIN_FUNCDESC_GOTOFFLO:
4654
0
    picrel->fdgoffhilo++;
4655
0
    break;
4656
4657
0
  case R_BFIN_FUNCDESC:
4658
0
    picrel->fd++;
4659
0
    picrel->relocsfd++;
4660
0
    break;
4661
4662
  /* This relocation describes the C++ object vtable hierarchy.
4663
     Reconstruct it for later use during GC.  */
4664
0
  case R_BFIN_GNU_VTINHERIT:
4665
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
4666
0
      return false;
4667
0
    break;
4668
4669
  /* This relocation describes which C++ vtable entries are actually
4670
     used.  Record for later use during GC.  */
4671
0
  case R_BFIN_GNU_VTENTRY:
4672
0
    BFD_ASSERT (h != NULL);
4673
0
    if (h != NULL
4674
0
        && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
4675
0
      return false;
4676
0
    break;
4677
4678
0
  case R_BFIN_HUIMM16:
4679
0
  case R_BFIN_LUIMM16:
4680
0
  case R_BFIN_PCREL12_JUMP_S:
4681
0
  case R_BFIN_PCREL10:
4682
0
    break;
4683
4684
0
  default:
4685
0
  bad_reloc:
4686
0
    _bfd_error_handler
4687
      /* xgettext:c-format */
4688
0
      (_("%pB: unsupported relocation type %#x"),
4689
0
       abfd, (int) ELF32_R_TYPE (rel->r_info));
4690
0
    return false;
4691
0
  }
4692
0
    }
4693
4694
0
  return true;
4695
0
}
4696
4697
/* Set the right machine number for a Blackfin ELF file.  */
4698
4699
static bool
4700
elf32_bfin_object_p (bfd *abfd)
4701
1.27k
{
4702
1.27k
  bfd_default_set_arch_mach (abfd, bfd_arch_bfin, 0);
4703
1.27k
  return (((elf_elfheader (abfd)->e_flags & EF_BFIN_FDPIC) != 0)
4704
1.27k
    == (IS_FDPIC (abfd)));
4705
1.27k
}
4706
4707
static bool
4708
elf32_bfin_set_private_flags (bfd * abfd, flagword flags)
4709
0
{
4710
0
  elf_elfheader (abfd)->e_flags = flags;
4711
0
  elf_flags_init (abfd) = true;
4712
0
  return true;
4713
0
}
4714
4715
/* Display the flags field.  */
4716
static bool
4717
elf32_bfin_print_private_bfd_data (bfd * abfd, void * ptr)
4718
600
{
4719
600
  FILE *file = (FILE *) ptr;
4720
600
  flagword flags;
4721
4722
600
  BFD_ASSERT (abfd != NULL && ptr != NULL);
4723
4724
  /* Print normal ELF private data.  */
4725
600
  _bfd_elf_print_private_bfd_data (abfd, ptr);
4726
4727
600
  flags = elf_elfheader (abfd)->e_flags;
4728
4729
  /* xgettext:c-format */
4730
600
  fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
4731
4732
600
  if (flags & EF_BFIN_PIC)
4733
0
    fprintf (file, " -fpic");
4734
4735
600
  if (flags & EF_BFIN_FDPIC)
4736
0
    fprintf (file, " -mfdpic");
4737
4738
600
  fputc ('\n', file);
4739
4740
600
  return true;
4741
600
}
4742
4743
/* Merge backend specific data from an object file to the output
4744
   object file when linking.  */
4745
4746
static bool
4747
elf32_bfin_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
4748
0
{
4749
0
  bfd *obfd = info->output_bfd;
4750
0
  flagword old_flags, new_flags;
4751
0
  bool error = false;
4752
4753
  /* FIXME: What should be checked when linking shared libraries?  */
4754
0
  if ((ibfd->flags & DYNAMIC) != 0)
4755
0
    return true;
4756
4757
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
4758
0
    return true;
4759
4760
0
  new_flags = elf_elfheader (ibfd)->e_flags;
4761
0
  old_flags = elf_elfheader (obfd)->e_flags;
4762
4763
0
  if (new_flags & EF_BFIN_FDPIC)
4764
0
    new_flags &= ~EF_BFIN_PIC;
4765
4766
0
#ifndef DEBUG
4767
0
  if (0)
4768
0
#endif
4769
0
  _bfd_error_handler
4770
0
    ("old_flags = 0x%.8x, new_flags = 0x%.8x, init = %s, filename = %pB",
4771
0
     old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no", ibfd);
4772
4773
0
  if (!elf_flags_init (obfd))      /* First call, no flags set.  */
4774
0
    {
4775
0
      elf_flags_init (obfd) = true;
4776
0
      elf_elfheader (obfd)->e_flags = new_flags;
4777
0
    }
4778
4779
0
  if (((new_flags & EF_BFIN_FDPIC) == 0) != (! IS_FDPIC (obfd)))
4780
0
    {
4781
0
      error = true;
4782
0
      if (IS_FDPIC (obfd))
4783
0
  _bfd_error_handler
4784
0
    (_("%pB: cannot link non-fdpic object file into fdpic executable"),
4785
0
     ibfd);
4786
0
      else
4787
0
  _bfd_error_handler
4788
0
    (_("%pB: cannot link fdpic object file into non-fdpic executable"),
4789
0
     ibfd);
4790
0
    }
4791
4792
0
  if (error)
4793
0
    bfd_set_error (bfd_error_bad_value);
4794
4795
0
  return !error;
4796
0
}
4797

4798
/* bfin ELF linker hash entry.  */
4799
4800
struct bfin_link_hash_entry
4801
{
4802
  struct elf_link_hash_entry root;
4803
4804
  /* Number of PC relative relocs copied for this symbol.  */
4805
  struct bfin_pcrel_relocs_copied *pcrel_relocs_copied;
4806
};
4807
4808
0
#define bfin_hash_entry(ent) ((struct bfin_link_hash_entry *) (ent))
4809
4810
static struct bfd_hash_entry *
4811
bfin_link_hash_newfunc (struct bfd_hash_entry *entry,
4812
      struct bfd_hash_table *table, const char *string)
4813
0
{
4814
0
  struct bfd_hash_entry *ret = entry;
4815
4816
  /* Allocate the structure if it has not already been allocated by a
4817
     subclass.  */
4818
0
  if (ret == NULL)
4819
0
    ret = bfd_hash_allocate (table, sizeof (struct bfin_link_hash_entry));
4820
0
  if (ret == NULL)
4821
0
    return ret;
4822
4823
  /* Call the allocation method of the superclass.  */
4824
0
  ret = _bfd_elf_link_hash_newfunc (ret, table, string);
4825
0
  if (ret != NULL)
4826
0
    bfin_hash_entry (ret)->pcrel_relocs_copied = NULL;
4827
4828
0
  return ret;
4829
0
}
4830
4831
/* Create an bfin ELF linker hash table.  */
4832
4833
static struct bfd_link_hash_table *
4834
bfin_link_hash_table_create (bfd * abfd)
4835
0
{
4836
0
  struct elf_link_hash_table *ret;
4837
0
  size_t amt = sizeof (struct elf_link_hash_table);
4838
4839
0
  ret = bfd_zmalloc (amt);
4840
0
  if (ret == NULL)
4841
0
    return NULL;
4842
4843
0
  if (!_bfd_elf_link_hash_table_init (ret, abfd, bfin_link_hash_newfunc,
4844
0
              sizeof (struct elf_link_hash_entry)))
4845
0
    {
4846
0
      free (ret);
4847
0
      return NULL;
4848
0
    }
4849
4850
0
  return &ret->root;
4851
0
}
4852
4853
/* The size in bytes of an entry in the procedure linkage table.  */
4854
4855
/* Finish up the dynamic sections.  */
4856
4857
static bool
4858
bfin_finish_dynamic_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
4859
            struct bfd_link_info *info,
4860
            bfd_byte *buf ATTRIBUTE_UNUSED)
4861
0
{
4862
0
  bfd *dynobj;
4863
0
  asection *sdyn;
4864
4865
0
  dynobj = elf_hash_table (info)->dynobj;
4866
4867
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4868
4869
0
  if (elf_hash_table (info)->dynamic_sections_created)
4870
0
    {
4871
0
      Elf32_External_Dyn *dyncon, *dynconend;
4872
4873
0
      BFD_ASSERT (sdyn != NULL);
4874
4875
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
4876
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4877
0
      for (; dyncon < dynconend; dyncon++)
4878
0
  {
4879
0
    Elf_Internal_Dyn dyn;
4880
4881
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4882
4883
0
  }
4884
4885
0
    }
4886
0
  return true;
4887
0
}
4888
4889
/* Finish up dynamic symbol handling.  We set the contents of various
4890
   dynamic sections here.  */
4891
4892
static bool
4893
bfin_finish_dynamic_symbol (bfd * output_bfd,
4894
          struct bfd_link_info *info,
4895
          struct elf_link_hash_entry *h,
4896
          Elf_Internal_Sym * sym)
4897
0
{
4898
0
  if (h->got.offset != (bfd_vma) - 1)
4899
0
    {
4900
0
      asection *sgot;
4901
0
      asection *srela;
4902
0
      Elf_Internal_Rela rela;
4903
0
      bfd_byte *loc;
4904
4905
      /* This symbol has an entry in the global offset table.
4906
   Set it up.  */
4907
4908
0
      sgot = elf_hash_table (info)->sgot;
4909
0
      srela = elf_hash_table (info)->srelgot;
4910
0
      BFD_ASSERT (sgot != NULL && srela != NULL);
4911
4912
0
      rela.r_offset = (sgot->output_section->vma
4913
0
           + sgot->output_offset
4914
0
           + (h->got.offset & ~(bfd_vma) 1));
4915
4916
      /* If this is a -Bsymbolic link, and the symbol is defined
4917
   locally, we just want to emit a RELATIVE reloc.  Likewise if
4918
   the symbol was forced to be local because of a version file.
4919
   The entry in the global offset table will already have been
4920
   initialized in the relocate_section function.  */
4921
0
      if (bfd_link_pic (info)
4922
0
    && (info->symbolic
4923
0
        || h->dynindx == -1 || h->forced_local) && h->def_regular)
4924
0
  {
4925
0
    _bfd_error_handler (_("*** check this relocation %s"), __func__);
4926
0
    rela.r_info = ELF32_R_INFO (0, R_BFIN_PCREL24);
4927
0
    rela.r_addend = bfd_get_signed_32 (output_bfd,
4928
0
               (sgot->contents
4929
0
                +
4930
0
                (h->got.
4931
0
                 offset & ~(bfd_vma) 1)));
4932
0
  }
4933
0
      else
4934
0
  {
4935
0
    bfd_put_32 (output_bfd, (bfd_vma) 0,
4936
0
          sgot->contents + (h->got.offset & ~(bfd_vma) 1));
4937
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_BFIN_GOT);
4938
0
    rela.r_addend = 0;
4939
0
  }
4940
4941
0
      loc = srela->contents;
4942
0
      loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
4943
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4944
0
    }
4945
4946
0
  if (h->needs_copy)
4947
0
    {
4948
0
      BFD_ASSERT (0);
4949
0
    }
4950
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
4951
0
  if (strcmp (h->root.root.string, "__DYNAMIC") == 0
4952
0
      || h == elf_hash_table (info)->hgot)
4953
0
    sym->st_shndx = SHN_ABS;
4954
4955
0
  return true;
4956
0
}
4957
4958
/* Adjust a symbol defined by a dynamic object and referenced by a
4959
   regular object.  The current definition is in some section of the
4960
   dynamic object, but we're not including those sections.  We have to
4961
   change the definition to something the rest of the link can
4962
   understand.  */
4963
4964
static bool
4965
bfin_adjust_dynamic_symbol (struct bfd_link_info *info,
4966
          struct elf_link_hash_entry *h)
4967
0
{
4968
0
  bfd *dynobj;
4969
0
  asection *s;
4970
0
  unsigned int power_of_two;
4971
4972
0
  dynobj = elf_hash_table (info)->dynobj;
4973
4974
  /* Make sure we know what is going on here.  */
4975
0
  BFD_ASSERT (dynobj != NULL
4976
0
        && (h->needs_plt
4977
0
      || h->is_weakalias
4978
0
      || (h->def_dynamic && h->ref_regular && !h->def_regular)));
4979
4980
  /* If this is a function, put it in the procedure linkage table.  We
4981
     will fill in the contents of the procedure linkage table later,
4982
     when we know the address of the .got section.  */
4983
0
  if (h->type == STT_FUNC || h->needs_plt)
4984
0
    {
4985
0
      BFD_ASSERT(0);
4986
0
    }
4987
4988
  /* If this is a weak symbol, and there is a real definition, the
4989
     processor independent code will have arranged for us to see the
4990
     real definition first, and we can just use the same value.  */
4991
0
  if (h->is_weakalias)
4992
0
    {
4993
0
      struct elf_link_hash_entry *def = weakdef (h);
4994
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
4995
0
      h->root.u.def.section = def->root.u.def.section;
4996
0
      h->root.u.def.value = def->root.u.def.value;
4997
0
      return true;
4998
0
    }
4999
5000
  /* This is a reference to a symbol defined by a dynamic object which
5001
     is not a function.  */
5002
5003
  /* If we are creating a shared library, we must presume that the
5004
     only references to the symbol are via the global offset table.
5005
     For such cases we need not do anything here; the relocations will
5006
     be handled correctly by relocate_section.  */
5007
0
  if (bfd_link_pic (info))
5008
0
    return true;
5009
5010
  /* We must allocate the symbol in our .dynbss section, which will
5011
     become part of the .bss section of the executable.  There will be
5012
     an entry for this symbol in the .dynsym section.  The dynamic
5013
     object will contain position independent code, so all references
5014
     from the dynamic object to this symbol will go through the global
5015
     offset table.  The dynamic linker will use the .dynsym entry to
5016
     determine the address it must put in the global offset table, so
5017
     both the dynamic object and the regular object will refer to the
5018
     same memory location for the variable.  */
5019
5020
0
  s = bfd_get_linker_section (dynobj, ".dynbss");
5021
0
  BFD_ASSERT (s != NULL);
5022
5023
#if 0 /* Bfin does not currently have a COPY reloc.  */
5024
  /* We must generate a R_BFIN_COPY reloc to tell the dynamic linker to
5025
     copy the initial value out of the dynamic object and into the
5026
     runtime process image.  We need to remember the offset into the
5027
     .rela.bss section we are going to use.  */
5028
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5029
    {
5030
      asection *srel;
5031
5032
      srel = bfd_get_linker_section (dynobj, ".rela.bss");
5033
      BFD_ASSERT (srel != NULL);
5034
      srel->size += sizeof (Elf32_External_Rela);
5035
      h->needs_copy = 1;
5036
    }
5037
#else
5038
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
5039
0
    {
5040
0
      _bfd_error_handler (_("the bfin target does not currently support the generation of copy relocations"));
5041
0
      return false;
5042
0
    }
5043
0
#endif
5044
  /* We need to figure out the alignment required for this symbol.  I
5045
     have no idea how ELF linkers handle this.  */
5046
0
  power_of_two = bfd_log2 (h->size);
5047
0
  if (power_of_two > 3)
5048
0
    power_of_two = 3;
5049
5050
  /* Apply the required alignment.  */
5051
0
  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
5052
0
  if (!bfd_link_align_section (s, power_of_two))
5053
0
    return false;
5054
5055
  /* Define the symbol as being at this point in the section.  */
5056
0
  h->root.u.def.section = s;
5057
0
  h->root.u.def.value = s->size;
5058
5059
  /* Increment the section size to make room for the symbol.  */
5060
0
  s->size += h->size;
5061
5062
0
  return true;
5063
0
}
5064
5065
/* The bfin linker needs to keep track of the number of relocs that it
5066
   decides to copy in check_relocs for each symbol.  This is so that it
5067
   can discard PC relative relocs if it doesn't need them when linking
5068
   with -Bsymbolic.  We store the information in a field extending the
5069
   regular ELF linker hash table.  */
5070
5071
/* This structure keeps track of the number of PC relative relocs we have
5072
   copied for a given symbol.  */
5073
5074
struct bfin_pcrel_relocs_copied
5075
{
5076
  /* Next section.  */
5077
  struct bfin_pcrel_relocs_copied *next;
5078
  /* A section in dynobj.  */
5079
  asection *section;
5080
  /* Number of relocs copied in this section.  */
5081
  bfd_size_type count;
5082
};
5083
5084
/* This function is called via elf_link_hash_traverse if we are
5085
   creating a shared object.  In the -Bsymbolic case it discards the
5086
   space allocated to copy PC relative relocs against symbols which
5087
   are defined in regular objects.  For the normal shared case, it
5088
   discards space for pc-relative relocs that have become local due to
5089
   symbol visibility changes.  We allocated space for them in the
5090
   check_relocs routine, but we won't fill them in in the
5091
   relocate_section routine.
5092
5093
   We also check whether any of the remaining relocations apply
5094
   against a readonly section, and set the DF_TEXTREL flag in this
5095
   case.  */
5096
5097
static bool
5098
bfin_discard_copies (struct elf_link_hash_entry *h, void * inf)
5099
0
{
5100
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
5101
0
  struct bfin_pcrel_relocs_copied *s;
5102
5103
0
  if (!h->def_regular || (!info->symbolic && !h->forced_local))
5104
0
    {
5105
0
      if ((info->flags & DF_TEXTREL) == 0)
5106
0
  {
5107
    /* Look for relocations against read-only sections.  */
5108
0
    for (s = bfin_hash_entry (h)->pcrel_relocs_copied;
5109
0
         s != NULL; s = s->next)
5110
0
      if ((s->section->flags & SEC_READONLY) != 0)
5111
0
        {
5112
0
    info->flags |= DF_TEXTREL;
5113
0
    break;
5114
0
        }
5115
0
  }
5116
5117
0
      return true;
5118
0
    }
5119
5120
0
  for (s = bfin_hash_entry (h)->pcrel_relocs_copied;
5121
0
       s != NULL; s = s->next)
5122
0
    s->section->size -= s->count * sizeof (Elf32_External_Rela);
5123
5124
0
  return true;
5125
0
}
5126
5127
static bool
5128
bfin_late_size_sections (bfd * output_bfd ATTRIBUTE_UNUSED,
5129
       struct bfd_link_info *info)
5130
0
{
5131
0
  bfd *dynobj;
5132
0
  asection *s;
5133
0
  bool relocs;
5134
5135
0
  dynobj = elf_hash_table (info)->dynobj;
5136
0
  if (dynobj == NULL)
5137
0
    return true;
5138
5139
0
  if (elf_hash_table (info)->dynamic_sections_created)
5140
0
    {
5141
      /* Set the contents of the .interp section to the interpreter.  */
5142
0
      if (bfd_link_executable (info) && !info->nointerp)
5143
0
  {
5144
0
    s = elf_hash_table (info)->interp;
5145
0
    BFD_ASSERT (s != NULL);
5146
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
5147
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
5148
0
    s->alloced = 1;
5149
0
  }
5150
0
    }
5151
0
  else
5152
0
    {
5153
      /* We may have created entries in the .rela.got section.
5154
   However, if we are not creating the dynamic sections, we will
5155
   not actually use these entries.  Reset the size of .rela.got,
5156
   which will cause it to get stripped from the output file
5157
   below.  */
5158
0
      s = elf_hash_table (info)->srelgot;
5159
0
      if (s != NULL)
5160
0
  s->size = 0;
5161
0
    }
5162
5163
  /* If this is a -Bsymbolic shared link, then we need to discard all
5164
     PC relative relocs against symbols defined in a regular object.
5165
     For the normal shared case we discard the PC relative relocs
5166
     against symbols that have become local due to visibility changes.
5167
     We allocated space for them in the check_relocs routine, but we
5168
     will not fill them in in the relocate_section routine.  */
5169
0
  if (bfd_link_pic (info))
5170
0
    elf_link_hash_traverse (elf_hash_table (info),
5171
0
          bfin_discard_copies, info);
5172
5173
  /* The check_relocs and adjust_dynamic_symbol entry points have
5174
     determined the sizes of the various dynamic sections.  Allocate
5175
     memory for them.  */
5176
0
  relocs = false;
5177
0
  for (s = dynobj->sections; s != NULL; s = s->next)
5178
0
    {
5179
0
      const char *name;
5180
0
      bool strip;
5181
5182
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
5183
0
  continue;
5184
5185
      /* It's OK to base decisions on the section name, because none
5186
   of the dynobj section names depend upon the input files.  */
5187
0
      name = bfd_section_name (s);
5188
5189
0
      strip = false;
5190
5191
0
       if (startswith (name, ".rela"))
5192
0
  {
5193
0
    if (s->size == 0)
5194
0
      {
5195
        /* If we don't need this section, strip it from the
5196
     output file.  This is mostly to handle .rela.bss and
5197
     .rela.plt.  We must create both sections in
5198
     create_dynamic_sections, because they must be created
5199
     before the linker maps input sections to output
5200
     sections.  The linker does that before
5201
     adjust_dynamic_symbol is called, and it is that
5202
     function which decides whether anything needs to go
5203
     into these sections.  */
5204
0
        strip = true;
5205
0
      }
5206
0
    else
5207
0
      {
5208
0
        relocs = true;
5209
5210
        /* We use the reloc_count field as a counter if we need
5211
     to copy relocs into the output file.  */
5212
0
        s->reloc_count = 0;
5213
0
      }
5214
0
  }
5215
0
      else if (! startswith (name, ".got"))
5216
0
  {
5217
    /* It's not one of our sections, so don't allocate space.  */
5218
0
    continue;
5219
0
  }
5220
5221
0
      if (strip)
5222
0
  {
5223
0
    s->flags |= SEC_EXCLUDE;
5224
0
    continue;
5225
0
  }
5226
5227
      /* Allocate memory for the section contents.  */
5228
      /* FIXME: This should be a call to bfd_alloc not bfd_zalloc.
5229
   Unused entries should be reclaimed before the section's contents
5230
   are written out, but at the moment this does not happen.  Thus in
5231
   order to prevent writing out garbage, we initialise the section's
5232
   contents to zero.  */
5233
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
5234
0
      if (s->contents == NULL && s->size != 0)
5235
0
  return false;
5236
0
      s->alloced = 1;
5237
0
    }
5238
5239
0
  if (elf_hash_table (info)->dynamic_sections_created)
5240
0
    {
5241
      /* Add some entries to the .dynamic section.  We fill in the
5242
   values later, in bfin_finish_dynamic_sections, but we
5243
   must add the entries now so that we get the correct size for
5244
   the .dynamic section.  The DT_DEBUG entry is filled in by the
5245
   dynamic linker and used by the debugger.  */
5246
0
#define add_dynamic_entry(TAG, VAL) \
5247
0
  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
5248
5249
0
      if (!bfd_link_pic (info))
5250
0
  {
5251
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
5252
0
      return false;
5253
0
  }
5254
5255
5256
0
      if (relocs)
5257
0
  {
5258
0
    if (!add_dynamic_entry (DT_RELA, 0)
5259
0
        || !add_dynamic_entry (DT_RELASZ, 0)
5260
0
        || !add_dynamic_entry (DT_RELAENT,
5261
0
             sizeof (Elf32_External_Rela)))
5262
0
      return false;
5263
0
  }
5264
5265
0
      if ((info->flags & DF_TEXTREL) != 0)
5266
0
  {
5267
0
    if (!add_dynamic_entry (DT_TEXTREL, 0))
5268
0
      return false;
5269
0
  }
5270
0
    }
5271
0
#undef add_dynamic_entry
5272
5273
0
  return true;
5274
0
}
5275

5276
/* Given a .data section and a .emreloc in-memory section, store
5277
   relocation information into the .emreloc section which can be
5278
   used at runtime to relocate the section.  This is called by the
5279
   linker when the --embedded-relocs switch is used.  This is called
5280
   after the add_symbols entry point has been called for all the
5281
   objects, and before the final_link entry point is called.  */
5282
5283
bool
5284
bfd_bfin_elf32_create_embedded_relocs (bfd *abfd,
5285
               struct bfd_link_info *info,
5286
               asection *datasec,
5287
               asection *relsec,
5288
               char **errmsg)
5289
0
{
5290
0
  Elf_Internal_Shdr *symtab_hdr;
5291
0
  Elf_Internal_Sym *isymbuf = NULL;
5292
0
  Elf_Internal_Rela *internal_relocs = NULL;
5293
0
  Elf_Internal_Rela *irel, *irelend;
5294
0
  bfd_byte *p;
5295
0
  bfd_size_type amt;
5296
5297
0
  BFD_ASSERT (! bfd_link_relocatable (info));
5298
5299
0
  *errmsg = NULL;
5300
5301
0
  if (datasec->reloc_count == 0)
5302
0
    return true;
5303
5304
0
  symtab_hdr = &elf_symtab_hdr (abfd);
5305
5306
  /* Get a copy of the native relocations.  */
5307
0
  internal_relocs = (_bfd_elf_link_read_relocs
5308
0
         (abfd, datasec, NULL, (Elf_Internal_Rela *) NULL,
5309
0
          info->keep_memory));
5310
0
  if (internal_relocs == NULL)
5311
0
    goto error_return;
5312
5313
0
  amt = (bfd_size_type) datasec->reloc_count * 12;
5314
0
  relsec->contents = (bfd_byte *) bfd_alloc (abfd, amt);
5315
0
  if (relsec->contents == NULL)
5316
0
    goto error_return;
5317
0
  relsec->alloced = 1;
5318
5319
0
  p = relsec->contents;
5320
5321
0
  irelend = internal_relocs + datasec->reloc_count;
5322
0
  for (irel = internal_relocs; irel < irelend; irel++, p += 12)
5323
0
    {
5324
0
      asection *targetsec;
5325
5326
      /* We are going to write a four byte longword into the runtime
5327
       reloc section.  The longword will be the address in the data
5328
       section which must be relocated.  It is followed by the name
5329
       of the target section NUL-padded or truncated to 8
5330
       characters.  */
5331
5332
      /* We can only relocate absolute longword relocs at run time.  */
5333
0
      if (ELF32_R_TYPE (irel->r_info) != (int) R_BFIN_BYTE4_DATA)
5334
0
  {
5335
0
    *errmsg = _("unsupported relocation type");
5336
0
    bfd_set_error (bfd_error_bad_value);
5337
0
    goto error_return;
5338
0
  }
5339
5340
      /* Get the target section referred to by the reloc.  */
5341
0
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
5342
0
  {
5343
    /* A local symbol.  */
5344
0
    Elf_Internal_Sym *isym;
5345
5346
    /* Read this BFD's local symbols if we haven't done so already.  */
5347
0
    if (isymbuf == NULL)
5348
0
      {
5349
0
        isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5350
0
        if (isymbuf == NULL)
5351
0
    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
5352
0
            symtab_hdr->sh_info, 0,
5353
0
            NULL, NULL, NULL);
5354
0
        if (isymbuf == NULL)
5355
0
    goto error_return;
5356
0
      }
5357
5358
0
    isym = isymbuf + ELF32_R_SYM (irel->r_info);
5359
0
    targetsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
5360
0
  }
5361
0
      else
5362
0
  {
5363
0
    unsigned long indx;
5364
0
    struct elf_link_hash_entry *h;
5365
5366
    /* An external symbol.  */
5367
0
    indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
5368
0
    h = elf_sym_hashes (abfd)[indx];
5369
0
    BFD_ASSERT (h != NULL);
5370
0
    if (h->root.type == bfd_link_hash_defined
5371
0
        || h->root.type == bfd_link_hash_defweak)
5372
0
      targetsec = h->root.u.def.section;
5373
0
    else
5374
0
      targetsec = NULL;
5375
0
  }
5376
5377
0
      bfd_put_32 (abfd, irel->r_offset + datasec->output_offset, p);
5378
0
      memset (p + 4, 0, 8);
5379
0
      if (targetsec != NULL)
5380
0
  strncpy ((char *) p + 4, targetsec->output_section->name, 8);
5381
0
    }
5382
5383
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
5384
0
    free (isymbuf);
5385
0
  if (elf_section_data (datasec)->relocs != internal_relocs)
5386
0
    free (internal_relocs);
5387
0
  return true;
5388
5389
0
 error_return:
5390
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
5391
0
    free (isymbuf);
5392
0
  if (elf_section_data (datasec)->relocs != internal_relocs)
5393
0
    free (internal_relocs);
5394
  return false;
5395
0
}
5396
5397
struct bfd_elf_special_section const elf32_bfin_special_sections[] =
5398
{
5399
  { ".l1.text",   8, -2, SHT_PROGBITS, SHF_ALLOC + SHF_EXECINSTR },
5400
  { ".l1.data",   8, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
5401
  { NULL,   0,  0, 0,      0 }
5402
};
5403
5404

5405
#define TARGET_LITTLE_SYM   bfin_elf32_vec
5406
#define TARGET_LITTLE_NAME    "elf32-bfin"
5407
#define ELF_ARCH      bfd_arch_bfin
5408
#define ELF_TARGET_ID     BFIN_ELF_DATA
5409
#define ELF_MACHINE_CODE    EM_BLACKFIN
5410
#define ELF_MAXPAGESIZE     0x1000
5411
#define elf_symbol_leading_char   '_'
5412
5413
#define bfd_elf32_bfd_reloc_type_lookup bfin_bfd_reloc_type_lookup
5414
#define bfd_elf32_bfd_reloc_name_lookup \
5415
          bfin_bfd_reloc_name_lookup
5416
#define elf_info_to_howto   bfin_info_to_howto
5417
#define elf_info_to_howto_rel   NULL
5418
#define elf_backend_object_p    elf32_bfin_object_p
5419
5420
#define bfd_elf32_bfd_is_local_label_name \
5421
          bfin_is_local_label_name
5422
5423
#define elf_backend_create_dynamic_sections \
5424
          _bfd_elf_create_dynamic_sections
5425
#define bfd_elf32_bfd_link_hash_table_create \
5426
          bfin_link_hash_table_create
5427
#define bfd_elf32_bfd_final_link  _bfd_elf_gc_common_final_link
5428
5429
#define elf_backend_check_relocs  bfin_check_relocs
5430
#define elf_backend_adjust_dynamic_symbol \
5431
          bfin_adjust_dynamic_symbol
5432
#define elf_backend_late_size_sections  bfin_late_size_sections
5433
#define elf_backend_relocate_section  bfin_relocate_section
5434
#define elf_backend_finish_dynamic_symbol \
5435
          bfin_finish_dynamic_symbol
5436
#define elf_backend_finish_dynamic_sections \
5437
          bfin_finish_dynamic_sections
5438
#define elf_backend_gc_mark_hook  bfin_gc_mark_hook
5439
#define bfd_elf32_bfd_merge_private_bfd_data \
5440
          elf32_bfin_merge_private_bfd_data
5441
#define bfd_elf32_bfd_set_private_flags \
5442
          elf32_bfin_set_private_flags
5443
#define bfd_elf32_bfd_print_private_bfd_data \
5444
          elf32_bfin_print_private_bfd_data
5445
#define elf_backend_final_write_processing \
5446
          elf32_bfin_final_write_processing
5447
#define elf_backend_reloc_type_class  elf32_bfin_reloc_type_class
5448
#define elf_backend_stack_align   8
5449
#define elf_backend_can_gc_sections 1
5450
#define elf_backend_special_sections  elf32_bfin_special_sections
5451
#define elf_backend_can_refcount 1
5452
#define elf_backend_want_got_plt 0
5453
#define elf_backend_plt_readonly 1
5454
#define elf_backend_want_plt_sym 0
5455
#define elf_backend_got_header_size 12
5456
#define elf_backend_rela_normal   1
5457
5458
#include "elf32-target.h"
5459
5460
#undef TARGET_LITTLE_SYM
5461
#define TARGET_LITTLE_SYM   bfin_elf32_fdpic_vec
5462
#undef TARGET_LITTLE_NAME
5463
#define TARGET_LITTLE_NAME    "elf32-bfinfdpic"
5464
#undef  elf32_bed
5465
#define elf32_bed     elf32_bfinfdpic_bed
5466
5467
#undef elf_backend_got_header_size
5468
#define elf_backend_got_header_size 0
5469
5470
#undef elf_backend_relocate_section
5471
#define elf_backend_relocate_section  bfinfdpic_relocate_section
5472
#undef elf_backend_check_relocs
5473
#define elf_backend_check_relocs  bfinfdpic_check_relocs
5474
5475
#undef bfd_elf32_bfd_link_hash_table_create
5476
#define bfd_elf32_bfd_link_hash_table_create \
5477
    bfinfdpic_elf_link_hash_table_create
5478
#undef elf_backend_early_size_sections
5479
#define elf_backend_early_size_sections \
5480
    elf32_bfinfdpic_early_size_sections
5481
5482
#undef elf_backend_create_dynamic_sections
5483
#define elf_backend_create_dynamic_sections \
5484
    elf32_bfinfdpic_create_dynamic_sections
5485
#undef elf_backend_adjust_dynamic_symbol
5486
#define elf_backend_adjust_dynamic_symbol \
5487
    elf32_bfinfdpic_adjust_dynamic_symbol
5488
#undef elf_backend_late_size_sections
5489
#define elf_backend_late_size_sections \
5490
    elf32_bfinfdpic_late_size_sections
5491
#undef elf_backend_finish_dynamic_symbol
5492
#define elf_backend_finish_dynamic_symbol \
5493
    elf32_bfinfdpic_finish_dynamic_symbol
5494
#undef elf_backend_finish_dynamic_sections
5495
#define elf_backend_finish_dynamic_sections \
5496
    elf32_bfinfdpic_finish_dynamic_sections
5497
5498
#undef elf_backend_discard_info
5499
#define elf_backend_discard_info \
5500
    bfinfdpic_elf_discard_info
5501
#undef elf_backend_can_make_relative_eh_frame
5502
#define elf_backend_can_make_relative_eh_frame \
5503
    bfinfdpic_elf_use_relative_eh_frame
5504
#undef elf_backend_can_make_lsda_relative_eh_frame
5505
#define elf_backend_can_make_lsda_relative_eh_frame \
5506
    bfinfdpic_elf_use_relative_eh_frame
5507
#undef elf_backend_encode_eh_address
5508
#define elf_backend_encode_eh_address \
5509
    bfinfdpic_elf_encode_eh_address
5510
5511
#undef elf_backend_may_use_rel_p
5512
#define elf_backend_may_use_rel_p 1
5513
#undef elf_backend_may_use_rela_p
5514
#define elf_backend_may_use_rela_p  1
5515
/* We use REL for dynamic relocations only.  */
5516
#undef elf_backend_default_use_rela_p
5517
#define elf_backend_default_use_rela_p  1
5518
5519
#undef elf_backend_omit_section_dynsym
5520
#define elf_backend_omit_section_dynsym _bfinfdpic_link_omit_section_dynsym
5521
5522
#include "elf32-target.h"