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-cris.c
Line
Count
Source
1
/* CRIS-specific support for 32-bit ELF.
2
   Copyright (C) 2000-2026 Free Software Foundation, Inc.
3
   Contributed by Axis Communications AB.
4
   Written by Hans-Peter Nilsson, based on elf32-fr30.c
5
   PIC and shlib bits based primarily on elf32-m68k.c and elf32-i386.c.
6
7
   This file is part of BFD, the Binary File Descriptor library.
8
9
   This program is free software; you can redistribute it and/or modify
10
   it under the terms of the GNU General Public License as published by
11
   the Free Software Foundation; either version 3 of the License, or
12
   (at your option) any later version.
13
14
   This program is distributed in the hope that it will be useful,
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
   GNU General Public License for more details.
18
19
   You should have received a copy of the GNU General Public License
20
   along with this program; if not, write to the Free Software
21
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
22
   MA 02110-1301, USA.  */
23
24
#include "sysdep.h"
25
#include "bfd.h"
26
#include "libbfd.h"
27
#include "elf-bfd.h"
28
#include "elf/cris.h"
29
#include <limits.h>
30
31
bfd_reloc_status_type
32
cris_elf_pcrel_reloc (bfd *, arelent *, asymbol *, void *,
33
          asection *, bfd *, char **);
34
static bool
35
cris_elf_set_mach_from_flags (bfd *, unsigned long);
36
37
/* Forward declarations.  */
38
static reloc_howto_type cris_elf_howto_table [] =
39
{
40
  /* This reloc does nothing.  */
41
  HOWTO (R_CRIS_NONE,   /* type */
42
   0,     /* rightshift */
43
   0,     /* size */
44
   0,     /* bitsize */
45
   false,     /* pc_relative */
46
   0,     /* bitpos */
47
   complain_overflow_dont, /* complain_on_overflow */
48
   bfd_elf_generic_reloc, /* special_function */
49
   "R_CRIS_NONE",   /* name */
50
   false,     /* partial_inplace */
51
   0,     /* src_mask */
52
   0,     /* dst_mask */
53
   false),    /* pcrel_offset */
54
55
  /* An 8 bit absolute relocation.  */
56
  HOWTO (R_CRIS_8,    /* type */
57
   0,     /* rightshift */
58
   1,     /* size */
59
   8,     /* bitsize */
60
   false,     /* pc_relative */
61
   0,     /* bitpos */
62
   complain_overflow_bitfield, /* complain_on_overflow */
63
   bfd_elf_generic_reloc, /* special_function */
64
   "R_CRIS_8",    /* name */
65
   false,     /* partial_inplace */
66
   0x0000,    /* src_mask */
67
   0x00ff,    /* dst_mask */
68
   false),    /* pcrel_offset */
69
70
  /* A 16 bit absolute relocation.  */
71
  HOWTO (R_CRIS_16,   /* type */
72
   0,     /* rightshift */
73
   2,     /* size */
74
   16,      /* bitsize */
75
   false,     /* pc_relative */
76
   0,     /* bitpos */
77
   complain_overflow_bitfield, /* complain_on_overflow */
78
   bfd_elf_generic_reloc, /* special_function */
79
   "R_CRIS_16",   /* name */
80
   false,     /* partial_inplace */
81
   0x00000000,    /* src_mask */
82
   0x0000ffff,    /* dst_mask */
83
   false),    /* pcrel_offset */
84
85
  /* A 32 bit absolute relocation.  */
86
  HOWTO (R_CRIS_32,   /* type */
87
   0,     /* rightshift */
88
   4,     /* size */
89
   32,      /* bitsize */
90
   false,     /* pc_relative */
91
   0,     /* bitpos */
92
   /* We don't want overflow complaints for 64-bit vma builds
93
      for e.g. sym+0x40000000 (or actually sym-0xc0000000 in
94
      32-bit ELF) where sym=0xc0001234.
95
      Don't do this for the PIC relocs, as we don't expect to
96
      see them with large offsets.  */
97
   complain_overflow_dont, /* complain_on_overflow */
98
   bfd_elf_generic_reloc, /* special_function */
99
   "R_CRIS_32",   /* name */
100
   false,     /* partial_inplace */
101
   0x00000000,    /* src_mask */
102
   0xffffffff,    /* dst_mask */
103
   false),    /* pcrel_offset */
104
105
  /* An 8 bit PC-relative relocation.  */
106
  HOWTO (R_CRIS_8_PCREL,  /* type */
107
   0,     /* rightshift */
108
   1,     /* size */
109
   8,     /* bitsize */
110
   true,      /* pc_relative */
111
   0,     /* bitpos */
112
   complain_overflow_bitfield, /* complain_on_overflow */
113
   cris_elf_pcrel_reloc,  /* special_function */
114
   "R_CRIS_8_PCREL",  /* name */
115
   false,     /* partial_inplace */
116
   0x0000,    /* src_mask */
117
   0x00ff,    /* dst_mask */
118
   true),     /* pcrel_offset */
119
120
  /* A 16 bit PC-relative relocation.  */
121
  HOWTO (R_CRIS_16_PCREL, /* type */
122
   0,     /* rightshift */
123
   2,     /* size */
124
   16,      /* bitsize */
125
   true,      /* pc_relative */
126
   0,     /* bitpos */
127
   complain_overflow_bitfield, /* complain_on_overflow */
128
   cris_elf_pcrel_reloc,  /* special_function */
129
   "R_CRIS_16_PCREL", /* name */
130
   false,     /* partial_inplace */
131
   0x00000000,    /* src_mask */
132
   0x0000ffff,    /* dst_mask */
133
   true),     /* pcrel_offset */
134
135
  /* A 32 bit PC-relative relocation.  */
136
  HOWTO (R_CRIS_32_PCREL, /* type */
137
   0,     /* rightshift */
138
   4,     /* size */
139
   32,      /* bitsize */
140
   true,      /* pc_relative */
141
   0,     /* bitpos */
142
   complain_overflow_bitfield, /* complain_on_overflow */
143
   cris_elf_pcrel_reloc,  /* special_function */
144
   "R_CRIS_32_PCREL", /* name */
145
   false,     /* partial_inplace */
146
   0x00000000,    /* src_mask */
147
   0xffffffff,    /* dst_mask */
148
   true),     /* pcrel_offset */
149
150
  /* GNU extension to record C++ vtable hierarchy.  */
151
  HOWTO (R_CRIS_GNU_VTINHERIT,  /* type */
152
   0,     /* rightshift */
153
   4,     /* size */
154
   0,     /* bitsize */
155
   false,     /* pc_relative */
156
   0,     /* bitpos */
157
   complain_overflow_dont, /* complain_on_overflow */
158
   NULL,      /* special_function */
159
   "R_CRIS_GNU_VTINHERIT", /* name */
160
   false,     /* partial_inplace */
161
   0,     /* src_mask */
162
   0,     /* dst_mask */
163
   false),    /* pcrel_offset */
164
165
  /* GNU extension to record C++ vtable member usage.  */
166
  HOWTO (R_CRIS_GNU_VTENTRY,  /* type */
167
   0,     /* rightshift */
168
   4,     /* size */
169
   0,     /* bitsize */
170
   false,     /* pc_relative */
171
   0,     /* bitpos */
172
   complain_overflow_dont, /* complain_on_overflow */
173
   _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
174
   "R_CRIS_GNU_VTENTRY",   /* name */
175
   false,     /* partial_inplace */
176
   0,     /* src_mask */
177
   0,     /* dst_mask */
178
   false),    /* pcrel_offset */
179
180
  /* This is used only by the dynamic linker.  The symbol should exist
181
     both in the object being run and in some shared library.  The
182
     dynamic linker copies the data addressed by the symbol from the
183
     shared library into the object, because the object being
184
     run has to have the data at some particular address.  */
185
  HOWTO (R_CRIS_COPY,   /* type */
186
   0,     /* rightshift */
187
   4,     /* size */
188
   32,      /* bitsize */
189
   false,     /* pc_relative */
190
   0,     /* bitpos */
191
   complain_overflow_bitfield, /* complain_on_overflow */
192
   bfd_elf_generic_reloc, /* special_function */
193
   "R_CRIS_COPY",   /* name */
194
   false,     /* partial_inplace */
195
   0,     /* src_mask */
196
   0,     /* dst_mask */
197
   false),    /* pcrel_offset */
198
199
  /* Like R_CRIS_32, but used when setting global offset table entries.  */
200
  HOWTO (R_CRIS_GLOB_DAT, /* type */
201
   0,     /* rightshift */
202
   4,     /* size */
203
   32,      /* bitsize */
204
   false,     /* pc_relative */
205
   0,     /* bitpos */
206
   complain_overflow_bitfield, /* complain_on_overflow */
207
   bfd_elf_generic_reloc, /* special_function */
208
   "R_CRIS_GLOB_DAT", /* name */
209
   false,     /* partial_inplace */
210
   0,     /* src_mask */
211
   0xffffffff,    /* dst_mask */
212
   false),    /* pcrel_offset */
213
214
  /* Marks a procedure linkage table entry for a symbol.  */
215
  HOWTO (R_CRIS_JUMP_SLOT,  /* type */
216
   0,     /* rightshift */
217
   4,     /* size */
218
   32,      /* bitsize */
219
   false,     /* pc_relative */
220
   0,     /* bitpos */
221
   complain_overflow_bitfield, /* complain_on_overflow */
222
   bfd_elf_generic_reloc, /* special_function */
223
   "R_CRIS_JUMP_SLOT",  /* name */
224
   false,     /* partial_inplace */
225
   0,     /* src_mask */
226
   0,     /* dst_mask */
227
   false),    /* pcrel_offset */
228
229
  /* Used only by the dynamic linker.  When the object is run, this
230
     longword is set to the load address of the object, plus the
231
     addend.  */
232
  HOWTO (R_CRIS_RELATIVE, /* type */
233
   0,     /* rightshift */
234
   4,     /* size */
235
   32,      /* bitsize */
236
   false,     /* pc_relative */
237
   0,     /* bitpos */
238
   complain_overflow_bitfield, /* complain_on_overflow */
239
   bfd_elf_generic_reloc, /* special_function */
240
   "R_CRIS_RELATIVE", /* name */
241
   false,     /* partial_inplace */
242
   0,     /* src_mask */
243
   0xffffffff,    /* dst_mask */
244
   false),    /* pcrel_offset */
245
246
  /* Like R_CRIS_32, but referring to the GOT table entry for the symbol.  */
247
  HOWTO (R_CRIS_16_GOT,   /* type */
248
   0,     /* rightshift */
249
   2,     /* size */
250
   16,      /* bitsize */
251
   false,     /* pc_relative */
252
   0,     /* bitpos */
253
   complain_overflow_bitfield, /* complain_on_overflow */
254
   bfd_elf_generic_reloc, /* special_function */
255
   "R_CRIS_16_GOT", /* name */
256
   false,     /* partial_inplace */
257
   0,     /* src_mask */
258
   0xffff,    /* dst_mask */
259
   false),    /* pcrel_offset */
260
261
  HOWTO (R_CRIS_32_GOT,   /* type */
262
   0,     /* rightshift */
263
   4,     /* size */
264
   32,      /* bitsize */
265
   false,     /* pc_relative */
266
   0,     /* bitpos */
267
   complain_overflow_bitfield, /* complain_on_overflow */
268
   bfd_elf_generic_reloc, /* special_function */
269
   "R_CRIS_32_GOT", /* name */
270
   false,     /* partial_inplace */
271
   0,     /* src_mask */
272
   0xffffffff,    /* dst_mask */
273
   false),    /* pcrel_offset */
274
275
  /* Like R_CRIS_32_GOT, but referring to (and requesting a) PLT part of
276
     the GOT table for the symbol.  */
277
  HOWTO (R_CRIS_16_GOTPLT,  /* type */
278
   0,     /* rightshift */
279
   2,     /* size */
280
   16,      /* bitsize */
281
   false,     /* pc_relative */
282
   0,     /* bitpos */
283
   complain_overflow_bitfield, /* complain_on_overflow */
284
   bfd_elf_generic_reloc, /* special_function */
285
   "R_CRIS_16_GOTPLT",  /* name */
286
   false,     /* partial_inplace */
287
   0,     /* src_mask */
288
   0xffff,    /* dst_mask */
289
   false),    /* pcrel_offset */
290
291
  HOWTO (R_CRIS_32_GOTPLT,  /* type */
292
   0,     /* rightshift */
293
   4,     /* size */
294
   32,      /* bitsize */
295
   false,     /* pc_relative */
296
   0,     /* bitpos */
297
   complain_overflow_bitfield, /* complain_on_overflow */
298
   bfd_elf_generic_reloc, /* special_function */
299
   "R_CRIS_32_GOTPLT",  /* name */
300
   false,     /* partial_inplace */
301
   0,     /* src_mask */
302
   0xffffffff,    /* dst_mask */
303
   false),    /* pcrel_offset */
304
305
  /* A 32-bit offset from GOT to (local const) symbol: no GOT entry should
306
     be necessary.  */
307
  HOWTO (R_CRIS_32_GOTREL,  /* type */
308
   0,     /* rightshift */
309
   4,     /* size */
310
   32,      /* bitsize */
311
   false,     /* pc_relative */
312
   0,     /* bitpos */
313
   complain_overflow_bitfield, /* complain_on_overflow */
314
   bfd_elf_generic_reloc, /* special_function */
315
   "R_CRIS_32_GOTREL",  /* name */
316
   false,     /* partial_inplace */
317
   0,     /* src_mask */
318
   0xffffffff,    /* dst_mask */
319
   false),    /* pcrel_offset */
320
321
  /* A 32-bit offset from GOT to entry for this symbol in PLT and request
322
     to create PLT entry for symbol.  */
323
  HOWTO (R_CRIS_32_PLT_GOTREL,  /* type */
324
   0,     /* rightshift */
325
   4,     /* size */
326
   32,      /* bitsize */
327
   false,     /* pc_relative */
328
   0,     /* bitpos */
329
   complain_overflow_bitfield, /* complain_on_overflow */
330
   bfd_elf_generic_reloc, /* special_function */
331
   "R_CRIS_32_PLT_GOTREL", /* name */
332
   false,     /* partial_inplace */
333
   0,     /* src_mask */
334
   0xffffffff,    /* dst_mask */
335
   false),    /* pcrel_offset */
336
337
  /* A 32-bit offset from PC (location after the relocation) + addend to
338
     entry for this symbol in PLT and request to create PLT entry for
339
     symbol.  */
340
  HOWTO (R_CRIS_32_PLT_PCREL, /* type */
341
   0,     /* rightshift */
342
   4,     /* size */
343
   32,      /* bitsize */
344
   true,      /* pc_relative */
345
   0,     /* bitpos */
346
   complain_overflow_bitfield, /* complain_on_overflow */
347
   cris_elf_pcrel_reloc,  /* special_function */
348
   "R_CRIS_32_PLT_PCREL", /* name */
349
   false,     /* partial_inplace */
350
   0,     /* src_mask */
351
   0xffffffff,    /* dst_mask */
352
   true),     /* pcrel_offset */
353
354
  /* We don't handle these in any special manner and cross-format
355
     linking is not supported; just recognize them enough to pass them
356
     around.  FIXME: do the same for most PIC relocs and add sanity
357
     tests to actually refuse gracefully to handle these and PIC
358
     relocs for cross-format linking.  */
359
#define TLSHOWTO32(name) \
360
 HOWTO (name, 0, 4, 32, false, 0, complain_overflow_bitfield, \
361
  bfd_elf_generic_reloc, #name, false, 0, 0xffffffff, false)
362
#define TLSHOWTO16X(name, X)       \
363
 HOWTO (name, 0, 2, 16, false, 0, complain_overflow_ ## X, \
364
  bfd_elf_generic_reloc, #name, false, 0, 0xffff, false)
365
#define TLSHOWTO16(name) TLSHOWTO16X(name, unsigned)
366
#define TLSHOWTO16S(name) TLSHOWTO16X(name, signed)
367
368
  TLSHOWTO32 (R_CRIS_32_GOT_GD),
369
  TLSHOWTO16 (R_CRIS_16_GOT_GD),
370
  TLSHOWTO32 (R_CRIS_32_GD),
371
  TLSHOWTO32 (R_CRIS_DTP),
372
  TLSHOWTO32 (R_CRIS_32_DTPREL),
373
  TLSHOWTO16S (R_CRIS_16_DTPREL),
374
  TLSHOWTO32 (R_CRIS_32_GOT_TPREL),
375
  TLSHOWTO16S (R_CRIS_16_GOT_TPREL),
376
  TLSHOWTO32 (R_CRIS_32_TPREL),
377
  TLSHOWTO16S (R_CRIS_16_TPREL),
378
  TLSHOWTO32 (R_CRIS_DTPMOD),
379
  TLSHOWTO32 (R_CRIS_32_IE)
380
};
381

382
/* Map BFD reloc types to CRIS ELF reloc types.  */
383
384
struct cris_reloc_map
385
{
386
  bfd_reloc_code_real_type bfd_reloc_val;
387
  unsigned int cris_reloc_val;
388
};
389
390
static const struct cris_reloc_map cris_reloc_map [] =
391
{
392
  { BFD_RELOC_NONE,   R_CRIS_NONE },
393
  { BFD_RELOC_8,    R_CRIS_8 },
394
  { BFD_RELOC_16,   R_CRIS_16 },
395
  { BFD_RELOC_32,   R_CRIS_32 },
396
  { BFD_RELOC_8_PCREL,    R_CRIS_8_PCREL },
397
  { BFD_RELOC_16_PCREL,   R_CRIS_16_PCREL },
398
  { BFD_RELOC_32_PCREL,   R_CRIS_32_PCREL },
399
  { BFD_RELOC_VTABLE_INHERIT, R_CRIS_GNU_VTINHERIT },
400
  { BFD_RELOC_VTABLE_ENTRY, R_CRIS_GNU_VTENTRY },
401
  { BFD_RELOC_COPY,   R_CRIS_COPY },
402
  { BFD_RELOC_GLOB_DAT,   R_CRIS_GLOB_DAT },
403
  { BFD_RELOC_JMP_SLOT,   R_CRIS_JUMP_SLOT },
404
  { BFD_RELOC_RELATIVE,   R_CRIS_RELATIVE },
405
  { BFD_RELOC_CRIS_16_GOT,  R_CRIS_16_GOT },
406
  { BFD_RELOC_CRIS_32_GOT,  R_CRIS_32_GOT },
407
  { BFD_RELOC_CRIS_16_GOTPLT, R_CRIS_16_GOTPLT },
408
  { BFD_RELOC_CRIS_32_GOTPLT, R_CRIS_32_GOTPLT },
409
  { BFD_RELOC_CRIS_32_GOTREL, R_CRIS_32_GOTREL },
410
  { BFD_RELOC_CRIS_32_PLT_GOTREL, R_CRIS_32_PLT_GOTREL },
411
  { BFD_RELOC_32_PLT_PCREL, R_CRIS_32_PLT_PCREL },
412
  { BFD_RELOC_CRIS_32_GOT_GD, R_CRIS_32_GOT_GD },
413
  { BFD_RELOC_CRIS_16_GOT_GD, R_CRIS_16_GOT_GD },
414
  { BFD_RELOC_CRIS_32_GD, R_CRIS_32_GD },
415
  { BFD_RELOC_CRIS_DTP, R_CRIS_DTP },
416
  { BFD_RELOC_CRIS_32_DTPREL, R_CRIS_32_DTPREL },
417
  { BFD_RELOC_CRIS_16_DTPREL, R_CRIS_16_DTPREL },
418
  { BFD_RELOC_CRIS_32_GOT_TPREL, R_CRIS_32_GOT_TPREL },
419
  { BFD_RELOC_CRIS_16_GOT_TPREL, R_CRIS_16_GOT_TPREL },
420
  { BFD_RELOC_CRIS_32_TPREL,  R_CRIS_32_TPREL },
421
  { BFD_RELOC_CRIS_16_TPREL,  R_CRIS_16_TPREL },
422
  { BFD_RELOC_CRIS_DTPMOD,  R_CRIS_DTPMOD },
423
  { BFD_RELOC_CRIS_32_IE, R_CRIS_32_IE }
424
};
425
426
static reloc_howto_type *
427
cris_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
428
      bfd_reloc_code_real_type code)
429
0
{
430
0
  unsigned int i;
431
432
0
  for (i = 0; i < sizeof (cris_reloc_map) / sizeof (cris_reloc_map[0]); i++)
433
0
    if (cris_reloc_map [i].bfd_reloc_val == code)
434
0
      return & cris_elf_howto_table [cris_reloc_map[i].cris_reloc_val];
435
436
0
  return NULL;
437
0
}
438
439
static reloc_howto_type *
440
cris_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
441
0
{
442
0
  unsigned int i;
443
444
0
  for (i = 0;
445
0
       i < sizeof (cris_elf_howto_table) / sizeof (cris_elf_howto_table[0]);
446
0
       i++)
447
0
    if (cris_elf_howto_table[i].name != NULL
448
0
  && strcasecmp (cris_elf_howto_table[i].name, r_name) == 0)
449
0
      return &cris_elf_howto_table[i];
450
451
0
  return NULL;
452
0
}
453
454
/* Set the howto pointer for an CRIS ELF reloc.  */
455
456
static bool
457
cris_info_to_howto_rela (bfd * abfd ATTRIBUTE_UNUSED,
458
       arelent * cache_ptr,
459
       Elf_Internal_Rela * dst)
460
55
{
461
55
  enum elf_cris_reloc_type r_type;
462
463
55
  r_type = ELF32_R_TYPE (dst->r_info);
464
55
  if (r_type >= R_CRIS_max)
465
6
    {
466
      /* xgettext:c-format */
467
6
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
468
6
        abfd, r_type);
469
6
      bfd_set_error (bfd_error_bad_value);
470
6
      return false;
471
6
    }
472
49
  cache_ptr->howto = & cris_elf_howto_table [r_type];
473
49
  return true;
474
55
}
475
476
bfd_reloc_status_type
477
cris_elf_pcrel_reloc (bfd *abfd ATTRIBUTE_UNUSED,
478
          arelent *reloc_entry,
479
          asymbol *symbol,
480
          void * data ATTRIBUTE_UNUSED,
481
          asection *input_section,
482
          bfd *output_bfd,
483
          char **error_message ATTRIBUTE_UNUSED)
484
0
{
485
  /* By default (using only bfd_elf_generic_reloc when linking to
486
     non-ELF formats) PC-relative relocs are relative to the beginning
487
     of the reloc.  CRIS PC-relative relocs are relative to the position
488
     *after* the reloc because that's what pre-CRISv32 PC points to
489
     after reading an insn field with that reloc.  (For CRISv32, PC is
490
     actually relative to the start of the insn, but we keep the old
491
     definition.)  Still, we use as much generic machinery as we can.
492
493
     Only adjust when doing a final link.  */
494
0
  if (output_bfd == (bfd *) NULL)
495
0
    reloc_entry->addend -= bfd_get_reloc_size (reloc_entry->howto);
496
497
0
  return
498
0
    bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
499
0
         input_section, output_bfd, error_message);
500
0
}
501

502
/* Support for core dump NOTE sections.
503
   The slightly unintuitive code layout is an attempt to keep at least
504
   some similarities with other ports, hoping to simplify general
505
   changes, while still keeping Linux/CRIS and Linux/CRISv32 code apart.  */
506
507
static bool
508
cris_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
509
0
{
510
0
  int offset;
511
0
  size_t size;
512
513
0
  if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
514
0
    switch (note->descsz)
515
0
      {
516
0
      default:
517
0
  return false;
518
519
0
      case 202:   /* Linux/CRISv32 */
520
  /* pr_cursig */
521
0
  elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
522
523
  /* pr_pid */
524
0
  elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
525
526
  /* pr_reg */
527
0
  offset = 70;
528
0
  size = 128;
529
530
0
  break;
531
0
      }
532
0
  else
533
0
    switch (note->descsz)
534
0
      {
535
0
      default:
536
0
  return false;
537
538
0
      case 214:   /* Linux/CRIS */
539
  /* pr_cursig */
540
0
  elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
541
542
  /* pr_pid */
543
0
  elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 22);
544
545
  /* pr_reg */
546
0
  offset = 70;
547
0
  size = 140;
548
549
0
  break;
550
0
      }
551
552
  /* Make a ".reg/999" section.  */
553
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
554
0
            size, note->descpos + offset);
555
0
}
556
557
static bool
558
cris_elf_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
559
0
{
560
0
  if (bfd_get_mach (abfd) == bfd_mach_cris_v32)
561
0
    switch (note->descsz)
562
0
      {
563
0
      default:
564
0
  return false;
565
566
0
      case 124:   /* Linux/CRISv32 elf_prpsinfo */
567
0
  elf_tdata (abfd)->core->program
568
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
569
0
  elf_tdata (abfd)->core->command
570
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
571
0
      }
572
0
  else
573
0
    switch (note->descsz)
574
0
      {
575
0
      default:
576
0
  return false;
577
578
0
      case 124:   /* Linux/CRIS elf_prpsinfo */
579
0
  elf_tdata (abfd)->core->program
580
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
581
0
  elf_tdata (abfd)->core->command
582
0
    = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
583
0
      }
584
585
  /* Note that for some reason, a spurious space is tacked
586
     onto the end of the args in some (at least one anyway)
587
     implementations, so strip it off if it exists.  */
588
589
0
  {
590
0
    char *command = elf_tdata (abfd)->core->command;
591
0
    int n = strlen (command);
592
593
0
    if (0 < n && command[n - 1] == ' ')
594
0
      command[n - 1] = '\0';
595
0
  }
596
597
0
  return true;
598
0
}
599

600
/* The name of the dynamic interpreter.  This is put in the .interp
601
   section.  */
602
603
0
#define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1"
604
605
/* The size in bytes of an entry in the procedure linkage table.  */
606
607
0
#define PLT_ENTRY_SIZE 20
608
0
#define PLT_ENTRY_SIZE_V32 26
609
610
/* The first entry in an absolute procedure linkage table looks like this.  */
611
612
static const bfd_byte elf_cris_plt0_entry[PLT_ENTRY_SIZE] =
613
{
614
  0xfc, 0xe1,
615
  0x7e, 0x7e, /* push mof.  */
616
  0x7f, 0x0d,   /*  (dip [pc+]) */
617
  0, 0, 0, 0, /*  Replaced with address of .got + 4.  */
618
  0x30, 0x7a, /* move [...],mof */
619
  0x7f, 0x0d,   /*  (dip [pc+]) */
620
  0, 0, 0, 0, /*  Replaced with address of .got + 8.  */
621
  0x30, 0x09  /* jump [...] */
622
};
623
624
static const bfd_byte elf_cris_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
625
{
626
  0x84, 0xe2, /* subq 4,$sp */
627
  0x6f, 0xfe, /* move.d 0,$acr */
628
  0, 0, 0, 0, /*  Replaced by address of .got + 4.  */
629
  0x7e, 0x7a, /* move $mof,[$sp] */
630
  0x3f, 0x7a, /* move [$acr],$mof */
631
  0x04, 0xf2, /* addq 4,acr */
632
  0x6f, 0xfa, /* move.d [$acr],$acr */
633
  0xbf, 0x09, /* jump $acr */
634
  0xb0, 0x05, /* nop */
635
  0, 0    /*  Pad out to 26 bytes.  */
636
};
637
638
/* Subsequent entries in an absolute procedure linkage table look like
639
   this.  */
640
641
static const bfd_byte elf_cris_plt_entry[PLT_ENTRY_SIZE] =
642
{
643
  0x7f, 0x0d,   /*  (dip [pc+]) */
644
  0, 0, 0, 0, /*  Replaced with address of this symbol in .got.  */
645
  0x30, 0x09, /* jump [...] */
646
  0x3f,  0x7e,  /* move [pc+],mof */
647
  0, 0, 0, 0, /*  Replaced with offset into relocation table.  */
648
  0x2f, 0xfe, /* add.d [pc+],pc */
649
  0xec, 0xff,
650
  0xff, 0xff  /*  Replaced with offset to start of .plt.  */
651
};
652
653
static const bfd_byte elf_cris_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
654
{
655
  0x6f, 0xfe, /* move.d 0,$acr */
656
  0, 0, 0, 0, /*  Replaced with address of this symbol in .got.  */
657
  0x6f, 0xfa,   /* move.d [$acr],$acr */
658
  0xbf, 0x09,   /* jump $acr */
659
  0xb0, 0x05, /* nop */
660
  0x3f, 0x7e, /* move 0,mof */
661
  0, 0, 0, 0, /*  Replaced with offset into relocation table. */
662
  0xbf, 0x0e, /* ba start_of_plt0_entry */
663
  0, 0, 0, 0, /*  Replaced with offset to plt0 entry.  */
664
  0xb0, 0x05  /* nop */
665
};
666
667
/* The first entry in a PIC procedure linkage table looks like this.  */
668
669
static const bfd_byte elf_cris_pic_plt0_entry[PLT_ENTRY_SIZE] =
670
{
671
  0xfc, 0xe1, 0x7e, 0x7e, /* push mof */
672
  0x04, 0x01, 0x30, 0x7a, /* move [r0+4],mof */
673
  0x08, 0x01, 0x30, 0x09, /* jump [r0+8] */
674
  0, 0, 0, 0, 0, 0, 0, 0, /*  Pad out to 20 bytes.  */
675
};
676
677
static const bfd_byte elf_cris_pic_plt0_entry_v32[PLT_ENTRY_SIZE_V32] =
678
{
679
  0x84, 0xe2, /* subq 4,$sp */
680
  0x04, 0x01, /* addoq 4,$r0,$acr */
681
  0x7e, 0x7a, /* move $mof,[$sp] */
682
  0x3f, 0x7a, /* move [$acr],$mof */
683
  0x04, 0xf2, /* addq 4,$acr */
684
  0x6f, 0xfa, /* move.d [$acr],$acr */
685
  0xbf, 0x09, /* jump $acr */
686
  0xb0, 0x05, /* nop */
687
  0, 0,   /*  Pad out to 26 bytes.  */
688
  0, 0, 0, 0,
689
  0, 0, 0, 0
690
};
691
692
/* Subsequent entries in a PIC procedure linkage table look like this.  */
693
694
static const bfd_byte elf_cris_pic_plt_entry[PLT_ENTRY_SIZE] =
695
{
696
  0x6f, 0x0d,   /*  (bdap [pc+].d,r0) */
697
  0, 0, 0, 0, /*  Replaced with offset of this symbol in .got.  */
698
  0x30, 0x09, /* jump [...] */
699
  0x3f, 0x7e, /* move [pc+],mof */
700
  0, 0, 0, 0, /*  Replaced with offset into relocation table.  */
701
  0x2f, 0xfe, /* add.d [pc+],pc */
702
  0xec, 0xff, /*  Replaced with offset to start of .plt.  */
703
  0xff, 0xff
704
};
705
706
static const bfd_byte elf_cris_pic_plt_entry_v32[PLT_ENTRY_SIZE_V32] =
707
{
708
  0x6f, 0x0d, /* addo.d 0,$r0,$acr */
709
  0, 0, 0, 0, /*  Replaced with offset of this symbol in .got.  */
710
  0x6f, 0xfa, /* move.d [$acr],$acr */
711
  0xbf, 0x09, /* jump $acr */
712
  0xb0, 0x05, /* nop */
713
  0x3f, 0x7e, /* move relocoffs,$mof */
714
  0, 0, 0, 0, /*  Replaced with offset into relocation table.  */
715
  0xbf, 0x0e, /* ba start_of_plt */
716
  0, 0, 0, 0, /*  Replaced with offset to start of .plt.  */
717
  0xb0, 0x05  /* nop */
718
};
719

720
/* We copy elf32-m68k.c and elf32-i386.c for the basic linker hash bits
721
   (and most other PIC/shlib stuff).  Check that we don't drift away
722
   without reason.
723
724
   The CRIS linker, like the m68k and i386 linkers (and probably the rest
725
   too) needs to keep track of the number of relocs that it decides to
726
   copy in check_relocs for each symbol.  This is so that it can discard
727
   PC relative relocs if it doesn't need them when linking with
728
   -Bsymbolic.  We store the information in a field extending the regular
729
   ELF linker hash table.  */
730
731
/* This structure keeps track of the number of PC relative relocs we have
732
   copied for a given symbol.  */
733
734
struct elf_cris_pcrel_relocs_copied
735
{
736
  /* Next section.  */
737
  struct elf_cris_pcrel_relocs_copied *next;
738
739
  /* A section in dynobj.  */
740
  asection *section;
741
742
  /* Number of relocs copied in this section.  */
743
  bfd_size_type count;
744
745
  /* Example of reloc being copied, for message.  */
746
  enum elf_cris_reloc_type r_type;
747
};
748
749
/* CRIS ELF linker hash entry.  */
750
751
struct elf_cris_link_hash_entry
752
{
753
  struct elf_link_hash_entry root;
754
755
  /* Number of PC relative relocs copied for this symbol.  */
756
  struct elf_cris_pcrel_relocs_copied *pcrel_relocs_copied;
757
758
  /* The GOTPLT references are CRIS-specific; the goal is to avoid having
759
     both a general GOT and a PLT-specific GOT entry for the same symbol,
760
     when it is referenced both as a function and as a function pointer.
761
762
     Number of GOTPLT references for a function.  */
763
  bfd_signed_vma gotplt_refcount;
764
765
  /* Actual GOTPLT index for this symbol, if applicable, or zero if not
766
     (zero is never used as an index).  FIXME: We should be able to fold
767
     this with gotplt_refcount in a union, like the got and plt unions in
768
     elf_link_hash_entry.  */
769
  bfd_size_type gotplt_offset;
770
771
  /* The root.got.refcount is the sum of the regular reference counts
772
     (this) and those members below.  We have to keep a separate count
773
     to track when we've found the first (or last) reference to a
774
     regular got entry.  The offset is in root.got.offset.  */
775
  bfd_signed_vma reg_got_refcount;
776
777
  /* Similar to the above, the number of reloc references to this
778
     symbols that need a R_CRIS_32_TPREL slot.  The offset is in
779
     root.got.offset, because this and .dtp_refcount can't validly
780
     happen when there's also a regular GOT entry; that's invalid
781
     input for which an error is emitted.  */
782
  bfd_signed_vma tprel_refcount;
783
784
  /* Similar to the above, the number of reloc references to this
785
     symbols that need a R_CRIS_DTP slot.  The offset is in
786
     root.got.offset; plus 4 if .tprel_refcount > 0.  */
787
  bfd_signed_vma dtp_refcount;
788
};
789
790
static bool
791
elf_cris_discard_excess_dso_dynamics (struct elf_cris_link_hash_entry *,
792
              void * );
793
static bool
794
elf_cris_discard_excess_program_dynamics (struct elf_cris_link_hash_entry *,
795
            void *);
796
797
/* The local_got_refcounts and local_got_offsets are a multiple of
798
   LSNUM in size, namely LGOT_ALLOC_NELTS_FOR(LSNUM) (plus one for the
799
   refcount for GOT itself, see code), with the summary / group offset
800
   for local symbols located at offset N, reference counts for
801
   ordinary (address) relocs at offset N + LSNUM, for R_CRIS_DTP
802
   relocs at offset N + 2*LSNUM, and for R_CRIS_32_TPREL relocs at N +
803
   3*LSNUM.  */
804
805
0
#define LGOT_REG_NDX(x) ((x) + symtab_hdr->sh_info)
806
0
#define LGOT_DTP_NDX(x) ((x) + 2 * symtab_hdr->sh_info)
807
0
#define LGOT_TPREL_NDX(x) ((x) + 3 * symtab_hdr->sh_info)
808
0
#define LGOT_ALLOC_NELTS_FOR(x) ((x) * 4)
809
810
/* CRIS ELF linker hash table.  */
811
812
struct elf_cris_link_hash_table
813
{
814
  struct elf_link_hash_table root;
815
816
  /* We can't use the PLT offset and calculate to get the GOTPLT offset,
817
     since we try and avoid creating GOTPLT:s when there's already a GOT.
818
     Instead, we keep and update the next available index here.  */
819
  bfd_size_type next_gotplt_entry;
820
821
  /* The number of R_CRIS_32_DTPREL and R_CRIS_16_DTPREL that have
822
     been seen for any input; if != 0, then the constant-offset
823
     R_CRIS_DTPMOD is needed for this DSO/executable.  This turns
824
     negative at relocation, so that we don't need an extra flag for
825
     when the reloc is output.  */
826
  bfd_signed_vma dtpmod_refcount;
827
};
828
829
/* Traverse a CRIS ELF linker hash table.  */
830
831
#define elf_cris_link_hash_traverse(table, func, info)      \
832
0
  (elf_link_hash_traverse           \
833
0
   (&(table)->root,             \
834
0
    (bool (*) (struct elf_link_hash_entry *, void *)) (func),   \
835
0
    (info)))
836
837
/* Get the CRIS ELF linker hash table from a link_info structure.  */
838
839
#define elf_cris_hash_table(p) \
840
0
  ((is_elf_hash_table ((p)->hash)          \
841
0
    && elf_hash_table_id (elf_hash_table (p)) == CRIS_ELF_DATA)   \
842
0
   ? (struct elf_cris_link_hash_table *) (p)->hash : NULL)
843
844
/* Get the CRIS ELF linker hash entry from a regular hash entry (the
845
   "parent class").  The .root reference is just a simple type
846
   check on the argument.  */
847
848
#define elf_cris_hash_entry(p) \
849
0
 ((struct elf_cris_link_hash_entry *) (&(p)->root))
850
851
/* Create an entry in a CRIS ELF linker hash table.  */
852
853
static struct bfd_hash_entry *
854
elf_cris_link_hash_newfunc (struct bfd_hash_entry *entry,
855
          struct bfd_hash_table *table,
856
          const char *string)
857
0
{
858
0
  struct elf_cris_link_hash_entry *ret =
859
0
    (struct elf_cris_link_hash_entry *) entry;
860
861
  /* Allocate the structure if it has not already been allocated by a
862
     subclass.  */
863
0
  if (ret == (struct elf_cris_link_hash_entry *) NULL)
864
0
    ret = ((struct elf_cris_link_hash_entry *)
865
0
     bfd_hash_allocate (table,
866
0
            sizeof (struct elf_cris_link_hash_entry)));
867
0
  if (ret == (struct elf_cris_link_hash_entry *) NULL)
868
0
    return (struct bfd_hash_entry *) ret;
869
870
  /* Call the allocation method of the superclass.  */
871
0
  ret = ((struct elf_cris_link_hash_entry *)
872
0
   _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
873
0
             table, string));
874
0
  if (ret != (struct elf_cris_link_hash_entry *) NULL)
875
0
    {
876
0
      ret->pcrel_relocs_copied = NULL;
877
0
      ret->gotplt_refcount = 0;
878
0
      ret->gotplt_offset = 0;
879
0
      ret->dtp_refcount = 0;
880
0
      ret->tprel_refcount = 0;
881
0
      ret->reg_got_refcount = 0;
882
0
    }
883
884
0
  return (struct bfd_hash_entry *) ret;
885
0
}
886
887
/* Create a CRIS ELF linker hash table.  */
888
889
static struct bfd_link_hash_table *
890
elf_cris_link_hash_table_create (bfd *abfd)
891
0
{
892
0
  struct elf_cris_link_hash_table *ret;
893
0
  size_t amt = sizeof (struct elf_cris_link_hash_table);
894
895
0
  ret = ((struct elf_cris_link_hash_table *) bfd_zmalloc (amt));
896
0
  if (ret == (struct elf_cris_link_hash_table *) NULL)
897
0
    return NULL;
898
899
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
900
0
              elf_cris_link_hash_newfunc,
901
0
              sizeof (struct elf_cris_link_hash_entry)))
902
0
    {
903
0
      free (ret);
904
0
      return NULL;
905
0
    }
906
907
  /* Initialize to skip over the first three entries in the gotplt; they
908
     are used for run-time symbol evaluation.  */
909
0
  ret->next_gotplt_entry = 12;
910
911
0
  return &ret->root.root;
912
0
}
913

914
/* Perform a single relocation.  By default we use the standard BFD
915
   routines, with a few tweaks.  */
916
917
static bfd_reloc_status_type
918
cris_final_link_relocate (reloc_howto_type *  howto,
919
        bfd *         input_bfd,
920
        asection *        input_section,
921
        bfd_byte *        contents,
922
        Elf_Internal_Rela * rel,
923
        bfd_vma       relocation)
924
0
{
925
0
  bfd_reloc_status_type r;
926
0
  enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rel->r_info);
927
928
  /* PC-relative relocations are relative to the position *after*
929
     the reloc.  Note that for R_CRIS_8_PCREL the adjustment is
930
     not a single byte, since PC must be 16-bit-aligned.  */
931
0
  switch (r_type)
932
0
    {
933
      /* Check that the 16-bit GOT relocs are positive.  */
934
0
    case R_CRIS_16_GOTPLT:
935
0
    case R_CRIS_16_GOT:
936
0
      if ((bfd_signed_vma) relocation < 0)
937
0
  return bfd_reloc_overflow;
938
0
      break;
939
940
0
    case R_CRIS_32_PLT_PCREL:
941
0
    case R_CRIS_32_PCREL:
942
0
      relocation -= 2;
943
      /* Fall through.  */
944
0
    case R_CRIS_8_PCREL:
945
0
    case R_CRIS_16_PCREL:
946
0
      relocation -= 2;
947
0
      break;
948
949
0
    default:
950
0
      break;
951
0
    }
952
953
0
  r = _bfd_final_link_relocate (howto, input_bfd, input_section,
954
0
        contents, rel->r_offset,
955
0
        relocation, rel->r_addend);
956
0
  return r;
957
0
}
958

959
960
/* The number of errors left before we stop outputting reloc-specific
961
   explanatory messages.  By coincidence, this works nicely together
962
   with the default number of messages you'll get from LD about
963
   "relocation truncated to fit" messages before you get an
964
   "additional relocation overflows omitted from the output".  */
965
static int additional_relocation_error_msg_count = 10;
966
967
/* Relocate an CRIS ELF section.  See elf32-fr30.c, from where this was
968
   copied, for further comments.  */
969
970
static int
971
cris_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
972
         struct bfd_link_info *info,
973
         bfd *input_bfd,
974
         asection *input_section,
975
         bfd_byte *contents,
976
         Elf_Internal_Rela *relocs,
977
         Elf_Internal_Sym *local_syms,
978
         asection **local_sections)
979
0
{
980
0
  struct elf_cris_link_hash_table * htab;
981
0
  bfd *dynobj;
982
0
  Elf_Internal_Shdr *symtab_hdr;
983
0
  struct elf_link_hash_entry **sym_hashes;
984
0
  bfd_vma *local_got_offsets;
985
0
  asection *sgot;
986
0
  asection *splt;
987
0
  asection *sreloc;
988
0
  Elf_Internal_Rela *rel;
989
0
  Elf_Internal_Rela *relend;
990
0
  asection *srelgot;
991
992
0
  htab = elf_cris_hash_table (info);
993
0
  if (htab == NULL)
994
0
    return false;
995
996
0
  dynobj = htab->root.dynobj;
997
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
998
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
999
0
  sym_hashes = elf_sym_hashes (input_bfd);
1000
0
  relend     = relocs + input_section->reloc_count;
1001
1002
0
  sgot = NULL;
1003
0
  splt = NULL;
1004
0
  sreloc = NULL;
1005
0
  srelgot = NULL;
1006
1007
0
  if (dynobj != NULL)
1008
0
    {
1009
0
      splt = htab->root.splt;
1010
0
      sgot = htab->root.sgot;
1011
0
    }
1012
1013
0
  for (rel = relocs; rel < relend; rel ++)
1014
0
    {
1015
0
      reloc_howto_type *howto;
1016
0
      unsigned long r_symndx;
1017
0
      Elf_Internal_Sym *sym;
1018
0
      asection *sec;
1019
0
      struct elf_link_hash_entry *h;
1020
0
      bfd_vma relocation;
1021
0
      bfd_reloc_status_type r;
1022
0
      const char *symname = NULL;
1023
0
      enum elf_cris_reloc_type r_type;
1024
0
      bool resolved_to_zero;
1025
1026
0
      r_type = ELF32_R_TYPE (rel->r_info);
1027
1028
0
      if (   r_type == R_CRIS_GNU_VTINHERIT
1029
0
    || r_type == R_CRIS_GNU_VTENTRY)
1030
0
  continue;
1031
1032
0
      r_symndx = ELF32_R_SYM (rel->r_info);
1033
0
      howto  = cris_elf_howto_table + r_type;
1034
0
      h      = NULL;
1035
0
      sym    = NULL;
1036
0
      sec    = NULL;
1037
1038
0
      if (r_symndx < symtab_hdr->sh_info)
1039
0
  {
1040
0
    sym = local_syms + r_symndx;
1041
0
    sec = local_sections [r_symndx];
1042
0
    relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
1043
1044
0
    symname = (bfd_elf_string_from_elf_section
1045
0
         (input_bfd, symtab_hdr->sh_link, sym->st_name));
1046
0
    if (symname == NULL)
1047
0
      symname = bfd_section_name (sec);
1048
0
  }
1049
0
      else
1050
0
  {
1051
0
    bool warned, ignored;
1052
0
    bool unresolved_reloc;
1053
1054
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1055
0
           r_symndx, symtab_hdr, sym_hashes,
1056
0
           h, sec, relocation,
1057
0
           unresolved_reloc, warned, ignored);
1058
1059
0
    symname = h->root.root.string;
1060
1061
0
    if (unresolved_reloc
1062
        /* Perhaps we should detect the cases that
1063
     sec->output_section is expected to be NULL like i386 and
1064
     m68k, but apparently (and according to elfxx-ia64.c) all
1065
     valid cases are where the symbol is defined in a shared
1066
     object which we link dynamically against.  This includes
1067
     PLT relocs for which we've created a PLT entry and other
1068
     relocs for which we're prepared to create dynamic
1069
     relocations.
1070
1071
     For now, new situations cause us to just err when
1072
     sec->output_offset is NULL but the object with the symbol
1073
     is *not* dynamically linked against.  Thus this will
1074
     automatically remind us so we can see if there are other
1075
     valid cases we need to revisit.  */
1076
0
        && (sec->owner->flags & DYNAMIC) != 0)
1077
0
      relocation = 0;
1078
1079
0
    else if (h->root.type == bfd_link_hash_defined
1080
0
       || h->root.type == bfd_link_hash_defweak)
1081
0
      {
1082
        /* Here follow the cases where the relocation value must
1083
     be zero (or when further handling is simplified when
1084
     zero).  I can't claim to understand the various
1085
     conditions and they weren't described in the files
1086
     where I copied them from (elf32-m68k.c and
1087
     elf32-i386.c), but let's mention examples of where
1088
     they happen.  FIXME: Perhaps define and use a
1089
     dynamic_symbol_p function like ia64.
1090
1091
     - When creating a shared library, we can have an
1092
     ordinary relocation for a symbol defined in a shared
1093
     library (perhaps the one we create).  We then make
1094
     the relocation value zero, as the value seen now will
1095
     be added into the relocation addend in this shared
1096
     library, but must be handled only at dynamic-link
1097
     time.  FIXME: Not sure this example covers the
1098
     h->elf_link_hash_flags test, though it's there in
1099
     other targets.  */
1100
0
        if (bfd_link_pic (info)
1101
0
      && ((!SYMBOLIC_BIND (info, h) && h->dynindx != -1)
1102
0
          || !h->def_regular)
1103
0
      && (input_section->flags & SEC_ALLOC) != 0
1104
0
      && (r_type == R_CRIS_8
1105
0
          || r_type == R_CRIS_16
1106
0
          || r_type == R_CRIS_32
1107
0
          || r_type == R_CRIS_8_PCREL
1108
0
          || r_type == R_CRIS_16_PCREL
1109
0
          || r_type == R_CRIS_32_PCREL))
1110
0
    relocation = 0;
1111
0
        else if (!bfd_link_relocatable (info) && unresolved_reloc
1112
0
           && (_bfd_elf_section_offset (output_bfd, info,
1113
0
                input_section,
1114
0
                rel->r_offset)
1115
0
         != (bfd_vma) -1))
1116
0
    {
1117
0
      _bfd_error_handler
1118
        /* xgettext:c-format */
1119
0
        (_("%pB, section %pA: unresolvable relocation %s against symbol `%s'"),
1120
0
         input_bfd,
1121
0
         input_section,
1122
0
         cris_elf_howto_table[r_type].name,
1123
0
         symname);
1124
0
      bfd_set_error (bfd_error_bad_value);
1125
0
      return false;
1126
0
    }
1127
0
      }
1128
0
  }
1129
1130
0
      if (sec != NULL && discarded_section (sec))
1131
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1132
0
           rel, 1, relend, R_CRIS_NONE,
1133
0
           howto, 0, contents);
1134
1135
0
      if (bfd_link_relocatable (info))
1136
0
  continue;
1137
1138
0
      resolved_to_zero = (h != NULL
1139
0
        && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
1140
1141
0
      switch (r_type)
1142
0
  {
1143
0
  case R_CRIS_16_GOTPLT:
1144
0
  case R_CRIS_32_GOTPLT:
1145
    /* This is like the case for R_CRIS_32_GOT and R_CRIS_16_GOT,
1146
       but we require a PLT, and the PLT handling will take care of
1147
       filling in the PLT-specific GOT entry.  For the GOT offset,
1148
       calculate it as we do when filling it in for the .got.plt
1149
       section.  If we don't have a PLT, punt to GOT handling.  */
1150
0
    if (h != NULL
1151
0
        && ((struct elf_cris_link_hash_entry *) h)->gotplt_offset != 0)
1152
0
      {
1153
0
        asection *sgotplt = htab->root.sgotplt;
1154
0
        bfd_vma got_offset;
1155
1156
0
        BFD_ASSERT (h->dynindx != -1);
1157
0
        BFD_ASSERT (sgotplt != NULL);
1158
1159
0
        got_offset
1160
0
    = ((struct elf_cris_link_hash_entry *) h)->gotplt_offset;
1161
1162
0
        relocation = got_offset;
1163
0
        break;
1164
0
      }
1165
1166
    /* We didn't make a PLT entry for this symbol.  Maybe everything is
1167
       folded into the GOT.  Other than folding, this happens when
1168
       statically linking PIC code, or when using -Bsymbolic.  Check
1169
       that we instead have a GOT entry as done for us by
1170
       elf_cris_adjust_dynamic_symbol, and drop through into the
1171
       ordinary GOT cases.  This must not happen for the
1172
       executable, because any reference it does to a function
1173
       that is satisfied by a DSO must generate a PLT.  We assume
1174
       these call-specific relocs don't address non-functions.  */
1175
0
    if (h != NULL
1176
0
        && (h->got.offset == (bfd_vma) -1
1177
0
      || (!bfd_link_pic (info)
1178
0
          && !(h->def_regular
1179
0
         || (!h->def_dynamic
1180
0
             && h->root.type == bfd_link_hash_undefweak)))))
1181
0
      {
1182
0
        _bfd_error_handler
1183
0
    ((h->got.offset == (bfd_vma) -1)
1184
     /* xgettext:c-format */
1185
0
     ? _("%pB, section %pA: no PLT nor GOT for relocation %s"
1186
0
         " against symbol `%s'")
1187
     /* xgettext:c-format */
1188
0
     : _("%pB, section %pA: no PLT for relocation %s"
1189
0
         " against symbol `%s'"),
1190
0
     input_bfd,
1191
0
     input_section,
1192
0
     cris_elf_howto_table[r_type].name,
1193
0
     (symname != NULL && symname[0] != '\0'
1194
0
      ? symname : _("[whose name is lost]")));
1195
1196
        /* FIXME: Perhaps blaming input is not the right thing to
1197
     do; this is probably an internal error.  But it is true
1198
     that we didn't like that particular input.  */
1199
0
        bfd_set_error (bfd_error_bad_value);
1200
0
        return false;
1201
0
      }
1202
    /* Fall through.  */
1203
1204
    /* The size of the actual relocation is not used here; we only
1205
       fill in the GOT table here.  */
1206
0
  case R_CRIS_16_GOT:
1207
0
  case R_CRIS_32_GOT:
1208
0
    {
1209
0
      bfd_vma off;
1210
1211
      /* Note that despite using RELA relocations, the .got contents
1212
         is always filled in with the link-relative relocation
1213
         value; the addend.  */
1214
1215
0
      if (h != NULL)
1216
0
        {
1217
0
    off = h->got.offset;
1218
0
    BFD_ASSERT (off != (bfd_vma) -1);
1219
1220
0
    if (!elf_hash_table (info)->dynamic_sections_created
1221
0
        || (! bfd_link_pic (info)
1222
0
      && (h->def_regular
1223
0
          || h->type == STT_FUNC
1224
0
          || h->needs_plt))
1225
0
        || (bfd_link_pic (info)
1226
0
      && (SYMBOLIC_BIND (info, h) || h->dynindx == -1)
1227
0
      && h->def_regular))
1228
0
      {
1229
        /* This wasn't checked above for ! bfd_link_pic (info), but
1230
           must hold there if we get here; the symbol must
1231
           be defined in the regular program or be undefweak
1232
           or be a function or otherwise need a PLT.  */
1233
0
        BFD_ASSERT (!elf_hash_table (info)->dynamic_sections_created
1234
0
        || bfd_link_pic (info)
1235
0
        || h->def_regular
1236
0
        || h->type == STT_FUNC
1237
0
        || h->needs_plt
1238
0
        || h->root.type == bfd_link_hash_undefweak);
1239
1240
        /* This is actually a static link, or it is a
1241
           -Bsymbolic link and the symbol is defined locally,
1242
           or is undefweak, or the symbol was forced to be
1243
           local because of a version file, or we're not
1244
           creating a dynamic object.  We must initialize this
1245
           entry in the global offset table.  Since the offset
1246
           must always be a multiple of 4, we use the least
1247
           significant bit to record whether we have
1248
           initialized it already.
1249
1250
           If this GOT entry should be runtime-initialized, we
1251
           will create a .rela.got relocation entry to
1252
           initialize the value.  This is done in the
1253
           finish_dynamic_symbol routine.  */
1254
0
        if ((off & 1) != 0)
1255
0
          off &= ~1;
1256
0
        else
1257
0
          {
1258
0
      bfd_put_32 (output_bfd, relocation,
1259
0
            sgot->contents + off);
1260
0
      h->got.offset |= 1;
1261
0
          }
1262
0
      }
1263
0
        }
1264
0
      else
1265
0
        {
1266
0
    BFD_ASSERT (local_got_offsets != NULL
1267
0
          && local_got_offsets[r_symndx] != (bfd_vma) -1);
1268
1269
0
    off = local_got_offsets[r_symndx];
1270
1271
    /* The offset must always be a multiple of 4.  We use
1272
       the least significant bit to record whether we have
1273
       already generated the necessary reloc.  */
1274
0
    if ((off & 1) != 0)
1275
0
      off &= ~1;
1276
0
    else
1277
0
      {
1278
0
        bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1279
1280
0
        if (bfd_link_pic (info))
1281
0
          {
1282
0
      Elf_Internal_Rela outrel;
1283
0
      bfd_byte *loc;
1284
1285
0
      srelgot = htab->root.srelgot;
1286
0
      BFD_ASSERT (srelgot != NULL);
1287
1288
0
      outrel.r_offset = (sgot->output_section->vma
1289
0
             + sgot->output_offset
1290
0
             + off);
1291
0
      outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1292
0
      outrel.r_addend = relocation;
1293
0
      loc = srelgot->contents;
1294
0
      loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1295
0
      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1296
0
          }
1297
1298
0
        local_got_offsets[r_symndx] |= 1;
1299
0
      }
1300
0
        }
1301
1302
0
      relocation = sgot->output_offset + off;
1303
0
      if (rel->r_addend != 0)
1304
0
        {
1305
    /* We can't do anything for a relocation which is against
1306
       a symbol *plus offset*.  GOT holds relocations for
1307
       symbols.  Make this an error; the compiler isn't
1308
       allowed to pass us these kinds of things.  */
1309
0
    if (h == NULL)
1310
0
      _bfd_error_handler
1311
        /* xgettext:c-format */
1312
0
        (_("%pB, section %pA: relocation %s with non-zero addend"
1313
0
           " %" PRId64 " against local symbol"),
1314
0
         input_bfd,
1315
0
         input_section,
1316
0
         cris_elf_howto_table[r_type].name,
1317
0
         (int64_t) rel->r_addend);
1318
0
    else
1319
0
      _bfd_error_handler
1320
        /* xgettext:c-format */
1321
0
        (_("%pB, section %pA: relocation %s with non-zero addend"
1322
0
           " %" PRId64 " against symbol `%s'"),
1323
0
         input_bfd,
1324
0
         input_section,
1325
0
         cris_elf_howto_table[r_type].name,
1326
0
         (int64_t) rel->r_addend,
1327
0
         symname[0] != '\0' ? symname : _("[whose name is lost]"));
1328
1329
0
    bfd_set_error (bfd_error_bad_value);
1330
0
    return false;
1331
0
        }
1332
0
    }
1333
0
    break;
1334
1335
0
  case R_CRIS_32_GOTREL:
1336
    /* This relocation must only be performed against local symbols.
1337
       It's also ok when we link a program and the symbol is either
1338
       defined in an ordinary (non-DSO) object or is undefined weak.  */
1339
0
    if (h != NULL
1340
0
        && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1341
0
        && !(!bfd_link_pic (info)
1342
0
       && (h->def_regular
1343
0
           || (!h->def_dynamic
1344
0
         && h->root.type == bfd_link_hash_undefweak))))
1345
0
      {
1346
0
        _bfd_error_handler
1347
    /* xgettext:c-format */
1348
0
    (_("%pB, section %pA: relocation %s is"
1349
0
       " not allowed for global symbol: `%s'"),
1350
0
     input_bfd,
1351
0
     input_section,
1352
0
     cris_elf_howto_table[r_type].name,
1353
0
     symname);
1354
0
        bfd_set_error (bfd_error_bad_value);
1355
0
        return false;
1356
0
      }
1357
1358
    /* This can happen if we get a link error with the input ELF
1359
       variant mismatching the output variant.  Emit an error so
1360
       it's noticed if it happens elsewhere.  */
1361
0
    if (sgot == NULL)
1362
0
      {
1363
0
        _bfd_error_handler
1364
    /* xgettext:c-format */
1365
0
    (_("%pB, section %pA: relocation %s with no GOT created"),
1366
0
     input_bfd,
1367
0
     input_section,
1368
0
     cris_elf_howto_table[r_type].name);
1369
0
        bfd_set_error (bfd_error_bad_value);
1370
0
        return false;
1371
0
      }
1372
1373
    /* This relocation is like a PC-relative one, except the
1374
       reference point is the location of GOT.  Note that
1375
       sgot->output_offset is not involved in this calculation.  We
1376
       always want the start of entire .got section, not the
1377
       position after the reserved header.  */
1378
0
    relocation -= sgot->output_section->vma;
1379
0
    break;
1380
1381
0
  case R_CRIS_32_PLT_PCREL:
1382
    /* Relocation is to the entry for this symbol in the
1383
       procedure linkage table.  */
1384
1385
    /* Resolve a PLT_PCREL reloc against a local symbol directly,
1386
       without using the procedure linkage table.  */
1387
0
    if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1388
0
      break;
1389
1390
0
    if (h->plt.offset == (bfd_vma) -1
1391
0
        || splt == NULL)
1392
0
      {
1393
        /* We didn't make a PLT entry for this symbol.  This
1394
     happens when statically linking PIC code, or when
1395
     using -Bsymbolic.  */
1396
0
        break;
1397
0
      }
1398
1399
0
    relocation = (splt->output_section->vma
1400
0
      + splt->output_offset
1401
0
      + h->plt.offset);
1402
0
    break;
1403
1404
0
  case R_CRIS_32_PLT_GOTREL:
1405
    /* Like R_CRIS_32_PLT_PCREL, but the reference point is the
1406
       start of the .got section.  See also comment at
1407
       R_CRIS_32_GOT.  */
1408
0
    relocation -= sgot->output_section->vma;
1409
1410
    /* Resolve a PLT_GOTREL reloc against a local symbol directly,
1411
       without using the procedure linkage table.  */
1412
0
    if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
1413
0
      break;
1414
1415
0
    if (h->plt.offset == (bfd_vma) -1
1416
0
        || splt == NULL)
1417
0
      {
1418
        /* We didn't make a PLT entry for this symbol.  This
1419
     happens when statically linking PIC code, or when
1420
     using -Bsymbolic.  */
1421
0
        break;
1422
0
      }
1423
1424
0
    relocation = (splt->output_section->vma
1425
0
      + splt->output_offset
1426
0
      + h->plt.offset
1427
0
      - sgot->output_section->vma);
1428
0
    break;
1429
1430
0
  case R_CRIS_8_PCREL:
1431
0
  case R_CRIS_16_PCREL:
1432
0
  case R_CRIS_32_PCREL:
1433
    /* If the symbol was local, we need no shlib-specific handling.  */
1434
0
    if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
1435
0
        || h->dynindx == -1)
1436
0
      break;
1437
1438
    /* Fall through.  */
1439
0
  case R_CRIS_8:
1440
0
  case R_CRIS_16:
1441
0
  case R_CRIS_32:
1442
0
    if (bfd_link_pic (info)
1443
0
        && !resolved_to_zero
1444
0
        && r_symndx != STN_UNDEF
1445
0
        && (input_section->flags & SEC_ALLOC) != 0
1446
0
        && ((r_type != R_CRIS_8_PCREL
1447
0
       && r_type != R_CRIS_16_PCREL
1448
0
       && r_type != R_CRIS_32_PCREL)
1449
0
      || (!SYMBOLIC_BIND (info, h)
1450
0
          || (h != NULL && !h->def_regular))))
1451
0
      {
1452
0
        Elf_Internal_Rela outrel;
1453
0
        bfd_byte *loc;
1454
0
        bool skip, relocate;
1455
1456
        /* When generating a shared object, these relocations
1457
     are copied into the output file to be resolved at run
1458
     time.  */
1459
1460
0
        sreloc = elf_section_data (input_section)->sreloc;
1461
        /* The section should have been created in cris_elf_check_relocs,
1462
     but that function will not be called for objects which fail in
1463
     cris_elf_merge_private_bfd_data.  */
1464
0
        if (sreloc == NULL)
1465
0
    {
1466
0
      bfd_set_error (bfd_error_bad_value);
1467
0
      return false;
1468
0
    }
1469
1470
0
        skip = false;
1471
0
        relocate = false;
1472
1473
0
        outrel.r_offset =
1474
0
    _bfd_elf_section_offset (output_bfd, info, input_section,
1475
0
           rel->r_offset);
1476
0
        if (outrel.r_offset == (bfd_vma) -1)
1477
0
    skip = true;
1478
0
        else if (outrel.r_offset == (bfd_vma) -2
1479
           /* For now, undefined weak symbols with non-default
1480
        visibility (yielding 0), like exception info for
1481
        discarded sections, will get a R_CRIS_NONE
1482
        relocation rather than no relocation, because we
1483
        notice too late that the symbol doesn't need a
1484
        relocation.  */
1485
0
           || (h != NULL
1486
0
         && h->root.type == bfd_link_hash_undefweak
1487
0
         && ELF_ST_VISIBILITY (h->other) != STV_DEFAULT))
1488
0
    skip = true, relocate = true;
1489
0
        outrel.r_offset += (input_section->output_section->vma
1490
0
          + input_section->output_offset);
1491
1492
0
        if (skip)
1493
0
    memset (&outrel, 0, sizeof outrel);
1494
        /* h->dynindx may be -1 if the symbol was marked to
1495
     become local.  */
1496
0
        else if (h != NULL
1497
0
           && ((!SYMBOLIC_BIND (info, h) && h->dynindx != -1)
1498
0
         || !h->def_regular))
1499
0
    {
1500
0
      BFD_ASSERT (h->dynindx != -1);
1501
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1502
0
      outrel.r_addend = relocation + rel->r_addend;
1503
0
    }
1504
0
        else
1505
0
    {
1506
0
      outrel.r_addend = relocation + rel->r_addend;
1507
1508
0
      if (r_type == R_CRIS_32)
1509
0
        {
1510
0
          relocate = true;
1511
0
          outrel.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
1512
0
        }
1513
0
      else
1514
0
        {
1515
0
          long indx;
1516
1517
0
          if (bfd_is_abs_section (sec))
1518
0
      indx = 0;
1519
0
          else if (sec == NULL || sec->owner == NULL)
1520
0
      {
1521
0
        bfd_set_error (bfd_error_bad_value);
1522
0
        return false;
1523
0
      }
1524
0
          else
1525
0
      {
1526
0
        asection *osec;
1527
1528
        /* We are turning this relocation into one
1529
           against a section symbol.  It would be
1530
           proper to subtract the symbol's value,
1531
           osec->vma, from the emitted reloc addend,
1532
           but ld.so expects buggy relocs.  */
1533
0
        osec = sec->output_section;
1534
0
        indx = elf_section_data (osec)->dynindx;
1535
0
        if (indx == 0)
1536
0
          {
1537
0
            osec = htab->root.text_index_section;
1538
0
            indx = elf_section_data (osec)->dynindx;
1539
0
          }
1540
0
        BFD_ASSERT (indx != 0);
1541
0
      }
1542
1543
0
          outrel.r_info = ELF32_R_INFO (indx, r_type);
1544
0
        }
1545
0
    }
1546
1547
0
        loc = sreloc->contents;
1548
0
        loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
1549
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1550
1551
        /* This reloc will be computed at runtime, so there's no
1552
     need to do anything now, except for R_CRIS_32 relocations
1553
     that have been turned into R_CRIS_RELATIVE.  */
1554
0
        if (!relocate)
1555
0
    continue;
1556
0
      }
1557
1558
0
    break;
1559
1560
0
  case R_CRIS_16_DTPREL:
1561
0
  case R_CRIS_32_DTPREL:
1562
    /* This relocation must only be performed against local
1563
       symbols, or to sections that are not loadable.  It's also
1564
       ok when we link a program and the symbol is defined in an
1565
       ordinary (non-DSO) object (if it's undefined there, we've
1566
       already seen an error).  */
1567
0
    if (h != NULL
1568
0
        && (input_section->flags & SEC_ALLOC) != 0
1569
0
        && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1570
0
        && (bfd_link_pic (info)
1571
0
      || (!h->def_regular
1572
0
          && h->root.type != bfd_link_hash_undefined)))
1573
0
      {
1574
0
        _bfd_error_handler
1575
0
    ((h->root.type == bfd_link_hash_undefined)
1576
     /* We shouldn't get here for GCC-emitted code.  */
1577
     /* xgettext:c-format */
1578
0
     ? _("%pB, section %pA: relocation %s has an undefined"
1579
0
         " reference to `%s', perhaps a declaration mixup?")
1580
     /* xgettext:c-format */
1581
0
     : _("%pB, section %pA: relocation %s is"
1582
0
         " not allowed for `%s', a global symbol with default"
1583
0
         " visibility, perhaps a declaration mixup?"),
1584
0
     input_bfd,
1585
0
     input_section,
1586
0
     cris_elf_howto_table[r_type].name,
1587
0
     symname != NULL && symname[0] != '\0'
1588
0
     ? symname : _("[whose name is lost]"));
1589
0
        bfd_set_error (bfd_error_bad_value);
1590
0
        return false;
1591
0
      }
1592
1593
0
    BFD_ASSERT ((input_section->flags & SEC_ALLOC) == 0
1594
0
          || htab->dtpmod_refcount != 0);
1595
1596
    /* Fill in a R_CRIS_DTPMOD reloc at offset 3 if we haven't
1597
       already done so.  Note that we do this in .got.plt, not
1598
       in .got, as .got.plt contains the first part, still the
1599
       reloc is against .got, because the linker script directs
1600
       (is required to direct) them both into .got.  */
1601
0
    if (htab->dtpmod_refcount > 0
1602
0
        && (input_section->flags & SEC_ALLOC) != 0)
1603
0
      {
1604
0
        asection *sgotplt = htab->root.sgotplt;
1605
0
        BFD_ASSERT (sgotplt != NULL);
1606
1607
0
        if (bfd_link_pic (info))
1608
0
    {
1609
0
      Elf_Internal_Rela outrel;
1610
0
      bfd_byte *loc;
1611
1612
0
      srelgot = htab->root.srelgot;
1613
0
      BFD_ASSERT (srelgot != NULL);
1614
0
      loc = srelgot->contents;
1615
0
      loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1616
1617
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 12);
1618
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
1619
0
      outrel.r_offset = (sgotplt->output_section->vma
1620
0
             + sgotplt->output_offset
1621
0
             + 12);
1622
0
      outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTPMOD);
1623
0
      outrel.r_addend = 0;
1624
0
      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1625
0
    }
1626
0
        else
1627
0
    {
1628
      /* For an executable, the GOT entry contents is known.  */
1629
0
      bfd_put_32 (output_bfd, (bfd_vma) 1, sgotplt->contents + 12);
1630
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 16);
1631
0
    }
1632
1633
        /* Reverse the sign to mark that we've emitted the
1634
     required GOT entry.  */
1635
0
        htab->dtpmod_refcount = - htab->dtpmod_refcount;
1636
0
      }
1637
1638
    /* The relocation is the offset from the start of the module
1639
       TLS block to the (local) symbol.  */
1640
0
    relocation -= elf_hash_table (info)->tls_sec == NULL
1641
0
      ? 0 : elf_hash_table (info)->tls_sec->vma;
1642
0
    break;
1643
1644
0
  case R_CRIS_32_GD:
1645
0
    if (bfd_link_pic (info))
1646
0
      {
1647
0
        bfd_set_error (bfd_error_invalid_operation);
1648
1649
        /* We've already informed in cris_elf_check_relocs that
1650
     this is an error.  */
1651
0
        return false;
1652
0
      }
1653
    /* Fall through.  */
1654
1655
0
  case R_CRIS_16_GOT_GD:
1656
0
  case R_CRIS_32_GOT_GD:
1657
0
    if (rel->r_addend != 0)
1658
0
      {
1659
        /* We can't do anything for a relocation which is against a
1660
     symbol *plus offset*.  The GOT holds relocations for
1661
     symbols.  Make this an error; the compiler isn't allowed
1662
     to pass us these kinds of things.  */
1663
0
        _bfd_error_handler
1664
    /* xgettext:c-format */
1665
0
    (_("%pB, section %pA: relocation %s with non-zero addend"
1666
0
       " %" PRId64 " against symbol `%s'"),
1667
0
     input_bfd,
1668
0
     input_section,
1669
0
     cris_elf_howto_table[r_type].name,
1670
0
     (int64_t) rel->r_addend,
1671
0
     symname[0] != '\0' ? symname : _("[whose name is lost]"));
1672
1673
0
        bfd_set_error (bfd_error_bad_value);
1674
0
        return false;
1675
0
      }
1676
1677
0
    if (!bfd_link_pic (info)
1678
0
        && (h == NULL || h->def_regular || ELF_COMMON_DEF_P (h)))
1679
0
      {
1680
        /* Known contents of the GOT.  */
1681
0
        bfd_vma off;
1682
1683
        /* The symbol is defined in the program, so just write
1684
     (1, known_tpoffset) into the GOT.  */
1685
0
        relocation -= elf_hash_table (info)->tls_sec->vma;
1686
1687
0
        if (h != NULL)
1688
0
    {
1689
0
      off = elf_cris_hash_entry (h)->tprel_refcount > 0
1690
0
        ? h->got.offset + 4 : h->got.offset;
1691
0
    }
1692
0
        else
1693
0
    {
1694
0
      off = local_got_offsets[r_symndx];
1695
0
      if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
1696
0
        off += 4;
1697
0
    }
1698
1699
        /* We use bit 1 of the offset as a flag for GOT entry with
1700
     the R_CRIS_DTP reloc, setting it when we've emitted the
1701
     GOT entry and reloc.  Bit 0 is used for R_CRIS_32_TPREL
1702
     relocs.  */
1703
0
        if ((off & 2) == 0)
1704
0
    {
1705
0
      off &= ~3;
1706
1707
0
      if (h != NULL)
1708
0
        h->got.offset |= 2;
1709
0
      else
1710
0
        local_got_offsets[r_symndx] |= 2;
1711
1712
0
      bfd_put_32 (output_bfd, 1, sgot->contents + off);
1713
0
      bfd_put_32 (output_bfd, relocation, sgot->contents + off + 4);
1714
0
    }
1715
0
        else
1716
0
    off &= ~3;
1717
1718
0
        relocation = sgot->output_offset + off
1719
0
    + (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
1720
0
      }
1721
0
    else
1722
0
      {
1723
        /* Not all parts of the GOT entry are known; emit a real
1724
     relocation.  */
1725
0
        bfd_vma off;
1726
1727
0
        if (h != NULL)
1728
0
    off = elf_cris_hash_entry (h)->tprel_refcount > 0
1729
0
      ? h->got.offset + 4 : h->got.offset;
1730
0
        else
1731
0
    {
1732
0
      off = local_got_offsets[r_symndx];
1733
0
      if (local_got_offsets[LGOT_TPREL_NDX (r_symndx)])
1734
0
        off += 4;
1735
0
    }
1736
1737
        /* See above re bit 1 and bit 0 usage.  */
1738
0
        if ((off & 2) == 0)
1739
0
    {
1740
0
      Elf_Internal_Rela outrel;
1741
0
      bfd_byte *loc;
1742
1743
0
      off &= ~3;
1744
1745
0
      if (h != NULL)
1746
0
        h->got.offset |= 2;
1747
0
      else
1748
0
        local_got_offsets[r_symndx] |= 2;
1749
1750
      /* Clear the target contents of the GOT (just as a
1751
         gesture; it's already cleared on allocation): this
1752
         relocation is not like the other dynrelocs.  */
1753
0
      bfd_put_32 (output_bfd, 0, sgot->contents + off);
1754
0
      bfd_put_32 (output_bfd, 0, sgot->contents + off + 4);
1755
1756
0
      srelgot = htab->root.srelgot;
1757
0
      BFD_ASSERT (srelgot != NULL);
1758
1759
0
      if (h != NULL && h->dynindx != -1)
1760
0
        {
1761
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_DTP);
1762
0
          relocation = 0;
1763
0
        }
1764
0
      else
1765
0
        {
1766
0
          outrel.r_info = ELF32_R_INFO (0, R_CRIS_DTP);
1767
1768
          /* NULL if we had an error.  */
1769
0
          relocation -= elf_hash_table (info)->tls_sec == NULL
1770
0
      ? 0 : elf_hash_table (info)->tls_sec->vma;
1771
0
        }
1772
1773
0
      outrel.r_offset = (sgot->output_section->vma
1774
0
             + sgot->output_offset
1775
0
             + off);
1776
0
      outrel.r_addend = relocation;
1777
0
      loc = srelgot->contents;
1778
0
      loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1779
1780
      /* NULL if we had an error.  */
1781
0
      if (srelgot->contents != NULL)
1782
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1783
0
    }
1784
0
        else
1785
0
    off &= ~3;
1786
1787
0
        relocation = sgot->output_offset + off
1788
0
    + (r_type == R_CRIS_32_GD ? sgot->output_section->vma : 0);
1789
0
      }
1790
1791
    /* The GOT-relative offset to the GOT entry is the
1792
       relocation, or for R_CRIS_32_GD, the actual address of
1793
       the GOT entry.  */
1794
0
    break;
1795
1796
0
  case R_CRIS_32_IE:
1797
0
    if (bfd_link_pic (info))
1798
0
      {
1799
0
        bfd_set_error (bfd_error_invalid_operation);
1800
1801
        /* We've already informed in cris_elf_check_relocs that
1802
     this is an error.  */
1803
0
        return false;
1804
0
      }
1805
    /* Fall through.  */
1806
1807
0
  case R_CRIS_32_GOT_TPREL:
1808
0
  case R_CRIS_16_GOT_TPREL:
1809
0
    if (rel->r_addend != 0)
1810
0
      {
1811
        /* We can't do anything for a relocation which is
1812
     against a symbol *plus offset*.  GOT holds
1813
     relocations for symbols.  Make this an error; the
1814
     compiler isn't allowed to pass us these kinds of
1815
     things.  */
1816
0
        _bfd_error_handler
1817
    /* xgettext:c-format */
1818
0
    (_("%pB, section %pA: relocation %s with non-zero addend"
1819
0
       " %" PRId64 " against symbol `%s'"),
1820
0
     input_bfd,
1821
0
     input_section,
1822
0
     cris_elf_howto_table[r_type].name,
1823
0
     (int64_t) rel->r_addend,
1824
0
     symname[0] != '\0' ? symname : _("[whose name is lost]"));
1825
0
        bfd_set_error (bfd_error_bad_value);
1826
0
        return false;
1827
0
      }
1828
1829
0
    if (!bfd_link_pic (info)
1830
0
        && (h == NULL || h->def_regular || ELF_COMMON_DEF_P (h)))
1831
0
      {
1832
        /* Known contents of the GOT.  */
1833
0
        bfd_vma off;
1834
1835
        /* The symbol is defined in the program, so just write
1836
     the -prog_tls_size+known_tpoffset into the GOT.  */
1837
0
        relocation -= elf_hash_table (info)->tls_sec->vma;
1838
0
        relocation -= elf_hash_table (info)->tls_size;
1839
1840
0
        if (h != NULL)
1841
0
    off = h->got.offset;
1842
0
        else
1843
0
    off = local_got_offsets[r_symndx];
1844
1845
        /* Bit 0 is used to mark whether we've emitted the required
1846
     entry (and if needed R_CRIS_32_TPREL reloc).  Bit 1
1847
     is used similarly for R_CRIS_DTP, see above.  */
1848
0
        if ((off & 1) == 0)
1849
0
    {
1850
0
      off &= ~3;
1851
1852
0
      if (h != NULL)
1853
0
        h->got.offset |= 1;
1854
0
      else
1855
0
        local_got_offsets[r_symndx] |= 1;
1856
1857
0
      bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1858
0
    }
1859
0
        else
1860
0
    off &= ~3;
1861
1862
0
        relocation = sgot->output_offset + off
1863
0
    + (r_type == R_CRIS_32_IE ? sgot->output_section->vma : 0);
1864
0
      }
1865
0
    else
1866
0
      {
1867
        /* Emit a real relocation.  */
1868
0
        bfd_vma off;
1869
1870
0
        if (h != NULL)
1871
0
    off = h->got.offset;
1872
0
        else
1873
0
    off = local_got_offsets[r_symndx];
1874
1875
        /* See above re usage of bit 0 and 1.  */
1876
0
        if ((off & 1) == 0)
1877
0
    {
1878
0
      Elf_Internal_Rela outrel;
1879
0
      bfd_byte *loc;
1880
1881
0
      off &= ~3;
1882
1883
0
      if (h != NULL)
1884
0
        h->got.offset |= 1;
1885
0
      else
1886
0
        local_got_offsets[r_symndx] |= 1;
1887
1888
0
      srelgot = htab->root.srelgot;
1889
0
      BFD_ASSERT (srelgot != NULL);
1890
1891
0
      if (h != NULL && h->dynindx != -1)
1892
0
        {
1893
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_32_TPREL);
1894
0
          relocation = 0;
1895
0
        }
1896
0
      else
1897
0
        {
1898
0
          outrel.r_info = ELF32_R_INFO (0, R_CRIS_32_TPREL);
1899
1900
          /* NULL if we had an error.  */
1901
0
          relocation -= elf_hash_table (info)->tls_sec == NULL
1902
0
      ? 0 : elf_hash_table (info)->tls_sec->vma;
1903
0
        }
1904
1905
      /* Just "define" the initial contents in some
1906
         semi-logical way.  */
1907
0
      bfd_put_32 (output_bfd, relocation, sgot->contents + off);
1908
1909
0
      outrel.r_offset = (sgot->output_section->vma
1910
0
             + sgot->output_offset
1911
0
             + off);
1912
0
      outrel.r_addend = relocation;
1913
0
      loc = srelgot->contents;
1914
0
      loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
1915
      /* NULL if we had an error.  */
1916
0
      if (srelgot->contents != NULL)
1917
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
1918
0
    }
1919
0
        else
1920
0
    off &= ~3;
1921
1922
0
        relocation = sgot->output_offset + off
1923
0
    + (r_type == R_CRIS_32_IE ? sgot->output_section->vma : 0);
1924
0
      }
1925
1926
    /* The GOT-relative offset to the GOT entry is the relocation,
1927
       or for R_CRIS_32_GD, the actual address of the GOT entry.  */
1928
0
    break;
1929
1930
0
  case R_CRIS_16_TPREL:
1931
0
  case R_CRIS_32_TPREL:
1932
    /* This relocation must only be performed against symbols
1933
       defined in an ordinary (non-DSO) object.  */
1934
0
    if (bfd_link_pic (info))
1935
0
      {
1936
0
        bfd_set_error (bfd_error_invalid_operation);
1937
1938
        /* We've already informed in cris_elf_check_relocs that
1939
     this is an error.  */
1940
0
        return false;
1941
0
      }
1942
1943
0
    if (h != NULL
1944
0
        && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
1945
0
        && !(h->def_regular || ELF_COMMON_DEF_P (h))
1946
        /* If it's undefined, then an error message has already
1947
     been emitted.  */
1948
0
        && h->root.type != bfd_link_hash_undefined)
1949
0
      {
1950
0
        _bfd_error_handler
1951
    /* xgettext:c-format */
1952
0
    (_("%pB, section %pA: relocation %s is"
1953
0
       " not allowed for symbol: `%s'"
1954
0
       " which is defined outside the program,"
1955
0
       " perhaps a declaration mixup?"),
1956
0
     input_bfd,
1957
0
     input_section,
1958
0
     cris_elf_howto_table[r_type].name,
1959
0
     symname);
1960
0
        bfd_set_error (bfd_error_bad_value);
1961
0
        return false;
1962
0
      }
1963
1964
    /* NULL if we had an error.  */
1965
0
    relocation -= elf_hash_table (info)->tls_sec == NULL
1966
0
      ? 0
1967
0
      : (elf_hash_table (info)->tls_sec->vma
1968
0
         + elf_hash_table (info)->tls_size);
1969
1970
    /* The TLS-relative offset is the relocation.  */
1971
0
    break;
1972
1973
0
  default:
1974
0
    BFD_FAIL ();
1975
0
    return false;
1976
0
  }
1977
1978
0
      r = cris_final_link_relocate (howto, input_bfd, input_section,
1979
0
             contents, rel, relocation);
1980
1981
0
      if (r != bfd_reloc_ok)
1982
0
  {
1983
0
    const char * msg = (const char *) NULL;
1984
1985
0
    switch (r)
1986
0
      {
1987
0
      case bfd_reloc_overflow:
1988
0
        (*info->callbacks->reloc_overflow)
1989
0
    (info, (h ? &h->root : NULL), symname, howto->name,
1990
0
     (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
1991
0
        if (additional_relocation_error_msg_count > 0)
1992
0
    {
1993
0
      additional_relocation_error_msg_count--;
1994
0
      switch (r_type)
1995
0
        {
1996
0
        case R_CRIS_16_GOTPLT:
1997
0
        case R_CRIS_16_GOT:
1998
1999
          /* Not just TLS is involved here, so we make
2000
       generation and message depend on -fPIC/-fpic
2001
       only.  */
2002
0
        case R_CRIS_16_GOT_TPREL:
2003
0
        case R_CRIS_16_GOT_GD:
2004
0
          _bfd_error_handler
2005
0
      (_("(too many global variables for -fpic:"
2006
0
         " recompile with -fPIC)"));
2007
0
          break;
2008
2009
0
        case R_CRIS_16_TPREL:
2010
0
        case R_CRIS_16_DTPREL:
2011
0
          _bfd_error_handler
2012
0
      (_("(thread-local data too big for -fpic or"
2013
0
         " -msmall-tls: recompile with -fPIC or"
2014
0
         " -mno-small-tls)"));
2015
0
          break;
2016
2017
          /* No known cause for overflow for other relocs.  */
2018
0
        default:
2019
0
          break;
2020
0
        }
2021
0
    }
2022
0
        break;
2023
2024
0
      case bfd_reloc_undefined:
2025
0
        (*info->callbacks->undefined_symbol)
2026
0
    (info, symname, input_bfd, input_section, rel->r_offset, true);
2027
0
        break;
2028
2029
0
      case bfd_reloc_outofrange:
2030
0
        msg = _("internal error: out of range error");
2031
0
        break;
2032
2033
0
      case bfd_reloc_notsupported:
2034
0
        msg = _("internal error: unsupported relocation error");
2035
0
        break;
2036
2037
0
      case bfd_reloc_dangerous:
2038
0
        msg = _("internal error: dangerous relocation");
2039
0
        break;
2040
2041
0
      default:
2042
0
        msg = _("internal error: unknown error");
2043
0
        break;
2044
0
      }
2045
2046
0
    if (msg)
2047
0
      (*info->callbacks->warning) (info, msg, symname, input_bfd,
2048
0
           input_section, rel->r_offset);
2049
0
  }
2050
0
    }
2051
2052
0
  return true;
2053
0
}
2054

2055
/* Finish up dynamic symbol handling.  We set the contents of various
2056
   dynamic sections here.  */
2057
2058
static bool
2059
elf_cris_finish_dynamic_symbol (bfd *output_bfd,
2060
        struct bfd_link_info *info,
2061
        struct elf_link_hash_entry *h,
2062
        Elf_Internal_Sym *sym)
2063
0
{
2064
0
  struct elf_cris_link_hash_table * htab;
2065
2066
  /* Where in the plt entry to put values.  */
2067
0
  int plt_off1 = 2, plt_off2 = 10, plt_off3 = 16;
2068
2069
  /* What offset to add to the distance to the first PLT entry for the
2070
     value at plt_off3.   */
2071
0
  int plt_off3_value_bias = 4;
2072
2073
  /* Where in the PLT entry the call-dynlink-stub is (happens to be same
2074
     for PIC and non-PIC for v32 and pre-v32).  */
2075
0
  int plt_stub_offset = 8;
2076
0
  int plt_entry_size = PLT_ENTRY_SIZE;
2077
0
  const bfd_byte *plt_entry = elf_cris_plt_entry;
2078
0
  const bfd_byte *plt_pic_entry = elf_cris_pic_plt_entry;
2079
2080
0
  htab = elf_cris_hash_table (info);
2081
2082
  /* Adjust the various PLT entry offsets.  */
2083
0
  if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
2084
0
    {
2085
0
      plt_off2 = 14;
2086
0
      plt_off3 = 20;
2087
0
      plt_off3_value_bias = -2;
2088
0
      plt_stub_offset = 12;
2089
0
      plt_entry_size = PLT_ENTRY_SIZE_V32;
2090
0
      plt_entry = elf_cris_plt_entry_v32;
2091
0
      plt_pic_entry = elf_cris_pic_plt_entry_v32;
2092
0
    }
2093
2094
0
  if (h->plt.offset != (bfd_vma) -1)
2095
0
    {
2096
0
      asection *splt;
2097
0
      asection *sgotplt;
2098
0
      asection *srela;
2099
0
      bfd_vma got_base;
2100
2101
0
      bfd_vma gotplt_offset
2102
0
  = elf_cris_hash_entry (h)->gotplt_offset;
2103
0
      Elf_Internal_Rela rela;
2104
0
      bfd_byte *loc;
2105
0
      bool has_gotplt = gotplt_offset != 0;
2106
2107
      /* Get the index in the .rela.plt relocations for the .got.plt
2108
   entry that corresponds to this symbol.
2109
   We have to count backwards here, and the result is only valid
2110
   as an index into .rela.plt.  We also have to undo the effect
2111
   of the R_CRIS_DTPMOD entry at .got index 3 (offset 12 into
2112
   .got.plt) for which gotplt_offset is adjusted, because while
2113
   that entry goes into .got.plt, its relocation goes into
2114
   .rela.got, not .rela.plt.  (It's not PLT-specific; not to be
2115
   processed as part of the runtime lazy .rela.plt relocation).
2116
   FIXME: There be literal constants here...  */
2117
0
      bfd_vma rela_plt_index
2118
0
  = (htab->dtpmod_refcount != 0
2119
0
     ? gotplt_offset/4 - 2 - 3 : gotplt_offset/4 - 3);
2120
2121
      /* Get the offset into the .got table of the entry that corresponds
2122
   to this function.  Note that we embed knowledge that "incoming"
2123
   .got goes after .got.plt in the output without padding (pointer
2124
   aligned).  However, that knowledge is present in several other
2125
   places too.  */
2126
0
      bfd_vma got_offset
2127
0
  = (has_gotplt
2128
0
     ? gotplt_offset
2129
0
     : h->got.offset + htab->next_gotplt_entry);
2130
2131
      /* This symbol has an entry in the procedure linkage table.  Set it
2132
   up.  */
2133
2134
0
      BFD_ASSERT (h->dynindx != -1);
2135
2136
0
      splt = htab->root.splt;
2137
0
      sgotplt = htab->root.sgotplt;
2138
0
      srela = htab->root.srelplt;
2139
0
      BFD_ASSERT (splt != NULL && sgotplt != NULL
2140
0
      && (! has_gotplt || srela != NULL));
2141
2142
0
      got_base = sgotplt->output_section->vma + sgotplt->output_offset;
2143
2144
      /* Fill in the entry in the procedure linkage table.  */
2145
0
      if (! bfd_link_pic (info))
2146
0
  {
2147
0
    memcpy (splt->contents + h->plt.offset, plt_entry,
2148
0
      plt_entry_size);
2149
2150
    /* We need to enter the absolute address of the GOT entry here.  */
2151
0
    bfd_put_32 (output_bfd, got_base + got_offset,
2152
0
          splt->contents + h->plt.offset + plt_off1);
2153
0
  }
2154
0
      else
2155
0
  {
2156
0
    memcpy (splt->contents + h->plt.offset, plt_pic_entry,
2157
0
      plt_entry_size);
2158
0
    bfd_put_32 (output_bfd, got_offset,
2159
0
          splt->contents + h->plt.offset + plt_off1);
2160
0
  }
2161
2162
      /* Fill in the plt entry and make a relocation, if this is a "real"
2163
   PLT entry.  */
2164
0
      if (has_gotplt)
2165
0
  {
2166
    /* Fill in the offset to the reloc table.  */
2167
0
    bfd_put_32 (output_bfd,
2168
0
          rela_plt_index * sizeof (Elf32_External_Rela),
2169
0
          splt->contents + h->plt.offset + plt_off2);
2170
2171
    /* Fill in the offset to the first PLT entry, where to "jump".  */
2172
0
    bfd_put_32 (output_bfd,
2173
0
          - (h->plt.offset + plt_off3 + plt_off3_value_bias),
2174
0
          splt->contents + h->plt.offset + plt_off3);
2175
2176
    /* Fill in the entry in the global offset table with the address of
2177
       the relocating stub.  */
2178
0
    bfd_put_32 (output_bfd,
2179
0
          (splt->output_section->vma
2180
0
           + splt->output_offset
2181
0
           + h->plt.offset
2182
0
           + plt_stub_offset),
2183
0
          sgotplt->contents + got_offset);
2184
2185
    /* Fill in the entry in the .rela.plt section.  */
2186
0
    rela.r_offset = (sgotplt->output_section->vma
2187
0
         + sgotplt->output_offset
2188
0
         + got_offset);
2189
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_JUMP_SLOT);
2190
0
    rela.r_addend = 0;
2191
0
    loc = srela->contents + rela_plt_index * sizeof (Elf32_External_Rela);
2192
0
    bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2193
0
  }
2194
2195
0
      if (!h->def_regular)
2196
0
  {
2197
    /* Mark the symbol as undefined, rather than as defined in
2198
       the .plt section.  Leave the value alone.  */
2199
0
    sym->st_shndx = SHN_UNDEF;
2200
2201
    /* FIXME: From elf32-sparc.c 2001-02-19 (1.18).  I still don't
2202
       know whether resetting the value is significant; if it really
2203
       is, rather than a quirk or bug in the sparc port, then I
2204
       believe we'd see this elsewhere.  */
2205
    /* If the symbol is weak, we do need to clear the value.
2206
       Otherwise, the PLT entry would provide a definition for
2207
       the symbol even if the symbol wasn't defined anywhere,
2208
       and so the symbol would never be NULL.  */
2209
0
    if (!h->ref_regular_nonweak)
2210
0
      sym->st_value = 0;
2211
0
  }
2212
0
    }
2213
2214
  /* For an ordinary program, we emit .got relocs only for symbols that
2215
     are in the dynamic-symbols table and are either defined by the
2216
     program or are undefined weak symbols, or are function symbols
2217
     where we do not output a PLT: the PLT reloc was output above and all
2218
     references to the function symbol are redirected to the PLT.  */
2219
0
  if (h->got.offset != (bfd_vma) -1
2220
0
      && (elf_cris_hash_entry (h)->reg_got_refcount > 0)
2221
0
      && (bfd_link_pic (info)
2222
0
    || (h->dynindx != -1
2223
0
        && h->plt.offset == (bfd_vma) -1
2224
0
        && !h->def_regular
2225
0
        && h->root.type != bfd_link_hash_undefweak)))
2226
0
    {
2227
0
      asection *sgot;
2228
0
      asection *srela;
2229
0
      Elf_Internal_Rela rela;
2230
0
      bfd_byte *loc;
2231
0
      bfd_byte *where;
2232
2233
      /* This symbol has an entry in the global offset table.  Set it up.  */
2234
2235
0
      sgot = htab->root.sgot;
2236
0
      srela = htab->root.srelgot;
2237
0
      BFD_ASSERT (sgot != NULL && srela != NULL);
2238
2239
0
      rela.r_offset = (sgot->output_section->vma
2240
0
           + sgot->output_offset
2241
0
           + (h->got.offset &~ (bfd_vma) 1));
2242
2243
      /* If this is a static link, or it is a -Bsymbolic link and the
2244
   symbol is defined locally or was forced to be local because
2245
   of a version file, we just want to emit a RELATIVE reloc.
2246
   The entry in the global offset table will already have been
2247
   initialized in the relocate_section function.  */
2248
0
      where = sgot->contents + (h->got.offset &~ (bfd_vma) 1);
2249
0
      if (! elf_hash_table (info)->dynamic_sections_created
2250
0
    || (bfd_link_pic (info)
2251
0
        && (SYMBOLIC_BIND (info, h) || h->dynindx == -1)
2252
0
        && h->def_regular))
2253
0
  {
2254
0
    rela.r_info = ELF32_R_INFO (0, R_CRIS_RELATIVE);
2255
0
    rela.r_addend = bfd_get_signed_32 (output_bfd, where);
2256
0
  }
2257
0
      else
2258
0
  {
2259
0
    bfd_put_32 (output_bfd, (bfd_vma) 0, where);
2260
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_GLOB_DAT);
2261
0
    rela.r_addend = 0;
2262
0
  }
2263
2264
0
      loc = srela->contents;
2265
0
      loc += srela->reloc_count++ * sizeof (Elf32_External_Rela);
2266
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2267
0
    }
2268
2269
0
  if (h->needs_copy)
2270
0
    {
2271
0
      asection *s;
2272
0
      Elf_Internal_Rela rela;
2273
0
      bfd_byte *loc;
2274
2275
      /* This symbol needs a copy reloc.  Set it up.  */
2276
2277
0
      BFD_ASSERT (h->dynindx != -1
2278
0
      && (h->root.type == bfd_link_hash_defined
2279
0
          || h->root.type == bfd_link_hash_defweak));
2280
2281
0
      if (h->root.u.def.section == htab->root.sdynrelro)
2282
0
  s = htab->root.sreldynrelro;
2283
0
      else
2284
0
  s = htab->root.srelbss;
2285
2286
0
      rela.r_offset = (h->root.u.def.value
2287
0
           + h->root.u.def.section->output_section->vma
2288
0
           + h->root.u.def.section->output_offset);
2289
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_CRIS_COPY);
2290
0
      rela.r_addend = 0;
2291
0
      loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
2292
0
      bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
2293
0
    }
2294
2295
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
2296
0
  if (h == elf_hash_table (info)->hdynamic
2297
0
      || h == elf_hash_table (info)->hgot)
2298
0
    sym->st_shndx = SHN_ABS;
2299
2300
0
  return true;
2301
0
}
2302

2303
/* Finish up the dynamic sections.  Do *not* emit relocs here, as their
2304
   offsets were changed, as part of -z combreloc handling, from those we
2305
   computed.  */
2306
2307
static bool
2308
elf_cris_finish_dynamic_sections (bfd *output_bfd,
2309
          struct bfd_link_info *info,
2310
          bfd_byte *buf ATTRIBUTE_UNUSED)
2311
0
{
2312
0
  bfd *dynobj;
2313
0
  asection *sgot;
2314
0
  asection *sdyn;
2315
2316
0
  dynobj = elf_hash_table (info)->dynobj;
2317
2318
0
  sgot = elf_hash_table (info)->sgotplt;
2319
0
  BFD_ASSERT (sgot != NULL);
2320
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2321
2322
0
  if (elf_hash_table (info)->dynamic_sections_created)
2323
0
    {
2324
0
      asection *splt;
2325
0
      Elf32_External_Dyn *dyncon, *dynconend;
2326
2327
0
      splt = elf_hash_table (info)->splt;
2328
0
      BFD_ASSERT (splt != NULL && sdyn != NULL);
2329
2330
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
2331
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2332
0
      for (; dyncon < dynconend; dyncon++)
2333
0
  {
2334
0
    Elf_Internal_Dyn dyn;
2335
0
    asection *s;
2336
2337
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
2338
2339
0
    switch (dyn.d_tag)
2340
0
      {
2341
0
      default:
2342
0
        break;
2343
2344
0
      case DT_PLTGOT:
2345
0
        dyn.d_un.d_ptr = sgot->output_section->vma + sgot->output_offset;
2346
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2347
0
        break;
2348
2349
0
      case DT_JMPREL:
2350
        /* Yes, we *can* have a .plt and no .plt.rela, for instance
2351
     if all symbols are found in the .got (not .got.plt).  */
2352
0
        s = elf_hash_table (info)->srelplt;
2353
0
        dyn.d_un.d_ptr = s != NULL ? (s->output_section->vma
2354
0
              + s->output_offset) : 0;
2355
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2356
0
        break;
2357
2358
0
      case DT_PLTRELSZ:
2359
0
        s = elf_hash_table (info)->srelplt;
2360
0
        if (s == NULL)
2361
0
    dyn.d_un.d_val = 0;
2362
0
        else
2363
0
    dyn.d_un.d_val = s->size;
2364
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
2365
0
        break;
2366
0
      }
2367
0
  }
2368
2369
      /* Fill in the first entry in the procedure linkage table.  */
2370
0
      if (splt->size > 0)
2371
0
  {
2372
0
    if (bfd_get_mach (output_bfd) == bfd_mach_cris_v32)
2373
0
      {
2374
0
        if (bfd_link_pic (info))
2375
0
    memcpy (splt->contents, elf_cris_pic_plt0_entry_v32,
2376
0
      PLT_ENTRY_SIZE_V32);
2377
0
        else
2378
0
    {
2379
0
      memcpy (splt->contents, elf_cris_plt0_entry_v32,
2380
0
        PLT_ENTRY_SIZE_V32);
2381
0
      bfd_put_32 (output_bfd,
2382
0
            sgot->output_section->vma
2383
0
            + sgot->output_offset + 4,
2384
0
            splt->contents + 4);
2385
2386
0
      elf_section_data (splt->output_section)->this_hdr.sh_entsize
2387
0
        = PLT_ENTRY_SIZE_V32;
2388
0
    }
2389
0
      }
2390
0
    else
2391
0
      {
2392
0
        if (bfd_link_pic (info))
2393
0
    memcpy (splt->contents, elf_cris_pic_plt0_entry,
2394
0
      PLT_ENTRY_SIZE);
2395
0
        else
2396
0
    {
2397
0
      memcpy (splt->contents, elf_cris_plt0_entry,
2398
0
        PLT_ENTRY_SIZE);
2399
0
      bfd_put_32 (output_bfd,
2400
0
            sgot->output_section->vma
2401
0
            + sgot->output_offset + 4,
2402
0
            splt->contents + 6);
2403
0
      bfd_put_32 (output_bfd,
2404
0
            sgot->output_section->vma
2405
0
            + sgot->output_offset + 8,
2406
0
            splt->contents + 14);
2407
2408
0
      elf_section_data (splt->output_section)->this_hdr.sh_entsize
2409
0
        = PLT_ENTRY_SIZE;
2410
0
    }
2411
0
      }
2412
0
  }
2413
0
    }
2414
2415
  /* Fill in the first three entries in the global offset table.  */
2416
0
  if (sgot->size > 0)
2417
0
    {
2418
0
      if (sdyn == NULL)
2419
0
  bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents);
2420
0
      else
2421
0
  bfd_put_32 (output_bfd,
2422
0
        sdyn->output_section->vma + sdyn->output_offset,
2423
0
        sgot->contents);
2424
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 4);
2425
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + 8);
2426
0
    }
2427
2428
0
  elf_section_data (sgot->output_section)->this_hdr.sh_entsize = 4;
2429
2430
0
  return true;
2431
0
}
2432

2433
/* Return the section that should be marked against GC for a given
2434
   relocation.  */
2435
2436
static asection *
2437
cris_elf_gc_mark_hook (asection *sec,
2438
           struct bfd_link_info *info,
2439
           struct elf_reloc_cookie *cookie,
2440
           struct elf_link_hash_entry *h,
2441
           unsigned int symndx)
2442
0
{
2443
0
  enum elf_cris_reloc_type r_type = ELF32_R_TYPE (cookie->rel->r_info);
2444
0
  if (h != NULL)
2445
0
    switch (r_type)
2446
0
      {
2447
0
      case R_CRIS_GNU_VTINHERIT:
2448
0
      case R_CRIS_GNU_VTENTRY:
2449
0
  return NULL;
2450
2451
0
      default:
2452
0
  break;
2453
0
      }
2454
2455
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
2456
0
}
2457
2458
/* The elf_backend_plt_sym_val hook function.  */
2459
2460
static bfd_vma
2461
cris_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED, const asection *plt,
2462
          const arelent *rel)
2463
0
{
2464
0
  bfd_size_type plt_entry_size;
2465
0
  bfd_size_type pltoffs;
2466
0
  bfd *abfd = plt->owner;
2467
2468
  /* Same for CRIS and CRIS v32; see elf_cris_(|pic_)plt_entry(|_v32)[].  */
2469
0
  bfd_size_type plt_entry_got_offset = 2;
2470
0
  bfd_size_type plt_sec_size;
2471
0
  bfd_size_type got_vma_for_dyn;
2472
0
  asection *got;
2473
2474
  /* FIXME: the .got section should be readily available also when
2475
     we're not linking.  */
2476
0
  if ((got = bfd_get_section_by_name (abfd, ".got")) == NULL)
2477
0
    return (bfd_vma) -1;
2478
2479
0
  plt_sec_size =  bfd_section_size (plt);
2480
0
  plt_entry_size
2481
0
    = (bfd_get_mach (abfd) == bfd_mach_cris_v32
2482
0
       ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
2483
2484
  /* Data in PLT is GOT-relative for DYN, but absolute for EXE.  */
2485
0
  got_vma_for_dyn = (abfd->flags & EXEC_P) ? 0 : got->vma;
2486
2487
  /* Because we can have merged GOT entries; a single .got entry for
2488
     both GOT and the PLT part of the GOT (.got.plt), the index of the
2489
     reloc in .rela.plt is not the same as the index in the PLT.
2490
     Instead, we have to hunt down the GOT offset in the PLT that
2491
     corresponds to that of this reloc.  Unfortunately, we will only
2492
     be called for the .rela.plt relocs, so we'll miss synthetic
2493
     symbols for .plt entries with merged GOT entries.  (FIXME:
2494
     fixable by providing our own bfd_elf32_get_synthetic_symtab.
2495
     Doesn't seem worthwile at time of this writing.)  FIXME: we've
2496
     gone from O(1) to O(N) (N number of PLT entries) for finding each
2497
     PLT address.  Shouldn't matter in practice though.  */
2498
2499
0
  for (pltoffs = plt_entry_size;
2500
0
       pltoffs < plt_sec_size;
2501
0
       pltoffs += plt_entry_size)
2502
0
    {
2503
0
      bfd_size_type got_offset;
2504
0
      bfd_byte gotoffs_raw[4];
2505
2506
0
      if (!bfd_get_section_contents (abfd, (asection *) plt, gotoffs_raw,
2507
0
             pltoffs + plt_entry_got_offset,
2508
0
             sizeof (gotoffs_raw)))
2509
0
  return (bfd_vma) -1;
2510
2511
0
      got_offset = bfd_get_32 (abfd, gotoffs_raw);
2512
0
      if (got_offset + got_vma_for_dyn == rel->address)
2513
0
  return plt->vma + pltoffs;
2514
0
    }
2515
2516
  /* While it's tempting to BFD_ASSERT that we shouldn't get here,
2517
     that'd not be graceful behavior for invalid input.  */
2518
0
  return (bfd_vma) -1;
2519
0
}
2520
2521
/* Make sure we emit a GOT entry if the symbol was supposed to have a PLT
2522
   entry but we found we will not create any.  Called when we find we will
2523
   not have any PLT for this symbol, by for example
2524
   elf_cris_adjust_dynamic_symbol when we're doing a proper dynamic link,
2525
   or elf_cris_late_size_sections if no dynamic sections will be
2526
   created (we're only linking static objects).  */
2527
2528
static bool
2529
elf_cris_adjust_gotplt_to_got (struct elf_cris_link_hash_entry *h, void * p)
2530
0
{
2531
0
  struct bfd_link_info *info = (struct bfd_link_info *) p;
2532
2533
  /* A GOTPLT reloc, when activated, is supposed to be included into
2534
     the PLT refcount, when the symbol isn't set-or-forced local.  */
2535
0
  BFD_ASSERT (h->gotplt_refcount == 0
2536
0
        || h->root.plt.refcount == -1
2537
0
        || h->gotplt_refcount <= h->root.plt.refcount);
2538
2539
  /* If nobody wanted a GOTPLT with this symbol, we're done.  */
2540
0
  if (h->gotplt_refcount <= 0)
2541
0
    return true;
2542
2543
0
  if (h->reg_got_refcount > 0)
2544
0
    {
2545
      /* There's a GOT entry for this symbol.  Just adjust the refcounts.
2546
   Probably not necessary at this stage, but keeping them accurate
2547
   helps avoiding surprises later.  */
2548
0
      h->root.got.refcount += h->gotplt_refcount;
2549
0
      h->reg_got_refcount += h->gotplt_refcount;
2550
0
      h->gotplt_refcount = 0;
2551
0
    }
2552
0
  else
2553
0
    {
2554
      /* No GOT entry for this symbol.  We need to create one.  */
2555
0
      asection *sgot;
2556
0
      asection *srelgot;
2557
2558
0
      sgot = elf_hash_table (info)->sgot;
2559
0
      srelgot = elf_hash_table (info)->srelgot;
2560
2561
      /* Put accurate refcounts there.  */
2562
0
      BFD_ASSERT (h->root.got.refcount >= 0);
2563
0
      h->root.got.refcount += h->gotplt_refcount;
2564
0
      h->reg_got_refcount = h->gotplt_refcount;
2565
2566
0
      h->gotplt_refcount = 0;
2567
2568
      /* We always have a .got and a .rela.got section if there were
2569
   GOTPLT relocs in input.  */
2570
0
      BFD_ASSERT (sgot != NULL && srelgot != NULL);
2571
2572
      /* Allocate space in the .got section.  */
2573
0
      sgot->size += 4;
2574
2575
      /* Allocate relocation space.  */
2576
0
      srelgot->size += sizeof (Elf32_External_Rela);
2577
0
    }
2578
2579
0
  return true;
2580
0
}
2581
2582
/* Try to fold PLT entries with GOT entries.  There are two cases when we
2583
   want to do this:
2584
2585
   - When all PLT references are GOTPLT references, and there are GOT
2586
     references, and this is not the executable.  We don't have to
2587
     generate a PLT at all.
2588
2589
   - When there are both (ordinary) PLT references and GOT references,
2590
     and this isn't the executable.
2591
     We want to make the PLT reference use the ordinary GOT entry rather
2592
     than R_CRIS_JUMP_SLOT, a run-time dynamically resolved GOTPLT entry,
2593
     since the GOT entry will have to be resolved at startup anyway.
2594
2595
   Though the latter case is handled when room for the PLT is allocated,
2596
   not here.
2597
2598
   By folding into the GOT, we may need a round-trip to a PLT in the
2599
   executable for calls, a loss in performance.  Still, losing a
2600
   reloc is a win in size and at least in start-up time.
2601
2602
   Note that this function is called before symbols are forced local by
2603
   version scripts.  The differing cases are handled by
2604
   elf_cris_hide_symbol.  */
2605
2606
static bool
2607
elf_cris_try_fold_plt_to_got (struct elf_cris_link_hash_entry *h, void * p)
2608
0
{
2609
0
  struct bfd_link_info *info = (struct bfd_link_info *) p;
2610
2611
  /* If there are no GOT references for this symbol, we can't fold any
2612
     other reference so there's nothing to do.  Likewise if there are no
2613
     PLT references; GOTPLT references included.  */
2614
0
  if (h->root.got.refcount <= 0 || h->root.plt.refcount <= 0)
2615
0
    return true;
2616
2617
  /* GOTPLT relocs are supposed to be included into the PLT refcount.  */
2618
0
  BFD_ASSERT (h->gotplt_refcount <= h->root.plt.refcount);
2619
2620
0
  if (h->gotplt_refcount == h->root.plt.refcount)
2621
0
    {
2622
      /* The only PLT references are GOTPLT references, and there are GOT
2623
   references.  Convert PLT to GOT references.  */
2624
0
      if (! elf_cris_adjust_gotplt_to_got (h, info))
2625
0
  return false;
2626
2627
      /* Clear the PLT references, so no PLT will be created.  */
2628
0
      h->root.plt.offset = (bfd_vma) -1;
2629
0
    }
2630
2631
0
  return true;
2632
0
}
2633
2634
/* Our own version of hide_symbol, so that we can adjust a GOTPLT reloc
2635
   to use a GOT entry (and create one) rather than requiring a GOTPLT
2636
   entry.  */
2637
2638
static void
2639
elf_cris_hide_symbol (struct bfd_link_info *info,
2640
          struct elf_link_hash_entry *h,
2641
          bool force_local)
2642
0
{
2643
0
  elf_cris_adjust_gotplt_to_got ((struct elf_cris_link_hash_entry *) h, info);
2644
2645
0
  _bfd_elf_link_hash_hide_symbol (info, h, force_local);
2646
0
}
2647
2648
/* Adjust a symbol defined by a dynamic object and referenced by a
2649
   regular object.  The current definition is in some section of the
2650
   dynamic object, but we're not including those sections.  We have to
2651
   change the definition to something the rest of the link can
2652
   understand.  */
2653
2654
static bool
2655
elf_cris_adjust_dynamic_symbol (struct bfd_link_info *info,
2656
        struct elf_link_hash_entry *h)
2657
0
{
2658
0
  struct elf_cris_link_hash_table * htab;
2659
0
  bfd *dynobj;
2660
0
  asection *s;
2661
0
  asection *srel;
2662
0
  bfd_size_type plt_entry_size;
2663
2664
0
  htab = elf_cris_hash_table (info);
2665
0
  if (htab == NULL)
2666
0
    return false;
2667
2668
0
  dynobj = htab->root.dynobj;
2669
2670
  /* Make sure we know what is going on here.  */
2671
0
  BFD_ASSERT (dynobj != NULL
2672
0
        && (h->needs_plt
2673
0
      || h->is_weakalias
2674
0
      || (h->def_dynamic
2675
0
          && h->ref_regular
2676
0
          && !h->def_regular)));
2677
2678
0
  plt_entry_size
2679
0
    = (bfd_get_mach (dynobj) == bfd_mach_cris_v32
2680
0
       ? PLT_ENTRY_SIZE_V32 : PLT_ENTRY_SIZE);
2681
2682
  /* If this is a function, put it in the procedure linkage table.  We
2683
     will fill in the contents of the procedure linkage table later,
2684
     when we know the address of the .got section.  */
2685
0
  if (h->type == STT_FUNC
2686
0
      || h->needs_plt)
2687
0
    {
2688
      /* If we link a program (not a DSO), we'll get rid of unnecessary
2689
   PLT entries; we point to the actual symbols -- even for pic
2690
   relocs, because a program built with -fpic should have the same
2691
   result as one built without -fpic, specifically considering weak
2692
   symbols.
2693
   FIXME: m68k and i386 differ here, for unclear reasons.  */
2694
0
      if (! bfd_link_pic (info)
2695
0
    && !h->def_dynamic)
2696
0
  {
2697
    /* This case can occur if we saw a PLT reloc in an input file,
2698
       but the symbol was not defined by a dynamic object.  In such
2699
       a case, we don't actually need to build a procedure linkage
2700
       table, and we can just do an absolute or PC reloc instead, or
2701
       change a .got.plt index to a .got index for GOTPLT relocs.  */
2702
0
    BFD_ASSERT (h->needs_plt);
2703
0
    h->needs_plt = 0;
2704
0
    h->plt.offset = (bfd_vma) -1;
2705
0
    return
2706
0
      elf_cris_adjust_gotplt_to_got ((struct
2707
0
              elf_cris_link_hash_entry *) h,
2708
0
             info);
2709
0
  }
2710
2711
      /* If we had a R_CRIS_GLOB_DAT that didn't have to point to a PLT;
2712
   where a pointer-equivalent symbol was unimportant (i.e. more
2713
   like R_CRIS_JUMP_SLOT after symbol evaluation) we could get rid
2714
   of the PLT.  We can't for the executable, because the GOT
2715
   entries will point to the PLT there (and be constant).  */
2716
0
      if (bfd_link_pic (info)
2717
0
    && !elf_cris_try_fold_plt_to_got ((struct elf_cris_link_hash_entry*)
2718
0
              h, info))
2719
0
  return false;
2720
2721
      /* GC or folding may have rendered this entry unused.  */
2722
0
      if (h->plt.refcount <= 0)
2723
0
  {
2724
0
    h->needs_plt = 0;
2725
0
    h->plt.offset = (bfd_vma) -1;
2726
0
    return true;
2727
0
  }
2728
2729
      /* Make sure this symbol is output as a dynamic symbol.  */
2730
0
      if (h->dynindx == -1)
2731
0
  {
2732
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
2733
0
      return false;
2734
0
  }
2735
2736
0
      s = htab->root.splt;
2737
0
      BFD_ASSERT (s != NULL);
2738
2739
      /* If this is the first .plt entry, make room for the special
2740
   first entry.  */
2741
0
      if (s->size == 0)
2742
0
  s->size += plt_entry_size;
2743
2744
      /* If this symbol is not defined in a regular file, and we are
2745
   not generating a shared library, then set the symbol to this
2746
   location in the .plt.  */
2747
0
      if (!bfd_link_pic (info)
2748
0
    && !h->def_regular)
2749
0
  {
2750
0
    h->root.u.def.section = s;
2751
0
    h->root.u.def.value = s->size;
2752
0
  }
2753
2754
      /* If there's already a GOT entry, use that, not a .got.plt.  A
2755
   GOT field still has a reference count when we get here; it's
2756
   not yet changed to an offset.  We can't do this for an
2757
   executable, because then the reloc associated with the PLT
2758
   would get a non-PLT reloc pointing to the PLT.  FIXME: Move
2759
   this to elf_cris_try_fold_plt_to_got.  */
2760
0
      if (bfd_link_pic (info) && h->got.refcount > 0)
2761
0
  {
2762
0
    h->got.refcount += h->plt.refcount;
2763
2764
    /* Mark the PLT offset to use the GOT entry by setting the low
2765
       bit in the plt offset; it is always a multiple of
2766
       plt_entry_size (which is at least a multiple of 2).  */
2767
0
    BFD_ASSERT ((s->size % plt_entry_size) == 0);
2768
2769
    /* Change the PLT refcount to an offset.  */
2770
0
    h->plt.offset = s->size;
2771
2772
    /* By not setting gotplt_offset (i.e. it remains at 0), we signal
2773
       that the got entry should be used instead.  */
2774
0
    BFD_ASSERT (((struct elf_cris_link_hash_entry *)
2775
0
           h)->gotplt_offset == 0);
2776
2777
    /* Make room for this entry.  */
2778
0
    s->size += plt_entry_size;
2779
2780
0
    return true;
2781
0
  }
2782
2783
      /* No GOT reference for this symbol; prepare for an ordinary PLT.  */
2784
0
      h->plt.offset = s->size;
2785
2786
      /* Make room for this entry.  */
2787
0
      s->size += plt_entry_size;
2788
2789
      /* We also need to make an entry in the .got.plt section, which
2790
   will be placed in the .got section by the linker script.  */
2791
0
      ((struct elf_cris_link_hash_entry *) h)->gotplt_offset
2792
0
  = htab->next_gotplt_entry;
2793
0
      htab->next_gotplt_entry += 4;
2794
2795
0
      s = htab->root.sgotplt;
2796
0
      BFD_ASSERT (s != NULL);
2797
0
      s->size += 4;
2798
2799
      /* We also need to make an entry in the .rela.plt section.  */
2800
2801
0
      s = htab->root.srelplt;
2802
0
      BFD_ASSERT (s != NULL);
2803
0
      s->size += sizeof (Elf32_External_Rela);
2804
2805
0
      return true;
2806
0
    }
2807
2808
  /* Reinitialize the plt offset now that it is not used as a reference
2809
     count any more.  */
2810
0
  h->plt.offset = (bfd_vma) -1;
2811
2812
  /* If this is a weak symbol, and there is a real definition, the
2813
     processor independent code will have arranged for us to see the
2814
     real definition first, and we can just use the same value.  */
2815
0
  if (h->is_weakalias)
2816
0
    {
2817
0
      struct elf_link_hash_entry *def = weakdef (h);
2818
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2819
0
      h->root.u.def.section = def->root.u.def.section;
2820
0
      h->root.u.def.value = def->root.u.def.value;
2821
0
      return true;
2822
0
    }
2823
2824
  /* This is a reference to a symbol defined by a dynamic object which
2825
     is not a function.  */
2826
2827
  /* If we are creating a shared library, we must presume that the
2828
     only references to the symbol are via the global offset table.
2829
     For such cases we need not do anything here; the relocations will
2830
     be handled correctly by relocate_section.  */
2831
0
  if (bfd_link_pic (info))
2832
0
    return true;
2833
2834
  /* If there are no references to this symbol that do not use the
2835
     GOT, we don't need to generate a copy reloc.  */
2836
0
  if (!h->non_got_ref)
2837
0
    return true;
2838
2839
  /* We must allocate the symbol in our .dynbss section, which will
2840
     become part of the .bss section of the executable.  There will be
2841
     an entry for this symbol in the .dynsym section.  The dynamic
2842
     object will contain position independent code, so all references
2843
     from the dynamic object to this symbol will go through the global
2844
     offset table.  The dynamic linker will use the .dynsym entry to
2845
     determine the address it must put in the global offset table, so
2846
     both the dynamic object and the regular object will refer to the
2847
     same memory location for the variable.  */
2848
2849
  /* We must generate a R_CRIS_COPY reloc to tell the dynamic linker to
2850
     copy the initial value out of the dynamic object and into the
2851
     runtime process image.  We need to remember the offset into the
2852
     .rela.bss section we are going to use.  */
2853
2854
0
  if ((h->root.u.def.section->flags & SEC_READONLY) != 0)
2855
0
    {
2856
0
      s = htab->root.sdynrelro;
2857
0
      srel = htab->root.sreldynrelro;
2858
0
    }
2859
0
  else
2860
0
    {
2861
0
      s = htab->root.sdynbss;
2862
0
      srel = htab->root.srelbss;
2863
0
    }
2864
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2865
0
    {
2866
0
      BFD_ASSERT (srel != NULL);
2867
0
      srel->size += sizeof (Elf32_External_Rela);
2868
0
      h->needs_copy = 1;
2869
0
    }
2870
2871
0
  BFD_ASSERT (s != NULL);
2872
2873
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
2874
0
}
2875
2876
/* Adjust our "subclass" elements for an indirect symbol.  */
2877
2878
static void
2879
elf_cris_copy_indirect_symbol (struct bfd_link_info *info,
2880
             struct elf_link_hash_entry *dir,
2881
             struct elf_link_hash_entry *ind)
2882
0
{
2883
0
  struct elf_cris_link_hash_entry *edir, *eind;
2884
2885
0
  edir = (struct elf_cris_link_hash_entry *) dir;
2886
0
  eind = (struct elf_cris_link_hash_entry *) ind;
2887
2888
  /* Only indirect symbols are replaced; we're not interested in
2889
     updating any of EIND's fields for other symbols.  */
2890
0
  if (eind->root.root.type != bfd_link_hash_indirect)
2891
0
    {
2892
      /* Still, we need to copy flags for e.g. weak definitions.  */
2893
0
      _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2894
0
      return;
2895
0
    }
2896
2897
0
  BFD_ASSERT (edir->gotplt_offset == 0 || eind->gotplt_offset == 0);
2898
2899
0
#define XMOVOPZ(F, OP, Z) edir->F OP eind->F; eind->F = Z
2900
0
#define XMOVE(F) XMOVOPZ (F, +=, 0)
2901
0
  if (eind->pcrel_relocs_copied != NULL)
2902
0
    {
2903
0
      if (edir->pcrel_relocs_copied != NULL)
2904
0
  {
2905
0
    struct elf_cris_pcrel_relocs_copied **pp;
2906
0
    struct elf_cris_pcrel_relocs_copied *p;
2907
2908
    /* Add reloc counts against the indirect sym to the direct sym
2909
       list.  Merge any entries against the same section.  */
2910
0
    for (pp = &eind->pcrel_relocs_copied; *pp != NULL;)
2911
0
      {
2912
0
        struct elf_cris_pcrel_relocs_copied *q;
2913
0
        p = *pp;
2914
0
        for (q = edir->pcrel_relocs_copied; q != NULL; q = q->next)
2915
0
    if (q->section == p->section)
2916
0
      {
2917
0
        q->count += p->count;
2918
0
        *pp = p->next;
2919
0
        break;
2920
0
      }
2921
0
        if (q == NULL)
2922
0
    pp = &p->next;
2923
0
      }
2924
0
    *pp = edir->pcrel_relocs_copied;
2925
0
  }
2926
0
      XMOVOPZ (pcrel_relocs_copied, =, NULL);
2927
0
    }
2928
0
  XMOVE (gotplt_refcount);
2929
0
  XMOVE (gotplt_offset);
2930
0
  XMOVE (reg_got_refcount);
2931
0
  XMOVE (tprel_refcount);
2932
0
  XMOVE (dtp_refcount);
2933
0
#undef XMOVE
2934
0
#undef XMOVOPZ
2935
2936
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
2937
0
}
2938
2939
/* Look through the relocs for a section during the first phase.  */
2940
2941
static bool
2942
cris_elf_check_relocs (bfd *abfd,
2943
           struct bfd_link_info *info,
2944
           asection *sec,
2945
           const Elf_Internal_Rela *relocs)
2946
0
{
2947
0
  struct elf_cris_link_hash_table * htab;
2948
0
  bfd *dynobj;
2949
0
  Elf_Internal_Shdr *symtab_hdr;
2950
0
  struct elf_link_hash_entry **sym_hashes;
2951
0
  bfd_signed_vma *local_got_refcounts;
2952
0
  const Elf_Internal_Rela *rel;
2953
0
  const Elf_Internal_Rela *rel_end;
2954
0
  asection *sgot;
2955
0
  asection *srelgot;
2956
0
  asection *sreloc;
2957
2958
0
  if (bfd_link_relocatable (info))
2959
0
    return true;
2960
2961
0
  htab = elf_cris_hash_table (info);
2962
0
  if (htab == NULL)
2963
0
    return false;
2964
2965
0
  dynobj = elf_hash_table (info)->dynobj;
2966
0
  symtab_hdr = &elf_symtab_hdr (abfd);
2967
0
  sym_hashes = elf_sym_hashes (abfd);
2968
0
  local_got_refcounts = elf_local_got_refcounts (abfd);
2969
2970
0
  sgot = NULL;
2971
0
  srelgot = NULL;
2972
0
  sreloc = NULL;
2973
2974
0
  rel_end = relocs + sec->reloc_count;
2975
0
  for (rel = relocs; rel < rel_end; rel++)
2976
0
    {
2977
0
      struct elf_link_hash_entry *h;
2978
0
      unsigned long r_symndx;
2979
0
      enum elf_cris_reloc_type r_type;
2980
0
      bfd_signed_vma got_element_size = 4;
2981
0
      unsigned long r_symndx_lgot = INT_MAX;
2982
2983
0
      r_symndx = ELF32_R_SYM (rel->r_info);
2984
0
      if (r_symndx < symtab_hdr->sh_info)
2985
0
  {
2986
0
    h = NULL;
2987
0
    r_symndx_lgot = LGOT_REG_NDX (r_symndx);
2988
0
  }
2989
0
      else
2990
0
  {
2991
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2992
0
    while (h->root.type == bfd_link_hash_indirect
2993
0
     || h->root.type == bfd_link_hash_warning)
2994
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
2995
0
  }
2996
2997
0
      r_type = ELF32_R_TYPE (rel->r_info);
2998
2999
      /* Some relocs require linker-created sections; we need to hang them
3000
   on the first input bfd we found that contained dynamic relocs.  */
3001
0
      switch (r_type)
3002
0
  {
3003
0
  case R_CRIS_32_DTPREL:
3004
0
    if ((sec->flags & SEC_ALLOC) == 0)
3005
      /* This'd be a .dtpreld entry in e.g. debug info.  We have
3006
         several different switch statements below, but none of
3007
         that is needed; we need no preparations for resolving
3008
         R_CRIS_32_DTPREL into a non-allocated section (debug
3009
         info), so let's just move on to the next
3010
         relocation.  */
3011
0
      continue;
3012
    /* Fall through.  */
3013
0
  case R_CRIS_16_DTPREL:
3014
    /* The first .got.plt entry is right after the R_CRIS_DTPMOD
3015
       entry at index 3. */
3016
0
    if (htab->dtpmod_refcount == 0)
3017
0
      htab->next_gotplt_entry += 8;
3018
3019
0
    htab->dtpmod_refcount++;
3020
    /* Fall through.  */
3021
3022
0
  case R_CRIS_32_IE:
3023
0
  case R_CRIS_32_GD:
3024
0
  case R_CRIS_16_GOT_GD:
3025
0
  case R_CRIS_32_GOT_GD:
3026
0
  case R_CRIS_32_GOT_TPREL:
3027
0
  case R_CRIS_16_GOT_TPREL:
3028
0
  case R_CRIS_16_GOT:
3029
0
  case R_CRIS_32_GOT:
3030
0
  case R_CRIS_32_GOTREL:
3031
0
  case R_CRIS_32_PLT_GOTREL:
3032
0
  case R_CRIS_32_PLT_PCREL:
3033
0
  case R_CRIS_16_GOTPLT:
3034
0
  case R_CRIS_32_GOTPLT:
3035
0
    if (dynobj == NULL)
3036
0
      {
3037
0
        elf_hash_table (info)->dynobj = dynobj = abfd;
3038
3039
        /* We could handle this if we can get a handle on the
3040
     output bfd in elf_cris_adjust_dynamic_symbol.  Failing
3041
     that, we must insist on dynobj being a specific mach.  */
3042
0
        if (bfd_get_mach (dynobj) == bfd_mach_cris_v10_v32)
3043
0
    {
3044
0
      _bfd_error_handler
3045
        /* xgettext:c-format */
3046
0
        (_("%pB, section %pA: v10/v32 compatible object"
3047
0
           " must not contain a PIC relocation"),
3048
0
         abfd, sec);
3049
0
      return false;
3050
0
    }
3051
0
      }
3052
3053
0
    if (sgot == NULL)
3054
0
      {
3055
        /* We may have a dynobj but no .got section, if machine-
3056
     independent parts of the linker found a reason to create
3057
     a dynobj.  We want to create the .got section now, so we
3058
     can assume it's always present whenever there's a dynobj.
3059
     It's ok to call this function more than once.  */
3060
0
        if (!_bfd_elf_create_got_section (dynobj, info))
3061
0
    return false;
3062
3063
0
        sgot = elf_hash_table (info)->sgot;
3064
0
        srelgot = elf_hash_table (info)->srelgot;
3065
0
      }
3066
3067
0
    if (local_got_refcounts == NULL)
3068
0
      {
3069
0
        bfd_size_type amt;
3070
3071
        /* We use index local_got_refcounts[-1] to count all
3072
     GOT-relative relocations that do not have explicit
3073
     GOT entries.  */
3074
0
        amt = LGOT_ALLOC_NELTS_FOR (symtab_hdr->sh_info) + 1;
3075
0
        amt *= sizeof (bfd_signed_vma);
3076
0
        local_got_refcounts = ((bfd_signed_vma *) bfd_zalloc (abfd, amt));
3077
0
        if (local_got_refcounts == NULL)
3078
0
    return false;
3079
3080
0
        local_got_refcounts++;
3081
0
        elf_local_got_refcounts (abfd) = local_got_refcounts;
3082
0
      }
3083
0
    break;
3084
3085
0
  default:
3086
0
    break;
3087
0
  }
3088
3089
      /* Warn and error for invalid input.  */
3090
0
      switch (r_type)
3091
0
  {
3092
0
  case R_CRIS_32_IE:
3093
0
  case R_CRIS_32_TPREL:
3094
0
  case R_CRIS_16_TPREL:
3095
0
  case R_CRIS_32_GD:
3096
0
    if (bfd_link_pic (info))
3097
0
      {
3098
0
        _bfd_error_handler
3099
    /* xgettext:c-format */
3100
0
    (_("%pB, section %pA:\n  relocation %s not valid"
3101
0
       " in a shared object;"
3102
0
       " typically an option mixup, recompile with -fPIC"),
3103
0
     abfd,
3104
0
     sec,
3105
0
     cris_elf_howto_table[r_type].name);
3106
        /* Don't return FALSE here; we want messages for all of
3107
     these and the error behavior is ungraceful
3108
     anyway.  */
3109
0
      }
3110
0
  default:
3111
0
    break;
3112
0
  }
3113
3114
0
      switch (r_type)
3115
0
  {
3116
0
  case R_CRIS_32_GD:
3117
0
  case R_CRIS_16_GOT_GD:
3118
0
  case R_CRIS_32_GOT_GD:
3119
    /* These are requests for tls_index entries, run-time R_CRIS_DTP.  */
3120
0
    got_element_size = 8;
3121
0
    r_symndx_lgot = LGOT_DTP_NDX (r_symndx);
3122
0
    break;
3123
3124
0
  case R_CRIS_16_DTPREL:
3125
0
  case R_CRIS_32_DTPREL:
3126
    /* These two just request for the constant-index
3127
       module-local tls_index-sized GOT entry, which we add
3128
       elsewhere.  */
3129
0
    break;
3130
3131
0
  case R_CRIS_32_IE:
3132
0
  case R_CRIS_32_GOT_TPREL:
3133
0
  case R_CRIS_16_GOT_TPREL:
3134
0
    r_symndx_lgot = LGOT_TPREL_NDX (r_symndx);
3135
3136
    /* Those relocs also require that a DSO is of type
3137
       Initial Exec.  Like other targets, we don't reset this
3138
       flag even if the relocs are GC:ed away.  */
3139
0
    if (bfd_link_pic (info))
3140
0
      info->flags |= DF_STATIC_TLS;
3141
0
    break;
3142
3143
    /* Let's list the other assembler-generated TLS-relocs too,
3144
       just to show that they're not forgotten. */
3145
0
  case R_CRIS_16_TPREL:
3146
0
  case R_CRIS_32_TPREL:
3147
0
  default:
3148
0
    break;
3149
0
  }
3150
3151
0
      switch (r_type)
3152
0
  {
3153
0
  case R_CRIS_16_GOTPLT:
3154
0
  case R_CRIS_32_GOTPLT:
3155
    /* Mark that we need a GOT entry if the PLT entry (and its GOT
3156
       entry) is eliminated.  We can only do this for a non-local
3157
       symbol.  */
3158
0
    if (h != NULL)
3159
0
      {
3160
0
        elf_cris_hash_entry (h)->gotplt_refcount++;
3161
0
        goto handle_gotplt_reloc;
3162
0
      }
3163
    /* If h is NULL then this is a local symbol, and we must make a
3164
       GOT entry for it, so handle it like a GOT reloc.  */
3165
    /* Fall through.  */
3166
3167
0
  case R_CRIS_32_IE:
3168
0
  case R_CRIS_32_GD:
3169
0
  case R_CRIS_16_GOT_GD:
3170
0
  case R_CRIS_32_GOT_GD:
3171
0
  case R_CRIS_32_GOT_TPREL:
3172
0
  case R_CRIS_16_GOT_TPREL:
3173
0
  case R_CRIS_16_GOT:
3174
0
  case R_CRIS_32_GOT:
3175
    /* This symbol requires a global offset table entry.  */
3176
0
    if (h != NULL)
3177
0
      {
3178
0
        if (h->got.refcount == 0)
3179
0
    {
3180
      /* Make sure this symbol is output as a dynamic symbol.  */
3181
0
      if (h->dynindx == -1)
3182
0
        {
3183
0
          if (!bfd_elf_link_record_dynamic_symbol (info, h))
3184
0
      return false;
3185
0
        }
3186
0
    }
3187
3188
        /* Update the sum of reloc counts for this symbol.  */
3189
0
        h->got.refcount++;
3190
3191
0
        switch (r_type)
3192
0
    {
3193
0
    case R_CRIS_16_GOT:
3194
0
    case R_CRIS_32_GOT:
3195
0
      if (elf_cris_hash_entry (h)->reg_got_refcount == 0)
3196
0
        {
3197
          /* Allocate space in the .got section.  */
3198
0
          sgot->size += got_element_size;
3199
          /* Allocate relocation space.  */
3200
0
          srelgot->size += sizeof (Elf32_External_Rela);
3201
0
        }
3202
0
      elf_cris_hash_entry (h)->reg_got_refcount++;
3203
0
      break;
3204
3205
0
    case R_CRIS_32_GD:
3206
0
    case R_CRIS_16_GOT_GD:
3207
0
    case R_CRIS_32_GOT_GD:
3208
0
      if (elf_cris_hash_entry (h)->dtp_refcount == 0)
3209
0
        {
3210
          /* Allocate space in the .got section.  */
3211
0
          sgot->size += got_element_size;
3212
          /* Allocate relocation space.  */
3213
0
          srelgot->size += sizeof (Elf32_External_Rela);
3214
0
        }
3215
0
      elf_cris_hash_entry (h)->dtp_refcount++;
3216
0
      break;
3217
3218
0
    case R_CRIS_32_IE:
3219
0
    case R_CRIS_32_GOT_TPREL:
3220
0
    case R_CRIS_16_GOT_TPREL:
3221
0
      if (elf_cris_hash_entry (h)->tprel_refcount == 0)
3222
0
        {
3223
          /* Allocate space in the .got section.  */
3224
0
          sgot->size += got_element_size;
3225
          /* Allocate relocation space.  */
3226
0
          srelgot->size += sizeof (Elf32_External_Rela);
3227
0
        }
3228
0
      elf_cris_hash_entry (h)->tprel_refcount++;
3229
0
      break;
3230
3231
0
    default:
3232
0
      BFD_FAIL ();
3233
0
      break;
3234
0
    }
3235
0
      }
3236
0
    else
3237
0
      {
3238
        /* This is a global offset table entry for a local symbol.  */
3239
0
        if (local_got_refcounts[r_symndx_lgot] == 0)
3240
0
    {
3241
0
      sgot->size += got_element_size;
3242
0
      if (bfd_link_pic (info))
3243
0
        {
3244
          /* If we are generating a shared object, we need
3245
       to output a R_CRIS_RELATIVE reloc so that the
3246
       dynamic linker can adjust this GOT entry.
3247
       Similarly for non-regular got entries.  */
3248
0
          srelgot->size += sizeof (Elf32_External_Rela);
3249
0
        }
3250
0
    }
3251
        /* Update the reloc-specific count.  */
3252
0
        local_got_refcounts[r_symndx_lgot]++;
3253
3254
        /* This one is the sum of all the others.  */
3255
0
        local_got_refcounts[r_symndx]++;
3256
0
      }
3257
0
    break;
3258
3259
0
  case R_CRIS_16_DTPREL:
3260
0
  case R_CRIS_32_DTPREL:
3261
0
  case R_CRIS_32_GOTREL:
3262
    /* This reference requires a global offset table.
3263
       FIXME: The actual refcount isn't used currently; the .got
3264
       section can't be removed if there were any references in the
3265
       input.  */
3266
0
    local_got_refcounts[-1]++;
3267
0
    break;
3268
3269
0
  handle_gotplt_reloc:
3270
3271
0
  case R_CRIS_32_PLT_GOTREL:
3272
    /* This reference requires a global offset table.  */
3273
0
    local_got_refcounts[-1]++;
3274
    /* Fall through.  */
3275
3276
0
  case R_CRIS_32_PLT_PCREL:
3277
    /* This symbol requires a procedure linkage table entry.  We
3278
       actually build the entry in adjust_dynamic_symbol,
3279
       because this might be a case of linking PIC code which is
3280
       never referenced by a dynamic object, in which case we
3281
       don't need to generate a procedure linkage table entry
3282
       after all.  */
3283
3284
    /* Beware: if we'd check for visibility of the symbol here
3285
       (and not marking the need for a PLT when non-visible), we'd
3286
       get into trouble with keeping handling consistent with
3287
       regards to relocs found before definition and GOTPLT
3288
       handling.  Eliminable PLT entries will be dealt with later
3289
       anyway.  */
3290
0
    if (h == NULL)
3291
0
      continue;
3292
3293
0
    h->needs_plt = 1;
3294
3295
    /* If the symbol is forced local, the refcount is unavailable.  */
3296
0
    if (h->plt.refcount != -1)
3297
0
      h->plt.refcount++;
3298
0
    break;
3299
3300
0
  case R_CRIS_8:
3301
0
  case R_CRIS_16:
3302
0
  case R_CRIS_32:
3303
    /* Let's help debug shared library creation.  Any of these
3304
       relocs *can* be used in shared libs, but pages containing
3305
       them cannot be shared, so they're not appropriate for
3306
       common use.  Don't warn for sections we don't care about,
3307
       such as debug sections or non-constant sections.  We
3308
       can't help tables of (global) function pointers, for
3309
       example, though they must be emitted in a (writable) data
3310
       section to avoid having impure text sections.  */
3311
0
    if (bfd_link_pic (info)
3312
0
        && (sec->flags & SEC_ALLOC) != 0
3313
0
        && (sec->flags & SEC_READONLY) != 0)
3314
0
      {
3315
        /* FIXME: How do we make this optionally a warning only?  */
3316
0
        _bfd_error_handler
3317
    /* xgettext:c-format */
3318
0
    (_("%pB, section %pA: relocation %s should not"
3319
0
       " be used in a shared object; recompile with -fPIC"),
3320
0
     abfd,
3321
0
     sec,
3322
0
     cris_elf_howto_table[r_type].name);
3323
0
      }
3324
3325
    /* We don't need to handle relocs into sections not going into
3326
       the "real" output.  */
3327
0
    if ((sec->flags & SEC_ALLOC) == 0)
3328
0
      break;
3329
3330
0
    if (h != NULL)
3331
0
      {
3332
0
        h->non_got_ref = 1;
3333
3334
        /* Make sure a plt entry is created for this symbol if it
3335
     turns out to be a function defined by a dynamic object.  */
3336
0
        if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3337
0
    h->plt.refcount++;
3338
0
      }
3339
3340
    /* If we are creating a shared library and this is not a local
3341
       symbol, we need to copy the reloc into the shared library.
3342
       However when linking with -Bsymbolic and this is a global
3343
       symbol which is defined in an object we are including in the
3344
       link (i.e., DEF_REGULAR is set), then we can resolve the
3345
       reloc directly.  At this point we have not seen all the input
3346
       files, so it is possible that DEF_REGULAR is not set now but
3347
       will be set later (it is never cleared).  In case of a weak
3348
       definition, DEF_REGULAR may be cleared later by a strong
3349
       definition in a shared library.  We account for that
3350
       possibility below by storing information in the relocs_copied
3351
       field of the hash table entry.  A similar situation occurs
3352
       when creating shared libraries and symbol visibility changes
3353
       render the symbol local.  */
3354
3355
    /* No need to do anything if we're not creating a shared object.  */
3356
0
    if (! bfd_link_pic (info)
3357
0
        || (h != NULL && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h)))
3358
0
      break;
3359
3360
    /* We may need to create a reloc section in the dynobj and made room
3361
       for this reloc.  */
3362
0
    if (sreloc == NULL)
3363
0
      {
3364
0
        sreloc = _bfd_elf_make_dynamic_reloc_section
3365
0
    (sec, dynobj, 2, abfd, /*rela?*/ true);
3366
3367
0
        if (sreloc == NULL)
3368
0
    return false;
3369
0
      }
3370
3371
0
    if (sec->flags & SEC_READONLY)
3372
0
      info->flags |= DF_TEXTREL;
3373
3374
0
    sreloc->size += sizeof (Elf32_External_Rela);
3375
0
    break;
3376
3377
0
  case R_CRIS_8_PCREL:
3378
0
  case R_CRIS_16_PCREL:
3379
0
  case R_CRIS_32_PCREL:
3380
0
    if (h != NULL)
3381
0
      {
3382
0
        h->non_got_ref = 1;
3383
3384
        /* Make sure a plt entry is created for this symbol if it
3385
     turns out to be a function defined by a dynamic object.  */
3386
0
        if (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3387
0
    h->plt.refcount++;
3388
0
      }
3389
3390
    /* If we are creating a shared library and this is not a local
3391
       symbol, we need to copy the reloc into the shared library.
3392
       However when linking with -Bsymbolic and this is a global
3393
       symbol which is defined in an object we are including in the
3394
       link (i.e., DEF_REGULAR is set), then we can resolve the
3395
       reloc directly.  At this point we have not seen all the input
3396
       files, so it is possible that DEF_REGULAR is not set now but
3397
       will be set later (it is never cleared).  In case of a weak
3398
       definition, DEF_REGULAR may be cleared later by a strong
3399
       definition in a shared library.  We account for that
3400
       possibility below by storing information in the relocs_copied
3401
       field of the hash table entry.  A similar situation occurs
3402
       when creating shared libraries and symbol visibility changes
3403
       render the symbol local.  */
3404
3405
    /* No need to do anything if we're not creating a shared object.  */
3406
0
    if (! bfd_link_pic (info))
3407
0
      break;
3408
3409
    /* We don't need to handle relocs into sections not going into
3410
       the "real" output.  */
3411
0
    if ((sec->flags & SEC_ALLOC) == 0)
3412
0
      break;
3413
3414
    /* If the symbol is local, then we know already we can
3415
       eliminate the reloc.  */
3416
0
    if (h == NULL || ELF_ST_VISIBILITY (h->other) != STV_DEFAULT)
3417
0
      break;
3418
3419
    /* If this is with -Bsymbolic and the symbol isn't weak, and
3420
       is defined by an ordinary object (the ones we include in
3421
       this shared library) then we can also eliminate the
3422
       reloc.  See comment above for more eliminable cases which
3423
       we can't identify at this time.  */
3424
0
    if (SYMBOLIC_BIND (info, h)
3425
0
        && h->root.type != bfd_link_hash_defweak
3426
0
        && h->def_regular)
3427
0
      break;
3428
3429
    /* We may need to create a reloc section in the dynobj and made room
3430
       for this reloc.  */
3431
0
    if (sreloc == NULL)
3432
0
      {
3433
0
        sreloc = _bfd_elf_make_dynamic_reloc_section
3434
0
    (sec, dynobj, 2, abfd, /*rela?*/ true);
3435
3436
0
        if (sreloc == NULL)
3437
0
    return false;
3438
0
      }
3439
3440
0
    sreloc->size += sizeof (Elf32_External_Rela);
3441
3442
    /* We count the number of PC relative relocations we have
3443
       entered for this symbol, so that we can discard them
3444
       again if the symbol is later defined by a regular object.
3445
       We know that h is really a pointer to an
3446
       elf_cris_link_hash_entry.  */
3447
0
    {
3448
0
      struct elf_cris_link_hash_entry *eh;
3449
0
      struct elf_cris_pcrel_relocs_copied *p;
3450
3451
0
      eh = elf_cris_hash_entry (h);
3452
3453
0
      for (p = eh->pcrel_relocs_copied; p != NULL; p = p->next)
3454
0
        if (p->section == sec)
3455
0
    break;
3456
3457
0
      if (p == NULL)
3458
0
        {
3459
0
    p = ((struct elf_cris_pcrel_relocs_copied *)
3460
0
         bfd_alloc (dynobj, (bfd_size_type) sizeof *p));
3461
0
    if (p == NULL)
3462
0
      return false;
3463
0
    p->next = eh->pcrel_relocs_copied;
3464
0
    eh->pcrel_relocs_copied = p;
3465
0
    p->section = sec;
3466
0
    p->count = 0;
3467
0
    p->r_type = r_type;
3468
0
        }
3469
3470
0
      ++p->count;
3471
0
    }
3472
0
    break;
3473
3474
  /* This relocation describes the C++ object vtable hierarchy.
3475
     Reconstruct it for later use during GC.  */
3476
0
  case R_CRIS_GNU_VTINHERIT:
3477
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3478
0
      return false;
3479
0
    break;
3480
3481
  /* This relocation describes which C++ vtable entries are actually
3482
     used.  Record for later use during GC.  */
3483
0
  case R_CRIS_GNU_VTENTRY:
3484
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3485
0
      return false;
3486
0
    break;
3487
3488
0
  case R_CRIS_16_TPREL:
3489
0
  case R_CRIS_32_TPREL:
3490
    /* Already warned above, when necessary.  */
3491
0
    break;
3492
3493
0
  default:
3494
    /* Other relocs do not appear here.  */
3495
0
    bfd_set_error (bfd_error_bad_value);
3496
0
    return false;
3497
0
  }
3498
0
    }
3499
3500
0
  return true;
3501
0
}
3502
3503
/* Set the sizes of the dynamic sections.  */
3504
3505
static bool
3506
elf_cris_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
3507
           struct bfd_link_info *info)
3508
0
{
3509
0
  struct elf_cris_link_hash_table * htab;
3510
0
  bfd *dynobj;
3511
0
  asection *s;
3512
0
  bool relocs;
3513
3514
0
  htab = elf_cris_hash_table (info);
3515
0
  if (htab == NULL)
3516
0
    return false;
3517
3518
0
  dynobj = htab->root.dynobj;
3519
0
  if (dynobj == NULL)
3520
0
    return true;
3521
3522
0
  if (htab->root.dynamic_sections_created)
3523
0
    {
3524
      /* Set the contents of the .interp section to the interpreter.  */
3525
0
      if (bfd_link_executable (info) && !info->nointerp)
3526
0
  {
3527
0
    s = htab->root.interp;
3528
0
    BFD_ASSERT (s != NULL);
3529
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
3530
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
3531
0
    s->alloced = 1;
3532
0
  }
3533
0
    }
3534
0
  else
3535
0
    {
3536
      /* Adjust all expected GOTPLT uses to use a GOT entry instead.  */
3537
0
      elf_cris_link_hash_traverse (htab, elf_cris_adjust_gotplt_to_got,
3538
0
           info);
3539
3540
      /* We may have created entries in the .rela.got section.
3541
   However, if we are not creating the dynamic sections, we will
3542
   not actually use these entries.  Reset the size of .rela.got,
3543
   which will cause it to get stripped from the output file
3544
   below.  */
3545
0
      s = htab->root.srelgot;
3546
0
      if (s != NULL)
3547
0
  s->size = 0;
3548
0
    }
3549
3550
  /* If this is a -Bsymbolic shared link, then we need to discard all PC
3551
     relative relocs against symbols defined in a regular object.  We
3552
     allocated space for them in the check_relocs routine, but we will not
3553
     fill them in in the relocate_section routine.  We also discard space
3554
     for relocs that have become for local symbols due to symbol
3555
     visibility changes.  For programs, we discard space for relocs for
3556
     symbols not referenced by any dynamic object.  */
3557
0
  if (bfd_link_pic (info))
3558
0
    elf_cris_link_hash_traverse (htab,
3559
0
         elf_cris_discard_excess_dso_dynamics,
3560
0
         info);
3561
0
  else
3562
0
    elf_cris_link_hash_traverse (htab,
3563
0
         elf_cris_discard_excess_program_dynamics,
3564
0
         info);
3565
3566
  /* The check_relocs and adjust_dynamic_symbol entry points have
3567
     determined the sizes of the various dynamic sections.  Allocate
3568
     memory for them.  */
3569
0
  relocs = false;
3570
0
  for (s = dynobj->sections; s != NULL; s = s->next)
3571
0
    {
3572
0
      const char *name;
3573
3574
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
3575
0
  continue;
3576
3577
      /* It's OK to base decisions on the section name, because none
3578
   of the dynobj section names depend upon the input files.  */
3579
0
      name = bfd_section_name (s);
3580
3581
0
      if (strcmp (name, ".plt") == 0)
3582
0
  {
3583
0
    ;
3584
0
  }
3585
0
      else if (strcmp (name, ".got.plt") == 0)
3586
0
  {
3587
    /* The .got.plt contains the .got header as well as the
3588
       actual .got.plt contents.  The .got header may contain a
3589
       R_CRIS_DTPMOD entry at index 3.  */
3590
0
    s->size += htab->dtpmod_refcount != 0
3591
0
      ? 8 : 0;
3592
0
  }
3593
0
      else if (startswith (name, ".rela"))
3594
0
  {
3595
0
    if (strcmp (name, ".rela.got") == 0
3596
0
        && htab->dtpmod_refcount != 0
3597
0
        && bfd_link_pic (info))
3598
0
      s->size += sizeof (Elf32_External_Rela);
3599
3600
0
    if (s->size != 0)
3601
0
      {
3602
        /* Remember whether there are any reloc sections other
3603
     than .rela.plt.  */
3604
0
        if (strcmp (name, ".rela.plt") != 0)
3605
0
      relocs = true;
3606
3607
        /* We use the reloc_count field as a counter if we need
3608
     to copy relocs into the output file.  */
3609
0
        s->reloc_count = 0;
3610
0
      }
3611
0
  }
3612
0
      else if (! startswith (name, ".got")
3613
0
         && strcmp (name, ".dynbss") != 0
3614
0
         && s != htab->root.sdynrelro)
3615
0
  {
3616
    /* It's not one of our sections, so don't allocate space.  */
3617
0
    continue;
3618
0
  }
3619
3620
0
      if (s->size == 0)
3621
0
  {
3622
    /* If we don't need this section, strip it from the
3623
       output file.  This is mostly to handle .rela.bss and
3624
       .rela.plt.  We must create both sections in
3625
       create_dynamic_sections, because they must be created
3626
       before the linker maps input sections to output
3627
       sections.  The linker does that before
3628
       adjust_dynamic_symbol is called, and it is that
3629
       function which decides whether anything needs to go
3630
       into these sections.  */
3631
0
    s->flags |= SEC_EXCLUDE;
3632
0
    continue;
3633
0
  }
3634
3635
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
3636
0
  continue;
3637
3638
      /* Allocate memory for the section contents. We use bfd_zalloc here
3639
   in case unused entries are not reclaimed before the section's
3640
   contents are written out.  This should not happen, but this way
3641
   if it does, we will not write out garbage.  For reloc sections,
3642
   this will make entries have the type R_CRIS_NONE.  */
3643
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3644
0
      if (s->contents == NULL)
3645
0
  return false;
3646
0
      s->alloced = 1;
3647
0
    }
3648
3649
0
  return _bfd_elf_add_dynamic_tags (output_bfd, info, relocs);
3650
0
}
3651
3652
/* This function is called via elf_cris_link_hash_traverse if we are
3653
   creating a shared object.  In the -Bsymbolic case, it discards the
3654
   space allocated to copy PC relative relocs against symbols which
3655
   are defined in regular objects.  For the normal non-symbolic case,
3656
   we also discard space for relocs that have become local due to
3657
   symbol visibility changes.  We allocated space for them in the
3658
   check_relocs routine, but we won't fill them in in the
3659
   relocate_section routine.  */
3660
3661
static bool
3662
elf_cris_discard_excess_dso_dynamics (struct elf_cris_link_hash_entry *h,
3663
              void * inf)
3664
0
{
3665
0
  struct elf_cris_pcrel_relocs_copied *s;
3666
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
3667
3668
  /* If a symbol has been forced local or we have found a regular
3669
     definition for the symbolic link case, then we won't be needing
3670
     any relocs.  */
3671
0
  if (h->root.def_regular
3672
0
      && (h->root.forced_local
3673
0
    || SYMBOLIC_BIND (info, &h->root)))
3674
0
    {
3675
0
      for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
3676
0
  {
3677
0
    asection *sreloc = elf_section_data (s->section)->sreloc;
3678
0
    sreloc->size -= s->count * sizeof (Elf32_External_Rela);
3679
0
  }
3680
0
      return true;
3681
0
    }
3682
3683
  /* If we have accounted for PC-relative relocs for read-only
3684
     sections, now is the time to warn for them.  We can't do it in
3685
     cris_elf_check_relocs, because we don't know the status of all
3686
     symbols at that time (and it's common to force symbols local
3687
     late).  */
3688
3689
0
  for (s = h->pcrel_relocs_copied; s != NULL; s = s->next)
3690
0
    if ((s->section->flags & SEC_READONLY) != 0)
3691
0
      {
3692
  /* FIXME: How do we make this optionally a warning only?  */
3693
0
  _bfd_error_handler
3694
    /* xgettext:c-format */
3695
0
    (_("%pB, section `%pA', to symbol `%s':"
3696
0
       " relocation %s should not be used"
3697
0
       " in a shared object; recompile with -fPIC"),
3698
0
     s->section->owner,
3699
0
     s->section,
3700
0
     h->root.root.root.string,
3701
0
     cris_elf_howto_table[s->r_type].name);
3702
3703
0
  info->flags |= DF_TEXTREL;
3704
0
      }
3705
3706
0
  return true;
3707
0
}
3708
3709
/* This function is called via elf_cris_link_hash_traverse if we are *not*
3710
   creating a shared object.  We discard space for relocs for symbols put
3711
   in the .got, but which we found we do not have to resolve at run-time.  */
3712
3713
static bool
3714
elf_cris_discard_excess_program_dynamics (struct elf_cris_link_hash_entry *h,
3715
            void * inf)
3716
0
{
3717
0
  struct bfd_link_info *info = (struct bfd_link_info *) inf;
3718
3719
  /* If we're not creating a shared library and have a symbol which is
3720
     referred to by .got references, but the symbol is defined locally,
3721
     (or rather, not defined by a DSO) then lose the reloc for the .got
3722
     (don't allocate room for it).  Likewise for relocs for something
3723
     for which we create a PLT.  */
3724
0
  if (!h->root.def_dynamic
3725
0
      || h->root.plt.refcount > 0)
3726
0
    {
3727
0
      if (h->reg_got_refcount > 0
3728
    /* The size of this section is only valid and in sync with the
3729
       various reference counts if we do dynamic; don't decrement it
3730
       otherwise.  */
3731
0
    && elf_hash_table (info)->dynamic_sections_created)
3732
0
  {
3733
0
    bfd *dynobj = elf_hash_table (info)->dynobj;
3734
0
    asection *srelgot = elf_hash_table (info)->srelgot;
3735
3736
0
    BFD_ASSERT (dynobj != NULL);
3737
0
    BFD_ASSERT (srelgot != NULL);
3738
3739
0
    srelgot->size -= sizeof (Elf32_External_Rela);
3740
0
  }
3741
3742
      /* If the locally-defined symbol isn't used by a DSO, then we don't
3743
   have to export it as a dynamic symbol.  This was already done for
3744
   functions; doing this for all symbols would presumably not
3745
   introduce new problems.  Of course we don't do this if we're
3746
   exporting all dynamic symbols, or all data symbols, regardless of
3747
   them being referenced or not.  */
3748
0
      if (! (info->export_dynamic
3749
0
       || (h->root.type != STT_FUNC && info->dynamic_data))
3750
0
    && h->root.dynindx != -1
3751
0
    && !h->root.dynamic
3752
0
    && !h->root.def_dynamic
3753
0
    && !h->root.ref_dynamic)
3754
0
  {
3755
0
    h->root.dynindx = -1;
3756
0
    _bfd_elf_strtab_delref (elf_hash_table (info)->dynstr,
3757
0
          h->root.dynstr_index);
3758
0
  }
3759
0
    }
3760
3761
0
  return true;
3762
0
}
3763
3764
/* Reject a file depending on presence and expectation of prefixed
3765
   underscores on symbols.  */
3766
3767
static bool
3768
cris_elf_object_p (bfd *abfd)
3769
473
{
3770
473
  if (! cris_elf_set_mach_from_flags (abfd, elf_elfheader (abfd)->e_flags))
3771
8
    return false;
3772
3773
465
  if ((elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE))
3774
173
    return (bfd_get_symbol_leading_char (abfd) == '_');
3775
292
  else
3776
292
    return (bfd_get_symbol_leading_char (abfd) == 0);
3777
465
}
3778
3779
/* Mark presence or absence of leading underscore.  Set machine type
3780
   flags from mach type.  */
3781
3782
static bool
3783
cris_elf_final_write_processing (bfd *abfd)
3784
0
{
3785
0
  unsigned long e_flags = elf_elfheader (abfd)->e_flags;
3786
3787
0
  e_flags &= ~EF_CRIS_UNDERSCORE;
3788
0
  if (bfd_get_symbol_leading_char (abfd) == '_')
3789
0
    e_flags |= EF_CRIS_UNDERSCORE;
3790
3791
0
  switch (bfd_get_mach (abfd))
3792
0
    {
3793
0
    case bfd_mach_cris_v0_v10:
3794
0
      e_flags |= EF_CRIS_VARIANT_ANY_V0_V10;
3795
0
      break;
3796
3797
0
    case bfd_mach_cris_v10_v32:
3798
0
      e_flags |= EF_CRIS_VARIANT_COMMON_V10_V32;
3799
0
      break;
3800
3801
0
    case bfd_mach_cris_v32:
3802
0
      e_flags |= EF_CRIS_VARIANT_V32;
3803
0
      break;
3804
3805
0
    default:
3806
0
      _bfd_abort (__FILE__, __LINE__,
3807
0
      _("unexpected machine number"));
3808
0
    }
3809
3810
0
  elf_elfheader (abfd)->e_flags = e_flags;
3811
0
  return _bfd_elf_final_write_processing (abfd);
3812
0
}
3813
3814
/* Set the mach type from e_flags value.  */
3815
3816
static bool
3817
cris_elf_set_mach_from_flags (bfd *abfd,
3818
            unsigned long flags)
3819
473
{
3820
473
  switch (flags & EF_CRIS_VARIANT_MASK)
3821
473
    {
3822
311
    case EF_CRIS_VARIANT_ANY_V0_V10:
3823
311
      bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v0_v10);
3824
311
      break;
3825
3826
146
    case EF_CRIS_VARIANT_V32:
3827
146
      bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v32);
3828
146
      break;
3829
3830
8
    case EF_CRIS_VARIANT_COMMON_V10_V32:
3831
8
      bfd_default_set_arch_mach (abfd, bfd_arch_cris, bfd_mach_cris_v10_v32);
3832
8
      break;
3833
3834
8
    default:
3835
      /* Since we don't recognize them, we obviously can't support them
3836
   with this code; we'd have to require that all future handling
3837
   would be optional.  */
3838
8
      bfd_set_error (bfd_error_wrong_format);
3839
8
      return false;
3840
473
    }
3841
3842
465
  return true;
3843
473
}
3844
3845
/* Display the flags field.  */
3846
3847
static bool
3848
cris_elf_print_private_bfd_data (bfd *abfd, void * ptr)
3849
191
{
3850
191
  FILE *file = (FILE *) ptr;
3851
3852
191
  BFD_ASSERT (abfd != NULL && ptr != NULL);
3853
3854
191
  _bfd_elf_print_private_bfd_data (abfd, ptr);
3855
3856
191
  fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
3857
3858
191
  if (elf_elfheader (abfd)->e_flags & EF_CRIS_UNDERSCORE)
3859
77
    fprintf (file, _(" [symbols have a _ prefix]"));
3860
191
  if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
3861
191
      == EF_CRIS_VARIANT_COMMON_V10_V32)
3862
0
    fprintf (file, _(" [v10 and v32]"));
3863
191
  if ((elf_elfheader (abfd)->e_flags & EF_CRIS_VARIANT_MASK)
3864
191
      == EF_CRIS_VARIANT_V32)
3865
69
    fprintf (file, _(" [v32]"));
3866
3867
191
  fputc ('\n', file);
3868
191
  return true;
3869
191
}
3870
3871
/* Don't mix files with and without a leading underscore.  */
3872
3873
static bool
3874
cris_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
3875
0
{
3876
0
  bfd *obfd = info->output_bfd;
3877
0
  int imach, omach;
3878
3879
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
3880
0
    return false;
3881
3882
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3883
0
    return true;
3884
3885
0
  imach = bfd_get_mach (ibfd);
3886
3887
0
  if (! elf_flags_init (obfd))
3888
0
    {
3889
      /* This happens when ld starts out with a 'blank' output file.  */
3890
0
      elf_flags_init (obfd) = true;
3891
3892
      /* We ignore the linker-set mach, and instead set it according to
3893
   the first input file.  This would also happen if we could
3894
   somehow filter out the OUTPUT_ARCH () setting from elf.sc.
3895
   This allows us to keep the same linker config across
3896
   cris(v0..v10) and crisv32.  The drawback is that we can't force
3897
   the output type, which might be a sane thing to do for a
3898
   v10+v32 compatibility object.  */
3899
0
      if (! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
3900
0
  return false;
3901
0
    }
3902
3903
0
  if (bfd_get_symbol_leading_char (ibfd)
3904
0
      != bfd_get_symbol_leading_char (obfd))
3905
0
    {
3906
0
      _bfd_error_handler
3907
0
  (bfd_get_symbol_leading_char (ibfd) == '_'
3908
0
   ? _("%pB: uses _-prefixed symbols, but writing file with non-prefixed symbols")
3909
0
   : _("%pB: uses non-prefixed symbols, but writing file with _-prefixed symbols"),
3910
0
   ibfd);
3911
0
      bfd_set_error (bfd_error_bad_value);
3912
0
      return false;
3913
0
    }
3914
3915
0
  omach = bfd_get_mach (obfd);
3916
3917
0
  if (imach != omach)
3918
0
    {
3919
      /* We can get an incompatible combination only if either is
3920
   bfd_mach_cris_v32, and the other one isn't compatible.  */
3921
0
      if ((imach == bfd_mach_cris_v32
3922
0
     && omach != bfd_mach_cris_v10_v32)
3923
0
    || (omach == bfd_mach_cris_v32
3924
0
        && imach != bfd_mach_cris_v10_v32))
3925
0
  {
3926
0
    _bfd_error_handler
3927
0
      ((imach == bfd_mach_cris_v32)
3928
0
       ? _("%pB contains CRIS v32 code, incompatible"
3929
0
     " with previous objects")
3930
0
       : _("%pB contains non-CRIS-v32 code, incompatible"
3931
0
     " with previous objects"),
3932
0
       ibfd);
3933
0
    bfd_set_error (bfd_error_bad_value);
3934
0
    return false;
3935
0
  }
3936
3937
      /* We don't have to check the case where the input is compatible
3938
   with v10 and v32, because the output is already known to be set
3939
   to the other (compatible) mach.  */
3940
0
      if (omach == bfd_mach_cris_v10_v32
3941
0
    && ! bfd_set_arch_mach (obfd, bfd_arch_cris, imach))
3942
0
  return false;
3943
0
    }
3944
3945
0
  return true;
3946
0
}
3947
3948
/* Do side-effects of e_flags copying to obfd.  */
3949
3950
static bool
3951
cris_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
3952
0
{
3953
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
3954
0
    return true;
3955
3956
  /* Call the base function.  */
3957
0
  if (!_bfd_elf_copy_private_bfd_data (ibfd, obfd))
3958
0
    return false;
3959
3960
  /* Do what we really came here for.  */
3961
0
  return bfd_set_arch_mach (obfd, bfd_arch_cris, bfd_get_mach (ibfd));
3962
0
}
3963
3964
static enum elf_reloc_type_class
3965
elf_cris_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3966
         const asection *rel_sec ATTRIBUTE_UNUSED,
3967
         const Elf_Internal_Rela *rela)
3968
0
{
3969
0
  enum elf_cris_reloc_type r_type = ELF32_R_TYPE (rela->r_info);
3970
0
  switch (r_type)
3971
0
    {
3972
0
    case R_CRIS_RELATIVE:
3973
0
      return reloc_class_relative;
3974
0
    case R_CRIS_JUMP_SLOT:
3975
0
      return reloc_class_plt;
3976
0
    case R_CRIS_COPY:
3977
0
      return reloc_class_copy;
3978
0
    default:
3979
0
      return reloc_class_normal;
3980
0
    }
3981
0
}
3982
3983
/* The elf_backend_got_elt_size worker.  For one symbol, we can have up to
3984
   two GOT entries from three types with two different sizes.  We handle
3985
   it as a single entry, so we can use the regular offset-calculation
3986
   machinery.  */
3987
3988
static bfd_vma
3989
elf_cris_got_elt_size (bfd *abfd ATTRIBUTE_UNUSED,
3990
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
3991
           struct elf_link_hash_entry *hr,
3992
           bfd *ibfd,
3993
           unsigned long symndx)
3994
0
{
3995
0
  struct elf_link_hash_entry *h = (struct elf_link_hash_entry *) hr;
3996
0
  bfd_vma eltsiz = 0;
3997
3998
  /* We may have one regular GOT entry or up to two TLS GOT
3999
     entries.  */
4000
0
  if (h == NULL)
4001
0
    {
4002
0
      Elf_Internal_Shdr *symtab_hdr = &elf_symtab_hdr (ibfd);
4003
0
      bfd_signed_vma *local_got_refcounts = elf_local_got_refcounts (ibfd);
4004
4005
0
      BFD_ASSERT (local_got_refcounts != NULL);
4006
4007
0
      if (local_got_refcounts[LGOT_REG_NDX (symndx)] > 0)
4008
0
  {
4009
    /* We can't have a variable referred to both as a regular
4010
       variable and through TLS relocs.  */
4011
0
    BFD_ASSERT (local_got_refcounts[LGOT_DTP_NDX (symndx)] == 0
4012
0
          && local_got_refcounts[LGOT_TPREL_NDX (symndx)] == 0);
4013
0
    return 4;
4014
0
  }
4015
4016
0
      if (local_got_refcounts[LGOT_DTP_NDX (symndx)] > 0)
4017
0
  eltsiz += 8;
4018
4019
0
      if (local_got_refcounts[LGOT_TPREL_NDX (symndx)] > 0)
4020
0
  eltsiz += 4;
4021
0
    }
4022
0
  else
4023
0
    {
4024
0
      struct elf_cris_link_hash_entry *hh = elf_cris_hash_entry (h);
4025
0
      if (hh->reg_got_refcount > 0)
4026
0
  {
4027
    /* The actual error-on-input is emitted elsewhere.  */
4028
0
    BFD_ASSERT (hh->dtp_refcount == 0 && hh->tprel_refcount == 0);
4029
0
    return 4;
4030
0
  }
4031
4032
0
      if (hh->dtp_refcount > 0)
4033
0
  eltsiz += 8;
4034
4035
0
      if (hh->tprel_refcount > 0)
4036
0
  eltsiz += 4;
4037
0
    }
4038
4039
  /* We're only called when h->got.refcount is non-zero, so we must
4040
     have a non-zero size.  */
4041
0
  BFD_ASSERT (eltsiz != 0);
4042
0
  return eltsiz;
4043
0
}
4044

4045
#define ELF_ARCH    bfd_arch_cris
4046
#define ELF_TARGET_ID   CRIS_ELF_DATA
4047
#define ELF_MACHINE_CODE  EM_CRIS
4048
#define ELF_MAXPAGESIZE   0x2000
4049
4050
#define TARGET_LITTLE_SYM cris_elf32_vec
4051
#define TARGET_LITTLE_NAME  "elf32-cris"
4052
#define elf_symbol_leading_char 0
4053
4054
#define elf_info_to_howto_rel     NULL
4055
#define elf_info_to_howto     cris_info_to_howto_rela
4056
#define elf_backend_relocate_section    cris_elf_relocate_section
4057
#define elf_backend_gc_mark_hook    cris_elf_gc_mark_hook
4058
#define elf_backend_plt_sym_val     cris_elf_plt_sym_val
4059
#define elf_backend_check_relocs    cris_elf_check_relocs
4060
#define elf_backend_grok_prstatus   cris_elf_grok_prstatus
4061
#define elf_backend_grok_psinfo     cris_elf_grok_psinfo
4062
4063
#define elf_backend_can_gc_sections   1
4064
#define elf_backend_can_refcount    1
4065
4066
#define elf_backend_object_p      cris_elf_object_p
4067
#define elf_backend_final_write_processing \
4068
  cris_elf_final_write_processing
4069
#define bfd_elf32_bfd_print_private_bfd_data \
4070
  cris_elf_print_private_bfd_data
4071
#define bfd_elf32_bfd_merge_private_bfd_data \
4072
  cris_elf_merge_private_bfd_data
4073
#define bfd_elf32_bfd_copy_private_bfd_data \
4074
  cris_elf_copy_private_bfd_data
4075
4076
#define bfd_elf32_bfd_reloc_type_lookup   cris_reloc_type_lookup
4077
#define bfd_elf32_bfd_reloc_name_lookup cris_reloc_name_lookup
4078
4079
#define bfd_elf32_bfd_link_hash_table_create \
4080
  elf_cris_link_hash_table_create
4081
#define elf_backend_adjust_dynamic_symbol \
4082
  elf_cris_adjust_dynamic_symbol
4083
#define elf_backend_copy_indirect_symbol \
4084
  elf_cris_copy_indirect_symbol
4085
#define elf_backend_late_size_sections \
4086
  elf_cris_late_size_sections
4087
#define elf_backend_init_index_section    _bfd_elf_init_1_index_section
4088
#define elf_backend_finish_dynamic_symbol \
4089
  elf_cris_finish_dynamic_symbol
4090
#define elf_backend_finish_dynamic_sections \
4091
  elf_cris_finish_dynamic_sections
4092
#define elf_backend_create_dynamic_sections \
4093
  _bfd_elf_create_dynamic_sections
4094
#define bfd_elf32_bfd_final_link    _bfd_elf_gc_common_final_link
4095
#define elf_backend_hide_symbol     elf_cris_hide_symbol
4096
#define elf_backend_reloc_type_class    elf_cris_reloc_type_class
4097
4098
#define elf_backend_want_got_plt  1
4099
#define elf_backend_plt_readonly  1
4100
#define elf_backend_want_plt_sym  0
4101
#define elf_backend_got_header_size 12
4102
#define elf_backend_got_elt_size elf_cris_got_elt_size
4103
#define elf_backend_dtrel_excludes_plt  1
4104
#define elf_backend_want_dynrelro 1
4105
4106
#define elf_backend_default_execstack 0
4107
4108
/* Later, we my want to optimize RELA entries into REL entries for dynamic
4109
   linking and libraries (if it's a win of any significance).  Until then,
4110
   take the easy route.  */
4111
#define elf_backend_may_use_rel_p 0
4112
#define elf_backend_may_use_rela_p 1
4113
#define elf_backend_rela_normal   1
4114
4115
#define elf_backend_linux_prpsinfo32_ugid16 true
4116
4117
#include "elf32-target.h"
4118
4119
#undef TARGET_LITTLE_SYM
4120
#undef TARGET_LITTLE_NAME
4121
#undef elf_symbol_leading_char
4122
4123
#define TARGET_LITTLE_SYM cris_elf32_us_vec
4124
#define TARGET_LITTLE_NAME "elf32-us-cris"
4125
#define elf_symbol_leading_char '_'
4126
#undef elf32_bed
4127
#define elf32_bed elf32_us_cris_bed
4128
4129
#include "elf32-target.h"