Coverage Report

Created: 2026-04-04 08:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-sh.c
Line
Count
Source
1
/* Renesas / SuperH SH specific support for 32-bit ELF
2
   Copyright (C) 1996-2026 Free Software Foundation, Inc.
3
   Contributed by Ian Lance Taylor, Cygnus Support.
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include "bfd.h"
24
#include "bfdlink.h"
25
#include "libbfd.h"
26
#include "elf-bfd.h"
27
#include "elf-vxworks.h"
28
#include "elf/sh.h"
29
#include "dwarf2.h"
30
#include "libiberty.h"
31
#include "../opcodes/sh-opc.h"
32
33
/* All users of this file have bfd_octets_per_byte (abfd, sec) == 1.  */
34
1
#define OCTETS_PER_BYTE(ABFD, SEC) 1
35
36
static bfd_reloc_status_type sh_elf_reloc
37
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
38
static bfd_reloc_status_type sh_elf_ignore_reloc
39
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
40
static bool sh_elf_relax_delete_bytes
41
  (bfd *, asection *, bfd_vma, int);
42
static bool sh_elf_align_loads
43
  (bfd *, asection *, Elf_Internal_Rela *, bfd_byte *, bool *);
44
static bool sh_elf_swap_insns
45
  (bfd *, asection *, void *, bfd_byte *, bfd_vma);
46
static int sh_elf_optimized_tls_reloc
47
  (struct bfd_link_info *, int, int);
48
static bfd_vma dtpoff_base
49
  (struct bfd_link_info *);
50
static bfd_vma tpoff
51
  (struct bfd_link_info *, bfd_vma);
52
53
/* The name of the dynamic interpreter.  This is put in the .interp
54
   section.  */
55
56
0
#define ELF_DYNAMIC_INTERPRETER "/usr/lib/libc.so.1"
57
58
/* FDPIC binaries have a default 128K stack.  */
59
0
#define DEFAULT_STACK_SIZE 0x20000
60
61
0
#define MINUS_ONE ((bfd_vma) 0 - 1)
62
63
/* Decide whether a reference to a symbol can be resolved locally or
64
   not.  If the symbol is protected, we want the local address, but
65
   its function descriptor must be assigned by the dynamic linker.  */
66
#define SYMBOL_FUNCDESC_LOCAL(INFO, H) \
67
0
  (SYMBOL_REFERENCES_LOCAL (INFO, H) \
68
0
   || ! elf_hash_table (INFO)->dynamic_sections_created)
69

70
#define SH_PARTIAL32 true
71
#define SH_SRC_MASK32 0xffffffff
72
#define SH_ELF_RELOC sh_elf_reloc
73
static reloc_howto_type sh_elf_howto_table[] =
74
{
75
#include "elf32-sh-relocs.h"
76
};
77
78
#define SH_PARTIAL32 false
79
#define SH_SRC_MASK32 0
80
#define SH_ELF_RELOC bfd_elf_generic_reloc
81
static reloc_howto_type sh_vxworks_howto_table[] =
82
{
83
#include "elf32-sh-relocs.h"
84
};
85

86
/* Return true if OUTPUT_BFD is a VxWorks object.  */
87
88
static bool
89
vxworks_object_p (bfd *abfd ATTRIBUTE_UNUSED)
90
486
{
91
486
#if defined (OBJ_MAYBE_ELF_VXWORKS) && !defined (SH_TARGET_ALREADY_DEFINED)
92
486
  extern const bfd_target sh_elf32_vxworks_le_vec;
93
486
  extern const bfd_target sh_elf32_vxworks_vec;
94
95
486
  return (abfd->xvec == &sh_elf32_vxworks_le_vec
96
486
    || abfd->xvec == &sh_elf32_vxworks_vec);
97
#else
98
  return false;
99
#endif
100
486
}
101
102
/* Return true if OUTPUT_BFD is an FDPIC object.  */
103
104
static bool
105
fdpic_object_p (bfd *abfd ATTRIBUTE_UNUSED)
106
1.22k
{
107
1.22k
#if !defined SH_TARGET_ALREADY_DEFINED
108
1.22k
  extern const bfd_target sh_elf32_fdpic_le_vec;
109
1.22k
  extern const bfd_target sh_elf32_fdpic_be_vec;
110
111
1.22k
  return (abfd->xvec == &sh_elf32_fdpic_le_vec
112
1.14k
    || abfd->xvec == &sh_elf32_fdpic_be_vec);
113
#else
114
  return false;
115
#endif
116
1.22k
}
117
118
/* Return the howto table for ABFD.  */
119
120
static reloc_howto_type *
121
get_howto_table (bfd *abfd)
122
486
{
123
486
  if (vxworks_object_p (abfd))
124
0
    return sh_vxworks_howto_table;
125
486
  return sh_elf_howto_table;
126
486
}
127
128
static bfd_reloc_status_type
129
sh_elf_reloc_loop (int r_type ATTRIBUTE_UNUSED, bfd *input_bfd,
130
       asection *input_section, bfd_byte *contents,
131
       bfd_vma addr, asection *symbol_section,
132
       bfd_vma start, bfd_vma end)
133
0
{
134
0
  static bfd_vma last_addr;
135
0
  static asection *last_symbol_section;
136
0
  bfd_byte *start_ptr, *ptr, *last_ptr;
137
0
  int diff, cum_diff;
138
0
  bfd_signed_vma x;
139
0
  int insn;
140
141
  /* Sanity check the address.  */
142
0
  if (addr > bfd_get_section_limit (input_bfd, input_section))
143
0
    return bfd_reloc_outofrange;
144
145
  /* We require the start and end relocations to be processed consecutively -
146
     although we allow then to be processed forwards or backwards.  */
147
0
  if (! last_addr)
148
0
    {
149
0
      last_addr = addr;
150
0
      last_symbol_section = symbol_section;
151
0
      return bfd_reloc_ok;
152
0
    }
153
0
  if (last_addr != addr)
154
0
    abort ();
155
0
  last_addr = 0;
156
157
0
  if (! symbol_section || last_symbol_section != symbol_section || end < start)
158
0
    return bfd_reloc_outofrange;
159
160
  /* Get the symbol_section contents.  */
161
0
  if (symbol_section != input_section)
162
0
    {
163
0
      if (elf_section_data (symbol_section)->this_hdr.contents != NULL)
164
0
  contents = elf_section_data (symbol_section)->this_hdr.contents;
165
0
      else
166
0
  {
167
0
    if (!bfd_malloc_and_get_section (input_bfd, symbol_section,
168
0
             &contents))
169
0
      {
170
0
        free (contents);
171
0
        return bfd_reloc_outofrange;
172
0
      }
173
0
  }
174
0
    }
175
0
#define IS_PPI(PTR) ((bfd_get_16 (input_bfd, (PTR)) & 0xfc00) == 0xf800)
176
0
  start_ptr = contents + start;
177
0
  for (cum_diff = -6, ptr = contents + end; cum_diff < 0 && ptr > start_ptr;)
178
0
    {
179
0
      for (last_ptr = ptr, ptr -= 4; ptr >= start_ptr && IS_PPI (ptr);)
180
0
  ptr -= 2;
181
0
      ptr += 2;
182
0
      diff = (last_ptr - ptr) >> 1;
183
0
      cum_diff += diff & 1;
184
0
      cum_diff += diff;
185
0
    }
186
  /* Calculate the start / end values to load into rs / re minus four -
187
     so that will cancel out the four we would otherwise have to add to
188
     addr to get the value to subtract in order to get relative addressing.  */
189
0
  if (cum_diff >= 0)
190
0
    {
191
0
      start -= 4;
192
0
      end = (ptr + cum_diff * 2) - contents;
193
0
    }
194
0
  else
195
0
    {
196
0
      bfd_vma start0 = start - 4;
197
198
0
      while (start0 && IS_PPI (contents + start0))
199
0
  start0 -= 2;
200
0
      start0 = start - 2 - ((start - start0) & 2);
201
0
      start = start0 - cum_diff - 2;
202
0
      end = start0;
203
0
    }
204
205
0
  if (elf_section_data (symbol_section)->this_hdr.contents != contents)
206
0
    free (contents);
207
208
0
  insn = bfd_get_16 (input_bfd, contents + addr);
209
210
0
  x = (insn & 0x200 ? end : start) - addr;
211
0
  if (input_section != symbol_section)
212
0
    x += ((symbol_section->output_section->vma + symbol_section->output_offset)
213
0
    - (input_section->output_section->vma
214
0
       + input_section->output_offset));
215
0
  x >>= 1;
216
0
  if (x < -128 || x > 127)
217
0
    return bfd_reloc_overflow;
218
219
0
  x = (insn & ~0xff) | (x & 0xff);
220
0
  bfd_put_16 (input_bfd, (bfd_vma) x, contents + addr);
221
222
0
  return bfd_reloc_ok;
223
0
}
224
225
/* This function is used for normal relocs.  This used to be like the COFF
226
   function, and is almost certainly incorrect for other ELF targets.  */
227
228
static bfd_reloc_status_type
229
sh_elf_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol_in,
230
        void *data, asection *input_section, bfd *output_bfd,
231
        char **error_message ATTRIBUTE_UNUSED)
232
1
{
233
1
  bfd_vma insn;
234
1
  bfd_vma sym_value;
235
1
  enum elf_sh_reloc_type r_type;
236
1
  bfd_vma addr = reloc_entry->address;
237
1
  bfd_size_type octets = addr * OCTETS_PER_BYTE (abfd, input_section);
238
1
  bfd_byte *hit_data = (bfd_byte *) data + octets;
239
240
1
  r_type = (enum elf_sh_reloc_type) reloc_entry->howto->type;
241
242
1
  if (output_bfd != NULL)
243
0
    {
244
      /* Partial linking--do nothing.  */
245
0
      reloc_entry->address += input_section->output_offset;
246
0
      return bfd_reloc_ok;
247
0
    }
248
249
  /* Almost all relocs have to do with relaxing.  If any work must be
250
     done for them, it has been done in sh_relax_section.  */
251
1
  if (r_type == R_SH_IND12W && (symbol_in->flags & BSF_LOCAL) != 0)
252
0
    return bfd_reloc_ok;
253
254
1
  if (symbol_in != NULL
255
1
      && bfd_is_und_section (symbol_in->section))
256
0
    return bfd_reloc_undefined;
257
258
  /* PR 17512: file: 9891ca98.  */
259
1
  if (octets + bfd_get_reloc_size (reloc_entry->howto)
260
1
      > bfd_get_section_limit_octets (abfd, input_section))
261
1
    return bfd_reloc_outofrange;
262
263
0
  if (bfd_is_com_section (symbol_in->section))
264
0
    sym_value = 0;
265
0
  else
266
0
    sym_value = (symbol_in->value +
267
0
     symbol_in->section->output_section->vma +
268
0
     symbol_in->section->output_offset);
269
270
0
  switch (r_type)
271
0
    {
272
0
    case R_SH_DIR32:
273
0
      insn = bfd_get_32 (abfd, hit_data);
274
0
      insn += sym_value + reloc_entry->addend;
275
0
      bfd_put_32 (abfd, insn, hit_data);
276
0
      break;
277
0
    case R_SH_IND12W:
278
0
      insn = bfd_get_16 (abfd, hit_data);
279
0
      sym_value += reloc_entry->addend;
280
0
      sym_value -= (input_section->output_section->vma
281
0
        + input_section->output_offset
282
0
        + addr
283
0
        + 4);
284
0
      sym_value += (((insn & 0xfff) ^ 0x800) - 0x800) << 1;
285
0
      insn = (insn & 0xf000) | ((sym_value >> 1) & 0xfff);
286
0
      bfd_put_16 (abfd, insn, hit_data);
287
0
      if (sym_value + 0x1000 >= 0x2000 || (sym_value & 1) != 0)
288
0
  return bfd_reloc_overflow;
289
0
      break;
290
0
    default:
291
0
      abort ();
292
0
      break;
293
0
    }
294
295
0
  return bfd_reloc_ok;
296
0
}
297
298
/* This function is used for relocs which are only used for relaxing,
299
   which the linker should otherwise ignore.  */
300
301
static bfd_reloc_status_type
302
sh_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
303
         asymbol *symbol ATTRIBUTE_UNUSED,
304
         void *data ATTRIBUTE_UNUSED, asection *input_section,
305
         bfd *output_bfd,
306
         char **error_message ATTRIBUTE_UNUSED)
307
138
{
308
138
  if (output_bfd != NULL)
309
0
    reloc_entry->address += input_section->output_offset;
310
138
  return bfd_reloc_ok;
311
138
}
312
313
/* This structure is used to map BFD reloc codes to SH ELF relocs.  */
314
315
struct elf_reloc_map
316
{
317
  bfd_reloc_code_real_type bfd_reloc_val;
318
  unsigned char elf_reloc_val;
319
};
320
321
/* An array mapping BFD reloc codes to SH ELF relocs.  */
322
323
static const struct elf_reloc_map sh_reloc_map[] =
324
{
325
  { BFD_RELOC_NONE, R_SH_NONE },
326
  { BFD_RELOC_32, R_SH_DIR32 },
327
  { BFD_RELOC_16, R_SH_DIR16 },
328
  { BFD_RELOC_8, R_SH_DIR8 },
329
  { BFD_RELOC_CTOR, R_SH_DIR32 },
330
  { BFD_RELOC_32_PCREL, R_SH_REL32 },
331
  { BFD_RELOC_SH_PCDISP8BY2, R_SH_DIR8WPN },
332
  { BFD_RELOC_SH_PCDISP12BY2, R_SH_IND12W },
333
  { BFD_RELOC_SH_PCRELIMM8BY2, R_SH_DIR8WPZ },
334
  { BFD_RELOC_SH_PCRELIMM8BY4, R_SH_DIR8WPL },
335
  { BFD_RELOC_8_PCREL, R_SH_SWITCH8 },
336
  { BFD_RELOC_SH_SWITCH16, R_SH_SWITCH16 },
337
  { BFD_RELOC_SH_SWITCH32, R_SH_SWITCH32 },
338
  { BFD_RELOC_SH_USES, R_SH_USES },
339
  { BFD_RELOC_SH_COUNT, R_SH_COUNT },
340
  { BFD_RELOC_SH_ALIGN, R_SH_ALIGN },
341
  { BFD_RELOC_SH_CODE, R_SH_CODE },
342
  { BFD_RELOC_SH_DATA, R_SH_DATA },
343
  { BFD_RELOC_SH_LABEL, R_SH_LABEL },
344
  { BFD_RELOC_VTABLE_INHERIT, R_SH_GNU_VTINHERIT },
345
  { BFD_RELOC_VTABLE_ENTRY, R_SH_GNU_VTENTRY },
346
  { BFD_RELOC_SH_LOOP_START, R_SH_LOOP_START },
347
  { BFD_RELOC_SH_LOOP_END, R_SH_LOOP_END },
348
  { BFD_RELOC_SH_TLS_GD_32, R_SH_TLS_GD_32 },
349
  { BFD_RELOC_SH_TLS_LD_32, R_SH_TLS_LD_32 },
350
  { BFD_RELOC_SH_TLS_LDO_32, R_SH_TLS_LDO_32 },
351
  { BFD_RELOC_SH_TLS_IE_32, R_SH_TLS_IE_32 },
352
  { BFD_RELOC_SH_TLS_LE_32, R_SH_TLS_LE_32 },
353
  { BFD_RELOC_SH_TLS_DTPMOD32, R_SH_TLS_DTPMOD32 },
354
  { BFD_RELOC_SH_TLS_DTPOFF32, R_SH_TLS_DTPOFF32 },
355
  { BFD_RELOC_SH_TLS_TPOFF32, R_SH_TLS_TPOFF32 },
356
  { BFD_RELOC_32_GOT_PCREL, R_SH_GOT32 },
357
  { BFD_RELOC_32_PLT_PCREL, R_SH_PLT32 },
358
  { BFD_RELOC_COPY, R_SH_COPY },
359
  { BFD_RELOC_GLOB_DAT, R_SH_GLOB_DAT },
360
  { BFD_RELOC_JMP_SLOT, R_SH_JMP_SLOT },
361
  { BFD_RELOC_RELATIVE, R_SH_RELATIVE },
362
  { BFD_RELOC_32_GOTOFF, R_SH_GOTOFF },
363
  { BFD_RELOC_SH_GOTPC, R_SH_GOTPC },
364
  { BFD_RELOC_SH_GOTPLT32, R_SH_GOTPLT32 },
365
  { BFD_RELOC_SH_GOT20, R_SH_GOT20 },
366
  { BFD_RELOC_SH_GOTOFF20, R_SH_GOTOFF20 },
367
  { BFD_RELOC_SH_GOTFUNCDESC, R_SH_GOTFUNCDESC },
368
  { BFD_RELOC_SH_GOTFUNCDESC20, R_SH_GOTFUNCDESC20 },
369
  { BFD_RELOC_SH_GOTOFFFUNCDESC, R_SH_GOTOFFFUNCDESC },
370
  { BFD_RELOC_SH_GOTOFFFUNCDESC20, R_SH_GOTOFFFUNCDESC20 },
371
  { BFD_RELOC_SH_FUNCDESC, R_SH_FUNCDESC },
372
};
373
374
/* Given a BFD reloc code, return the howto structure for the
375
   corresponding SH ELF reloc.  */
376
377
static reloc_howto_type *
378
sh_elf_reloc_type_lookup (bfd *abfd, bfd_reloc_code_real_type code)
379
0
{
380
0
  unsigned int i;
381
382
0
  for (i = 0; i < sizeof (sh_reloc_map) / sizeof (struct elf_reloc_map); i++)
383
0
    {
384
0
      if (sh_reloc_map[i].bfd_reloc_val == code)
385
0
  return get_howto_table (abfd) + (int) sh_reloc_map[i].elf_reloc_val;
386
0
    }
387
388
0
  return NULL;
389
0
}
390
391
static reloc_howto_type *
392
sh_elf_reloc_name_lookup (bfd *abfd, const char *r_name)
393
0
{
394
0
  unsigned int i;
395
396
0
  if (vxworks_object_p (abfd))
397
0
    {
398
0
      for (i = 0;
399
0
     i < (sizeof (sh_vxworks_howto_table)
400
0
    / sizeof (sh_vxworks_howto_table[0]));
401
0
     i++)
402
0
  if (sh_vxworks_howto_table[i].name != NULL
403
0
      && strcasecmp (sh_vxworks_howto_table[i].name, r_name) == 0)
404
0
    return &sh_vxworks_howto_table[i];
405
0
    }
406
0
  else
407
0
    {
408
0
      for (i = 0;
409
0
     i < (sizeof (sh_elf_howto_table)
410
0
    / sizeof (sh_elf_howto_table[0]));
411
0
     i++)
412
0
  if (sh_elf_howto_table[i].name != NULL
413
0
      && strcasecmp (sh_elf_howto_table[i].name, r_name) == 0)
414
0
    return &sh_elf_howto_table[i];
415
0
    }
416
417
0
  return NULL;
418
0
}
419
420
/* Given an ELF reloc, fill in the howto field of a relent.  */
421
422
static bool
423
sh_elf_info_to_howto (bfd *abfd, arelent *cache_ptr, Elf_Internal_Rela *dst)
424
501
{
425
501
  unsigned int r;
426
427
501
  r = ELF32_R_TYPE (dst->r_info);
428
429
501
  if (r >= R_SH_FIRST_INVALID_RELOC_6
430
495
      || (r >= R_SH_FIRST_INVALID_RELOC   && r <= R_SH_LAST_INVALID_RELOC)
431
494
      || (r >= R_SH_FIRST_INVALID_RELOC_2 && r <= R_SH_LAST_INVALID_RELOC_2)
432
494
      || (r >= R_SH_FIRST_INVALID_RELOC_3 && r <= R_SH_LAST_INVALID_RELOC_3)
433
489
      || (r >= R_SH_FIRST_INVALID_RELOC_4 && r <= R_SH_LAST_INVALID_RELOC_4)
434
487
      || (r >= R_SH_FIRST_INVALID_RELOC_5 && r <= R_SH_LAST_INVALID_RELOC_5))
435
15
    {
436
      /* xgettext:c-format */
437
15
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
438
15
        abfd, r);
439
15
      bfd_set_error (bfd_error_bad_value);
440
15
      return false;
441
15
    }
442
443
486
  cache_ptr->howto = get_howto_table (abfd) + r;
444
486
  return true;
445
501
}
446

447
/* This function handles relaxing for SH ELF.  See the corresponding
448
   function in coff-sh.c for a description of what this does.  FIXME:
449
   There is a lot of duplication here between this code and the COFF
450
   specific code.  The format of relocs and symbols is wound deeply
451
   into this code, but it would still be better if the duplication
452
   could be eliminated somehow.  Note in particular that although both
453
   functions use symbols like R_SH_CODE, those symbols have different
454
   values; in coff-sh.c they come from include/coff/sh.h, whereas here
455
   they come from enum elf_sh_reloc_type in include/elf/sh.h.  */
456
457
static bool
458
sh_elf_relax_section (bfd *abfd, asection *sec,
459
          struct bfd_link_info *link_info, bool *again)
460
0
{
461
0
  Elf_Internal_Shdr *symtab_hdr;
462
0
  Elf_Internal_Rela *internal_relocs;
463
0
  bool have_code;
464
0
  Elf_Internal_Rela *irel, *irelend;
465
0
  bfd_byte *contents = NULL;
466
0
  Elf_Internal_Sym *isymbuf = NULL;
467
468
0
  *again = false;
469
470
0
  if (bfd_link_relocatable (link_info)
471
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
472
0
      || (sec->flags & SEC_RELOC) == 0
473
0
      || sec->reloc_count == 0)
474
0
    return true;
475
476
0
  symtab_hdr = &elf_symtab_hdr (abfd);
477
478
0
  internal_relocs = (_bfd_elf_link_read_relocs
479
0
         (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
480
0
          link_info->keep_memory));
481
0
  if (internal_relocs == NULL)
482
0
    goto error_return;
483
484
0
  have_code = false;
485
486
0
  irelend = internal_relocs + sec->reloc_count;
487
0
  for (irel = internal_relocs; irel < irelend; irel++)
488
0
    {
489
0
      bfd_vma laddr, paddr, symval;
490
0
      unsigned short insn;
491
0
      Elf_Internal_Rela *irelfn, *irelscan, *irelcount;
492
0
      bfd_signed_vma foff;
493
494
0
      if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_CODE)
495
0
  have_code = true;
496
497
0
      if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_USES)
498
0
  continue;
499
500
      /* Get the section contents.  */
501
0
      if (contents == NULL)
502
0
  {
503
0
    if (elf_section_data (sec)->this_hdr.contents != NULL)
504
0
      contents = elf_section_data (sec)->this_hdr.contents;
505
0
    else
506
0
      {
507
0
        if (!bfd_malloc_and_get_section (abfd, sec, &contents))
508
0
    goto error_return;
509
0
      }
510
0
  }
511
512
      /* The r_addend field of the R_SH_USES reloc will point us to
513
   the register load.  The 4 is because the r_addend field is
514
   computed as though it were a jump offset, which are based
515
   from 4 bytes after the jump instruction.  */
516
0
      laddr = irel->r_offset + 4 + irel->r_addend;
517
0
      if (laddr >= sec->size)
518
0
  {
519
    /* xgettext:c-format */
520
0
    _bfd_error_handler
521
0
      (_("%pB: %#" PRIx64 ": warning: bad R_SH_USES offset"),
522
0
       abfd, (uint64_t) irel->r_offset);
523
0
    continue;
524
0
  }
525
0
      insn = bfd_get_16 (abfd, contents + laddr);
526
527
      /* If the instruction is not mov.l NN,rN, we don't know what to
528
   do.  */
529
0
      if ((insn & 0xf000) != 0xd000)
530
0
  {
531
0
    _bfd_error_handler
532
      /* xgettext:c-format */
533
0
      (_("%pB: %#" PRIx64 ": warning: "
534
0
         "R_SH_USES points to unrecognized insn 0x%x"),
535
0
       abfd, (uint64_t) irel->r_offset, insn);
536
0
    continue;
537
0
  }
538
539
      /* Get the address from which the register is being loaded.  The
540
   displacement in the mov.l instruction is quadrupled.  It is a
541
   displacement from four bytes after the movl instruction, but,
542
   before adding in the PC address, two least significant bits
543
   of the PC are cleared.  We assume that the section is aligned
544
   on a four byte boundary.  */
545
0
      paddr = insn & 0xff;
546
0
      paddr *= 4;
547
0
      paddr += (laddr + 4) &~ (bfd_vma) 3;
548
0
      if (paddr >= sec->size)
549
0
  {
550
0
    _bfd_error_handler
551
      /* xgettext:c-format */
552
0
      (_("%pB: %#" PRIx64 ": warning: bad R_SH_USES load offset"),
553
0
       abfd, (uint64_t) irel->r_offset);
554
0
    continue;
555
0
  }
556
557
      /* Get the reloc for the address from which the register is
558
   being loaded.  This reloc will tell us which function is
559
   actually being called.  */
560
0
      for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
561
0
  if (irelfn->r_offset == paddr
562
0
      && ELF32_R_TYPE (irelfn->r_info) == (int) R_SH_DIR32)
563
0
    break;
564
0
      if (irelfn >= irelend)
565
0
  {
566
0
    _bfd_error_handler
567
      /* xgettext:c-format */
568
0
      (_("%pB: %#" PRIx64 ": warning: could not find expected reloc"),
569
0
       abfd, (uint64_t) paddr);
570
0
    continue;
571
0
  }
572
573
      /* Read this BFD's symbols if we haven't done so already.  */
574
0
      if (isymbuf == NULL && symtab_hdr->sh_info != 0)
575
0
  {
576
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
577
0
    if (isymbuf == NULL)
578
0
      isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
579
0
              symtab_hdr->sh_info, 0,
580
0
              NULL, NULL, NULL);
581
0
    if (isymbuf == NULL)
582
0
      goto error_return;
583
0
  }
584
585
      /* Get the value of the symbol referred to by the reloc.  */
586
0
      if (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
587
0
  {
588
    /* A local symbol.  */
589
0
    Elf_Internal_Sym *isym;
590
591
0
    isym = isymbuf + ELF32_R_SYM (irelfn->r_info);
592
0
    if (isym->st_shndx
593
0
        != (unsigned int) _bfd_elf_section_from_bfd_section (abfd, sec))
594
0
      {
595
0
        _bfd_error_handler
596
    /* xgettext:c-format */
597
0
    (_("%pB: %#" PRIx64 ": warning: symbol in unexpected section"),
598
0
     abfd, (uint64_t) paddr);
599
0
        continue;
600
0
      }
601
602
0
    symval = (isym->st_value
603
0
        + sec->output_section->vma
604
0
        + sec->output_offset);
605
0
  }
606
0
      else
607
0
  {
608
0
    unsigned long indx;
609
0
    struct elf_link_hash_entry *h;
610
611
0
    indx = ELF32_R_SYM (irelfn->r_info) - symtab_hdr->sh_info;
612
0
    h = elf_sym_hashes (abfd)[indx];
613
0
    BFD_ASSERT (h != NULL);
614
0
    if (h->root.type != bfd_link_hash_defined
615
0
        && h->root.type != bfd_link_hash_defweak)
616
0
      {
617
        /* This appears to be a reference to an undefined
618
     symbol.  Just ignore it--it will be caught by the
619
     regular reloc processing.  */
620
0
        continue;
621
0
      }
622
623
0
    symval = (h->root.u.def.value
624
0
        + h->root.u.def.section->output_section->vma
625
0
        + h->root.u.def.section->output_offset);
626
0
  }
627
628
0
      if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
629
0
  symval += bfd_get_32 (abfd, contents + paddr);
630
0
      else
631
0
  symval += irelfn->r_addend;
632
633
      /* See if this function call can be shortened.  */
634
0
      foff = (symval
635
0
        - (irel->r_offset
636
0
     + sec->output_section->vma
637
0
     + sec->output_offset
638
0
     + 4));
639
      /* A branch to an address beyond ours might be increased by an
640
   .align that doesn't move when bytes behind us are deleted.
641
   So, we add some slop in this calculation to allow for
642
   that.  */
643
0
      if (foff < -0x1000 || foff >= 0x1000 - 8)
644
0
  {
645
    /* After all that work, we can't shorten this function call.  */
646
0
    continue;
647
0
  }
648
649
      /* Shorten the function call.  */
650
651
      /* For simplicity of coding, we are going to modify the section
652
   contents, the section relocs, and the BFD symbol table.  We
653
   must tell the rest of the code not to free up this
654
   information.  It would be possible to instead create a table
655
   of changes which have to be made, as is done in coff-mips.c;
656
   that would be more work, but would require less memory when
657
   the linker is run.  */
658
659
0
      elf_section_data (sec)->relocs = internal_relocs;
660
0
      elf_section_data (sec)->this_hdr.contents = contents;
661
0
      symtab_hdr->contents = (unsigned char *) isymbuf;
662
663
      /* Replace the jmp/jsr with a bra/bsr.  */
664
665
      /* Change the R_SH_USES reloc into an R_SH_IND12W reloc, and
666
   replace the jmp/jsr with a bra/bsr.  */
667
0
      irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irelfn->r_info), R_SH_IND12W);
668
      /* We used to test (ELF32_R_SYM (irelfn->r_info) < symtab_hdr->sh_info)
669
   here, but that only checks if the symbol is an external symbol,
670
   not if the symbol is in a different section.  Besides, we need
671
   a consistent meaning for the relocation, so we just assume here that
672
   the value of the symbol is not available.  */
673
674
      /* We can't fully resolve this yet, because the external
675
   symbol value may be changed by future relaxing.  We let
676
   the final link phase handle it.  */
677
0
      if (bfd_get_16 (abfd, contents + irel->r_offset) & 0x0020)
678
0
  bfd_put_16 (abfd, (bfd_vma) 0xa000, contents + irel->r_offset);
679
0
      else
680
0
  bfd_put_16 (abfd, (bfd_vma) 0xb000, contents + irel->r_offset);
681
682
0
      irel->r_addend = -4;
683
684
      /* When we calculated the symbol "value" we had an offset in the
685
   DIR32's word in memory (we read and add it above).  However,
686
   the jsr we create does NOT have this offset encoded, so we
687
   have to add it to the addend to preserve it.  */
688
0
      irel->r_addend += bfd_get_32 (abfd, contents + paddr);
689
690
      /* See if there is another R_SH_USES reloc referring to the same
691
   register load.  */
692
0
      for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
693
0
  if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_USES
694
0
      && laddr == irelscan->r_offset + 4 + irelscan->r_addend)
695
0
    break;
696
0
      if (irelscan < irelend)
697
0
  {
698
    /* Some other function call depends upon this register load,
699
       and we have not yet converted that function call.
700
       Indeed, we may never be able to convert it.  There is
701
       nothing else we can do at this point.  */
702
0
    continue;
703
0
  }
704
705
      /* Look for a R_SH_COUNT reloc on the location where the
706
   function address is stored.  Do this before deleting any
707
   bytes, to avoid confusion about the address.  */
708
0
      for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
709
0
  if (irelcount->r_offset == paddr
710
0
      && ELF32_R_TYPE (irelcount->r_info) == (int) R_SH_COUNT)
711
0
    break;
712
713
      /* Delete the register load.  */
714
0
      if (! sh_elf_relax_delete_bytes (abfd, sec, laddr, 2))
715
0
  goto error_return;
716
717
      /* That will change things, so, just in case it permits some
718
   other function call to come within range, we should relax
719
   again.  Note that this is not required, and it may be slow.  */
720
0
      *again = true;
721
722
      /* Now check whether we got a COUNT reloc.  */
723
0
      if (irelcount >= irelend)
724
0
  {
725
0
    _bfd_error_handler
726
      /* xgettext:c-format */
727
0
      (_("%pB: %#" PRIx64 ": warning: "
728
0
         "could not find expected COUNT reloc"),
729
0
       abfd, (uint64_t) paddr);
730
0
    continue;
731
0
  }
732
733
      /* The number of uses is stored in the r_addend field.  We've
734
   just deleted one.  */
735
0
      if (irelcount->r_addend == 0)
736
0
  {
737
    /* xgettext:c-format */
738
0
    _bfd_error_handler (_("%pB: %#" PRIx64 ": warning: bad count"),
739
0
            abfd, (uint64_t) paddr);
740
0
    continue;
741
0
  }
742
743
0
      --irelcount->r_addend;
744
745
      /* If there are no more uses, we can delete the address.  Reload
746
   the address from irelfn, in case it was changed by the
747
   previous call to sh_elf_relax_delete_bytes.  */
748
0
      if (irelcount->r_addend == 0)
749
0
  {
750
0
    if (! sh_elf_relax_delete_bytes (abfd, sec, irelfn->r_offset, 4))
751
0
      goto error_return;
752
0
  }
753
754
      /* We've done all we can with that function call.  */
755
0
    }
756
757
  /* Look for load and store instructions that we can align on four
758
     byte boundaries.  */
759
0
  if ((elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK) != EF_SH4
760
0
      && have_code)
761
0
    {
762
0
      bool swapped;
763
764
      /* Get the section contents.  */
765
0
      if (contents == NULL)
766
0
  {
767
0
    if (elf_section_data (sec)->this_hdr.contents != NULL)
768
0
      contents = elf_section_data (sec)->this_hdr.contents;
769
0
    else
770
0
      {
771
0
        if (!bfd_malloc_and_get_section (abfd, sec, &contents))
772
0
    goto error_return;
773
0
      }
774
0
  }
775
776
0
      if (! sh_elf_align_loads (abfd, sec, internal_relocs, contents,
777
0
        &swapped))
778
0
  goto error_return;
779
780
0
      if (swapped)
781
0
  {
782
0
    elf_section_data (sec)->relocs = internal_relocs;
783
0
    elf_section_data (sec)->this_hdr.contents = contents;
784
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
785
0
  }
786
0
    }
787
788
0
  if (isymbuf != NULL
789
0
      && symtab_hdr->contents != (unsigned char *) isymbuf)
790
0
    {
791
0
      if (! link_info->keep_memory)
792
0
  free (isymbuf);
793
0
      else
794
0
  {
795
    /* Cache the symbols for elf_link_input_bfd.  */
796
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
797
0
  }
798
0
    }
799
800
0
  if (contents != NULL
801
0
      && elf_section_data (sec)->this_hdr.contents != contents)
802
0
    {
803
0
      if (! link_info->keep_memory)
804
0
  free (contents);
805
0
      else
806
0
  {
807
    /* Cache the section contents for elf_link_input_bfd.  */
808
0
    elf_section_data (sec)->this_hdr.contents = contents;
809
0
  }
810
0
    }
811
812
0
  if (elf_section_data (sec)->relocs != internal_relocs)
813
0
    free (internal_relocs);
814
815
0
  return true;
816
817
0
 error_return:
818
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
819
0
    free (isymbuf);
820
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
821
0
    free (contents);
822
0
  if (elf_section_data (sec)->relocs != internal_relocs)
823
0
    free (internal_relocs);
824
825
0
  return false;
826
0
}
827
828
/* Delete some bytes from a section while relaxing.  FIXME: There is a
829
   lot of duplication between this function and sh_relax_delete_bytes
830
   in coff-sh.c.  */
831
832
static bool
833
sh_elf_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr,
834
         int count)
835
0
{
836
0
  Elf_Internal_Shdr *symtab_hdr;
837
0
  unsigned int sec_shndx;
838
0
  bfd_byte *contents;
839
0
  Elf_Internal_Rela *irel, *irelend;
840
0
  Elf_Internal_Rela *irelalign;
841
0
  bfd_vma toaddr;
842
0
  Elf_Internal_Sym *isymbuf, *isym, *isymend;
843
0
  struct elf_link_hash_entry **sym_hashes;
844
0
  struct elf_link_hash_entry **end_hashes;
845
0
  unsigned int symcount;
846
0
  asection *o;
847
848
0
  symtab_hdr = &elf_symtab_hdr (abfd);
849
0
  isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
850
851
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
852
853
0
  contents = elf_section_data (sec)->this_hdr.contents;
854
855
  /* The deletion must stop at the next ALIGN reloc for an alignment
856
     power larger than the number of bytes we are deleting.  */
857
858
0
  irelalign = NULL;
859
0
  toaddr = sec->size;
860
861
0
  irel = elf_section_data (sec)->relocs;
862
0
  irelend = irel + sec->reloc_count;
863
0
  for (; irel < irelend; irel++)
864
0
    {
865
0
      if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
866
0
    && irel->r_offset > addr
867
0
    && count < (1 << irel->r_addend))
868
0
  {
869
0
    irelalign = irel;
870
0
    toaddr = irel->r_offset;
871
0
    break;
872
0
  }
873
0
    }
874
875
  /* Actually delete the bytes.  */
876
0
  memmove (contents + addr, contents + addr + count,
877
0
     (size_t) (toaddr - addr - count));
878
0
  if (irelalign == NULL)
879
0
    sec->size -= count;
880
0
  else
881
0
    {
882
0
      int i;
883
884
0
#define NOP_OPCODE (0x0009)
885
886
0
      BFD_ASSERT ((count & 1) == 0);
887
0
      for (i = 0; i < count; i += 2)
888
0
  bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
889
0
    }
890
891
  /* Adjust all the relocs.  */
892
0
  for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
893
0
    {
894
0
      bfd_vma nraddr, stop;
895
0
      bfd_vma start = 0;
896
0
      int insn = 0;
897
0
      int off, adjust, oinsn;
898
0
      bfd_signed_vma voff = 0;
899
0
      bool overflow;
900
901
      /* Get the new reloc address.  */
902
0
      nraddr = irel->r_offset;
903
0
      if ((irel->r_offset > addr
904
0
     && irel->r_offset < toaddr)
905
0
    || (ELF32_R_TYPE (irel->r_info) == (int) R_SH_ALIGN
906
0
        && irel->r_offset == toaddr))
907
0
  nraddr -= count;
908
909
      /* See if this reloc was for the bytes we have deleted, in which
910
   case we no longer care about it.  Don't delete relocs which
911
   represent addresses, though.  */
912
0
      if (irel->r_offset >= addr
913
0
    && irel->r_offset < addr + count
914
0
    && ELF32_R_TYPE (irel->r_info) != (int) R_SH_ALIGN
915
0
    && ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE
916
0
    && ELF32_R_TYPE (irel->r_info) != (int) R_SH_DATA
917
0
    && ELF32_R_TYPE (irel->r_info) != (int) R_SH_LABEL)
918
0
  irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
919
0
             (int) R_SH_NONE);
920
921
      /* If this is a PC relative reloc, see if the range it covers
922
   includes the bytes we have deleted.  */
923
0
      switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
924
0
  {
925
0
  default:
926
0
    break;
927
928
0
  case R_SH_DIR8WPN:
929
0
  case R_SH_IND12W:
930
0
  case R_SH_DIR8WPZ:
931
0
  case R_SH_DIR8WPL:
932
0
    start = irel->r_offset;
933
0
    insn = bfd_get_16 (abfd, contents + nraddr);
934
0
    break;
935
0
  }
936
937
0
      switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
938
0
  {
939
0
  default:
940
0
    start = stop = addr;
941
0
    break;
942
943
0
  case R_SH_DIR32:
944
    /* If this reloc is against a symbol defined in this
945
       section, and the symbol will not be adjusted below, we
946
       must check the addend to see it will put the value in
947
       range to be adjusted, and hence must be changed.  */
948
0
    if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
949
0
      {
950
0
        isym = isymbuf + ELF32_R_SYM (irel->r_info);
951
0
        if (isym->st_shndx == sec_shndx
952
0
      && (isym->st_value <= addr
953
0
          || isym->st_value >= toaddr))
954
0
    {
955
0
      bfd_vma val;
956
957
0
      if (get_howto_table (abfd)[R_SH_DIR32].partial_inplace)
958
0
        {
959
0
          val = bfd_get_32 (abfd, contents + nraddr);
960
0
          val += isym->st_value;
961
0
          if (val > addr && val < toaddr)
962
0
      bfd_put_32 (abfd, val - count, contents + nraddr);
963
0
        }
964
0
      else
965
0
        {
966
0
          val = isym->st_value + irel->r_addend;
967
0
          if (val > addr && val < toaddr)
968
0
      irel->r_addend -= count;
969
0
        }
970
0
    }
971
0
      }
972
0
    start = stop = addr;
973
0
    break;
974
975
0
  case R_SH_DIR8WPN:
976
0
    off = insn & 0xff;
977
0
    if (off & 0x80)
978
0
      off -= 0x100;
979
0
    stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
980
0
    break;
981
982
0
  case R_SH_IND12W:
983
0
    off = insn & 0xfff;
984
0
    if (! off)
985
0
      {
986
        /* This has been made by previous relaxation.  Since the
987
     relocation will be against an external symbol, the
988
     final relocation will just do the right thing.  */
989
0
        start = stop = addr;
990
0
      }
991
0
    else
992
0
      {
993
0
        if (off & 0x800)
994
0
    off -= 0x1000;
995
0
        stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
996
997
        /* The addend will be against the section symbol, thus
998
     for adjusting the addend, the relevant start is the
999
     start of the section.
1000
     N.B. If we want to abandon in-place changes here and
1001
     test directly using symbol + addend, we have to take into
1002
     account that the addend has already been adjusted by -4.  */
1003
0
        if (stop > addr && stop < toaddr)
1004
0
    irel->r_addend -= count;
1005
0
      }
1006
0
    break;
1007
1008
0
  case R_SH_DIR8WPZ:
1009
0
    off = insn & 0xff;
1010
0
    stop = start + 4 + off * 2;
1011
0
    break;
1012
1013
0
  case R_SH_DIR8WPL:
1014
0
    off = insn & 0xff;
1015
0
    stop = (start & ~(bfd_vma) 3) + 4 + off * 4;
1016
0
    break;
1017
1018
0
  case R_SH_SWITCH8:
1019
0
  case R_SH_SWITCH16:
1020
0
  case R_SH_SWITCH32:
1021
    /* These relocs types represent
1022
         .word L2-L1
1023
       The r_addend field holds the difference between the reloc
1024
       address and L1.  That is the start of the reloc, and
1025
       adding in the contents gives us the top.  We must adjust
1026
       both the r_offset field and the section contents.
1027
       N.B. in gas / coff bfd, the elf bfd r_addend is called r_offset,
1028
       and the elf bfd r_offset is called r_vaddr.  */
1029
1030
0
    stop = irel->r_offset;
1031
0
    start = (bfd_vma) ((bfd_signed_vma) stop - (long) irel->r_addend);
1032
1033
0
    if (start > addr
1034
0
        && start < toaddr
1035
0
        && (stop <= addr || stop >= toaddr))
1036
0
      irel->r_addend += count;
1037
0
    else if (stop > addr
1038
0
       && stop < toaddr
1039
0
       && (start <= addr || start >= toaddr))
1040
0
      irel->r_addend -= count;
1041
1042
0
    if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH16)
1043
0
      voff = bfd_get_signed_16 (abfd, contents + nraddr);
1044
0
    else if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_SWITCH8)
1045
0
      voff = bfd_get_8 (abfd, contents + nraddr);
1046
0
    else
1047
0
      voff = bfd_get_signed_32 (abfd, contents + nraddr);
1048
0
    stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1049
1050
0
    break;
1051
1052
0
  case R_SH_USES:
1053
0
    start = irel->r_offset;
1054
0
    stop = (bfd_vma) ((bfd_signed_vma) start
1055
0
          + (long) irel->r_addend
1056
0
          + 4);
1057
0
    break;
1058
0
  }
1059
1060
0
      if (start > addr
1061
0
    && start < toaddr
1062
0
    && (stop <= addr || stop >= toaddr))
1063
0
  adjust = count;
1064
0
      else if (stop > addr
1065
0
         && stop < toaddr
1066
0
         && (start <= addr || start >= toaddr))
1067
0
  adjust = - count;
1068
0
      else
1069
0
  adjust = 0;
1070
1071
0
      if (adjust != 0)
1072
0
  {
1073
0
    oinsn = insn;
1074
0
    overflow = false;
1075
0
    switch ((enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info))
1076
0
      {
1077
0
      default:
1078
0
        abort ();
1079
0
        break;
1080
1081
0
      case R_SH_DIR8WPN:
1082
0
      case R_SH_DIR8WPZ:
1083
0
        insn += adjust / 2;
1084
0
        if ((oinsn & 0xff00) != (insn & 0xff00))
1085
0
    overflow = true;
1086
0
        bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1087
0
        break;
1088
1089
0
      case R_SH_IND12W:
1090
0
        insn += adjust / 2;
1091
0
        if ((oinsn & 0xf000) != (insn & 0xf000))
1092
0
    overflow = true;
1093
0
        bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1094
0
        break;
1095
1096
0
      case R_SH_DIR8WPL:
1097
0
        BFD_ASSERT (adjust == count || count >= 4);
1098
0
        if (count >= 4)
1099
0
    insn += adjust / 4;
1100
0
        else
1101
0
    {
1102
0
      if ((irel->r_offset & 3) == 0)
1103
0
        ++insn;
1104
0
    }
1105
0
        if ((oinsn & 0xff00) != (insn & 0xff00))
1106
0
    overflow = true;
1107
0
        bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
1108
0
        break;
1109
1110
0
      case R_SH_SWITCH8:
1111
0
        voff += adjust;
1112
0
        if (voff < 0 || voff >= 0xff)
1113
0
    overflow = true;
1114
0
        bfd_put_8 (abfd, voff, contents + nraddr);
1115
0
        break;
1116
1117
0
      case R_SH_SWITCH16:
1118
0
        voff += adjust;
1119
0
        if (voff < - 0x8000 || voff >= 0x8000)
1120
0
    overflow = true;
1121
0
        bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
1122
0
        break;
1123
1124
0
      case R_SH_SWITCH32:
1125
0
        voff += adjust;
1126
0
        bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
1127
0
        break;
1128
1129
0
      case R_SH_USES:
1130
0
        irel->r_addend += adjust;
1131
0
        break;
1132
0
      }
1133
1134
0
    if (overflow)
1135
0
      {
1136
0
        _bfd_error_handler
1137
    /* xgettext:c-format */
1138
0
    (_("%pB: %#" PRIx64 ": fatal: reloc overflow while relaxing"),
1139
0
     abfd, (uint64_t) irel->r_offset);
1140
0
        bfd_set_error (bfd_error_bad_value);
1141
0
        return false;
1142
0
      }
1143
0
  }
1144
1145
0
      irel->r_offset = nraddr;
1146
0
    }
1147
1148
  /* Look through all the other sections.  If there contain any IMM32
1149
     relocs against internal symbols which we are not going to adjust
1150
     below, we may need to adjust the addends.  */
1151
0
  for (o = abfd->sections; o != NULL; o = o->next)
1152
0
    {
1153
0
      Elf_Internal_Rela *internal_relocs;
1154
0
      Elf_Internal_Rela *irelscan, *irelscanend;
1155
0
      bfd_byte *ocontents;
1156
1157
0
      if (o == sec
1158
0
    || (o->flags & SEC_HAS_CONTENTS) == 0
1159
0
    || (o->flags & SEC_RELOC) == 0
1160
0
    || o->reloc_count == 0)
1161
0
  continue;
1162
1163
      /* We always cache the relocs.  Perhaps, if info->keep_memory is
1164
   FALSE, we should free them, if we are permitted to, when we
1165
   leave sh_coff_relax_section.  */
1166
0
      internal_relocs = (_bfd_elf_link_read_relocs
1167
0
       (abfd, o, NULL, (Elf_Internal_Rela *) NULL, true));
1168
0
      if (internal_relocs == NULL)
1169
0
  return false;
1170
1171
0
      ocontents = NULL;
1172
0
      irelscanend = internal_relocs + o->reloc_count;
1173
0
      for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
1174
0
  {
1175
    /* Dwarf line numbers use R_SH_SWITCH32 relocs.  */
1176
0
    if (ELF32_R_TYPE (irelscan->r_info) == (int) R_SH_SWITCH32)
1177
0
      {
1178
0
        bfd_vma start, stop;
1179
0
        bfd_signed_vma voff;
1180
1181
0
        if (ocontents == NULL)
1182
0
    {
1183
0
      if (elf_section_data (o)->this_hdr.contents != NULL)
1184
0
        ocontents = elf_section_data (o)->this_hdr.contents;
1185
0
      else
1186
0
        {
1187
          /* We always cache the section contents.
1188
       Perhaps, if info->keep_memory is FALSE, we
1189
       should free them, if we are permitted to,
1190
       when we leave sh_coff_relax_section.  */
1191
0
          if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1192
0
      {
1193
0
        free (ocontents);
1194
0
        return false;
1195
0
      }
1196
1197
0
          elf_section_data (o)->this_hdr.contents = ocontents;
1198
0
        }
1199
0
    }
1200
1201
0
        stop = irelscan->r_offset;
1202
0
        start
1203
0
    = (bfd_vma) ((bfd_signed_vma) stop - (long) irelscan->r_addend);
1204
1205
        /* STOP is in a different section, so it won't change.  */
1206
0
        if (start > addr && start < toaddr)
1207
0
    irelscan->r_addend += count;
1208
1209
0
        voff = bfd_get_signed_32 (abfd, ocontents + irelscan->r_offset);
1210
0
        stop = (bfd_vma) ((bfd_signed_vma) start + voff);
1211
1212
0
        if (start > addr
1213
0
      && start < toaddr
1214
0
      && (stop <= addr || stop >= toaddr))
1215
0
    bfd_put_signed_32 (abfd, (bfd_vma) voff + count,
1216
0
           ocontents + irelscan->r_offset);
1217
0
        else if (stop > addr
1218
0
           && stop < toaddr
1219
0
           && (start <= addr || start >= toaddr))
1220
0
    bfd_put_signed_32 (abfd, (bfd_vma) voff - count,
1221
0
           ocontents + irelscan->r_offset);
1222
0
      }
1223
1224
0
    if (ELF32_R_TYPE (irelscan->r_info) != (int) R_SH_DIR32)
1225
0
      continue;
1226
1227
0
    if (ELF32_R_SYM (irelscan->r_info) >= symtab_hdr->sh_info)
1228
0
      continue;
1229
1230
1231
0
    isym = isymbuf + ELF32_R_SYM (irelscan->r_info);
1232
0
    if (isym->st_shndx == sec_shndx
1233
0
        && (isym->st_value <= addr
1234
0
      || isym->st_value >= toaddr))
1235
0
      {
1236
0
        bfd_vma val;
1237
1238
0
        if (ocontents == NULL)
1239
0
    {
1240
0
      if (elf_section_data (o)->this_hdr.contents != NULL)
1241
0
        ocontents = elf_section_data (o)->this_hdr.contents;
1242
0
      else
1243
0
        {
1244
          /* We always cache the section contents.
1245
       Perhaps, if info->keep_memory is FALSE, we
1246
       should free them, if we are permitted to,
1247
       when we leave sh_coff_relax_section.  */
1248
0
          if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
1249
0
      {
1250
0
        free (ocontents);
1251
0
        return false;
1252
0
      }
1253
1254
0
          elf_section_data (o)->this_hdr.contents = ocontents;
1255
0
        }
1256
0
    }
1257
1258
0
        val = bfd_get_32 (abfd, ocontents + irelscan->r_offset);
1259
0
        val += isym->st_value;
1260
0
        if (val > addr && val < toaddr)
1261
0
    bfd_put_32 (abfd, val - count,
1262
0
          ocontents + irelscan->r_offset);
1263
0
      }
1264
0
  }
1265
0
    }
1266
1267
  /* Adjust the local symbols defined in this section.  */
1268
0
  isymend = isymbuf + symtab_hdr->sh_info;
1269
0
  for (isym = isymbuf; isym < isymend; isym++)
1270
0
    {
1271
0
      if (isym->st_shndx == sec_shndx
1272
0
    && isym->st_value > addr
1273
0
    && isym->st_value < toaddr)
1274
0
  isym->st_value -= count;
1275
0
    }
1276
1277
  /* Now adjust the global symbols defined in this section.  */
1278
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1279
0
        - symtab_hdr->sh_info);
1280
0
  sym_hashes = elf_sym_hashes (abfd);
1281
0
  end_hashes = sym_hashes + symcount;
1282
0
  for (; sym_hashes < end_hashes; sym_hashes++)
1283
0
    {
1284
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
1285
0
      if ((sym_hash->root.type == bfd_link_hash_defined
1286
0
     || sym_hash->root.type == bfd_link_hash_defweak)
1287
0
    && sym_hash->root.u.def.section == sec
1288
0
    && sym_hash->root.u.def.value > addr
1289
0
    && sym_hash->root.u.def.value < toaddr)
1290
0
  {
1291
0
    sym_hash->root.u.def.value -= count;
1292
0
  }
1293
0
    }
1294
1295
  /* See if we can move the ALIGN reloc forward.  We have adjusted
1296
     r_offset for it already.  */
1297
0
  if (irelalign != NULL)
1298
0
    {
1299
0
      bfd_vma alignto, alignaddr;
1300
1301
0
      alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_addend);
1302
0
      alignaddr = BFD_ALIGN (irelalign->r_offset,
1303
0
           1 << irelalign->r_addend);
1304
0
      if (alignto != alignaddr)
1305
0
  {
1306
    /* Tail recursion.  */
1307
0
    return sh_elf_relax_delete_bytes (abfd, sec, alignaddr,
1308
0
              (int) (alignto - alignaddr));
1309
0
  }
1310
0
    }
1311
1312
0
  return true;
1313
0
}
1314
1315
/* Look for loads and stores which we can align to four byte
1316
   boundaries.  This is like sh_align_loads in coff-sh.c.  */
1317
1318
static bool
1319
sh_elf_align_loads (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
1320
        Elf_Internal_Rela *internal_relocs,
1321
        bfd_byte *contents ATTRIBUTE_UNUSED,
1322
        bool *pswapped)
1323
0
{
1324
0
  Elf_Internal_Rela *irel, *irelend;
1325
0
  bfd_vma *labels = NULL;
1326
0
  bfd_vma *label, *label_end;
1327
0
  bfd_size_type amt;
1328
1329
0
  *pswapped = false;
1330
1331
0
  irelend = internal_relocs + sec->reloc_count;
1332
1333
  /* Get all the addresses with labels on them.  */
1334
0
  amt = sec->reloc_count;
1335
0
  amt *= sizeof (bfd_vma);
1336
0
  labels = (bfd_vma *) bfd_malloc (amt);
1337
0
  if (labels == NULL)
1338
0
    goto error_return;
1339
0
  label_end = labels;
1340
0
  for (irel = internal_relocs; irel < irelend; irel++)
1341
0
    {
1342
0
      if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_LABEL)
1343
0
  {
1344
0
    *label_end = irel->r_offset;
1345
0
    ++label_end;
1346
0
  }
1347
0
    }
1348
1349
  /* Note that the assembler currently always outputs relocs in
1350
     address order.  If that ever changes, this code will need to sort
1351
     the label values and the relocs.  */
1352
1353
0
  label = labels;
1354
1355
0
  for (irel = internal_relocs; irel < irelend; irel++)
1356
0
    {
1357
0
      bfd_vma start, stop;
1358
1359
0
      if (ELF32_R_TYPE (irel->r_info) != (int) R_SH_CODE)
1360
0
  continue;
1361
1362
0
      start = irel->r_offset;
1363
1364
0
      for (irel++; irel < irelend; irel++)
1365
0
  if (ELF32_R_TYPE (irel->r_info) == (int) R_SH_DATA)
1366
0
    break;
1367
0
      if (irel < irelend)
1368
0
  stop = irel->r_offset;
1369
0
      else
1370
0
  stop = sec->size;
1371
1372
0
      if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_elf_swap_insns,
1373
0
             internal_relocs, &label,
1374
0
             label_end, start, stop, pswapped))
1375
0
  goto error_return;
1376
0
    }
1377
1378
0
  free (labels);
1379
1380
0
  return true;
1381
1382
0
 error_return:
1383
0
  free (labels);
1384
0
  return false;
1385
0
}
1386
1387
/* Swap two SH instructions.  This is like sh_swap_insns in coff-sh.c.  */
1388
1389
static bool
1390
sh_elf_swap_insns (bfd *abfd, asection *sec, void *relocs,
1391
       bfd_byte *contents, bfd_vma addr)
1392
0
{
1393
0
  Elf_Internal_Rela *internal_relocs = (Elf_Internal_Rela *) relocs;
1394
0
  unsigned short i1, i2;
1395
0
  Elf_Internal_Rela *irel, *irelend;
1396
1397
  /* Swap the instructions themselves.  */
1398
0
  i1 = bfd_get_16 (abfd, contents + addr);
1399
0
  i2 = bfd_get_16 (abfd, contents + addr + 2);
1400
0
  bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
1401
0
  bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
1402
1403
  /* Adjust all reloc addresses.  */
1404
0
  irelend = internal_relocs + sec->reloc_count;
1405
0
  for (irel = internal_relocs; irel < irelend; irel++)
1406
0
    {
1407
0
      enum elf_sh_reloc_type type;
1408
0
      int add;
1409
1410
      /* There are a few special types of relocs that we don't want to
1411
   adjust.  These relocs do not apply to the instruction itself,
1412
   but are only associated with the address.  */
1413
0
      type = (enum elf_sh_reloc_type) ELF32_R_TYPE (irel->r_info);
1414
0
      if (type == R_SH_ALIGN
1415
0
    || type == R_SH_CODE
1416
0
    || type == R_SH_DATA
1417
0
    || type == R_SH_LABEL)
1418
0
  continue;
1419
1420
      /* If an R_SH_USES reloc points to one of the addresses being
1421
   swapped, we must adjust it.  It would be incorrect to do this
1422
   for a jump, though, since we want to execute both
1423
   instructions after the jump.  (We have avoided swapping
1424
   around a label, so the jump will not wind up executing an
1425
   instruction it shouldn't).  */
1426
0
      if (type == R_SH_USES)
1427
0
  {
1428
0
    bfd_vma off;
1429
1430
0
    off = irel->r_offset + 4 + irel->r_addend;
1431
0
    if (off == addr)
1432
0
      irel->r_addend += 2;
1433
0
    else if (off == addr + 2)
1434
0
      irel->r_addend -= 2;
1435
0
  }
1436
1437
0
      if (irel->r_offset == addr)
1438
0
  {
1439
0
    irel->r_offset += 2;
1440
0
    add = -2;
1441
0
  }
1442
0
      else if (irel->r_offset == addr + 2)
1443
0
  {
1444
0
    irel->r_offset -= 2;
1445
0
    add = 2;
1446
0
  }
1447
0
      else
1448
0
  add = 0;
1449
1450
0
      if (add != 0)
1451
0
  {
1452
0
    bfd_byte *loc;
1453
0
    unsigned short insn, oinsn;
1454
0
    bool overflow;
1455
1456
0
    loc = contents + irel->r_offset;
1457
0
    overflow = false;
1458
0
    switch (type)
1459
0
      {
1460
0
      default:
1461
0
        break;
1462
1463
0
      case R_SH_DIR8WPN:
1464
0
      case R_SH_DIR8WPZ:
1465
0
        insn = bfd_get_16 (abfd, loc);
1466
0
        oinsn = insn;
1467
0
        insn += add / 2;
1468
0
        if ((oinsn & 0xff00) != (insn & 0xff00))
1469
0
    overflow = true;
1470
0
        bfd_put_16 (abfd, (bfd_vma) insn, loc);
1471
0
        break;
1472
1473
0
      case R_SH_IND12W:
1474
0
        insn = bfd_get_16 (abfd, loc);
1475
0
        oinsn = insn;
1476
0
        insn += add / 2;
1477
0
        if ((oinsn & 0xf000) != (insn & 0xf000))
1478
0
    overflow = true;
1479
0
        bfd_put_16 (abfd, (bfd_vma) insn, loc);
1480
0
        break;
1481
1482
0
      case R_SH_DIR8WPL:
1483
        /* This reloc ignores the least significant 3 bits of
1484
     the program counter before adding in the offset.
1485
     This means that if ADDR is at an even address, the
1486
     swap will not affect the offset.  If ADDR is an at an
1487
     odd address, then the instruction will be crossing a
1488
     four byte boundary, and must be adjusted.  */
1489
0
        if ((addr & 3) != 0)
1490
0
    {
1491
0
      insn = bfd_get_16 (abfd, loc);
1492
0
      oinsn = insn;
1493
0
      insn += add / 2;
1494
0
      if ((oinsn & 0xff00) != (insn & 0xff00))
1495
0
        overflow = true;
1496
0
      bfd_put_16 (abfd, (bfd_vma) insn, loc);
1497
0
    }
1498
1499
0
        break;
1500
0
      }
1501
1502
0
    if (overflow)
1503
0
      {
1504
0
        _bfd_error_handler
1505
    /* xgettext:c-format */
1506
0
    (_("%pB: %#" PRIx64 ": fatal: reloc overflow while relaxing"),
1507
0
     abfd, (uint64_t) irel->r_offset);
1508
0
        bfd_set_error (bfd_error_bad_value);
1509
0
        return false;
1510
0
      }
1511
0
  }
1512
0
    }
1513
1514
0
  return true;
1515
0
}
1516

1517
/* Describes one of the various PLT styles.  */
1518
1519
struct elf_sh_plt_info
1520
{
1521
  /* The template for the first PLT entry, or NULL if there is no special
1522
     first entry.  */
1523
  const bfd_byte *plt0_entry;
1524
1525
  /* The size of PLT0_ENTRY in bytes, or 0 if PLT0_ENTRY is NULL.  */
1526
  bfd_vma plt0_entry_size;
1527
1528
  /* Index I is the offset into PLT0_ENTRY of a pointer to
1529
     _GLOBAL_OFFSET_TABLE_ + I * 4.  The value is MINUS_ONE
1530
     if there is no such pointer.  */
1531
  bfd_vma plt0_got_fields[3];
1532
1533
  /* The template for a symbol's PLT entry.  */
1534
  const bfd_byte *symbol_entry;
1535
1536
  /* The size of SYMBOL_ENTRY in bytes.  */
1537
  bfd_vma symbol_entry_size;
1538
1539
  /* Byte offsets of fields in SYMBOL_ENTRY.  Not all fields are used
1540
     on all targets.  The comments by each member indicate the value
1541
     that the field must hold.  */
1542
  struct {
1543
    bfd_vma got_entry; /* the address of the symbol's .got.plt entry */
1544
    bfd_vma plt; /* .plt (or a branch to .plt on VxWorks) */
1545
    bfd_vma reloc_offset; /* the offset of the symbol's JMP_SLOT reloc */
1546
    bool got20; /* TRUE if got_entry points to a movi20 instruction
1547
       (instead of a constant pool entry).  */
1548
  } symbol_fields;
1549
1550
  /* The offset of the resolver stub from the start of SYMBOL_ENTRY.  */
1551
  bfd_vma symbol_resolve_offset;
1552
1553
  /* A different PLT layout which can be used for the first
1554
     MAX_SHORT_PLT entries.  It must share the same plt0.  NULL in
1555
     other cases.  */
1556
  const struct elf_sh_plt_info *short_plt;
1557
};
1558
1559
/* The size in bytes of an entry in the procedure linkage table.  */
1560
1561
#define ELF_PLT_ENTRY_SIZE 28
1562
1563
/* First entry in an absolute procedure linkage table look like this.  */
1564
1565
/* Note - this code has been "optimised" not to use r2.  r2 is used by
1566
   GCC to return the address of large structures, so it should not be
1567
   corrupted here.  This does mean however, that this PLT does not conform
1568
   to the SH PIC ABI.  That spec says that r0 contains the type of the PLT
1569
   and r2 contains the GOT id.  This version stores the GOT id in r0 and
1570
   ignores the type.  Loaders can easily detect this difference however,
1571
   since the type will always be 0 or 8, and the GOT ids will always be
1572
   greater than or equal to 12.  */
1573
static const bfd_byte elf_sh_plt0_entry_be[ELF_PLT_ENTRY_SIZE] =
1574
{
1575
  0xd0, 0x05, /* mov.l 2f,r0 */
1576
  0x60, 0x02, /* mov.l @r0,r0 */
1577
  0x2f, 0x06, /* mov.l r0,@-r15 */
1578
  0xd0, 0x03, /* mov.l 1f,r0 */
1579
  0x60, 0x02, /* mov.l @r0,r0 */
1580
  0x40, 0x2b, /* jmp @r0 */
1581
  0x60, 0xf6, /*  mov.l @r15+,r0 */
1582
  0x00, 0x09, /* nop */
1583
  0x00, 0x09, /* nop */
1584
  0x00, 0x09, /* nop */
1585
  0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8.  */
1586
  0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4.  */
1587
};
1588
1589
static const bfd_byte elf_sh_plt0_entry_le[ELF_PLT_ENTRY_SIZE] =
1590
{
1591
  0x05, 0xd0, /* mov.l 2f,r0 */
1592
  0x02, 0x60, /* mov.l @r0,r0 */
1593
  0x06, 0x2f, /* mov.l r0,@-r15 */
1594
  0x03, 0xd0, /* mov.l 1f,r0 */
1595
  0x02, 0x60, /* mov.l @r0,r0 */
1596
  0x2b, 0x40, /* jmp @r0 */
1597
  0xf6, 0x60, /*  mov.l @r15+,r0 */
1598
  0x09, 0x00, /* nop */
1599
  0x09, 0x00, /* nop */
1600
  0x09, 0x00, /* nop */
1601
  0, 0, 0, 0, /* 1: replaced with address of .got.plt + 8.  */
1602
  0, 0, 0, 0, /* 2: replaced with address of .got.plt + 4.  */
1603
};
1604
1605
/* Sebsequent entries in an absolute procedure linkage table look like
1606
   this.  */
1607
1608
static const bfd_byte elf_sh_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1609
{
1610
  0xd0, 0x04, /* mov.l 1f,r0 */
1611
  0x60, 0x02, /* mov.l @(r0,r12),r0 */
1612
  0xd1, 0x02, /* mov.l 0f,r1 */
1613
  0x40, 0x2b,   /* jmp @r0 */
1614
  0x60, 0x13, /*  mov r1,r0 */
1615
  0xd1, 0x03, /* mov.l 2f,r1 */
1616
  0x40, 0x2b, /* jmp @r0 */
1617
  0x00, 0x09, /* nop */
1618
  0, 0, 0, 0, /* 0: replaced with address of .PLT0.  */
1619
  0, 0, 0, 0, /* 1: replaced with address of this symbol in .got.  */
1620
  0, 0, 0, 0, /* 2: replaced with offset into relocation table.  */
1621
};
1622
1623
static const bfd_byte elf_sh_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1624
{
1625
  0x04, 0xd0, /* mov.l 1f,r0 */
1626
  0x02, 0x60, /* mov.l @r0,r0 */
1627
  0x02, 0xd1, /* mov.l 0f,r1 */
1628
  0x2b, 0x40,   /* jmp @r0 */
1629
  0x13, 0x60, /*  mov r1,r0 */
1630
  0x03, 0xd1, /* mov.l 2f,r1 */
1631
  0x2b, 0x40, /* jmp @r0 */
1632
  0x09, 0x00, /*  nop */
1633
  0, 0, 0, 0, /* 0: replaced with address of .PLT0.  */
1634
  0, 0, 0, 0, /* 1: replaced with address of this symbol in .got.  */
1635
  0, 0, 0, 0, /* 2: replaced with offset into relocation table.  */
1636
};
1637
1638
/* Entries in a PIC procedure linkage table look like this.  */
1639
1640
static const bfd_byte elf_sh_pic_plt_entry_be[ELF_PLT_ENTRY_SIZE] =
1641
{
1642
  0xd0, 0x04, /* mov.l 1f,r0 */
1643
  0x00, 0xce, /* mov.l @(r0,r12),r0 */
1644
  0x40, 0x2b, /* jmp @r0 */
1645
  0x00, 0x09, /*  nop */
1646
  0x50, 0xc2, /* mov.l @(8,r12),r0 */
1647
  0xd1, 0x03, /* mov.l 2f,r1 */
1648
  0x40, 0x2b, /* jmp @r0 */
1649
  0x50, 0xc1, /*  mov.l @(4,r12),r0 */
1650
  0x00, 0x09, /* nop */
1651
  0x00, 0x09, /* nop */
1652
  0, 0, 0, 0, /* 1: replaced with address of this symbol in .got.  */
1653
  0, 0, 0, 0    /* 2: replaced with offset into relocation table.  */
1654
};
1655
1656
static const bfd_byte elf_sh_pic_plt_entry_le[ELF_PLT_ENTRY_SIZE] =
1657
{
1658
  0x04, 0xd0, /* mov.l 1f,r0 */
1659
  0xce, 0x00, /* mov.l @(r0,r12),r0 */
1660
  0x2b, 0x40, /* jmp @r0 */
1661
  0x09, 0x00, /*  nop */
1662
  0xc2, 0x50, /* mov.l @(8,r12),r0 */
1663
  0x03, 0xd1, /* mov.l 2f,r1 */
1664
  0x2b, 0x40, /* jmp @r0 */
1665
  0xc1, 0x50, /*  mov.l @(4,r12),r0 */
1666
  0x09, 0x00, /*  nop */
1667
  0x09, 0x00, /* nop */
1668
  0, 0, 0, 0, /* 1: replaced with address of this symbol in .got.  */
1669
  0, 0, 0, 0    /* 2: replaced with offset into relocation table.  */
1670
};
1671
1672
static const struct elf_sh_plt_info elf_sh_plts[2][2] = {
1673
  {
1674
    {
1675
      /* Big-endian non-PIC.  */
1676
      elf_sh_plt0_entry_be,
1677
      ELF_PLT_ENTRY_SIZE,
1678
      { MINUS_ONE, 24, 20 },
1679
      elf_sh_plt_entry_be,
1680
      ELF_PLT_ENTRY_SIZE,
1681
      { 20, 16, 24, false },
1682
      8,
1683
      NULL
1684
    },
1685
    {
1686
      /* Little-endian non-PIC.  */
1687
      elf_sh_plt0_entry_le,
1688
      ELF_PLT_ENTRY_SIZE,
1689
      { MINUS_ONE, 24, 20 },
1690
      elf_sh_plt_entry_le,
1691
      ELF_PLT_ENTRY_SIZE,
1692
      { 20, 16, 24, false },
1693
      8,
1694
      NULL
1695
    },
1696
  },
1697
  {
1698
    {
1699
      /* Big-endian PIC.  */
1700
      elf_sh_plt0_entry_be,
1701
      ELF_PLT_ENTRY_SIZE,
1702
      { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1703
      elf_sh_pic_plt_entry_be,
1704
      ELF_PLT_ENTRY_SIZE,
1705
      { 20, MINUS_ONE, 24, false },
1706
      8,
1707
      NULL
1708
    },
1709
    {
1710
      /* Little-endian PIC.  */
1711
      elf_sh_plt0_entry_le,
1712
      ELF_PLT_ENTRY_SIZE,
1713
      { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1714
      elf_sh_pic_plt_entry_le,
1715
      ELF_PLT_ENTRY_SIZE,
1716
      { 20, MINUS_ONE, 24, false },
1717
      8,
1718
      NULL
1719
    },
1720
  }
1721
};
1722
1723
#define VXWORKS_PLT_HEADER_SIZE 12
1724
#define VXWORKS_PLT_ENTRY_SIZE 24
1725
1726
static const bfd_byte vxworks_sh_plt0_entry_be[VXWORKS_PLT_HEADER_SIZE] =
1727
{
1728
  0xd1, 0x01, /* mov.l @(8,pc),r1 */
1729
  0x61, 0x12, /* mov.l @r1,r1 */
1730
  0x41, 0x2b, /* jmp @r1 */
1731
  0x00, 0x09, /* nop */
1732
  0, 0, 0, 0  /* 0: replaced with _GLOBAL_OFFSET_TABLE+8.  */
1733
};
1734
1735
static const bfd_byte vxworks_sh_plt0_entry_le[VXWORKS_PLT_HEADER_SIZE] =
1736
{
1737
  0x01, 0xd1, /* mov.l @(8,pc),r1 */
1738
  0x12, 0x61, /* mov.l @r1,r1 */
1739
  0x2b, 0x41, /* jmp @r1 */
1740
  0x09, 0x00, /* nop */
1741
  0, 0, 0, 0  /* 0: replaced with _GLOBAL_OFFSET_TABLE+8.  */
1742
};
1743
1744
static const bfd_byte vxworks_sh_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
1745
{
1746
  0xd0, 0x01, /* mov.l @(8,pc),r0 */
1747
  0x60, 0x02, /* mov.l @r0,r0 */
1748
  0x40, 0x2b, /* jmp @r0 */
1749
  0x00, 0x09, /* nop */
1750
  0, 0, 0, 0, /* 0: replaced with address of this symbol in .got.  */
1751
  0xd0, 0x01, /* mov.l @(8,pc),r0 */
1752
  0xa0, 0x00, /* bra PLT (We need to fix the offset.)  */
1753
  0x00, 0x09, /* nop */
1754
  0x00, 0x09, /* nop */
1755
  0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
1756
};
1757
1758
static const bfd_byte vxworks_sh_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
1759
{
1760
  0x01, 0xd0, /* mov.l @(8,pc),r0 */
1761
  0x02, 0x60, /* mov.l @r0,r0 */
1762
  0x2b, 0x40, /* jmp @r0 */
1763
  0x09, 0x00, /* nop */
1764
  0, 0, 0, 0, /* 0: replaced with address of this symbol in .got.  */
1765
  0x01, 0xd0, /* mov.l @(8,pc),r0 */
1766
  0x00, 0xa0, /* bra PLT (We need to fix the offset.)  */
1767
  0x09, 0x00, /* nop */
1768
  0x09, 0x00, /* nop */
1769
  0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
1770
};
1771
1772
static const bfd_byte vxworks_sh_pic_plt_entry_be[VXWORKS_PLT_ENTRY_SIZE] =
1773
{
1774
  0xd0, 0x01, /* mov.l @(8,pc),r0 */
1775
  0x00, 0xce, /* mov.l @(r0,r12),r0 */
1776
  0x40, 0x2b, /* jmp @r0 */
1777
  0x00, 0x09, /* nop */
1778
  0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got.  */
1779
  0xd0, 0x01, /* mov.l @(8,pc),r0 */
1780
  0x51, 0xc2, /* mov.l @(8,r12),r1 */
1781
  0x41, 0x2b, /* jmp @r1 */
1782
  0x00, 0x09, /* nop */
1783
  0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
1784
};
1785
1786
static const bfd_byte vxworks_sh_pic_plt_entry_le[VXWORKS_PLT_ENTRY_SIZE] =
1787
{
1788
  0x01, 0xd0, /* mov.l @(8,pc),r0 */
1789
  0xce, 0x00, /* mov.l @(r0,r12),r0 */
1790
  0x2b, 0x40, /* jmp @r0 */
1791
  0x09, 0x00, /* nop */
1792
  0, 0, 0, 0, /* 0: replaced with offset of this symbol in .got.  */
1793
  0x01, 0xd0, /* mov.l @(8,pc),r0 */
1794
  0xc2, 0x51, /* mov.l @(8,r12),r1 */
1795
  0x2b, 0x41, /* jmp @r1 */
1796
  0x09, 0x00, /* nop */
1797
  0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
1798
};
1799
1800
static const struct elf_sh_plt_info vxworks_sh_plts[2][2] = {
1801
  {
1802
    {
1803
      /* Big-endian non-PIC.  */
1804
      vxworks_sh_plt0_entry_be,
1805
      VXWORKS_PLT_HEADER_SIZE,
1806
      { MINUS_ONE, MINUS_ONE, 8 },
1807
      vxworks_sh_plt_entry_be,
1808
      VXWORKS_PLT_ENTRY_SIZE,
1809
      { 8, 14, 20, false },
1810
      12,
1811
      NULL
1812
    },
1813
    {
1814
      /* Little-endian non-PIC.  */
1815
      vxworks_sh_plt0_entry_le,
1816
      VXWORKS_PLT_HEADER_SIZE,
1817
      { MINUS_ONE, MINUS_ONE, 8 },
1818
      vxworks_sh_plt_entry_le,
1819
      VXWORKS_PLT_ENTRY_SIZE,
1820
      { 8, 14, 20, false },
1821
      12,
1822
      NULL
1823
    },
1824
  },
1825
  {
1826
    {
1827
      /* Big-endian PIC.  */
1828
      NULL,
1829
      0,
1830
      { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1831
      vxworks_sh_pic_plt_entry_be,
1832
      VXWORKS_PLT_ENTRY_SIZE,
1833
      { 8, MINUS_ONE, 20, false },
1834
      12,
1835
      NULL
1836
    },
1837
    {
1838
      /* Little-endian PIC.  */
1839
      NULL,
1840
      0,
1841
      { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1842
      vxworks_sh_pic_plt_entry_le,
1843
      VXWORKS_PLT_ENTRY_SIZE,
1844
      { 8, MINUS_ONE, 20, false },
1845
      12,
1846
      NULL
1847
    },
1848
  }
1849
};
1850
1851
/* FDPIC PLT entries.  Two unimplemented optimizations for lazy
1852
   binding are to omit the lazy binding stub when linking with -z now
1853
   and to move lazy binding stubs into a separate region for better
1854
   cache behavior.  */
1855
1856
#define FDPIC_PLT_ENTRY_SIZE 28
1857
#define FDPIC_PLT_LAZY_OFFSET 20
1858
1859
/* FIXME: The lazy binding stub requires a plt0 - which may need to be
1860
   duplicated if it is out of range, or which can be inlined.  So
1861
   right now it is always inlined, which wastes a word per stub.  It
1862
   might be easier to handle the duplication if we put the lazy
1863
   stubs separately.  */
1864
1865
static const bfd_byte fdpic_sh_plt_entry_be[FDPIC_PLT_ENTRY_SIZE] =
1866
{
1867
  0xd0, 0x02, /* mov.l @(12,pc),r0 */
1868
  0x01, 0xce, /* mov.l @(r0,r12),r1 */
1869
  0x70, 0x04, /* add #4, r0 */
1870
  0x41, 0x2b, /* jmp @r1 */
1871
  0x0c, 0xce, /* mov.l @(r0,r12),r12 */
1872
  0x00, 0x09, /* nop */
1873
  0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
1874
  0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
1875
  0x60, 0xc2, /* mov.l @r12,r0 */
1876
  0x40, 0x2b, /* jmp @r0 */
1877
  0x53, 0xc1, /*  mov.l @(4,r12),r3 */
1878
  0x00, 0x09, /* nop */
1879
};
1880
1881
static const bfd_byte fdpic_sh_plt_entry_le[FDPIC_PLT_ENTRY_SIZE] =
1882
{
1883
  0x02, 0xd0, /* mov.l @(12,pc),r0 */
1884
  0xce, 0x01, /* mov.l @(r0,r12),r1 */
1885
  0x04, 0x70, /* add #4, r0 */
1886
  0x2b, 0x41, /* jmp @r1 */
1887
  0xce, 0x0c, /* mov.l @(r0,r12),r12 */
1888
  0x09, 0x00, /* nop */
1889
  0, 0, 0, 0, /* 0: replaced with offset of this symbol's funcdesc */
1890
  0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
1891
  0xc2, 0x60, /* mov.l @r12,r0 */
1892
  0x2b, 0x40, /* jmp @r0 */
1893
  0xc1, 0x53, /*  mov.l @(4,r12),r3 */
1894
  0x09, 0x00, /* nop */
1895
};
1896
1897
static const struct elf_sh_plt_info fdpic_sh_plts[2] = {
1898
  {
1899
    /* Big-endian PIC.  */
1900
    NULL,
1901
    0,
1902
    { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1903
    fdpic_sh_plt_entry_be,
1904
    FDPIC_PLT_ENTRY_SIZE,
1905
    { 12, MINUS_ONE, 16, false },
1906
    FDPIC_PLT_LAZY_OFFSET,
1907
    NULL
1908
  },
1909
  {
1910
    /* Little-endian PIC.  */
1911
    NULL,
1912
    0,
1913
    { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1914
    fdpic_sh_plt_entry_le,
1915
    FDPIC_PLT_ENTRY_SIZE,
1916
    { 12, MINUS_ONE, 16, false },
1917
    FDPIC_PLT_LAZY_OFFSET,
1918
    NULL
1919
  },
1920
};
1921
1922
/* On SH2A, we can use the movi20 instruction to generate shorter PLT
1923
   entries for the first 64K slots.  We use the normal FDPIC PLT entry
1924
   past that point; we could also use movi20s, which might be faster,
1925
   but would not be any smaller.  */
1926
1927
#define FDPIC_SH2A_PLT_ENTRY_SIZE 24
1928
#define FDPIC_SH2A_PLT_LAZY_OFFSET 16
1929
1930
static const bfd_byte fdpic_sh2a_plt_entry_be[FDPIC_SH2A_PLT_ENTRY_SIZE] =
1931
{
1932
  0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
1933
  0x01, 0xce, /* mov.l @(r0,r12),r1 */
1934
  0x70, 0x04, /* add #4, r0 */
1935
  0x41, 0x2b, /* jmp @r1 */
1936
  0x0c, 0xce, /* mov.l @(r0,r12),r12 */
1937
  0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
1938
  0x60, 0xc2, /* mov.l @r12,r0 */
1939
  0x40, 0x2b, /* jmp @r0 */
1940
  0x53, 0xc1, /*  mov.l @(4,r12),r3 */
1941
  0x00, 0x09, /* nop */
1942
};
1943
1944
static const bfd_byte fdpic_sh2a_plt_entry_le[FDPIC_SH2A_PLT_ENTRY_SIZE] =
1945
{
1946
  0, 0, 0, 0, /* movi20 #gotofffuncdesc,r0 */
1947
  0xce, 0x01, /* mov.l @(r0,r12),r1 */
1948
  0x04, 0x70, /* add #4, r0 */
1949
  0x2b, 0x41, /* jmp @r1 */
1950
  0xce, 0x0c, /* mov.l @(r0,r12),r12 */
1951
  0, 0, 0, 0, /* 1: replaced with offset into relocation table.  */
1952
  0xc2, 0x60, /* mov.l @r12,r0 */
1953
  0x2b, 0x40, /* jmp @r0 */
1954
  0xc1, 0x53, /*  mov.l @(4,r12),r3 */
1955
  0x09, 0x00, /* nop */
1956
};
1957
1958
static const struct elf_sh_plt_info fdpic_sh2a_short_plt_be = {
1959
  /* Big-endian FDPIC, max index 64K.  */
1960
  NULL,
1961
  0,
1962
  { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1963
  fdpic_sh2a_plt_entry_be,
1964
  FDPIC_SH2A_PLT_ENTRY_SIZE,
1965
  { 0, MINUS_ONE, 12, true },
1966
  FDPIC_SH2A_PLT_LAZY_OFFSET,
1967
  NULL
1968
};
1969
1970
static const struct elf_sh_plt_info fdpic_sh2a_short_plt_le = {
1971
  /* Little-endian FDPIC, max index 64K.  */
1972
  NULL,
1973
  0,
1974
  { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1975
  fdpic_sh2a_plt_entry_le,
1976
  FDPIC_SH2A_PLT_ENTRY_SIZE,
1977
  { 0, MINUS_ONE, 12, true },
1978
  FDPIC_SH2A_PLT_LAZY_OFFSET,
1979
  NULL
1980
};
1981
1982
static const struct elf_sh_plt_info fdpic_sh2a_plts[2] = {
1983
  {
1984
    /* Big-endian PIC.  */
1985
    NULL,
1986
    0,
1987
    { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1988
    fdpic_sh_plt_entry_be,
1989
    FDPIC_PLT_ENTRY_SIZE,
1990
    { 12, MINUS_ONE, 16, false },
1991
    FDPIC_PLT_LAZY_OFFSET,
1992
    &fdpic_sh2a_short_plt_be
1993
  },
1994
  {
1995
    /* Little-endian PIC.  */
1996
    NULL,
1997
    0,
1998
    { MINUS_ONE, MINUS_ONE, MINUS_ONE },
1999
    fdpic_sh_plt_entry_le,
2000
    FDPIC_PLT_ENTRY_SIZE,
2001
    { 12, MINUS_ONE, 16, false },
2002
    FDPIC_PLT_LAZY_OFFSET,
2003
    &fdpic_sh2a_short_plt_le
2004
  },
2005
};
2006
2007
/* Return the type of PLT associated with ABFD.  PIC_P is true if
2008
   the object is position-independent.  */
2009
2010
static const struct elf_sh_plt_info *
2011
get_plt_info (bfd *abfd, bool pic_p)
2012
0
{
2013
0
  if (fdpic_object_p (abfd))
2014
0
    {
2015
      /* If any input file requires SH2A we can use a shorter PLT
2016
   sequence.  */
2017
0
      if (sh_get_arch_from_bfd_mach (bfd_get_mach (abfd)) & arch_sh2a_base)
2018
0
  return &fdpic_sh2a_plts[!bfd_big_endian (abfd)];
2019
0
      else
2020
0
  return &fdpic_sh_plts[!bfd_big_endian (abfd)];
2021
0
    }
2022
0
  if (vxworks_object_p (abfd))
2023
0
    return &vxworks_sh_plts[pic_p][!bfd_big_endian (abfd)];
2024
0
  return &elf_sh_plts[pic_p][!bfd_big_endian (abfd)];
2025
0
}
2026
2027
/* Install a 32-bit PLT field starting at ADDR, which occurs in OUTPUT_BFD.
2028
   VALUE is the field's value and CODE_P is true if VALUE refers to code,
2029
   not data.  */
2030
2031
inline static void
2032
install_plt_field (bfd *output_bfd, bool code_p ATTRIBUTE_UNUSED,
2033
       unsigned long value, bfd_byte *addr)
2034
0
{
2035
0
  bfd_put_32 (output_bfd, value, addr);
2036
0
}
2037
2038
/* The number of PLT entries which can use a shorter PLT, if any.
2039
   Currently always 64K, since only SH-2A FDPIC uses this; a
2040
   20-bit movi20 can address that many function descriptors below
2041
   _GLOBAL_OFFSET_TABLE_.  */
2042
0
#define MAX_SHORT_PLT 65536
2043
2044
/* Return the index of the PLT entry at byte offset OFFSET.  */
2045
2046
static bfd_vma
2047
get_plt_index (const struct elf_sh_plt_info *info, bfd_vma offset)
2048
0
{
2049
0
  bfd_vma plt_index = 0;
2050
2051
0
  offset -= info->plt0_entry_size;
2052
0
  if (info->short_plt != NULL)
2053
0
    {
2054
0
      if (offset > MAX_SHORT_PLT * info->short_plt->symbol_entry_size)
2055
0
  {
2056
0
    plt_index = MAX_SHORT_PLT;
2057
0
    offset -= MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2058
0
  }
2059
0
      else
2060
0
  info = info->short_plt;
2061
0
    }
2062
0
  return plt_index + offset / info->symbol_entry_size;
2063
0
}
2064
2065
/* Do the inverse operation.  */
2066
2067
static bfd_vma
2068
get_plt_offset (const struct elf_sh_plt_info *info, bfd_vma plt_index)
2069
0
{
2070
0
  bfd_vma offset = 0;
2071
2072
0
  if (info->short_plt != NULL)
2073
0
    {
2074
0
      if (plt_index > MAX_SHORT_PLT)
2075
0
  {
2076
0
    offset = MAX_SHORT_PLT * info->short_plt->symbol_entry_size;
2077
0
    plt_index -= MAX_SHORT_PLT;
2078
0
  }
2079
0
      else
2080
0
  info = info->short_plt;
2081
0
    }
2082
0
  return (offset + info->plt0_entry_size
2083
0
    + (plt_index * info->symbol_entry_size));
2084
0
}
2085
2086
union gotref
2087
{
2088
  bfd_signed_vma refcount;
2089
  bfd_vma offset;
2090
};
2091
2092
/* sh ELF linker hash entry.  */
2093
2094
struct elf_sh_link_hash_entry
2095
{
2096
  struct elf_link_hash_entry root;
2097
2098
  bfd_signed_vma gotplt_refcount;
2099
2100
  /* A local function descriptor, for FDPIC.  The refcount counts
2101
     R_SH_FUNCDESC, R_SH_GOTOFFFUNCDESC, and R_SH_GOTOFFFUNCDESC20
2102
     relocations; the PLT and GOT entry are accounted
2103
     for separately.  After adjust_dynamic_symbol, the offset is
2104
     MINUS_ONE if there is no local descriptor (dynamic linker
2105
     managed and no PLT entry, or undefined weak non-dynamic).
2106
     During check_relocs we do not yet know whether the local
2107
     descriptor will be canonical.  */
2108
  union gotref funcdesc;
2109
2110
  /* How many of the above refcounted relocations were R_SH_FUNCDESC,
2111
     and thus require fixups or relocations.  */
2112
  bfd_signed_vma abs_funcdesc_refcount;
2113
2114
  enum got_type {
2115
    GOT_UNKNOWN = 0, GOT_NORMAL, GOT_TLS_GD, GOT_TLS_IE, GOT_FUNCDESC
2116
  } got_type;
2117
};
2118
2119
0
#define sh_elf_hash_entry(ent) ((struct elf_sh_link_hash_entry *)(ent))
2120
2121
struct sh_elf_obj_tdata
2122
{
2123
  struct elf_obj_tdata root;
2124
2125
  /* got_type for each local got entry.  */
2126
  char *local_got_type;
2127
2128
  /* Function descriptor refcount and offset for each local symbol.  */
2129
  union gotref *local_funcdesc;
2130
};
2131
2132
#define sh_elf_tdata(abfd) \
2133
0
  ((struct sh_elf_obj_tdata *) (abfd)->tdata.any)
2134
2135
#define sh_elf_local_got_type(abfd) \
2136
0
  (sh_elf_tdata (abfd)->local_got_type)
2137
2138
#define sh_elf_local_funcdesc(abfd) \
2139
0
  (sh_elf_tdata (abfd)->local_funcdesc)
2140
2141
#define is_sh_elf(bfd) \
2142
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour \
2143
0
   && elf_tdata (bfd) != NULL \
2144
0
   && elf_object_id (bfd) == SH_ELF_DATA)
2145
2146
/* Override the generic function because we need to store sh_elf_obj_tdata
2147
   as the specific tdata.  */
2148
2149
static bool
2150
sh_elf_mkobject (bfd *abfd)
2151
137k
{
2152
137k
  return bfd_elf_allocate_object (abfd, sizeof (struct sh_elf_obj_tdata));
2153
137k
}
2154
2155
/* sh ELF linker hash table.  */
2156
2157
struct elf_sh_link_hash_table
2158
{
2159
  struct elf_link_hash_table root;
2160
2161
  /* Short-cuts to get to dynamic linker sections.  */
2162
  asection *sfuncdesc;
2163
  asection *srelfuncdesc;
2164
  asection *srofixup;
2165
2166
  /* The (unloaded but important) VxWorks .rela.plt.unloaded section.  */
2167
  asection *srelplt2;
2168
2169
  /* A counter or offset to track a TLS got entry.  */
2170
  union
2171
    {
2172
      bfd_signed_vma refcount;
2173
      bfd_vma offset;
2174
    } tls_ldm_got;
2175
2176
  /* The type of PLT to use.  */
2177
  const struct elf_sh_plt_info *plt_info;
2178
2179
  /* True if the target system uses FDPIC.  */
2180
  bool fdpic_p;
2181
};
2182
2183
/* Traverse an sh ELF linker hash table.  */
2184
2185
#define sh_elf_link_hash_traverse(table, func, info)      \
2186
  (elf_link_hash_traverse           \
2187
   (&(table)->root,             \
2188
    (bool (*) (struct elf_link_hash_entry *, void *)) (func),   \
2189
    (info)))
2190
2191
/* Get the sh ELF linker hash table from a link_info structure.  */
2192
2193
#define sh_elf_hash_table(p) \
2194
0
  ((is_elf_hash_table ((p)->hash)          \
2195
0
    && elf_hash_table_id (elf_hash_table (p)) == SH_ELF_DATA)   \
2196
0
   ? (struct elf_sh_link_hash_table *) (p)->hash : NULL)
2197
2198
/* Create an entry in an sh ELF linker hash table.  */
2199
2200
static struct bfd_hash_entry *
2201
sh_elf_link_hash_newfunc (struct bfd_hash_entry *entry,
2202
        struct bfd_hash_table *table,
2203
        const char *string)
2204
0
{
2205
0
  struct elf_sh_link_hash_entry *ret =
2206
0
    (struct elf_sh_link_hash_entry *) entry;
2207
2208
  /* Allocate the structure if it has not already been allocated by a
2209
     subclass.  */
2210
0
  if (ret == (struct elf_sh_link_hash_entry *) NULL)
2211
0
    ret = ((struct elf_sh_link_hash_entry *)
2212
0
     bfd_hash_allocate (table,
2213
0
            sizeof (struct elf_sh_link_hash_entry)));
2214
0
  if (ret == (struct elf_sh_link_hash_entry *) NULL)
2215
0
    return (struct bfd_hash_entry *) ret;
2216
2217
  /* Call the allocation method of the superclass.  */
2218
0
  ret = ((struct elf_sh_link_hash_entry *)
2219
0
   _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2220
0
             table, string));
2221
0
  if (ret != (struct elf_sh_link_hash_entry *) NULL)
2222
0
    {
2223
0
      ret->gotplt_refcount = 0;
2224
0
      ret->funcdesc.refcount = 0;
2225
0
      ret->abs_funcdesc_refcount = 0;
2226
0
      ret->got_type = GOT_UNKNOWN;
2227
0
    }
2228
2229
0
  return (struct bfd_hash_entry *) ret;
2230
0
}
2231
2232
/* Create an sh ELF linker hash table.  */
2233
2234
static struct bfd_link_hash_table *
2235
sh_elf_link_hash_table_create (bfd *abfd)
2236
0
{
2237
0
  struct elf_sh_link_hash_table *ret;
2238
0
  size_t amt = sizeof (struct elf_sh_link_hash_table);
2239
2240
0
  ret = (struct elf_sh_link_hash_table *) bfd_zmalloc (amt);
2241
0
  if (ret == (struct elf_sh_link_hash_table *) NULL)
2242
0
    return NULL;
2243
2244
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
2245
0
              sh_elf_link_hash_newfunc,
2246
0
              sizeof (struct elf_sh_link_hash_entry)))
2247
0
    {
2248
0
      free (ret);
2249
0
      return NULL;
2250
0
    }
2251
2252
0
  if (fdpic_object_p (abfd))
2253
0
    {
2254
0
      ret->root.dt_pltgot_required = true;
2255
0
      ret->fdpic_p = true;
2256
0
    }
2257
2258
0
  return &ret->root.root;
2259
0
}
2260
2261
static bool
2262
sh_elf_omit_section_dynsym (bfd *output_bfd ATTRIBUTE_UNUSED,
2263
          struct bfd_link_info *info, asection *p)
2264
0
{
2265
0
  struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
2266
2267
  /* Non-FDPIC binaries do not need dynamic symbols for sections.  */
2268
0
  if (!htab->fdpic_p)
2269
0
    return true;
2270
2271
  /* We need dynamic symbols for every section, since segments can
2272
     relocate independently.  */
2273
0
  switch (elf_section_data (p)->this_hdr.sh_type)
2274
0
    {
2275
0
    case SHT_PROGBITS:
2276
0
    case SHT_NOBITS:
2277
      /* If sh_type is yet undecided, assume it could be
2278
   SHT_PROGBITS/SHT_NOBITS.  */
2279
0
    case SHT_NULL:
2280
0
      return false;
2281
2282
      /* There shouldn't be section relative relocations
2283
   against any other section.  */
2284
0
    default:
2285
0
      return true;
2286
0
    }
2287
0
}
2288
2289
/* Create .got, .gotplt, and .rela.got sections in DYNOBJ, and set up
2290
   shortcuts to them in our hash table.  */
2291
2292
static bool
2293
create_got_section (bfd *dynobj, struct bfd_link_info *info)
2294
0
{
2295
0
  struct elf_sh_link_hash_table *htab;
2296
2297
0
  if (! _bfd_elf_create_got_section (dynobj, info))
2298
0
    return false;
2299
2300
0
  htab = sh_elf_hash_table (info);
2301
0
  if (htab == NULL)
2302
0
    return false;
2303
2304
0
  htab->sfuncdesc = bfd_make_section_anyway_with_flags (dynobj, ".got.funcdesc",
2305
0
              (SEC_ALLOC | SEC_LOAD
2306
0
               | SEC_HAS_CONTENTS
2307
0
               | SEC_IN_MEMORY
2308
0
               | SEC_LINKER_CREATED));
2309
0
  if (htab->sfuncdesc == NULL
2310
0
      || !bfd_set_section_alignment (htab->sfuncdesc, 2))
2311
0
    return false;
2312
2313
0
  htab->srelfuncdesc = bfd_make_section_anyway_with_flags (dynobj,
2314
0
                 ".rela.got.funcdesc",
2315
0
                 (SEC_ALLOC | SEC_LOAD
2316
0
                  | SEC_HAS_CONTENTS
2317
0
                  | SEC_IN_MEMORY
2318
0
                  | SEC_LINKER_CREATED
2319
0
                  | SEC_READONLY));
2320
0
  if (htab->srelfuncdesc == NULL
2321
0
      || !bfd_set_section_alignment (htab->srelfuncdesc, 2))
2322
0
    return false;
2323
2324
  /* Also create .rofixup.  */
2325
0
  htab->srofixup = bfd_make_section_anyway_with_flags (dynobj, ".rofixup",
2326
0
                   (SEC_ALLOC | SEC_LOAD
2327
0
              | SEC_HAS_CONTENTS
2328
0
              | SEC_IN_MEMORY
2329
0
              | SEC_LINKER_CREATED
2330
0
              | SEC_READONLY));
2331
0
  if (htab->srofixup == NULL
2332
0
      || !bfd_set_section_alignment (htab->srofixup, 2))
2333
0
    return false;
2334
2335
0
  return true;
2336
0
}
2337
2338
/* Create dynamic sections when linking against a dynamic object.  */
2339
2340
static bool
2341
sh_elf_create_dynamic_sections (bfd *abfd, struct bfd_link_info *info)
2342
0
{
2343
0
  struct elf_sh_link_hash_table *htab;
2344
0
  flagword flags, pltflags;
2345
0
  asection *s;
2346
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
2347
0
  int ptralign = 0;
2348
2349
0
  switch (bed->s->arch_size)
2350
0
    {
2351
0
    case 32:
2352
0
      ptralign = 2;
2353
0
      break;
2354
2355
0
    case 64:
2356
0
      ptralign = 3;
2357
0
      break;
2358
2359
0
    default:
2360
0
      bfd_set_error (bfd_error_bad_value);
2361
0
      return false;
2362
0
    }
2363
2364
0
  htab = sh_elf_hash_table (info);
2365
0
  if (htab == NULL)
2366
0
    return false;
2367
2368
0
  if (htab->root.dynamic_sections_created)
2369
0
    return true;
2370
2371
  /* We need to create .plt, .rel[a].plt, .got, .got.plt, .dynbss, and
2372
     .rel[a].bss sections.  */
2373
2374
0
  flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS | SEC_IN_MEMORY
2375
0
     | SEC_LINKER_CREATED);
2376
2377
0
  pltflags = flags;
2378
0
  pltflags |= SEC_CODE;
2379
0
  if (bed->plt_not_loaded)
2380
0
    pltflags &= ~ (SEC_LOAD | SEC_HAS_CONTENTS);
2381
0
  if (bed->plt_readonly)
2382
0
    pltflags |= SEC_READONLY;
2383
2384
0
  s = bfd_make_section_anyway_with_flags (abfd, ".plt", pltflags);
2385
0
  htab->root.splt = s;
2386
0
  if (s == NULL
2387
0
      || !bfd_set_section_alignment (s, bed->plt_alignment))
2388
0
    return false;
2389
2390
0
  if (bed->want_plt_sym)
2391
0
    {
2392
      /* Define the symbol _PROCEDURE_LINKAGE_TABLE_ at the start of the
2393
   .plt section.  */
2394
0
      struct elf_link_hash_entry *h;
2395
0
      struct bfd_link_hash_entry *bh = NULL;
2396
2397
0
      if (! (_bfd_generic_link_add_one_symbol
2398
0
       (info, abfd, "_PROCEDURE_LINKAGE_TABLE_", BSF_GLOBAL, s,
2399
0
        (bfd_vma) 0, (const char *) NULL, false,
2400
0
        get_elf_backend_data (abfd)->collect, &bh)))
2401
0
  return false;
2402
2403
0
      h = (struct elf_link_hash_entry *) bh;
2404
0
      h->def_regular = 1;
2405
0
      h->type = STT_OBJECT;
2406
0
      htab->root.hplt = h;
2407
2408
0
      if (bfd_link_pic (info)
2409
0
    && ! bfd_elf_link_record_dynamic_symbol (info, h))
2410
0
  return false;
2411
0
    }
2412
2413
0
  s = bfd_make_section_anyway_with_flags (abfd,
2414
0
            bed->default_use_rela_p
2415
0
            ? ".rela.plt" : ".rel.plt",
2416
0
            flags | SEC_READONLY);
2417
0
  htab->root.srelplt = s;
2418
0
  if (s == NULL
2419
0
      || !bfd_set_section_alignment (s, ptralign))
2420
0
    return false;
2421
2422
0
  if (htab->root.sgot == NULL
2423
0
      && !create_got_section (abfd, info))
2424
0
    return false;
2425
2426
0
  if (bed->want_dynbss)
2427
0
    {
2428
      /* The .dynbss section is a place to put symbols which are defined
2429
   by dynamic objects, are referenced by regular objects, and are
2430
   not functions.  We must allocate space for them in the process
2431
   image and use a R_*_COPY reloc to tell the dynamic linker to
2432
   initialize them at run time.  The linker script puts the .dynbss
2433
   section into the .bss section of the final image.  */
2434
0
      s = bfd_make_section_anyway_with_flags (abfd, ".dynbss",
2435
0
                SEC_ALLOC | SEC_LINKER_CREATED);
2436
0
      htab->root.sdynbss = s;
2437
0
      if (s == NULL)
2438
0
  return false;
2439
2440
      /* The .rel[a].bss section holds copy relocs.  This section is not
2441
   normally needed.  We need to create it here, though, so that the
2442
   linker will map it to an output section.  We can't just create it
2443
   only if we need it, because we will not know whether we need it
2444
   until we have seen all the input files, and the first time the
2445
   main linker code calls BFD after examining all the input files
2446
   (size_dynamic_sections) the input sections have already been
2447
   mapped to the output sections.  If the section turns out not to
2448
   be needed, we can discard it later.  We will never need this
2449
   section when generating a shared object, since they do not use
2450
   copy relocs.  */
2451
0
      if (! bfd_link_pic (info))
2452
0
  {
2453
0
    s = bfd_make_section_anyway_with_flags (abfd,
2454
0
              (bed->default_use_rela_p
2455
0
               ? ".rela.bss" : ".rel.bss"),
2456
0
              flags | SEC_READONLY);
2457
0
    htab->root.srelbss = s;
2458
0
    if (s == NULL
2459
0
        || !bfd_set_section_alignment (s, ptralign))
2460
0
      return false;
2461
0
  }
2462
0
    }
2463
2464
0
#ifdef OBJ_MAYBE_ELF_VXWORKS
2465
0
  if (htab->root.target_os == is_vxworks)
2466
0
    {
2467
0
      if (!elf_vxworks_create_dynamic_sections (abfd, info, &htab->srelplt2))
2468
0
  return false;
2469
0
    }
2470
0
#endif /* OBJ_MAYBE_ELF_VXWORKS */
2471
2472
0
  return true;
2473
0
}
2474

2475
/* Adjust a symbol defined by a dynamic object and referenced by a
2476
   regular object.  The current definition is in some section of the
2477
   dynamic object, but we're not including those sections.  We have to
2478
   change the definition to something the rest of the link can
2479
   understand.  */
2480
2481
static bool
2482
sh_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
2483
            struct elf_link_hash_entry *h)
2484
0
{
2485
0
  struct elf_sh_link_hash_table *htab;
2486
0
  asection *s;
2487
2488
0
  htab = sh_elf_hash_table (info);
2489
0
  if (htab == NULL)
2490
0
    return false;
2491
2492
  /* Make sure we know what is going on here.  */
2493
0
  BFD_ASSERT (htab->root.dynobj != NULL
2494
0
        && (h->needs_plt
2495
0
      || h->is_weakalias
2496
0
      || (h->def_dynamic
2497
0
          && h->ref_regular
2498
0
          && !h->def_regular)));
2499
2500
  /* If this is a function, put it in the procedure linkage table.  We
2501
     will fill in the contents of the procedure linkage table later,
2502
     when we know the address of the .got section.  */
2503
0
  if (h->type == STT_FUNC
2504
0
      || h->needs_plt)
2505
0
    {
2506
0
      if (h->plt.refcount <= 0
2507
0
    || SYMBOL_CALLS_LOCAL (info, h)
2508
0
    || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2509
0
        && h->root.type == bfd_link_hash_undefweak))
2510
0
  {
2511
    /* This case can occur if we saw a PLT reloc in an input
2512
       file, but the symbol was never referred to by a dynamic
2513
       object.  In such a case, we don't actually need to build
2514
       a procedure linkage table, and we can just do a REL32
2515
       reloc instead.  */
2516
0
    h->plt.offset = (bfd_vma) -1;
2517
0
    h->needs_plt = 0;
2518
0
  }
2519
2520
0
      return true;
2521
0
    }
2522
0
  else
2523
0
    h->plt.offset = (bfd_vma) -1;
2524
2525
  /* If this is a weak symbol, and there is a real definition, the
2526
     processor independent code will have arranged for us to see the
2527
     real definition first, and we can just use the same value.  */
2528
0
  if (h->is_weakalias)
2529
0
    {
2530
0
      struct elf_link_hash_entry *def = weakdef (h);
2531
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2532
0
      h->root.u.def.section = def->root.u.def.section;
2533
0
      h->root.u.def.value = def->root.u.def.value;
2534
0
      if (info->nocopyreloc)
2535
0
  h->non_got_ref = def->non_got_ref;
2536
0
      return true;
2537
0
    }
2538
2539
  /* This is a reference to a symbol defined by a dynamic object which
2540
     is not a function.  */
2541
2542
  /* If we are creating a shared library, we must presume that the
2543
     only references to the symbol are via the global offset table.
2544
     For such cases we need not do anything here; the relocations will
2545
     be handled correctly by relocate_section.  */
2546
0
  if (bfd_link_pic (info))
2547
0
    return true;
2548
2549
  /* If there are no references to this symbol that do not use the
2550
     GOT, we don't need to generate a copy reloc.  */
2551
0
  if (!h->non_got_ref)
2552
0
    return true;
2553
2554
  /* If -z nocopyreloc was given, we won't generate them either.  */
2555
0
  if (0 && info->nocopyreloc)
2556
0
    {
2557
0
      h->non_got_ref = 0;
2558
0
      return true;
2559
0
    }
2560
2561
  /* If we don't find any dynamic relocs in read-only sections, then
2562
     we'll be keeping the dynamic relocs and avoiding the copy reloc.  */
2563
0
  if (0 && !_bfd_elf_readonly_dynrelocs (h))
2564
0
    {
2565
0
      h->non_got_ref = 0;
2566
0
      return true;
2567
0
    }
2568
2569
  /* We must allocate the symbol in our .dynbss section, which will
2570
     become part of the .bss section of the executable.  There will be
2571
     an entry for this symbol in the .dynsym section.  The dynamic
2572
     object will contain position independent code, so all references
2573
     from the dynamic object to this symbol will go through the global
2574
     offset table.  The dynamic linker will use the .dynsym entry to
2575
     determine the address it must put in the global offset table, so
2576
     both the dynamic object and the regular object will refer to the
2577
     same memory location for the variable.  */
2578
2579
0
  s = htab->root.sdynbss;
2580
0
  BFD_ASSERT (s != NULL);
2581
2582
  /* We must generate a R_SH_COPY reloc to tell the dynamic linker to
2583
     copy the initial value out of the dynamic object and into the
2584
     runtime process image.  We need to remember the offset into the
2585
     .rela.bss section we are going to use.  */
2586
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
2587
0
    {
2588
0
      asection *srel;
2589
2590
0
      srel = htab->root.srelbss;
2591
0
      BFD_ASSERT (srel != NULL);
2592
0
      srel->size += sizeof (Elf32_External_Rela);
2593
0
      h->needs_copy = 1;
2594
0
    }
2595
2596
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
2597
0
}
2598
2599
/* Allocate space in .plt, .got and associated reloc sections for
2600
   dynamic relocs.  */
2601
2602
static bool
2603
allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
2604
0
{
2605
0
  struct bfd_link_info *info;
2606
0
  struct elf_sh_link_hash_table *htab;
2607
0
  struct elf_sh_link_hash_entry *eh;
2608
0
  struct elf_dyn_relocs *p;
2609
2610
0
  if (h->root.type == bfd_link_hash_indirect)
2611
0
    return true;
2612
2613
0
  info = (struct bfd_link_info *) inf;
2614
0
  htab = sh_elf_hash_table (info);
2615
0
  if (htab == NULL)
2616
0
    return false;
2617
2618
0
  eh = (struct elf_sh_link_hash_entry *) h;
2619
0
  if ((h->got.refcount > 0
2620
0
       || h->forced_local)
2621
0
      && eh->gotplt_refcount > 0)
2622
0
    {
2623
      /* The symbol has been forced local, or we have some direct got refs,
2624
   so treat all the gotplt refs as got refs. */
2625
0
      h->got.refcount += eh->gotplt_refcount;
2626
0
      if (h->plt.refcount >= eh->gotplt_refcount)
2627
0
  h->plt.refcount -= eh->gotplt_refcount;
2628
0
    }
2629
2630
0
  if (htab->root.dynamic_sections_created
2631
0
      && h->plt.refcount > 0
2632
0
      && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2633
0
    || h->root.type != bfd_link_hash_undefweak))
2634
0
    {
2635
      /* Make sure this symbol is output as a dynamic symbol.
2636
   Undefined weak syms won't yet be marked as dynamic.  */
2637
0
      if (h->dynindx == -1
2638
0
    && !h->forced_local)
2639
0
  {
2640
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
2641
0
      return false;
2642
0
  }
2643
2644
0
      if (bfd_link_pic (info)
2645
0
    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2646
0
  {
2647
0
    asection *s = htab->root.splt;
2648
0
    const struct elf_sh_plt_info *plt_info;
2649
2650
    /* If this is the first .plt entry, make room for the special
2651
       first entry.  */
2652
0
    if (s->size == 0)
2653
0
      s->size += htab->plt_info->plt0_entry_size;
2654
2655
0
    h->plt.offset = s->size;
2656
2657
    /* If this symbol is not defined in a regular file, and we are
2658
       not generating a shared library, then set the symbol to this
2659
       location in the .plt.  This is required to make function
2660
       pointers compare as equal between the normal executable and
2661
       the shared library.  Skip this for FDPIC, since the
2662
       function's address will be the address of the canonical
2663
       function descriptor.  */
2664
0
    if (!htab->fdpic_p && !bfd_link_pic (info) && !h->def_regular)
2665
0
      {
2666
0
        h->root.u.def.section = s;
2667
0
        h->root.u.def.value = h->plt.offset;
2668
0
      }
2669
2670
    /* Make room for this entry.  */
2671
0
    plt_info = htab->plt_info;
2672
0
    if (plt_info->short_plt != NULL
2673
0
        && (get_plt_index (plt_info->short_plt, s->size) < MAX_SHORT_PLT))
2674
0
      plt_info = plt_info->short_plt;
2675
0
    s->size += plt_info->symbol_entry_size;
2676
2677
    /* We also need to make an entry in the .got.plt section, which
2678
       will be placed in the .got section by the linker script.  */
2679
0
    if (!htab->fdpic_p)
2680
0
      htab->root.sgotplt->size += 4;
2681
0
    else
2682
0
      htab->root.sgotplt->size += 8;
2683
2684
    /* We also need to make an entry in the .rel.plt section.  */
2685
0
    htab->root.srelplt->size += sizeof (Elf32_External_Rela);
2686
2687
0
    if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
2688
0
      {
2689
        /* VxWorks executables have a second set of relocations
2690
     for each PLT entry.  They go in a separate relocation
2691
     section, which is processed by the kernel loader.  */
2692
2693
        /* There is a relocation for the initial PLT entry:
2694
     an R_SH_DIR32 relocation for _GLOBAL_OFFSET_TABLE_.  */
2695
0
        if (h->plt.offset == htab->plt_info->plt0_entry_size)
2696
0
    htab->srelplt2->size += sizeof (Elf32_External_Rela);
2697
2698
        /* There are two extra relocations for each subsequent
2699
     PLT entry: an R_SH_DIR32 relocation for the GOT entry,
2700
     and an R_SH_DIR32 relocation for the PLT entry.  */
2701
0
        htab->srelplt2->size += sizeof (Elf32_External_Rela) * 2;
2702
0
      }
2703
0
  }
2704
0
      else
2705
0
  {
2706
0
    h->plt.offset = (bfd_vma) -1;
2707
0
    h->needs_plt = 0;
2708
0
  }
2709
0
    }
2710
0
  else
2711
0
    {
2712
0
      h->plt.offset = (bfd_vma) -1;
2713
0
      h->needs_plt = 0;
2714
0
    }
2715
2716
0
  if (h->got.refcount > 0)
2717
0
    {
2718
0
      asection *s;
2719
0
      bool dyn;
2720
0
      enum got_type got_type = sh_elf_hash_entry (h)->got_type;
2721
2722
      /* Make sure this symbol is output as a dynamic symbol.
2723
   Undefined weak syms won't yet be marked as dynamic.  */
2724
0
      if (h->dynindx == -1
2725
0
    && !h->forced_local)
2726
0
  {
2727
0
    if (! bfd_elf_link_record_dynamic_symbol (info, h))
2728
0
      return false;
2729
0
  }
2730
2731
0
      s = htab->root.sgot;
2732
0
      h->got.offset = s->size;
2733
0
      s->size += 4;
2734
      /* R_SH_TLS_GD needs 2 consecutive GOT slots.  */
2735
0
      if (got_type == GOT_TLS_GD)
2736
0
  s->size += 4;
2737
0
      dyn = htab->root.dynamic_sections_created;
2738
0
      if (!dyn)
2739
0
  {
2740
    /* No dynamic relocations required.  */
2741
0
    if (htab->fdpic_p && !bfd_link_pic (info)
2742
0
        && h->root.type != bfd_link_hash_undefweak
2743
0
        && (got_type == GOT_NORMAL || got_type == GOT_FUNCDESC))
2744
0
      htab->srofixup->size += 4;
2745
0
  }
2746
      /* No dynamic relocations required when IE->LE conversion happens.  */
2747
0
      else if (got_type == GOT_TLS_IE
2748
0
         && !h->def_dynamic
2749
0
         && !bfd_link_pic (info))
2750
0
  ;
2751
      /* R_SH_TLS_IE_32 needs one dynamic relocation if dynamic,
2752
   R_SH_TLS_GD needs one if local symbol and two if global.  */
2753
0
      else if ((got_type == GOT_TLS_GD && h->dynindx == -1)
2754
0
         || got_type == GOT_TLS_IE)
2755
0
  htab->root.srelgot->size += sizeof (Elf32_External_Rela);
2756
0
      else if (got_type == GOT_TLS_GD)
2757
0
  htab->root.srelgot->size += 2 * sizeof (Elf32_External_Rela);
2758
0
      else if (got_type == GOT_FUNCDESC)
2759
0
  {
2760
0
    if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
2761
0
      htab->srofixup->size += 4;
2762
0
    else
2763
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
2764
0
  }
2765
0
      else if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2766
0
    || h->root.type != bfd_link_hash_undefweak)
2767
0
         && (bfd_link_pic (info)
2768
0
       || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
2769
0
  htab->root.srelgot->size += sizeof (Elf32_External_Rela);
2770
0
      else if (htab->fdpic_p
2771
0
         && !bfd_link_pic (info)
2772
0
         && got_type == GOT_NORMAL
2773
0
         && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2774
0
       || h->root.type != bfd_link_hash_undefweak))
2775
0
  htab->srofixup->size += 4;
2776
0
    }
2777
0
  else
2778
0
    h->got.offset = (bfd_vma) -1;
2779
2780
  /* Allocate space for any dynamic relocations to function
2781
     descriptors, canonical or otherwise.  We need to relocate the
2782
     reference unless it resolves to zero, which only happens for
2783
     undefined weak symbols (either non-default visibility, or when
2784
     static linking).  Any GOT slot is accounted for elsewhere.  */
2785
0
  if (eh->abs_funcdesc_refcount > 0
2786
0
      && (h->root.type != bfd_link_hash_undefweak
2787
0
    || (htab->root.dynamic_sections_created
2788
0
        && ! SYMBOL_CALLS_LOCAL (info, h))))
2789
0
    {
2790
0
      if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
2791
0
  htab->srofixup->size += eh->abs_funcdesc_refcount * 4;
2792
0
      else
2793
0
  htab->root.srelgot->size
2794
0
    += eh->abs_funcdesc_refcount * sizeof (Elf32_External_Rela);
2795
0
    }
2796
2797
  /* We must allocate a function descriptor if there are references to
2798
     a canonical descriptor (R_SH_GOTFUNCDESC or R_SH_FUNCDESC) and
2799
     the dynamic linker isn't going to allocate it.  None of this
2800
     applies if we already created one in .got.plt, but if the
2801
     canonical function descriptor can be in this object, there
2802
     won't be a PLT entry at all.  */
2803
0
  if ((eh->funcdesc.refcount > 0
2804
0
       || (h->got.offset != MINUS_ONE && eh->got_type == GOT_FUNCDESC))
2805
0
      && h->root.type != bfd_link_hash_undefweak
2806
0
      && SYMBOL_FUNCDESC_LOCAL (info, h))
2807
0
    {
2808
      /* Make room for this function descriptor.  */
2809
0
      eh->funcdesc.offset = htab->sfuncdesc->size;
2810
0
      htab->sfuncdesc->size += 8;
2811
2812
      /* We will need a relocation or two fixups to initialize the
2813
   function descriptor, so allocate those too.  */
2814
0
      if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
2815
0
  htab->srofixup->size += 8;
2816
0
      else
2817
0
  htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
2818
0
    }
2819
2820
0
  if (h->dyn_relocs == NULL)
2821
0
    return true;
2822
2823
  /* In the shared -Bsymbolic case, discard space allocated for
2824
     dynamic pc-relative relocs against symbols which turn out to be
2825
     defined in regular objects.  For the normal shared case, discard
2826
     space for pc-relative relocs that have become local due to symbol
2827
     visibility changes.  */
2828
2829
0
  if (bfd_link_pic (info))
2830
0
    {
2831
0
      if (SYMBOL_CALLS_LOCAL (info, h))
2832
0
  {
2833
0
    struct elf_dyn_relocs **pp;
2834
2835
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
2836
0
      {
2837
0
        p->count -= p->pc_count;
2838
0
        p->pc_count = 0;
2839
0
        if (p->count == 0)
2840
0
    *pp = p->next;
2841
0
        else
2842
0
    pp = &p->next;
2843
0
      }
2844
0
  }
2845
2846
0
      if (htab->root.target_os == is_vxworks)
2847
0
  {
2848
0
    struct elf_dyn_relocs **pp;
2849
2850
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL; )
2851
0
      {
2852
0
        if (strcmp (p->sec->output_section->name, ".tls_vars") == 0)
2853
0
    *pp = p->next;
2854
0
        else
2855
0
    pp = &p->next;
2856
0
      }
2857
0
  }
2858
2859
      /* Also discard relocs on undefined weak syms with non-default
2860
   visibility.  */
2861
0
      if (h->dyn_relocs != NULL
2862
0
    && h->root.type == bfd_link_hash_undefweak)
2863
0
  {
2864
0
    if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
2865
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
2866
0
      h->dyn_relocs = NULL;
2867
2868
    /* Make sure undefined weak symbols are output as a dynamic
2869
       symbol in PIEs.  */
2870
0
    else if (h->dynindx == -1
2871
0
       && !h->forced_local)
2872
0
      {
2873
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
2874
0
    return false;
2875
0
      }
2876
0
  }
2877
0
    }
2878
0
  else
2879
0
    {
2880
      /* For the non-shared case, discard space for relocs against
2881
   symbols which turn out to need copy relocs or are not
2882
   dynamic.  */
2883
2884
0
      if (!h->non_got_ref
2885
0
    && ((h->def_dynamic
2886
0
         && !h->def_regular)
2887
0
        || (htab->root.dynamic_sections_created
2888
0
      && (h->root.type == bfd_link_hash_undefweak
2889
0
          || h->root.type == bfd_link_hash_undefined))))
2890
0
  {
2891
    /* Make sure this symbol is output as a dynamic symbol.
2892
       Undefined weak syms won't yet be marked as dynamic.  */
2893
0
    if (h->dynindx == -1
2894
0
        && !h->forced_local)
2895
0
      {
2896
0
        if (! bfd_elf_link_record_dynamic_symbol (info, h))
2897
0
    return false;
2898
0
      }
2899
2900
    /* If that succeeded, we know we'll be keeping all the
2901
       relocs.  */
2902
0
    if (h->dynindx != -1)
2903
0
      goto keep;
2904
0
  }
2905
2906
0
      h->dyn_relocs = NULL;
2907
2908
0
    keep: ;
2909
0
    }
2910
2911
  /* Finally, allocate space.  */
2912
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
2913
0
    {
2914
0
      asection *sreloc = elf_section_data (p->sec)->sreloc;
2915
0
      sreloc->size += p->count * sizeof (Elf32_External_Rela);
2916
2917
      /* If we need relocations, we do not need fixups.  */
2918
0
      if (htab->fdpic_p && !bfd_link_pic (info))
2919
0
  htab->srofixup->size -= 4 * (p->count - p->pc_count);
2920
0
    }
2921
2922
0
  return true;
2923
0
}
2924
2925
/* This function is called after all the input files have been read,
2926
   and the input sections have been assigned to output sections.
2927
   It's a convenient place to determine the PLT style.  */
2928
2929
static bool
2930
sh_elf_early_size_sections (bfd *output_bfd, struct bfd_link_info *info)
2931
0
{
2932
0
  sh_elf_hash_table (info)->plt_info = get_plt_info (output_bfd,
2933
0
                 bfd_link_pic (info));
2934
2935
0
  if (sh_elf_hash_table (info)->fdpic_p && !bfd_link_relocatable (info)
2936
0
      && !bfd_elf_stack_segment_size (output_bfd, info,
2937
0
              "__stacksize", DEFAULT_STACK_SIZE))
2938
0
    return false;
2939
0
  return true;
2940
0
}
2941
2942
/* Set the sizes of the dynamic sections.  */
2943
2944
static bool
2945
sh_elf_late_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
2946
         struct bfd_link_info *info)
2947
0
{
2948
0
  struct elf_sh_link_hash_table *htab;
2949
0
  bfd *dynobj;
2950
0
  asection *s;
2951
0
  bool relocs;
2952
0
  bfd *ibfd;
2953
2954
0
  htab = sh_elf_hash_table (info);
2955
0
  if (htab == NULL)
2956
0
    return false;
2957
2958
0
  dynobj = htab->root.dynobj;
2959
0
  if (dynobj == NULL)
2960
0
    return true;
2961
2962
0
  if (htab->root.dynamic_sections_created)
2963
0
    {
2964
      /* Set the contents of the .interp section to the interpreter.  */
2965
0
      if (bfd_link_executable (info) && !info->nointerp)
2966
0
  {
2967
0
    s = htab->root.interp;
2968
0
    BFD_ASSERT (s != NULL);
2969
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
2970
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2971
0
    s->alloced = 1;
2972
0
  }
2973
0
    }
2974
2975
  /* Set up .got offsets for local syms, and space for local dynamic
2976
     relocs.  */
2977
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
2978
0
    {
2979
0
      bfd_signed_vma *local_got;
2980
0
      bfd_signed_vma *end_local_got;
2981
0
      union gotref *local_funcdesc, *end_local_funcdesc;
2982
0
      char *local_got_type;
2983
0
      bfd_size_type locsymcount;
2984
0
      Elf_Internal_Shdr *symtab_hdr;
2985
0
      asection *srel;
2986
2987
0
      if (! is_sh_elf (ibfd))
2988
0
  continue;
2989
2990
0
      for (s = ibfd->sections; s != NULL; s = s->next)
2991
0
  {
2992
0
    struct elf_dyn_relocs *p;
2993
2994
0
    for (p = ((struct elf_dyn_relocs *)
2995
0
        elf_section_data (s)->local_dynrel);
2996
0
         p != NULL;
2997
0
         p = p->next)
2998
0
      {
2999
0
        if (! bfd_is_abs_section (p->sec)
3000
0
      && bfd_is_abs_section (p->sec->output_section))
3001
0
    {
3002
      /* Input section has been discarded, either because
3003
         it is a copy of a linkonce section or due to
3004
         linker script /DISCARD/, so we'll be discarding
3005
         the relocs too.  */
3006
0
    }
3007
0
        else if (htab->root.target_os == is_vxworks
3008
0
           && strcmp (p->sec->output_section->name,
3009
0
          ".tls_vars") == 0)
3010
0
    {
3011
      /* Relocations in vxworks .tls_vars sections are
3012
         handled specially by the loader.  */
3013
0
    }
3014
0
        else if (p->count != 0)
3015
0
    {
3016
0
      srel = elf_section_data (p->sec)->sreloc;
3017
0
      srel->size += p->count * sizeof (Elf32_External_Rela);
3018
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
3019
0
        {
3020
0
          info->flags |= DF_TEXTREL;
3021
0
          info->callbacks->minfo (_("%pB: dynamic relocation in read-only section `%pA'\n"),
3022
0
                p->sec->owner, p->sec);
3023
0
        }
3024
3025
      /* If we need relocations, we do not need fixups.  */
3026
0
      if (htab->fdpic_p && !bfd_link_pic (info))
3027
0
        htab->srofixup->size -= 4 * (p->count - p->pc_count);
3028
0
    }
3029
0
      }
3030
0
  }
3031
3032
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
3033
0
      locsymcount = symtab_hdr->sh_info;
3034
0
      s = htab->root.sgot;
3035
0
      srel = htab->root.srelgot;
3036
3037
0
      local_got = elf_local_got_refcounts (ibfd);
3038
0
      if (local_got)
3039
0
  {
3040
0
    end_local_got = local_got + locsymcount;
3041
0
    local_got_type = sh_elf_local_got_type (ibfd);
3042
0
    local_funcdesc = sh_elf_local_funcdesc (ibfd);
3043
0
    for (; local_got < end_local_got; ++local_got)
3044
0
      {
3045
0
        if (*local_got > 0)
3046
0
    {
3047
0
      *local_got = s->size;
3048
0
      s->size += 4;
3049
0
      if (*local_got_type == GOT_TLS_GD)
3050
0
        s->size += 4;
3051
0
      if (bfd_link_pic (info))
3052
0
        srel->size += sizeof (Elf32_External_Rela);
3053
0
      else
3054
0
        htab->srofixup->size += 4;
3055
3056
0
      if (*local_got_type == GOT_FUNCDESC)
3057
0
        {
3058
0
          if (local_funcdesc == NULL)
3059
0
      {
3060
0
        bfd_size_type size;
3061
3062
0
        size = locsymcount * sizeof (union gotref);
3063
0
        local_funcdesc = (union gotref *) bfd_zalloc (ibfd,
3064
0
                  size);
3065
0
        if (local_funcdesc == NULL)
3066
0
          return false;
3067
0
        sh_elf_local_funcdesc (ibfd) = local_funcdesc;
3068
0
        local_funcdesc += (local_got
3069
0
               - elf_local_got_refcounts (ibfd));
3070
0
      }
3071
0
          local_funcdesc->refcount++;
3072
0
          ++local_funcdesc;
3073
0
        }
3074
0
    }
3075
0
        else
3076
0
    *local_got = (bfd_vma) -1;
3077
0
        ++local_got_type;
3078
0
      }
3079
0
  }
3080
3081
0
      local_funcdesc = sh_elf_local_funcdesc (ibfd);
3082
0
      if (local_funcdesc)
3083
0
  {
3084
0
    end_local_funcdesc = local_funcdesc + locsymcount;
3085
3086
0
    for (; local_funcdesc < end_local_funcdesc; ++local_funcdesc)
3087
0
      {
3088
0
        if (local_funcdesc->refcount > 0)
3089
0
    {
3090
0
      local_funcdesc->offset = htab->sfuncdesc->size;
3091
0
      htab->sfuncdesc->size += 8;
3092
0
      if (!bfd_link_pic (info))
3093
0
        htab->srofixup->size += 8;
3094
0
      else
3095
0
        htab->srelfuncdesc->size += sizeof (Elf32_External_Rela);
3096
0
    }
3097
0
        else
3098
0
    local_funcdesc->offset = MINUS_ONE;
3099
0
      }
3100
0
  }
3101
3102
0
    }
3103
3104
0
  if (htab->tls_ldm_got.refcount > 0)
3105
0
    {
3106
      /* Allocate 2 got entries and 1 dynamic reloc for R_SH_TLS_LD_32
3107
   relocs.  */
3108
0
      htab->tls_ldm_got.offset = htab->root.sgot->size;
3109
0
      htab->root.sgot->size += 8;
3110
0
      htab->root.srelgot->size += sizeof (Elf32_External_Rela);
3111
0
    }
3112
0
  else
3113
0
    htab->tls_ldm_got.offset = -1;
3114
3115
  /* Only the reserved entries should be present.  For FDPIC, they go at
3116
     the end of .got.plt.  */
3117
0
  if (htab->fdpic_p)
3118
0
    {
3119
0
      BFD_ASSERT (htab->root.sgotplt && htab->root.sgotplt->size == 12);
3120
0
      htab->root.sgotplt->size = 0;
3121
0
    }
3122
3123
  /* Allocate global sym .plt and .got entries, and space for global
3124
     sym dynamic relocs.  */
3125
0
  elf_link_hash_traverse (&htab->root, allocate_dynrelocs, info);
3126
3127
  /* Move the reserved entries and the _GLOBAL_OFFSET_TABLE_ symbol to the
3128
     end of the FDPIC .got.plt.  */
3129
0
  if (htab->fdpic_p)
3130
0
    {
3131
0
      htab->root.hgot->root.u.def.value = htab->root.sgotplt->size;
3132
0
      htab->root.sgotplt->size += 12;
3133
0
    }
3134
3135
  /* At the very end of the .rofixup section is a pointer to the GOT.  */
3136
0
  if (htab->fdpic_p && htab->srofixup != NULL)
3137
0
    htab->srofixup->size += 4;
3138
3139
  /* We now have determined the sizes of the various dynamic sections.
3140
     Allocate memory for them.  */
3141
0
  relocs = false;
3142
0
  for (s = dynobj->sections; s != NULL; s = s->next)
3143
0
    {
3144
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
3145
0
  continue;
3146
3147
0
      if (s == htab->root.splt
3148
0
    || s == htab->root.sgot
3149
0
    || s == htab->root.sgotplt
3150
0
    || s == htab->sfuncdesc
3151
0
    || s == htab->srofixup
3152
0
    || s == htab->root.sdynbss)
3153
0
  {
3154
    /* Strip this section if we don't need it; see the
3155
       comment below.  */
3156
0
  }
3157
0
      else if (startswith (bfd_section_name (s), ".rela"))
3158
0
  {
3159
0
    if (s->size != 0 && s != htab->root.srelplt && s != htab->srelplt2)
3160
0
      relocs = true;
3161
3162
    /* We use the reloc_count field as a counter if we need
3163
       to copy relocs into the output file.  */
3164
0
    s->reloc_count = 0;
3165
0
  }
3166
0
      else
3167
0
  {
3168
    /* It's not one of our sections, so don't allocate space.  */
3169
0
    continue;
3170
0
  }
3171
3172
0
      if (s->size == 0)
3173
0
  {
3174
    /* If we don't need this section, strip it from the
3175
       output file.  This is mostly to handle .rela.bss and
3176
       .rela.plt.  We must create both sections in
3177
       create_dynamic_sections, because they must be created
3178
       before the linker maps input sections to output
3179
       sections.  The linker does that before
3180
       adjust_dynamic_symbol is called, and it is that
3181
       function which decides whether anything needs to go
3182
       into these sections.  */
3183
3184
0
    s->flags |= SEC_EXCLUDE;
3185
0
    continue;
3186
0
  }
3187
3188
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
3189
0
  continue;
3190
3191
      /* Allocate memory for the section contents.  We use bfd_zalloc
3192
   here in case unused entries are not reclaimed before the
3193
   section's contents are written out.  This should not happen,
3194
   but this way if it does, we get a R_SH_NONE reloc instead
3195
   of garbage.  */
3196
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
3197
0
      if (s->contents == NULL)
3198
0
  return false;
3199
0
      s->alloced = 1;
3200
0
    }
3201
3202
0
  return _bfd_elf_maybe_vxworks_add_dynamic_tags (output_bfd, info,
3203
0
              relocs);
3204
0
}
3205

3206
/* Add a dynamic relocation to the SRELOC section.  */
3207
3208
inline static bfd_vma
3209
sh_elf_add_dyn_reloc (bfd *output_bfd, asection *sreloc, bfd_vma offset,
3210
          int reloc_type, long dynindx, bfd_vma addend)
3211
0
{
3212
0
  Elf_Internal_Rela outrel;
3213
0
  bfd_vma reloc_offset;
3214
3215
0
  outrel.r_offset = offset;
3216
0
  outrel.r_info = ELF32_R_INFO (dynindx, reloc_type);
3217
0
  outrel.r_addend = addend;
3218
3219
0
  reloc_offset = sreloc->reloc_count * sizeof (Elf32_External_Rela);
3220
0
  BFD_ASSERT (reloc_offset < sreloc->size);
3221
0
  bfd_elf32_swap_reloca_out (output_bfd, &outrel,
3222
0
           sreloc->contents + reloc_offset);
3223
0
  sreloc->reloc_count++;
3224
3225
0
  return reloc_offset;
3226
0
}
3227
3228
/* Add an FDPIC read-only fixup.  */
3229
3230
inline static void
3231
sh_elf_add_rofixup (bfd *output_bfd, asection *srofixup, bfd_vma offset)
3232
0
{
3233
0
  bfd_vma fixup_offset;
3234
3235
0
  fixup_offset = srofixup->reloc_count++ * 4;
3236
0
  BFD_ASSERT (fixup_offset < srofixup->size);
3237
0
  bfd_put_32 (output_bfd, offset, srofixup->contents + fixup_offset);
3238
0
}
3239
3240
/* Return the offset of the generated .got section from the
3241
   _GLOBAL_OFFSET_TABLE_ symbol.  */
3242
3243
static bfd_signed_vma
3244
sh_elf_got_offset (struct elf_sh_link_hash_table *htab)
3245
0
{
3246
0
  return (htab->root.sgot->output_offset - htab->root.sgotplt->output_offset
3247
0
    - htab->root.hgot->root.u.def.value);
3248
0
}
3249
3250
/* Find the segment number in which OSEC, and output section, is
3251
   located.  */
3252
3253
static unsigned
3254
sh_elf_osec_to_segment (bfd *output_bfd, asection *osec)
3255
0
{
3256
0
  Elf_Internal_Phdr *p = NULL;
3257
3258
0
  if (output_bfd->xvec->flavour == bfd_target_elf_flavour
3259
      /* PR ld/17110: Do not look for output segments in an input bfd.  */
3260
0
      && output_bfd->direction != read_direction)
3261
0
    p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
3262
3263
  /* FIXME: Nothing ever says what this index is relative to.  The kernel
3264
     supplies data in terms of the number of load segments but this is
3265
     a phdr index and the first phdr may not be a load segment.  */
3266
0
  return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
3267
0
}
3268
3269
static bool
3270
sh_elf_osec_readonly_p (bfd *output_bfd, asection *osec)
3271
0
{
3272
0
  unsigned seg = sh_elf_osec_to_segment (output_bfd, osec);
3273
3274
0
  return (seg != (unsigned) -1
3275
0
    && ! (elf_tdata (output_bfd)->phdr[seg].p_flags & PF_W));
3276
0
}
3277
3278
/* Generate the initial contents of a local function descriptor, along
3279
   with any relocations or fixups required.  */
3280
static bool
3281
sh_elf_initialize_funcdesc (bfd *output_bfd,
3282
          struct bfd_link_info *info,
3283
          struct elf_link_hash_entry *h,
3284
          bfd_vma offset,
3285
          asection *section,
3286
          bfd_vma value)
3287
0
{
3288
0
  struct elf_sh_link_hash_table *htab;
3289
0
  int dynindx;
3290
0
  bfd_vma addr, seg;
3291
3292
0
  htab = sh_elf_hash_table (info);
3293
3294
  /* FIXME: The ABI says that the offset to the function goes in the
3295
     descriptor, along with the segment index.  We're RELA, so it could
3296
     go in the reloc instead... */
3297
3298
0
  if (h != NULL && SYMBOL_CALLS_LOCAL (info, h))
3299
0
    {
3300
0
      section = h->root.u.def.section;
3301
0
      value = h->root.u.def.value;
3302
0
    }
3303
3304
0
  if (h == NULL || SYMBOL_CALLS_LOCAL (info, h))
3305
0
    {
3306
0
      dynindx = elf_section_data (section->output_section)->dynindx;
3307
0
      addr = value + section->output_offset;
3308
0
      seg = sh_elf_osec_to_segment (output_bfd, section->output_section);
3309
0
    }
3310
0
  else
3311
0
    {
3312
0
      BFD_ASSERT (h->dynindx != -1);
3313
0
      dynindx = h->dynindx;
3314
0
      addr = seg = 0;
3315
0
    }
3316
3317
0
  if (!bfd_link_pic (info) && SYMBOL_CALLS_LOCAL (info, h))
3318
0
    {
3319
0
      if (h == NULL || h->root.type != bfd_link_hash_undefweak)
3320
0
  {
3321
0
    sh_elf_add_rofixup (output_bfd, htab->srofixup,
3322
0
            offset
3323
0
            + htab->sfuncdesc->output_section->vma
3324
0
            + htab->sfuncdesc->output_offset);
3325
0
    sh_elf_add_rofixup (output_bfd, htab->srofixup,
3326
0
            offset + 4
3327
0
            + htab->sfuncdesc->output_section->vma
3328
0
            + htab->sfuncdesc->output_offset);
3329
0
  }
3330
3331
      /* There are no dynamic relocations so fill in the final
3332
   address and gp value (barring fixups).  */
3333
0
      addr += section->output_section->vma;
3334
0
      seg = htab->root.hgot->root.u.def.value
3335
0
  + htab->root.hgot->root.u.def.section->output_section->vma
3336
0
  + htab->root.hgot->root.u.def.section->output_offset;
3337
0
    }
3338
0
  else
3339
0
    sh_elf_add_dyn_reloc (output_bfd, htab->srelfuncdesc,
3340
0
        offset
3341
0
        + htab->sfuncdesc->output_section->vma
3342
0
        + htab->sfuncdesc->output_offset,
3343
0
        R_SH_FUNCDESC_VALUE, dynindx, 0);
3344
3345
0
  bfd_put_32 (output_bfd, addr, htab->sfuncdesc->contents + offset);
3346
0
  bfd_put_32 (output_bfd, seg, htab->sfuncdesc->contents + offset + 4);
3347
3348
0
  return true;
3349
0
}
3350
3351
/* Install a 20-bit movi20 field starting at ADDR, which occurs in OUTPUT_BFD.
3352
   VALUE is the field's value.  Return bfd_reloc_ok if successful or an error
3353
   otherwise.  */
3354
3355
static bfd_reloc_status_type
3356
install_movi20_field (bfd *output_bfd, unsigned long relocation,
3357
          bfd *input_bfd, asection *input_section,
3358
          bfd_byte *contents, bfd_vma offset)
3359
0
{
3360
0
  unsigned long cur_val;
3361
0
  bfd_byte *addr;
3362
0
  bfd_reloc_status_type r;
3363
3364
0
  if (offset > bfd_get_section_limit (input_bfd, input_section))
3365
0
    return bfd_reloc_outofrange;
3366
3367
0
  r = bfd_check_overflow (complain_overflow_signed, 20, 0,
3368
0
        bfd_arch_bits_per_address (input_bfd), relocation);
3369
0
  if (r != bfd_reloc_ok)
3370
0
    return r;
3371
3372
0
  addr = contents + offset;
3373
0
  cur_val = bfd_get_16 (output_bfd, addr);
3374
0
  bfd_put_16 (output_bfd, cur_val | ((relocation & 0xf0000) >> 12), addr);
3375
0
  bfd_put_16 (output_bfd, relocation & 0xffff, addr + 2);
3376
3377
0
  return bfd_reloc_ok;
3378
0
}
3379
3380
/* Relocate an SH ELF section.  */
3381
3382
static int
3383
sh_elf_relocate_section (bfd *output_bfd, struct bfd_link_info *info,
3384
       bfd *input_bfd, asection *input_section,
3385
       bfd_byte *contents, Elf_Internal_Rela *relocs,
3386
       Elf_Internal_Sym *local_syms,
3387
       asection **local_sections)
3388
0
{
3389
0
  struct elf_sh_link_hash_table *htab;
3390
0
  Elf_Internal_Shdr *symtab_hdr;
3391
0
  struct elf_link_hash_entry **sym_hashes;
3392
0
  Elf_Internal_Rela *rel, *relend;
3393
0
  bfd_vma *local_got_offsets;
3394
0
  asection *sgot = NULL;
3395
0
  asection *sgotplt = NULL;
3396
0
  asection *splt = NULL;
3397
0
  asection *sreloc = NULL;
3398
0
  asection *srelgot = NULL;
3399
0
  bool is_vxworks_tls;
3400
0
  unsigned isec_segment, got_segment, plt_segment, check_segment[2];
3401
0
  bool fdpic_p = false;
3402
3403
0
  if (!is_sh_elf (input_bfd))
3404
0
    {
3405
0
      bfd_set_error (bfd_error_wrong_format);
3406
0
      return false;
3407
0
    }
3408
3409
0
  htab = sh_elf_hash_table (info);
3410
0
  if (htab != NULL)
3411
0
    {
3412
0
      sgot = htab->root.sgot;
3413
0
      sgotplt = htab->root.sgotplt;
3414
0
      srelgot = htab->root.srelgot;
3415
0
      splt = htab->root.splt;
3416
0
      fdpic_p = htab->fdpic_p;
3417
0
    }
3418
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
3419
0
  sym_hashes = elf_sym_hashes (input_bfd);
3420
0
  local_got_offsets = elf_local_got_offsets (input_bfd);
3421
3422
0
  isec_segment = sh_elf_osec_to_segment (output_bfd,
3423
0
           input_section->output_section);
3424
0
  if (fdpic_p && sgot)
3425
0
    got_segment = sh_elf_osec_to_segment (output_bfd,
3426
0
            sgot->output_section);
3427
0
  else
3428
0
    got_segment = -1;
3429
0
  if (fdpic_p && splt)
3430
0
    plt_segment = sh_elf_osec_to_segment (output_bfd,
3431
0
            splt->output_section);
3432
0
  else
3433
0
    plt_segment = -1;
3434
3435
  /* We have to handle relocations in vxworks .tls_vars sections
3436
     specially, because the dynamic loader is 'weird'.  */
3437
0
  is_vxworks_tls = (htab && htab->root.target_os == is_vxworks && bfd_link_pic (info)
3438
0
        && !strcmp (input_section->output_section->name,
3439
0
        ".tls_vars"));
3440
3441
0
  rel = relocs;
3442
0
  relend = relocs + input_section->reloc_count;
3443
0
  for (; rel < relend; rel++)
3444
0
    {
3445
0
      int r_type;
3446
0
      reloc_howto_type *howto;
3447
0
      unsigned long r_symndx;
3448
0
      Elf_Internal_Sym *sym;
3449
0
      asection *sec;
3450
0
      struct elf_link_hash_entry *h;
3451
0
      bfd_vma relocation;
3452
0
      bfd_vma addend = (bfd_vma) 0;
3453
0
      bfd_reloc_status_type r;
3454
0
      bfd_vma off;
3455
0
      enum got_type got_type;
3456
0
      const char *symname = NULL;
3457
0
      bool resolved_to_zero;
3458
3459
0
      r_symndx = ELF32_R_SYM (rel->r_info);
3460
3461
0
      r_type = ELF32_R_TYPE (rel->r_info);
3462
3463
      /* Many of the relocs are only used for relaxing, and are
3464
   handled entirely by the relaxation code.  */
3465
0
      if (r_type >= (int) R_SH_GNU_VTINHERIT
3466
0
    && r_type <= (int) R_SH_LABEL)
3467
0
  continue;
3468
0
      if (r_type == (int) R_SH_NONE)
3469
0
  continue;
3470
3471
0
      if (r_type < 0
3472
0
    || r_type >= R_SH_max
3473
0
    || (r_type >= (int) R_SH_FIRST_INVALID_RELOC
3474
0
        && r_type <= (int) R_SH_LAST_INVALID_RELOC)
3475
0
    || (r_type >= (int) R_SH_FIRST_INVALID_RELOC_2
3476
0
        && r_type <= (int) R_SH_LAST_INVALID_RELOC_2)
3477
0
    || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_3
3478
0
        && r_type <= (int) R_SH_LAST_INVALID_RELOC_3)
3479
0
    || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_4
3480
0
        && r_type <= (int) R_SH_LAST_INVALID_RELOC_4)
3481
0
    || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_5
3482
0
        && r_type <= (int) R_SH_LAST_INVALID_RELOC_5)
3483
0
    || (   r_type >= (int) R_SH_FIRST_INVALID_RELOC_6
3484
0
        && r_type <= (int) R_SH_LAST_INVALID_RELOC_6))
3485
0
  {
3486
0
    bfd_set_error (bfd_error_bad_value);
3487
0
    return false;
3488
0
  }
3489
3490
0
      howto = get_howto_table (output_bfd) + r_type;
3491
3492
      /* For relocs that aren't partial_inplace, we get the addend from
3493
   the relocation.  */
3494
0
      if (! howto->partial_inplace)
3495
0
  addend = rel->r_addend;
3496
3497
0
      resolved_to_zero = false;
3498
0
      h = NULL;
3499
0
      sym = NULL;
3500
0
      sec = NULL;
3501
0
      check_segment[0] = -1;
3502
0
      check_segment[1] = -1;
3503
0
      if (r_symndx < symtab_hdr->sh_info)
3504
0
  {
3505
0
    sym = local_syms + r_symndx;
3506
0
    sec = local_sections[r_symndx];
3507
3508
0
    symname = bfd_elf_string_from_elf_section
3509
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name);
3510
0
    if (symname == NULL || *symname == '\0')
3511
0
      symname = bfd_section_name (sec);
3512
3513
0
    relocation = (sec->output_section->vma
3514
0
      + sec->output_offset
3515
0
      + sym->st_value);
3516
3517
0
    if (sec != NULL && discarded_section (sec))
3518
      /* Handled below.  */
3519
0
      ;
3520
0
    else if (bfd_link_relocatable (info))
3521
0
      {
3522
        /* This is a relocatable link.  We don't have to change
3523
     anything, unless the reloc is against a section symbol,
3524
     in which case we have to adjust according to where the
3525
     section symbol winds up in the output section.  */
3526
0
        if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3527
0
    {
3528
0
      if (! howto->partial_inplace)
3529
0
        {
3530
          /* For relocations with the addend in the
3531
       relocation, we need just to update the addend.
3532
       All real relocs are of type partial_inplace; this
3533
       code is mostly for completeness.  */
3534
0
          rel->r_addend += sec->output_offset;
3535
3536
0
          continue;
3537
0
        }
3538
3539
      /* Relocs of type partial_inplace need to pick up the
3540
         contents in the contents and add the offset resulting
3541
         from the changed location of the section symbol.
3542
         Using _bfd_final_link_relocate (e.g. goto
3543
         final_link_relocate) here would be wrong, because
3544
         relocations marked pc_relative would get the current
3545
         location subtracted, and we must only do that at the
3546
         final link.  */
3547
0
      r = _bfd_relocate_contents (howto, input_bfd,
3548
0
                sec->output_offset
3549
0
                + sym->st_value,
3550
0
                contents + rel->r_offset);
3551
0
      goto relocation_done;
3552
0
    }
3553
3554
0
        continue;
3555
0
      }
3556
0
    else if (! howto->partial_inplace)
3557
0
      {
3558
0
        relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
3559
0
        addend = rel->r_addend;
3560
0
      }
3561
0
    else if ((sec->flags & SEC_MERGE)
3562
0
       && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
3563
0
      {
3564
0
        asection *msec;
3565
3566
0
        if (howto->rightshift || howto->src_mask != 0xffffffff)
3567
0
    {
3568
0
      _bfd_error_handler
3569
        /* xgettext:c-format */
3570
0
        (_("%pB(%pA+%#" PRIx64 "): "
3571
0
           "%s relocation against SEC_MERGE section"),
3572
0
         input_bfd, input_section,
3573
0
         (uint64_t) rel->r_offset, howto->name);
3574
0
      return false;
3575
0
    }
3576
3577
0
        addend = bfd_get_32 (input_bfd, contents + rel->r_offset);
3578
0
        msec = sec;
3579
0
        addend =
3580
0
    _bfd_elf_rel_local_sym (output_bfd, sym, &msec, addend)
3581
0
    - relocation;
3582
0
        addend += msec->output_section->vma + msec->output_offset;
3583
0
        bfd_put_32 (input_bfd, addend, contents + rel->r_offset);
3584
0
        addend = 0;
3585
0
      }
3586
0
  }
3587
0
      else
3588
0
  {
3589
    /* FIXME: Ought to make use of the RELOC_FOR_GLOBAL_SYMBOL macro.  */
3590
3591
0
    relocation = 0;
3592
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3593
0
    symname = h->root.root.string;
3594
0
    while (h->root.type == bfd_link_hash_indirect
3595
0
     || h->root.type == bfd_link_hash_warning)
3596
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
3597
0
    if (h->root.type == bfd_link_hash_defined
3598
0
        || h->root.type == bfd_link_hash_defweak)
3599
0
      {
3600
0
        bool dyn;
3601
3602
0
        dyn = htab ? htab->root.dynamic_sections_created : false;
3603
0
        sec = h->root.u.def.section;
3604
        /* In these cases, we don't need the relocation value.
3605
     We check specially because in some obscure cases
3606
     sec->output_section will be NULL.  */
3607
0
        if (r_type == R_SH_GOTPC
3608
0
      || r_type == R_SH_GOTPC_LOW16
3609
0
      || r_type == R_SH_GOTPC_MEDLOW16
3610
0
      || r_type == R_SH_GOTPC_MEDHI16
3611
0
      || r_type == R_SH_GOTPC_HI16
3612
0
      || ((r_type == R_SH_PLT32
3613
0
           || r_type == R_SH_PLT_LOW16
3614
0
           || r_type == R_SH_PLT_MEDLOW16
3615
0
           || r_type == R_SH_PLT_MEDHI16
3616
0
           || r_type == R_SH_PLT_HI16)
3617
0
          && h->plt.offset != (bfd_vma) -1)
3618
0
      || ((r_type == R_SH_GOT32
3619
0
           || r_type == R_SH_GOT20
3620
0
           || r_type == R_SH_GOTFUNCDESC
3621
0
           || r_type == R_SH_GOTFUNCDESC20
3622
0
           || r_type == R_SH_GOTOFFFUNCDESC
3623
0
           || r_type == R_SH_GOTOFFFUNCDESC20
3624
0
           || r_type == R_SH_FUNCDESC
3625
0
           || r_type == R_SH_GOT_LOW16
3626
0
           || r_type == R_SH_GOT_MEDLOW16
3627
0
           || r_type == R_SH_GOT_MEDHI16
3628
0
           || r_type == R_SH_GOT_HI16)
3629
0
          && WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
3630
0
                bfd_link_pic (info),
3631
0
                h)
3632
0
          && (! bfd_link_pic (info)
3633
0
        || (! info->symbolic && h->dynindx != -1)
3634
0
        || !h->def_regular))
3635
      /* The cases above are those in which relocation is
3636
         overwritten in the switch block below.  The cases
3637
         below are those in which we must defer relocation
3638
         to run-time, because we can't resolve absolute
3639
         addresses when creating a shared library.  */
3640
0
      || (bfd_link_pic (info)
3641
0
          && ((! info->symbolic && h->dynindx != -1)
3642
0
        || !h->def_regular)
3643
0
          && ((r_type == R_SH_DIR32
3644
0
         && !h->forced_local)
3645
0
        || (r_type == R_SH_REL32
3646
0
            && !SYMBOL_CALLS_LOCAL (info, h)))
3647
0
          && ((input_section->flags & SEC_ALLOC) != 0
3648
        /* DWARF will emit R_SH_DIR32 relocations in its
3649
           sections against symbols defined externally
3650
           in shared libraries.  We can't do anything
3651
           with them here.  */
3652
0
        || ((input_section->flags & SEC_DEBUGGING) != 0
3653
0
            && h->def_dynamic)))
3654
      /* Dynamic relocs are not propagated for SEC_DEBUGGING
3655
         sections because such sections are not SEC_ALLOC and
3656
         thus ld.so will not process them.  */
3657
0
      || (sec->output_section == NULL
3658
0
          && ((input_section->flags & SEC_DEBUGGING) != 0
3659
0
        && h->def_dynamic))
3660
0
      || (sec->output_section == NULL
3661
0
          && (sh_elf_hash_entry (h)->got_type == GOT_TLS_IE
3662
0
        || sh_elf_hash_entry (h)->got_type == GOT_TLS_GD)))
3663
0
    ;
3664
0
        else if (sec->output_section != NULL)
3665
0
    relocation = (h->root.u.def.value
3666
0
            + sec->output_section->vma
3667
0
            + sec->output_offset);
3668
0
        else if (!bfd_link_relocatable (info)
3669
0
           && (_bfd_elf_section_offset (output_bfd, info,
3670
0
                input_section,
3671
0
                rel->r_offset)
3672
0
         != (bfd_vma) -1))
3673
0
    {
3674
0
      _bfd_error_handler
3675
        /* xgettext:c-format */
3676
0
        (_("%pB(%pA+%#" PRIx64 "): "
3677
0
           "unresolvable %s relocation against symbol `%s'"),
3678
0
         input_bfd,
3679
0
         input_section,
3680
0
         (uint64_t) rel->r_offset,
3681
0
         howto->name,
3682
0
         h->root.root.string);
3683
0
      return false;
3684
0
    }
3685
0
      }
3686
0
    else if (h->root.type == bfd_link_hash_undefweak)
3687
0
      resolved_to_zero = UNDEFWEAK_NO_DYNAMIC_RELOC (info, h);
3688
0
    else if (info->unresolved_syms_in_objects == RM_IGNORE
3689
0
       && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
3690
0
      ;
3691
0
    else if (!bfd_link_relocatable (info))
3692
0
            info->callbacks->undefined_symbol
3693
0
        (info, h->root.root.string, input_bfd, input_section,
3694
0
         rel->r_offset,
3695
0
         (info->unresolved_syms_in_objects == RM_DIAGNOSE
3696
0
    && !info->warn_unresolved_syms)
3697
0
         || ELF_ST_VISIBILITY (h->other));
3698
0
        }
3699
3700
0
      if (sec != NULL && discarded_section (sec))
3701
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
3702
0
           rel, 1, relend, R_SH_NONE,
3703
0
           howto, 0, contents);
3704
3705
0
      if (bfd_link_relocatable (info))
3706
0
  continue;
3707
3708
      /* Check for inter-segment relocations in FDPIC files.  Most
3709
   relocations connect the relocation site to the location of
3710
   the target symbol, but there are some exceptions below.  */
3711
0
      check_segment[0] = isec_segment;
3712
0
      if (sec != NULL)
3713
0
  check_segment[1] = sh_elf_osec_to_segment (output_bfd,
3714
0
               sec->output_section);
3715
0
      else
3716
0
  check_segment[1] = -1;
3717
3718
0
      switch ((int) r_type)
3719
0
  {
3720
0
  final_link_relocate:
3721
    /* COFF relocs don't use the addend. The addend is used for
3722
       R_SH_DIR32 to be compatible with other compilers.  */
3723
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3724
0
          contents, rel->r_offset,
3725
0
          relocation, addend);
3726
0
    break;
3727
3728
0
  case R_SH_IND12W:
3729
0
    goto final_link_relocate;
3730
3731
0
  case R_SH_DIR8WPN:
3732
0
  case R_SH_DIR8WPZ:
3733
0
  case R_SH_DIR8WPL:
3734
    /* If the reloc is against the start of this section, then
3735
       the assembler has already taken care of it and the reloc
3736
       is here only to assist in relaxing.  If the reloc is not
3737
       against the start of this section, then it's against an
3738
       external symbol and we must deal with it ourselves.  */
3739
0
    if (input_section->output_section->vma + input_section->output_offset
3740
0
        != relocation)
3741
0
      {
3742
0
        int disp = (relocation
3743
0
        - input_section->output_section->vma
3744
0
        - input_section->output_offset
3745
0
        - rel->r_offset);
3746
0
        int mask = 0;
3747
0
        switch (r_type)
3748
0
    {
3749
0
    case R_SH_DIR8WPN:
3750
0
    case R_SH_DIR8WPZ: mask = 1; break;
3751
0
    case R_SH_DIR8WPL: mask = 3; break;
3752
0
    default: mask = 0; break;
3753
0
    }
3754
0
        if (disp & mask)
3755
0
    {
3756
0
      _bfd_error_handler
3757
        /* xgettext:c-format */
3758
0
        (_("%pB: %#" PRIx64 ": fatal: "
3759
0
           "unaligned branch target for relax-support relocation"),
3760
0
         input_section->owner,
3761
0
         (uint64_t) rel->r_offset);
3762
0
      bfd_set_error (bfd_error_bad_value);
3763
0
      return false;
3764
0
    }
3765
0
        relocation -= 4;
3766
0
        goto final_link_relocate;
3767
0
      }
3768
0
    r = bfd_reloc_ok;
3769
0
    break;
3770
3771
0
  default:
3772
0
    bfd_set_error (bfd_error_bad_value);
3773
0
    return false;
3774
3775
0
  case R_SH_DIR16:
3776
0
  case R_SH_DIR8:
3777
0
  case R_SH_DIR8U:
3778
0
  case R_SH_DIR8S:
3779
0
  case R_SH_DIR4U:
3780
0
    goto final_link_relocate;
3781
3782
0
  case R_SH_DIR8UL:
3783
0
  case R_SH_DIR4UL:
3784
0
    if (relocation & 3)
3785
0
      {
3786
0
        _bfd_error_handler
3787
    /* xgettext:c-format */
3788
0
    (_("%pB: %#" PRIx64 ": fatal: "
3789
0
       "unaligned %s relocation %#" PRIx64),
3790
0
     input_section->owner, (uint64_t) rel->r_offset,
3791
0
     howto->name, (uint64_t) relocation);
3792
0
        bfd_set_error (bfd_error_bad_value);
3793
0
        return false;
3794
0
      }
3795
0
    goto final_link_relocate;
3796
3797
0
  case R_SH_DIR8UW:
3798
0
  case R_SH_DIR8SW:
3799
0
  case R_SH_DIR4UW:
3800
0
    if (relocation & 1)
3801
0
      {
3802
0
        _bfd_error_handler
3803
    /* xgettext:c-format */
3804
0
    (_("%pB: %#" PRIx64 ": fatal: "
3805
0
       "unaligned %s relocation %#" PRIx64 ""),
3806
0
     input_section->owner,
3807
0
     (uint64_t) rel->r_offset, howto->name,
3808
0
     (uint64_t) relocation);
3809
0
        bfd_set_error (bfd_error_bad_value);
3810
0
        return false;
3811
0
      }
3812
0
    goto final_link_relocate;
3813
3814
0
  case R_SH_PSHA:
3815
0
    if ((signed int)relocation < -32
3816
0
        || (signed int)relocation > 32)
3817
0
      {
3818
0
        _bfd_error_handler
3819
    /* xgettext:c-format */
3820
0
    (_("%pB: %#" PRIx64 ": fatal: R_SH_PSHA relocation %" PRId64
3821
0
       " not in range -32..32"),
3822
0
     input_section->owner,
3823
0
     (uint64_t) rel->r_offset,
3824
0
     (int64_t) relocation);
3825
0
        bfd_set_error (bfd_error_bad_value);
3826
0
        return false;
3827
0
      }
3828
0
    goto final_link_relocate;
3829
3830
0
  case R_SH_PSHL:
3831
0
    if ((signed int)relocation < -16
3832
0
        || (signed int)relocation > 16)
3833
0
      {
3834
0
        _bfd_error_handler
3835
    /* xgettext:c-format */
3836
0
    (_("%pB: %#" PRIx64 ": fatal: R_SH_PSHL relocation %" PRId64
3837
0
       " not in range -32..32"),
3838
0
     input_section->owner,
3839
0
     (uint64_t) rel->r_offset,
3840
0
     (int64_t) relocation);
3841
0
        bfd_set_error (bfd_error_bad_value);
3842
0
        return false;
3843
0
      }
3844
0
    goto final_link_relocate;
3845
3846
0
  case R_SH_DIR32:
3847
0
  case R_SH_REL32:
3848
0
    if (bfd_link_pic (info)
3849
0
        && (h == NULL
3850
0
      || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3851
0
          && !resolved_to_zero)
3852
0
      || h->root.type != bfd_link_hash_undefweak)
3853
0
        && r_symndx != STN_UNDEF
3854
0
        && (input_section->flags & SEC_ALLOC) != 0
3855
0
        && !is_vxworks_tls
3856
0
        && (r_type == R_SH_DIR32
3857
0
      || !SYMBOL_CALLS_LOCAL (info, h)))
3858
0
      {
3859
0
        Elf_Internal_Rela outrel;
3860
0
        bfd_byte *loc;
3861
0
        bool skip, relocate;
3862
3863
        /* When generating a shared object, these relocations
3864
     are copied into the output file to be resolved at run
3865
     time.  */
3866
3867
0
        if (sreloc == NULL)
3868
0
    {
3869
0
      sreloc = _bfd_elf_get_dynamic_reloc_section
3870
0
        (input_bfd, input_section, /*rela?*/ true);
3871
0
      if (sreloc == NULL)
3872
0
        return false;
3873
0
    }
3874
3875
0
        skip = false;
3876
0
        relocate = false;
3877
3878
0
        outrel.r_offset =
3879
0
    _bfd_elf_section_offset (output_bfd, info, input_section,
3880
0
           rel->r_offset);
3881
0
        if (outrel.r_offset == (bfd_vma) -1)
3882
0
    skip = true;
3883
0
        else if (outrel.r_offset == (bfd_vma) -2)
3884
0
    skip = true, relocate = true;
3885
0
        outrel.r_offset += (input_section->output_section->vma
3886
0
          + input_section->output_offset);
3887
3888
0
        if (skip)
3889
0
    memset (&outrel, 0, sizeof outrel);
3890
0
        else if (r_type == R_SH_REL32)
3891
0
    {
3892
0
      BFD_ASSERT (h != NULL && h->dynindx != -1);
3893
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_REL32);
3894
0
      outrel.r_addend
3895
0
        = (howto->partial_inplace
3896
0
           ? bfd_get_32 (input_bfd, contents + rel->r_offset)
3897
0
           : addend);
3898
0
    }
3899
0
        else if (fdpic_p
3900
0
           && (h == NULL
3901
0
         || ((info->symbolic || h->dynindx == -1)
3902
0
             && h->def_regular)))
3903
0
    {
3904
0
      int dynindx;
3905
3906
0
      BFD_ASSERT (sec != NULL);
3907
0
      BFD_ASSERT (sec->output_section != NULL);
3908
0
      dynindx = elf_section_data (sec->output_section)->dynindx;
3909
0
      outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
3910
0
      outrel.r_addend = relocation;
3911
0
      outrel.r_addend
3912
0
        += (howto->partial_inplace
3913
0
      ? bfd_get_32 (input_bfd, contents + rel->r_offset)
3914
0
      : addend);
3915
0
      outrel.r_addend -= sec->output_section->vma;
3916
0
    }
3917
0
        else
3918
0
    {
3919
      /* h->dynindx may be -1 if this symbol was marked to
3920
         become local.  */
3921
0
      if (h == NULL
3922
0
          || ((info->symbolic || h->dynindx == -1)
3923
0
        && h->def_regular))
3924
0
        {
3925
0
          relocate = howto->partial_inplace;
3926
0
          outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
3927
0
        }
3928
0
      else
3929
0
        {
3930
0
          BFD_ASSERT (h->dynindx != -1);
3931
0
          outrel.r_info = ELF32_R_INFO (h->dynindx, R_SH_DIR32);
3932
0
        }
3933
0
      outrel.r_addend = relocation;
3934
0
      outrel.r_addend
3935
0
        += (howto->partial_inplace
3936
0
      ? bfd_get_32 (input_bfd, contents + rel->r_offset)
3937
0
      : addend);
3938
0
    }
3939
3940
0
        loc = sreloc->contents;
3941
0
        loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
3942
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
3943
3944
0
        check_segment[0] = check_segment[1] = -1;
3945
3946
        /* If this reloc is against an external symbol, we do
3947
     not want to fiddle with the addend.  Otherwise, we
3948
     need to include the symbol value so that it becomes
3949
     an addend for the dynamic reloc.  */
3950
0
        if (! relocate)
3951
0
    continue;
3952
0
      }
3953
0
    else if (fdpic_p && !bfd_link_pic (info)
3954
0
       && r_type == R_SH_DIR32
3955
0
       && (input_section->flags & SEC_ALLOC) != 0)
3956
0
      {
3957
0
        bfd_vma offset;
3958
3959
0
        BFD_ASSERT (htab);
3960
3961
0
    if (sh_elf_osec_readonly_p (output_bfd,
3962
0
              input_section->output_section))
3963
0
      {
3964
0
        _bfd_error_handler
3965
          /* xgettext:c-format */
3966
0
          (_("%pB(%pA+%#" PRIx64 "): "
3967
0
       "cannot emit fixup to `%s' in read-only section"),
3968
0
           input_bfd,
3969
0
           input_section,
3970
0
           (uint64_t) rel->r_offset,
3971
0
           symname);
3972
0
        return false;
3973
0
      }
3974
3975
0
        offset = _bfd_elf_section_offset (output_bfd, info,
3976
0
            input_section, rel->r_offset);
3977
0
        if (offset != (bfd_vma)-1)
3978
0
    sh_elf_add_rofixup (output_bfd, htab->srofixup,
3979
0
            input_section->output_section->vma
3980
0
            + input_section->output_offset
3981
0
            + rel->r_offset);
3982
3983
0
        check_segment[0] = check_segment[1] = -1;
3984
0
      }
3985
      /* We don't want warnings for non-NULL tests on undefined weak
3986
         symbols.  */
3987
0
      else if (r_type == R_SH_REL32
3988
0
         && h
3989
0
         && h->root.type == bfd_link_hash_undefweak)
3990
0
        check_segment[0] = check_segment[1] = -1;
3991
0
    goto final_link_relocate;
3992
3993
0
  case R_SH_GOTPLT32:
3994
    /* Relocation is to the entry for this symbol in the
3995
       procedure linkage table.  */
3996
3997
0
    if (h == NULL
3998
0
        || h->forced_local
3999
0
        || ! bfd_link_pic (info)
4000
0
        || info->symbolic
4001
0
        || h->dynindx == -1
4002
0
        || h->plt.offset == (bfd_vma) -1
4003
0
        || h->got.offset != (bfd_vma) -1)
4004
0
      goto force_got;
4005
4006
    /* Relocation is to the entry for this symbol in the global
4007
       offset table extension for the procedure linkage table.  */
4008
4009
0
    BFD_ASSERT (htab);
4010
0
    BFD_ASSERT (sgotplt != NULL);
4011
0
    relocation = (sgotplt->output_offset
4012
0
      + (get_plt_index (htab->plt_info, h->plt.offset)
4013
0
         + 3) * 4);
4014
4015
#ifdef GOT_BIAS
4016
    relocation -= GOT_BIAS;
4017
#endif
4018
4019
0
    goto final_link_relocate;
4020
4021
0
  force_got:
4022
0
  case R_SH_GOT32:
4023
0
  case R_SH_GOT20:
4024
    /* Relocation is to the entry for this symbol in the global
4025
       offset table.  */
4026
4027
0
    BFD_ASSERT (htab);
4028
0
    BFD_ASSERT (sgot != NULL);
4029
0
    check_segment[0] = check_segment[1] = -1;
4030
4031
0
    if (h != NULL)
4032
0
      {
4033
0
        bool dyn;
4034
4035
0
        off = h->got.offset;
4036
0
        BFD_ASSERT (off != (bfd_vma) -1);
4037
4038
0
        dyn = htab->root.dynamic_sections_created;
4039
0
        if (! WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn,
4040
0
                 bfd_link_pic (info),
4041
0
                 h)
4042
0
      || (bfd_link_pic (info)
4043
0
          && SYMBOL_REFERENCES_LOCAL (info, h))
4044
0
      || ((ELF_ST_VISIBILITY (h->other)
4045
0
           || resolved_to_zero)
4046
0
          && h->root.type == bfd_link_hash_undefweak))
4047
0
    {
4048
      /* This is actually a static link, or it is a
4049
         -Bsymbolic link and the symbol is defined
4050
         locally, or the symbol was forced to be local
4051
         because of a version file.  We must initialize
4052
         this entry in the global offset table.  Since the
4053
         offset must always be a multiple of 4, we use the
4054
         least significant bit to record whether we have
4055
         initialized it already.
4056
4057
         When doing a dynamic link, we create a .rela.got
4058
         relocation entry to initialize the value.  This
4059
         is done in the finish_dynamic_symbol routine.  */
4060
0
      if ((off & 1) != 0)
4061
0
        off &= ~1;
4062
0
      else
4063
0
        {
4064
0
          bfd_put_32 (output_bfd, relocation,
4065
0
          sgot->contents + off);
4066
0
          h->got.offset |= 1;
4067
4068
          /* If we initialize the GOT entry here with a valid
4069
       symbol address, also add a fixup.  */
4070
0
          if (fdpic_p && !bfd_link_pic (info)
4071
0
        && sh_elf_hash_entry (h)->got_type == GOT_NORMAL
4072
0
        && (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
4073
0
            || h->root.type != bfd_link_hash_undefweak))
4074
0
      sh_elf_add_rofixup (output_bfd, htab->srofixup,
4075
0
              sgot->output_section->vma
4076
0
              + sgot->output_offset
4077
0
              + off);
4078
0
        }
4079
0
    }
4080
4081
0
        relocation = sh_elf_got_offset (htab) + off;
4082
0
      }
4083
0
    else
4084
0
      {
4085
0
        BFD_ASSERT (local_got_offsets != NULL
4086
0
        && local_got_offsets[r_symndx] != (bfd_vma) -1);
4087
4088
0
        off = local_got_offsets[r_symndx];
4089
4090
        /* The offset must always be a multiple of 4.  We use
4091
     the least significant bit to record whether we have
4092
     already generated the necessary reloc.  */
4093
0
        if ((off & 1) != 0)
4094
0
    off &= ~1;
4095
0
        else
4096
0
    {
4097
0
      bfd_put_32 (output_bfd, relocation, sgot->contents + off);
4098
4099
0
      if (bfd_link_pic (info))
4100
0
        {
4101
0
          Elf_Internal_Rela outrel;
4102
0
          bfd_byte *loc;
4103
4104
0
          outrel.r_offset = (sgot->output_section->vma
4105
0
           + sgot->output_offset
4106
0
           + off);
4107
0
          if (fdpic_p)
4108
0
      {
4109
0
        int dynindx
4110
0
          = elf_section_data (sec->output_section)->dynindx;
4111
0
        outrel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
4112
0
        outrel.r_addend = relocation;
4113
0
        outrel.r_addend -= sec->output_section->vma;
4114
0
      }
4115
0
          else
4116
0
      {
4117
0
        outrel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
4118
0
        outrel.r_addend = relocation;
4119
0
      }
4120
0
          loc = srelgot->contents;
4121
0
          loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4122
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4123
0
        }
4124
0
      else if (fdpic_p
4125
0
         && (sh_elf_local_got_type (input_bfd) [r_symndx]
4126
0
             == GOT_NORMAL))
4127
0
        sh_elf_add_rofixup (output_bfd, htab->srofixup,
4128
0
          sgot->output_section->vma
4129
0
          + sgot->output_offset
4130
0
          + off);
4131
4132
0
      local_got_offsets[r_symndx] |= 1;
4133
0
    }
4134
4135
0
        relocation = sh_elf_got_offset (htab) + off;
4136
0
      }
4137
4138
#ifdef GOT_BIAS
4139
    relocation -= GOT_BIAS;
4140
#endif
4141
4142
0
    if (r_type == R_SH_GOT20)
4143
0
      {
4144
0
        r = install_movi20_field (output_bfd, relocation + addend,
4145
0
          input_bfd, input_section, contents,
4146
0
          rel->r_offset);
4147
0
        break;
4148
0
      }
4149
0
    else
4150
0
      goto final_link_relocate;
4151
4152
0
  case R_SH_GOTOFF:
4153
0
  case R_SH_GOTOFF20:
4154
    /* GOTOFF relocations are relative to _GLOBAL_OFFSET_TABLE_, which
4155
       we place at the start of the .got.plt section.  This is the same
4156
       as the start of the output .got section, unless there are function
4157
       descriptors in front of it.  */
4158
0
    BFD_ASSERT (htab);
4159
0
    BFD_ASSERT (sgotplt != NULL);
4160
0
    check_segment[0] = got_segment;
4161
0
    relocation -= sgotplt->output_section->vma + sgotplt->output_offset
4162
0
      + htab->root.hgot->root.u.def.value;
4163
4164
#ifdef GOT_BIAS
4165
    relocation -= GOT_BIAS;
4166
#endif
4167
4168
0
    addend = rel->r_addend;
4169
4170
0
    if (r_type == R_SH_GOTOFF20)
4171
0
      {
4172
0
        r = install_movi20_field (output_bfd, relocation + addend,
4173
0
          input_bfd, input_section, contents,
4174
0
          rel->r_offset);
4175
0
        break;
4176
0
      }
4177
0
    else
4178
0
      goto final_link_relocate;
4179
4180
0
  case R_SH_GOTPC:
4181
    /* Use global offset table as symbol value.  */
4182
4183
0
    BFD_ASSERT (sgotplt != NULL);
4184
0
    relocation = sgotplt->output_section->vma + sgotplt->output_offset;
4185
4186
#ifdef GOT_BIAS
4187
    relocation += GOT_BIAS;
4188
#endif
4189
4190
0
    addend = rel->r_addend;
4191
4192
0
    goto final_link_relocate;
4193
4194
0
  case R_SH_PLT32:
4195
    /* Relocation is to the entry for this symbol in the
4196
       procedure linkage table.  */
4197
4198
    /* Resolve a PLT reloc against a local symbol directly,
4199
       without using the procedure linkage table.  */
4200
0
    if (h == NULL)
4201
0
      goto final_link_relocate;
4202
4203
    /* We don't want to warn on calls to undefined weak symbols,
4204
       as calls to them must be protected by non-NULL tests
4205
       anyway, and unprotected calls would invoke undefined
4206
       behavior.  */
4207
0
    if (h->root.type == bfd_link_hash_undefweak)
4208
0
      check_segment[0] = check_segment[1] = -1;
4209
4210
0
    if (h->forced_local)
4211
0
      goto final_link_relocate;
4212
4213
0
    if (h->plt.offset == (bfd_vma) -1)
4214
0
      {
4215
        /* We didn't make a PLT entry for this symbol.  This
4216
     happens when statically linking PIC code, or when
4217
     using -Bsymbolic.  */
4218
0
        goto final_link_relocate;
4219
0
      }
4220
4221
0
    BFD_ASSERT (splt != NULL);
4222
0
    check_segment[1] = plt_segment;
4223
0
    relocation = (splt->output_section->vma
4224
0
      + splt->output_offset
4225
0
      + h->plt.offset);
4226
4227
0
    addend = rel->r_addend;
4228
4229
0
    goto final_link_relocate;
4230
4231
  /* Relocation is to the canonical function descriptor for this
4232
     symbol, possibly via the GOT.  Initialize the GOT
4233
     entry and function descriptor if necessary.  */
4234
0
  case R_SH_GOTFUNCDESC:
4235
0
  case R_SH_GOTFUNCDESC20:
4236
0
  case R_SH_FUNCDESC:
4237
0
    {
4238
0
      int dynindx = -1;
4239
0
      asection *reloc_section;
4240
0
      bfd_vma reloc_offset;
4241
0
      int reloc_type = R_SH_FUNCDESC;
4242
4243
0
      BFD_ASSERT (htab);
4244
4245
0
      check_segment[0] = check_segment[1] = -1;
4246
4247
      /* FIXME: See what FRV does for global symbols in the
4248
         executable, with --export-dynamic.  Do they need ld.so
4249
         to allocate official descriptors?  See what this code
4250
         does.  */
4251
4252
0
      relocation = 0;
4253
0
      addend = 0;
4254
4255
0
      if (r_type == R_SH_FUNCDESC)
4256
0
        {
4257
0
    reloc_section = input_section;
4258
0
    reloc_offset = rel->r_offset;
4259
0
        }
4260
0
      else
4261
0
        {
4262
0
    reloc_section = sgot;
4263
4264
0
    if (h != NULL)
4265
0
      reloc_offset = h->got.offset;
4266
0
    else
4267
0
      {
4268
0
        BFD_ASSERT (local_got_offsets != NULL);
4269
0
        reloc_offset = local_got_offsets[r_symndx];
4270
0
      }
4271
0
    BFD_ASSERT (reloc_offset != MINUS_ONE);
4272
4273
0
    if (reloc_offset & 1)
4274
0
      {
4275
0
        reloc_offset &= ~1;
4276
0
        goto funcdesc_done_got;
4277
0
      }
4278
0
        }
4279
4280
0
      if (h && h->root.type == bfd_link_hash_undefweak
4281
0
    && (SYMBOL_CALLS_LOCAL (info, h)
4282
0
        || !htab->root.dynamic_sections_created))
4283
        /* Undefined weak symbol which will not be dynamically
4284
     resolved later; leave it at zero.  */
4285
0
        goto funcdesc_leave_zero;
4286
0
      else if (SYMBOL_CALLS_LOCAL (info, h)
4287
0
         && ! SYMBOL_FUNCDESC_LOCAL (info, h))
4288
0
        {
4289
    /* If the symbol needs a non-local function descriptor
4290
       but binds locally (i.e., its visibility is
4291
       protected), emit a dynamic relocation decayed to
4292
       section+offset.  This is an optimization; the dynamic
4293
       linker would resolve our function descriptor request
4294
       to our copy of the function anyway.  */
4295
0
    dynindx = elf_section_data (h->root.u.def.section
4296
0
              ->output_section)->dynindx;
4297
0
    relocation += h->root.u.def.section->output_offset
4298
0
      + h->root.u.def.value;
4299
0
        }
4300
0
      else if (! SYMBOL_FUNCDESC_LOCAL (info, h))
4301
0
        {
4302
    /* If the symbol is dynamic and there will be dynamic
4303
       symbol resolution because we are or are linked with a
4304
       shared library, emit a FUNCDESC relocation such that
4305
       the dynamic linker will allocate the function
4306
       descriptor.  */
4307
0
    BFD_ASSERT (h->dynindx != -1);
4308
0
    dynindx = h->dynindx;
4309
0
        }
4310
0
      else
4311
0
        {
4312
0
    bfd_vma offset;
4313
4314
    /* Otherwise, we know we have a private function
4315
       descriptor, so reference it directly.  */
4316
0
    reloc_type = R_SH_DIR32;
4317
0
    dynindx = elf_section_data (htab->sfuncdesc
4318
0
              ->output_section)->dynindx;
4319
4320
0
    if (h)
4321
0
      {
4322
0
        offset = sh_elf_hash_entry (h)->funcdesc.offset;
4323
0
        BFD_ASSERT (offset != MINUS_ONE);
4324
0
        if ((offset & 1) == 0)
4325
0
          {
4326
0
      if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
4327
0
               offset, NULL, 0))
4328
0
        return false;
4329
0
      sh_elf_hash_entry (h)->funcdesc.offset |= 1;
4330
0
          }
4331
0
      }
4332
0
    else
4333
0
      {
4334
0
        union gotref *local_funcdesc;
4335
4336
0
        local_funcdesc = sh_elf_local_funcdesc (input_bfd);
4337
0
        offset = local_funcdesc[r_symndx].offset;
4338
0
        BFD_ASSERT (offset != MINUS_ONE);
4339
0
        if ((offset & 1) == 0)
4340
0
          {
4341
0
      if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
4342
0
               offset, sec,
4343
0
               sym->st_value))
4344
0
        return false;
4345
0
      local_funcdesc[r_symndx].offset |= 1;
4346
0
          }
4347
0
      }
4348
4349
0
    relocation = htab->sfuncdesc->output_offset + (offset & ~1);
4350
0
        }
4351
4352
0
      if (!bfd_link_pic (info) && SYMBOL_FUNCDESC_LOCAL (info, h))
4353
0
        {
4354
0
    bfd_vma offset;
4355
4356
0
    if (sh_elf_osec_readonly_p (output_bfd,
4357
0
              reloc_section->output_section))
4358
0
      {
4359
0
        _bfd_error_handler
4360
          /* xgettext:c-format */
4361
0
          (_("%pB(%pA+%#" PRIx64 "): "
4362
0
       "cannot emit fixup to `%s' in read-only section"),
4363
0
           input_bfd,
4364
0
           input_section,
4365
0
           (uint64_t) rel->r_offset,
4366
0
           symname);
4367
0
        return false;
4368
0
      }
4369
4370
0
    offset = _bfd_elf_section_offset (output_bfd, info,
4371
0
              reloc_section, reloc_offset);
4372
4373
0
    if (offset != (bfd_vma)-1)
4374
0
      sh_elf_add_rofixup (output_bfd, htab->srofixup,
4375
0
              offset
4376
0
              + reloc_section->output_section->vma
4377
0
              + reloc_section->output_offset);
4378
0
        }
4379
0
      else if ((reloc_section->output_section->flags
4380
0
          & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
4381
0
        {
4382
0
    bfd_vma offset;
4383
4384
0
    if (sh_elf_osec_readonly_p (output_bfd,
4385
0
              reloc_section->output_section))
4386
0
      {
4387
0
        info->callbacks->warning
4388
0
          (info,
4389
0
           _("cannot emit dynamic relocations in read-only section"),
4390
0
           symname, input_bfd, reloc_section, reloc_offset);
4391
0
        return false;
4392
0
      }
4393
4394
0
    offset = _bfd_elf_section_offset (output_bfd, info,
4395
0
              reloc_section, reloc_offset);
4396
4397
0
    if (offset != (bfd_vma)-1)
4398
0
      sh_elf_add_dyn_reloc (output_bfd, srelgot,
4399
0
          offset
4400
0
          + reloc_section->output_section->vma
4401
0
          + reloc_section->output_offset,
4402
0
          reloc_type, dynindx, relocation);
4403
4404
0
    if (r_type == R_SH_FUNCDESC)
4405
0
      {
4406
0
        r = bfd_reloc_ok;
4407
0
        break;
4408
0
      }
4409
0
    else
4410
0
      {
4411
0
        relocation = 0;
4412
0
        goto funcdesc_leave_zero;
4413
0
      }
4414
0
        }
4415
4416
0
      if (SYMBOL_FUNCDESC_LOCAL (info, h))
4417
0
        relocation += htab->sfuncdesc->output_section->vma;
4418
0
    funcdesc_leave_zero:
4419
0
      if (r_type != R_SH_FUNCDESC)
4420
0
        {
4421
0
    bfd_put_32 (output_bfd, relocation,
4422
0
          reloc_section->contents + reloc_offset);
4423
0
    if (h != NULL)
4424
0
      h->got.offset |= 1;
4425
0
    else
4426
0
      local_got_offsets[r_symndx] |= 1;
4427
4428
0
        funcdesc_done_got:
4429
4430
0
    relocation = sh_elf_got_offset (htab) + reloc_offset;
4431
#ifdef GOT_BIAS
4432
    relocation -= GOT_BIAS;
4433
#endif
4434
0
        }
4435
0
      if (r_type == R_SH_GOTFUNCDESC20)
4436
0
        {
4437
0
    r = install_movi20_field (output_bfd, relocation + addend,
4438
0
            input_bfd, input_section, contents,
4439
0
            rel->r_offset);
4440
0
    break;
4441
0
        }
4442
0
      else
4443
0
        goto final_link_relocate;
4444
0
    }
4445
0
    break;
4446
4447
0
  case R_SH_GOTOFFFUNCDESC:
4448
0
  case R_SH_GOTOFFFUNCDESC20:
4449
    /* FIXME: See R_SH_FUNCDESC comment about global symbols in the
4450
       executable and --export-dynamic.  If such symbols get
4451
       ld.so-allocated descriptors we can not use R_SH_GOTOFFFUNCDESC
4452
       for them.  */
4453
0
    BFD_ASSERT (htab);
4454
4455
0
    check_segment[0] = check_segment[1] = -1;
4456
0
    relocation = 0;
4457
0
    addend = rel->r_addend;
4458
4459
0
    if (h && (h->root.type == bfd_link_hash_undefweak
4460
0
        || !SYMBOL_FUNCDESC_LOCAL (info, h)))
4461
0
      {
4462
0
        _bfd_error_handler
4463
    /* xgettext:c-format */
4464
0
    (_("%pB(%pA+%#" PRIx64 "): "
4465
0
       "%s relocation against external symbol \"%s\""),
4466
0
     input_bfd, input_section, (uint64_t) rel->r_offset,
4467
0
     howto->name, h->root.root.string);
4468
0
        return false;
4469
0
      }
4470
0
    else
4471
0
      {
4472
0
        bfd_vma offset;
4473
4474
        /* Otherwise, we know we have a private function
4475
     descriptor, so reference it directly.  */
4476
0
        if (h)
4477
0
    {
4478
0
      offset = sh_elf_hash_entry (h)->funcdesc.offset;
4479
0
      BFD_ASSERT (offset != MINUS_ONE);
4480
0
      if ((offset & 1) == 0)
4481
0
        {
4482
0
          if (!sh_elf_initialize_funcdesc (output_bfd, info, h,
4483
0
                   offset, NULL, 0))
4484
0
      return false;
4485
0
          sh_elf_hash_entry (h)->funcdesc.offset |= 1;
4486
0
        }
4487
0
    }
4488
0
        else
4489
0
    {
4490
0
      union gotref *local_funcdesc;
4491
4492
0
      local_funcdesc = sh_elf_local_funcdesc (input_bfd);
4493
0
      offset = local_funcdesc[r_symndx].offset;
4494
0
      BFD_ASSERT (offset != MINUS_ONE);
4495
0
      if ((offset & 1) == 0)
4496
0
        {
4497
0
          if (!sh_elf_initialize_funcdesc (output_bfd, info, NULL,
4498
0
                   offset, sec,
4499
0
                   sym->st_value))
4500
0
      return false;
4501
0
          local_funcdesc[r_symndx].offset |= 1;
4502
0
        }
4503
0
    }
4504
4505
0
        relocation = htab->sfuncdesc->output_offset + (offset & ~1);
4506
0
      }
4507
4508
0
    relocation -= (htab->root.hgot->root.u.def.value
4509
0
       + sgotplt->output_offset);
4510
#ifdef GOT_BIAS
4511
    relocation -= GOT_BIAS;
4512
#endif
4513
4514
0
    if (r_type == R_SH_GOTOFFFUNCDESC20)
4515
0
      {
4516
0
        r = install_movi20_field (output_bfd, relocation + addend,
4517
0
          input_bfd, input_section, contents,
4518
0
          rel->r_offset);
4519
0
        break;
4520
0
      }
4521
0
    else
4522
0
      goto final_link_relocate;
4523
4524
0
  case R_SH_LOOP_START:
4525
0
    {
4526
0
      static bfd_vma start, end;
4527
4528
0
      start = (relocation + rel->r_addend
4529
0
         - (sec->output_section->vma + sec->output_offset));
4530
0
      r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
4531
0
           rel->r_offset, sec, start, end);
4532
0
      break;
4533
4534
0
  case R_SH_LOOP_END:
4535
0
      end = (relocation + rel->r_addend
4536
0
       - (sec->output_section->vma + sec->output_offset));
4537
0
      r = sh_elf_reloc_loop (r_type, input_bfd, input_section, contents,
4538
0
           rel->r_offset, sec, start, end);
4539
0
      break;
4540
0
    }
4541
4542
0
  case R_SH_TLS_GD_32:
4543
0
  case R_SH_TLS_IE_32:
4544
0
    BFD_ASSERT (htab);
4545
0
    check_segment[0] = check_segment[1] = -1;
4546
0
    r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
4547
0
    got_type = GOT_UNKNOWN;
4548
0
    if (h == NULL && local_got_offsets)
4549
0
      got_type = sh_elf_local_got_type (input_bfd) [r_symndx];
4550
0
    else if (h != NULL)
4551
0
      {
4552
0
        got_type = sh_elf_hash_entry (h)->got_type;
4553
0
        if (! bfd_link_pic (info)
4554
0
      && (h->dynindx == -1
4555
0
          || h->def_regular))
4556
0
    r_type = R_SH_TLS_LE_32;
4557
0
      }
4558
4559
0
    if (r_type == R_SH_TLS_GD_32 && got_type == GOT_TLS_IE)
4560
0
      r_type = R_SH_TLS_IE_32;
4561
4562
0
    if (r_type == R_SH_TLS_LE_32)
4563
0
      {
4564
0
        bfd_vma offset;
4565
0
        unsigned short insn;
4566
4567
0
        if (ELF32_R_TYPE (rel->r_info) == R_SH_TLS_GD_32)
4568
0
    {
4569
      /* GD->LE transition:
4570
           mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4571
           jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4572
           1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4573
         We change it into:
4574
           mov.l 1f,r4; stc gbr,r0; add r4,r0; nop;
4575
           nop; nop; ...
4576
           1: .long x@TPOFF; 2: .long __tls_get_addr@PLT; 3:.  */
4577
4578
0
      offset = rel->r_offset;
4579
0
      if (offset < 16)
4580
0
        {
4581
0
          _bfd_error_handler
4582
      /* xgettext:c-format */
4583
0
      (_("%pB(%pA): offset in relocation for GD->LE translation is too small: %#" PRIx64),
4584
0
       input_bfd, input_section, (uint64_t) offset);
4585
0
          return false;
4586
0
        }
4587
4588
      /* Size of GD instructions is 16 or 18.  */
4589
0
      offset -= 16;
4590
0
      insn = bfd_get_16 (input_bfd, contents + offset + 0);
4591
0
      if ((insn & 0xff00) == 0xc700)
4592
0
        {
4593
0
          BFD_ASSERT (offset >= 2);
4594
0
          offset -= 2;
4595
0
          insn = bfd_get_16 (input_bfd, contents + offset + 0);
4596
0
        }
4597
4598
0
      if ((insn & 0xff00) != 0xd400)
4599
0
        _bfd_error_handler
4600
          /* xgettext:c-format */  /* The backslash is to prevent bogus trigraph detection.  */
4601
0
          (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd4?\?)"),
4602
0
           input_bfd, input_section, (uint64_t) offset, (int) insn);
4603
4604
0
      insn = bfd_get_16 (input_bfd, contents + offset + 2);
4605
4606
0
      if ((insn & 0xff00) != 0xc700)
4607
0
        _bfd_error_handler
4608
          /* xgettext:c-format */
4609
0
          (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xc7?\?)"),
4610
0
           input_bfd, input_section, (uint64_t) offset, (int) insn);
4611
4612
0
      insn = bfd_get_16 (input_bfd, contents + offset + 4);
4613
0
      if ((insn & 0xff00) != 0xd100)
4614
0
        _bfd_error_handler
4615
          /* xgettext:c-format */
4616
0
          (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd1?\?)"),
4617
0
           input_bfd, input_section, (uint64_t) offset, (int) insn);
4618
4619
0
      insn = bfd_get_16 (input_bfd, contents + offset + 6);
4620
0
      if (insn != 0x310c)
4621
0
        _bfd_error_handler
4622
          /* xgettext:c-format */
4623
0
          (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x310c)"),
4624
0
           input_bfd, input_section, (uint64_t) offset, (int) insn);
4625
4626
0
      insn = bfd_get_16 (input_bfd, contents + offset + 8);
4627
0
      if (insn != 0x410b)
4628
0
        _bfd_error_handler
4629
          /* xgettext:c-format */
4630
0
          (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x410b)"),
4631
0
           input_bfd, input_section, (uint64_t) offset, (int) insn);
4632
4633
0
      insn = bfd_get_16 (input_bfd, contents + offset + 10);
4634
0
      if (insn != 0x34cc)
4635
0
        _bfd_error_handler
4636
          /* xgettext:c-format */
4637
0
          (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x34cc)"),
4638
0
           input_bfd, input_section, (uint64_t) offset, (int) insn);
4639
4640
0
      bfd_put_16 (output_bfd, 0x0012, contents + offset + 2);
4641
0
      bfd_put_16 (output_bfd, 0x304c, contents + offset + 4);
4642
0
      bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
4643
0
      bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4644
0
      bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4645
0
    }
4646
0
        else
4647
0
    {
4648
0
      int target;
4649
4650
      /* IE->LE transition:
4651
             mov.l 1f,r0;
4652
             stc gbr,rN;
4653
             mov.l @(r0,r12),rM;
4654
             bra 2f;
4655
             add ...;
4656
             .align 2;
4657
           1: x@GOTTPOFF;
4658
           2:
4659
         We change it into:
4660
             mov.l .Ln,rM;
4661
       stc gbr,rN;
4662
       nop;
4663
       ...;
4664
           1: x@TPOFF;
4665
           2:.  */
4666
4667
0
      offset = rel->r_offset;
4668
0
      if (offset < 16)
4669
0
        {
4670
0
          _bfd_error_handler
4671
      /* xgettext:c-format */
4672
0
      (_("%pB(%pA): offset in relocation for IE->LE translation is too small: %#" PRIx64),
4673
0
       input_bfd, input_section, (uint64_t) offset);
4674
0
          return false;
4675
0
        }
4676
4677
      /* Size of IE instructions is 10 or 12.  */
4678
0
      offset -= 10;
4679
0
      insn = bfd_get_16 (input_bfd, contents + offset + 0);
4680
0
      if ((insn & 0xf0ff) == 0x0012)
4681
0
        {
4682
0
          BFD_ASSERT (offset >= 2);
4683
0
          offset -= 2;
4684
0
          insn = bfd_get_16 (input_bfd, contents + offset + 0);
4685
0
        }
4686
4687
0
      if ((insn & 0xff00) != 0xd000)
4688
0
        _bfd_error_handler
4689
          /* xgettext:c-format */
4690
0
          (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0xd0??: mov.l)"),
4691
0
           input_bfd, input_section, (uint64_t) offset, (int) insn);
4692
4693
0
      target = insn & 0x00ff;
4694
4695
0
      insn = bfd_get_16 (input_bfd, contents + offset + 2);
4696
0
      if ((insn & 0xf0ff) != 0x0012)
4697
0
        _bfd_error_handler
4698
          /* xgettext:c-format */
4699
0
          (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x0?12: stc)"),
4700
0
           input_bfd, input_section, (uint64_t) (offset + 2), (int) insn);
4701
4702
0
      insn = bfd_get_16 (input_bfd, contents + offset + 4);
4703
0
      if ((insn & 0xf0ff) != 0x00ce)
4704
0
        _bfd_error_handler
4705
          /* xgettext:c-format */
4706
0
          (_("%pB(%pA+%#" PRIx64 "): unexpected instruction %#04X (expected 0x0?ce: mov.l)"),
4707
0
           input_bfd, input_section, (uint64_t) (offset + 4), (int) insn);
4708
4709
0
      insn = 0xd000 | (insn & 0x0f00) | target;
4710
0
      bfd_put_16 (output_bfd, insn, contents + offset + 0);
4711
0
      bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
4712
0
    }
4713
4714
0
        bfd_put_32 (output_bfd, tpoff (info, relocation),
4715
0
        contents + rel->r_offset);
4716
0
        continue;
4717
0
      }
4718
4719
0
    if (sgot == NULL || sgotplt == NULL)
4720
0
      abort ();
4721
4722
0
    if (h != NULL)
4723
0
      off = h->got.offset;
4724
0
    else
4725
0
      {
4726
0
        if (local_got_offsets == NULL)
4727
0
    abort ();
4728
4729
0
        off = local_got_offsets[r_symndx];
4730
0
      }
4731
4732
    /* Relocate R_SH_TLS_IE_32 directly when statically linking.  */
4733
0
    if (r_type == R_SH_TLS_IE_32
4734
0
        && ! htab->root.dynamic_sections_created)
4735
0
      {
4736
0
        off &= ~1;
4737
0
        bfd_put_32 (output_bfd, tpoff (info, relocation),
4738
0
        sgot->contents + off);
4739
0
        bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
4740
0
        contents + rel->r_offset);
4741
0
        continue;
4742
0
      }
4743
4744
0
    if ((off & 1) != 0)
4745
0
      off &= ~1;
4746
0
    else
4747
0
      {
4748
0
        Elf_Internal_Rela outrel;
4749
0
        bfd_byte *loc;
4750
0
        int dr_type, indx;
4751
4752
0
        outrel.r_offset = (sgot->output_section->vma
4753
0
         + sgot->output_offset + off);
4754
4755
0
        if (h == NULL || h->dynindx == -1)
4756
0
    indx = 0;
4757
0
        else
4758
0
    indx = h->dynindx;
4759
4760
0
        dr_type = (r_type == R_SH_TLS_GD_32 ? R_SH_TLS_DTPMOD32 :
4761
0
       R_SH_TLS_TPOFF32);
4762
0
        if (dr_type == R_SH_TLS_TPOFF32 && indx == 0)
4763
0
    outrel.r_addend = relocation - dtpoff_base (info);
4764
0
        else
4765
0
    outrel.r_addend = 0;
4766
0
        outrel.r_info = ELF32_R_INFO (indx, dr_type);
4767
0
        loc = srelgot->contents;
4768
0
        loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4769
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4770
4771
0
        if (r_type == R_SH_TLS_GD_32)
4772
0
    {
4773
0
      if (indx == 0)
4774
0
        {
4775
0
          bfd_put_32 (output_bfd,
4776
0
          relocation - dtpoff_base (info),
4777
0
          sgot->contents + off + 4);
4778
0
        }
4779
0
      else
4780
0
        {
4781
0
          outrel.r_info = ELF32_R_INFO (indx,
4782
0
                R_SH_TLS_DTPOFF32);
4783
0
          outrel.r_offset += 4;
4784
0
          outrel.r_addend = 0;
4785
0
          srelgot->reloc_count++;
4786
0
          loc += sizeof (Elf32_External_Rela);
4787
0
          bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4788
0
        }
4789
0
    }
4790
4791
0
        if (h != NULL)
4792
0
    h->got.offset |= 1;
4793
0
        else
4794
0
    local_got_offsets[r_symndx] |= 1;
4795
0
      }
4796
4797
0
    if (off >= (bfd_vma) -2)
4798
0
      abort ();
4799
4800
0
    if (r_type == (int) ELF32_R_TYPE (rel->r_info))
4801
0
      relocation = sh_elf_got_offset (htab) + off;
4802
0
    else
4803
0
      {
4804
0
        bfd_vma offset;
4805
0
        unsigned short insn;
4806
4807
        /* GD->IE transition:
4808
       mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4809
       jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4810
       1: .long x$TLSGD; 2: .long __tls_get_addr@PLT; 3:
4811
     We change it into:
4812
       mov.l 1f,r0; stc gbr,r4; mov.l @(r0,r12),r0; add r4,r0;
4813
       nop; nop; bra 3f; nop; .align 2;
4814
       1: .long x@TPOFF; 2:...; 3:.  */
4815
4816
0
        offset = rel->r_offset;
4817
0
        if (offset < 16)
4818
0
    {
4819
0
      _bfd_error_handler
4820
        /* xgettext:c-format */
4821
0
        (_("%pB(%pA): offset in relocation for GD->IE translation is too small: %#" PRIx64),
4822
0
         input_bfd, input_section, (uint64_t) offset);
4823
0
      return false;
4824
0
    }
4825
4826
        /* Size of GD instructions is 16 or 18.  */
4827
0
        offset -= 16;
4828
0
        insn = bfd_get_16 (input_bfd, contents + offset + 0);
4829
0
        if ((insn & 0xff00) == 0xc700)
4830
0
    {
4831
0
      BFD_ASSERT (offset >= 2);
4832
0
      offset -= 2;
4833
0
      insn = bfd_get_16 (input_bfd, contents + offset + 0);
4834
0
    }
4835
4836
0
        BFD_ASSERT ((insn & 0xff00) == 0xd400);
4837
4838
        /* Replace mov.l 1f,R4 with mov.l 1f,r0.  */
4839
0
        bfd_put_16 (output_bfd, insn & 0xf0ff, contents + offset);
4840
4841
0
        insn = bfd_get_16 (input_bfd, contents + offset + 2);
4842
0
        BFD_ASSERT ((insn & 0xff00) == 0xc700);
4843
0
        insn = bfd_get_16 (input_bfd, contents + offset + 4);
4844
0
        BFD_ASSERT ((insn & 0xff00) == 0xd100);
4845
0
        insn = bfd_get_16 (input_bfd, contents + offset + 6);
4846
0
        BFD_ASSERT (insn == 0x310c);
4847
0
        insn = bfd_get_16 (input_bfd, contents + offset + 8);
4848
0
        BFD_ASSERT (insn == 0x410b);
4849
0
        insn = bfd_get_16 (input_bfd, contents + offset + 10);
4850
0
        BFD_ASSERT (insn == 0x34cc);
4851
4852
0
        bfd_put_16 (output_bfd, 0x0412, contents + offset + 2);
4853
0
        bfd_put_16 (output_bfd, 0x00ce, contents + offset + 4);
4854
0
        bfd_put_16 (output_bfd, 0x304c, contents + offset + 6);
4855
0
        bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4856
0
        bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4857
4858
0
        bfd_put_32 (output_bfd, sh_elf_got_offset (htab) + off,
4859
0
        contents + rel->r_offset);
4860
4861
0
        continue;
4862
0
    }
4863
4864
0
    addend = rel->r_addend;
4865
4866
0
    goto final_link_relocate;
4867
4868
0
  case R_SH_TLS_LD_32:
4869
0
    BFD_ASSERT (htab);
4870
0
    check_segment[0] = check_segment[1] = -1;
4871
0
    if (! bfd_link_pic (info))
4872
0
      {
4873
0
        bfd_vma offset;
4874
0
        unsigned short insn;
4875
4876
        /* LD->LE transition:
4877
       mov.l 1f,r4; mova 2f,r0; mov.l 2f,r1; add r0,r1;
4878
       jsr @r1; add r12,r4; bra 3f; nop; .align 2;
4879
       1: .long x$TLSLD; 2: .long __tls_get_addr@PLT; 3:
4880
     We change it into:
4881
       stc gbr,r0; nop; nop; nop;
4882
       nop; nop; bra 3f; ...; 3:.  */
4883
4884
0
        offset = rel->r_offset;
4885
0
        if (offset < 16)
4886
0
    {
4887
0
      _bfd_error_handler
4888
        /* xgettext:c-format */
4889
0
        (_("%pB(%pA): offset in relocation for LD->LE translation is too small: %#" PRIx64),
4890
0
         input_bfd, input_section, (uint64_t) offset);
4891
0
      return false;
4892
0
    }
4893
4894
        /* Size of LD instructions is 16 or 18.  */
4895
0
        offset -= 16;
4896
0
        insn = bfd_get_16 (input_bfd, contents + offset + 0);
4897
0
        if ((insn & 0xff00) == 0xc700)
4898
0
    {
4899
0
      BFD_ASSERT (offset >= 2);
4900
0
      offset -= 2;
4901
0
      insn = bfd_get_16 (input_bfd, contents + offset + 0);
4902
0
    }
4903
4904
0
        BFD_ASSERT ((insn & 0xff00) == 0xd400);
4905
0
        insn = bfd_get_16 (input_bfd, contents + offset + 2);
4906
0
        BFD_ASSERT ((insn & 0xff00) == 0xc700);
4907
0
        insn = bfd_get_16 (input_bfd, contents + offset + 4);
4908
0
        BFD_ASSERT ((insn & 0xff00) == 0xd100);
4909
0
        insn = bfd_get_16 (input_bfd, contents + offset + 6);
4910
0
        BFD_ASSERT (insn == 0x310c);
4911
0
        insn = bfd_get_16 (input_bfd, contents + offset + 8);
4912
0
        BFD_ASSERT (insn == 0x410b);
4913
0
        insn = bfd_get_16 (input_bfd, contents + offset + 10);
4914
0
        BFD_ASSERT (insn == 0x34cc);
4915
4916
0
        bfd_put_16 (output_bfd, 0x0012, contents + offset + 0);
4917
0
        bfd_put_16 (output_bfd, 0x0009, contents + offset + 2);
4918
0
        bfd_put_16 (output_bfd, 0x0009, contents + offset + 4);
4919
0
        bfd_put_16 (output_bfd, 0x0009, contents + offset + 6);
4920
0
        bfd_put_16 (output_bfd, 0x0009, contents + offset + 8);
4921
0
        bfd_put_16 (output_bfd, 0x0009, contents + offset + 10);
4922
4923
0
        continue;
4924
0
      }
4925
4926
0
    if (sgot == NULL || sgotplt == NULL)
4927
0
      abort ();
4928
4929
0
    off = htab->tls_ldm_got.offset;
4930
0
    if (off & 1)
4931
0
      off &= ~1;
4932
0
    else
4933
0
      {
4934
0
        Elf_Internal_Rela outrel;
4935
0
        bfd_byte *loc;
4936
4937
0
        outrel.r_offset = (sgot->output_section->vma
4938
0
         + sgot->output_offset + off);
4939
0
        outrel.r_addend = 0;
4940
0
        outrel.r_info = ELF32_R_INFO (0, R_SH_TLS_DTPMOD32);
4941
0
        loc = srelgot->contents;
4942
0
        loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
4943
0
        bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
4944
0
        htab->tls_ldm_got.offset |= 1;
4945
0
      }
4946
4947
0
    relocation = sh_elf_got_offset (htab) + off;
4948
0
    addend = rel->r_addend;
4949
4950
0
    goto final_link_relocate;
4951
4952
0
  case R_SH_TLS_LDO_32:
4953
0
    check_segment[0] = check_segment[1] = -1;
4954
0
    if (! bfd_link_pic (info))
4955
0
      relocation = tpoff (info, relocation);
4956
0
    else
4957
0
      relocation -= dtpoff_base (info);
4958
4959
0
    addend = rel->r_addend;
4960
0
    goto final_link_relocate;
4961
4962
0
  case R_SH_TLS_LE_32:
4963
0
    {
4964
0
      int indx;
4965
0
      Elf_Internal_Rela outrel;
4966
0
      bfd_byte *loc;
4967
4968
0
      check_segment[0] = check_segment[1] = -1;
4969
4970
0
      if (!bfd_link_dll (info))
4971
0
        {
4972
0
    relocation = tpoff (info, relocation);
4973
0
    addend = rel->r_addend;
4974
0
    goto final_link_relocate;
4975
0
        }
4976
4977
0
      if (sreloc == NULL)
4978
0
        {
4979
0
    sreloc = _bfd_elf_get_dynamic_reloc_section
4980
0
      (input_bfd, input_section, /*rela?*/ true);
4981
0
    if (sreloc == NULL)
4982
0
      return false;
4983
0
        }
4984
4985
0
      if (h == NULL || h->dynindx == -1)
4986
0
        indx = 0;
4987
0
      else
4988
0
        indx = h->dynindx;
4989
4990
0
      outrel.r_offset = (input_section->output_section->vma
4991
0
             + input_section->output_offset
4992
0
             + rel->r_offset);
4993
0
      outrel.r_info = ELF32_R_INFO (indx, R_SH_TLS_TPOFF32);
4994
0
      if (indx == 0)
4995
0
        outrel.r_addend = relocation - dtpoff_base (info);
4996
0
      else
4997
0
        outrel.r_addend = 0;
4998
4999
0
      loc = sreloc->contents;
5000
0
      loc += sreloc->reloc_count++ * sizeof (Elf32_External_Rela);
5001
0
      bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
5002
0
      continue;
5003
0
    }
5004
0
  }
5005
5006
0
    relocation_done:
5007
0
      if (fdpic_p && check_segment[0] != (unsigned) -1
5008
0
    && check_segment[0] != check_segment[1])
5009
0
  {
5010
    /* We don't want duplicate errors for undefined symbols.  */
5011
0
    if (!h || h->root.type != bfd_link_hash_undefined)
5012
0
      {
5013
0
        if (bfd_link_pic (info))
5014
0
    {
5015
0
      info->callbacks->einfo
5016
        /* xgettext:c-format */
5017
0
        (_("%X%H: relocation to \"%s\" references a different segment\n"),
5018
0
         input_bfd, input_section, rel->r_offset, symname);
5019
0
      return false;
5020
0
    }
5021
0
        else
5022
0
    info->callbacks->einfo
5023
      /* xgettext:c-format */
5024
0
      (_("%H: warning: relocation to \"%s\" references a different segment\n"),
5025
0
       input_bfd, input_section, rel->r_offset, symname);
5026
0
      }
5027
5028
0
    elf_elfheader (output_bfd)->e_flags |= EF_SH_PIC;
5029
0
  }
5030
5031
0
      if (r != bfd_reloc_ok)
5032
0
  {
5033
0
    switch (r)
5034
0
      {
5035
0
      default:
5036
0
      case bfd_reloc_outofrange:
5037
0
        abort ();
5038
0
      case bfd_reloc_overflow:
5039
0
        {
5040
0
    const char *name;
5041
5042
0
    if (h != NULL)
5043
0
      name = NULL;
5044
0
    else
5045
0
      {
5046
0
        name = (bfd_elf_string_from_elf_section
5047
0
          (input_bfd, symtab_hdr->sh_link, sym->st_name));
5048
0
        if (name == NULL)
5049
0
          return false;
5050
0
        if (*name == '\0')
5051
0
          name = bfd_section_name (sec);
5052
0
      }
5053
0
    (*info->callbacks->reloc_overflow)
5054
0
      (info, (h ? &h->root : NULL), name, howto->name,
5055
0
       (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
5056
0
        }
5057
0
        break;
5058
0
      }
5059
0
  }
5060
0
    }
5061
5062
0
  return true;
5063
0
}
5064
5065
/* This is a version of bfd_generic_get_relocated_section_contents
5066
   which uses sh_elf_relocate_section.  */
5067
5068
static bfd_byte *
5069
sh_elf_get_relocated_section_contents (bfd *output_bfd,
5070
               struct bfd_link_info *link_info,
5071
               struct bfd_link_order *link_order,
5072
               bfd_byte *data,
5073
               bool relocatable,
5074
               asymbol **symbols)
5075
43
{
5076
43
  Elf_Internal_Shdr *symtab_hdr;
5077
43
  asection *input_section = link_order->u.indirect.section;
5078
43
  bfd *input_bfd = input_section->owner;
5079
43
  asection **sections = NULL;
5080
43
  Elf_Internal_Rela *internal_relocs = NULL;
5081
43
  Elf_Internal_Sym *isymbuf = NULL;
5082
5083
  /* We only need to handle the case of relaxing, or of having a
5084
     particular set of section contents, specially.  */
5085
43
  if (relocatable
5086
43
      || elf_section_data (input_section)->this_hdr.contents == NULL)
5087
43
    return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
5088
43
                   link_order, data,
5089
43
                   relocatable,
5090
43
                   symbols);
5091
5092
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
5093
5094
0
  bfd_byte *orig_data = data;
5095
0
  if (data == NULL)
5096
0
    {
5097
0
      data = bfd_malloc (input_section->size);
5098
0
      if (data == NULL)
5099
0
  return NULL;
5100
0
    }
5101
0
  memcpy (data, elf_section_data (input_section)->this_hdr.contents,
5102
0
    (size_t) input_section->size);
5103
5104
0
  if ((input_section->flags & SEC_RELOC) != 0
5105
0
      && input_section->reloc_count > 0)
5106
0
    {
5107
0
      asection **secpp;
5108
0
      Elf_Internal_Sym *isym, *isymend;
5109
0
      bfd_size_type amt;
5110
5111
0
      internal_relocs = (_bfd_elf_link_read_relocs
5112
0
       (input_bfd, input_section, NULL,
5113
0
        (Elf_Internal_Rela *) NULL, false));
5114
0
      if (internal_relocs == NULL)
5115
0
  goto error_return;
5116
5117
0
      if (symtab_hdr->sh_info != 0)
5118
0
  {
5119
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
5120
0
    if (isymbuf == NULL)
5121
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
5122
0
              symtab_hdr->sh_info, 0,
5123
0
              NULL, NULL, NULL);
5124
0
    if (isymbuf == NULL)
5125
0
      goto error_return;
5126
0
  }
5127
5128
0
      amt = symtab_hdr->sh_info;
5129
0
      amt *= sizeof (asection *);
5130
0
      sections = (asection **) bfd_malloc (amt);
5131
0
      if (sections == NULL && amt != 0)
5132
0
  goto error_return;
5133
5134
0
      isymend = isymbuf + symtab_hdr->sh_info;
5135
0
      for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
5136
0
  {
5137
0
    asection *isec;
5138
5139
0
    if (isym->st_shndx == SHN_UNDEF)
5140
0
      isec = bfd_und_section_ptr;
5141
0
    else if (isym->st_shndx == SHN_ABS)
5142
0
      isec = bfd_abs_section_ptr;
5143
0
    else if (isym->st_shndx == SHN_COMMON)
5144
0
      isec = bfd_com_section_ptr;
5145
0
    else
5146
0
      isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
5147
5148
0
    *secpp = isec;
5149
0
  }
5150
5151
0
      if (! sh_elf_relocate_section (output_bfd, link_info, input_bfd,
5152
0
             input_section, data, internal_relocs,
5153
0
             isymbuf, sections))
5154
0
  goto error_return;
5155
5156
0
      free (sections);
5157
0
      if (symtab_hdr->contents != (unsigned char *) isymbuf)
5158
0
  free (isymbuf);
5159
0
      if (elf_section_data (input_section)->relocs != internal_relocs)
5160
0
  free (internal_relocs);
5161
0
    }
5162
5163
0
  return data;
5164
5165
0
 error_return:
5166
0
  free (sections);
5167
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
5168
0
    free (isymbuf);
5169
0
  if (elf_section_data (input_section)->relocs != internal_relocs)
5170
0
    free (internal_relocs);
5171
0
  if (orig_data == NULL)
5172
0
    free (data);
5173
0
  return NULL;
5174
0
}
5175
5176
/* Return the base VMA address which should be subtracted from real addresses
5177
   when resolving @dtpoff relocation.
5178
   This is PT_TLS segment p_vaddr.  */
5179
5180
static bfd_vma
5181
dtpoff_base (struct bfd_link_info *info)
5182
0
{
5183
  /* If tls_sec is NULL, we should have signalled an error already.  */
5184
0
  if (elf_hash_table (info)->tls_sec == NULL)
5185
0
    return 0;
5186
0
  return elf_hash_table (info)->tls_sec->vma;
5187
0
}
5188
5189
/* Return the relocation value for R_SH_TLS_TPOFF32..  */
5190
5191
static bfd_vma
5192
tpoff (struct bfd_link_info *info, bfd_vma address)
5193
0
{
5194
  /* If tls_sec is NULL, we should have signalled an error already.  */
5195
0
  if (elf_hash_table (info)->tls_sec == NULL)
5196
0
    return 0;
5197
  /* SH TLS ABI is variant I and static TLS block start just after tcbhead
5198
     structure which has 2 pointer fields.  */
5199
0
  return (address - elf_hash_table (info)->tls_sec->vma
5200
0
    + align_power ((bfd_vma) 8,
5201
0
       elf_hash_table (info)->tls_sec->alignment_power));
5202
0
}
5203
5204
static asection *
5205
sh_elf_gc_mark_hook (asection *sec,
5206
         struct bfd_link_info *info,
5207
         struct elf_reloc_cookie *cookie,
5208
         struct elf_link_hash_entry *h,
5209
         unsigned int symndx)
5210
0
{
5211
0
  if (h != NULL)
5212
0
    switch (ELF32_R_TYPE (cookie->rel->r_info))
5213
0
      {
5214
0
      case R_SH_GNU_VTINHERIT:
5215
0
      case R_SH_GNU_VTENTRY:
5216
0
  return NULL;
5217
0
      }
5218
5219
0
  return _bfd_elf_gc_mark_hook (sec, info, cookie, h, symndx);
5220
0
}
5221
5222
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
5223
5224
static void
5225
sh_elf_copy_indirect_symbol (struct bfd_link_info *info,
5226
           struct elf_link_hash_entry *dir,
5227
           struct elf_link_hash_entry *ind)
5228
0
{
5229
0
  struct elf_sh_link_hash_entry *edir, *eind;
5230
5231
0
  edir = (struct elf_sh_link_hash_entry *) dir;
5232
0
  eind = (struct elf_sh_link_hash_entry *) ind;
5233
5234
0
  edir->gotplt_refcount = eind->gotplt_refcount;
5235
0
  eind->gotplt_refcount = 0;
5236
0
  edir->funcdesc.refcount += eind->funcdesc.refcount;
5237
0
  eind->funcdesc.refcount = 0;
5238
0
  edir->abs_funcdesc_refcount += eind->abs_funcdesc_refcount;
5239
0
  eind->abs_funcdesc_refcount = 0;
5240
5241
0
  if (ind->root.type == bfd_link_hash_indirect
5242
0
      && dir->got.refcount <= 0)
5243
0
    {
5244
0
      edir->got_type = eind->got_type;
5245
0
      eind->got_type = GOT_UNKNOWN;
5246
0
    }
5247
5248
0
  if (ind->root.type != bfd_link_hash_indirect
5249
0
      && dir->dynamic_adjusted)
5250
0
    {
5251
      /* If called to transfer flags for a weakdef during processing
5252
   of elf_adjust_dynamic_symbol, don't copy non_got_ref.
5253
   We clear it ourselves for ELIMINATE_COPY_RELOCS.  */
5254
0
      if (dir->versioned != versioned_hidden)
5255
0
  dir->ref_dynamic |= ind->ref_dynamic;
5256
0
      dir->ref_regular |= ind->ref_regular;
5257
0
      dir->ref_regular_nonweak |= ind->ref_regular_nonweak;
5258
0
      dir->needs_plt |= ind->needs_plt;
5259
0
    }
5260
0
  else
5261
0
    _bfd_elf_link_hash_copy_indirect (info, dir, ind);
5262
0
}
5263
5264
static int
5265
sh_elf_optimized_tls_reloc (struct bfd_link_info *info, int r_type,
5266
          int is_local)
5267
0
{
5268
0
  if (bfd_link_pic (info))
5269
0
    return r_type;
5270
5271
0
  switch (r_type)
5272
0
    {
5273
0
    case R_SH_TLS_GD_32:
5274
0
    case R_SH_TLS_IE_32:
5275
0
      if (is_local)
5276
0
  return R_SH_TLS_LE_32;
5277
0
      return R_SH_TLS_IE_32;
5278
0
    case R_SH_TLS_LD_32:
5279
0
      return R_SH_TLS_LE_32;
5280
0
    }
5281
5282
0
  return r_type;
5283
0
}
5284
5285
/* Look through the relocs for a section during the first phase.
5286
   Since we don't do .gots or .plts, we just need to consider the
5287
   virtual table relocs for gc.  */
5288
5289
static bool
5290
sh_elf_check_relocs (bfd *abfd, struct bfd_link_info *info, asection *sec,
5291
         const Elf_Internal_Rela *relocs)
5292
0
{
5293
0
  Elf_Internal_Shdr *symtab_hdr;
5294
0
  struct elf_link_hash_entry **sym_hashes;
5295
0
  struct elf_sh_link_hash_table *htab;
5296
0
  const Elf_Internal_Rela *rel;
5297
0
  const Elf_Internal_Rela *rel_end;
5298
0
  asection *sreloc;
5299
0
  unsigned int r_type;
5300
0
  enum got_type got_type, old_got_type;
5301
5302
0
  sreloc = NULL;
5303
5304
0
  if (bfd_link_relocatable (info))
5305
0
    return true;
5306
5307
0
  BFD_ASSERT (is_sh_elf (abfd));
5308
5309
0
  symtab_hdr = &elf_symtab_hdr (abfd);
5310
0
  sym_hashes = elf_sym_hashes (abfd);
5311
5312
0
  htab = sh_elf_hash_table (info);
5313
0
  if (htab == NULL)
5314
0
    return false;
5315
5316
0
  rel_end = relocs + sec->reloc_count;
5317
0
  for (rel = relocs; rel < rel_end; rel++)
5318
0
    {
5319
0
      struct elf_link_hash_entry *h;
5320
0
      unsigned long r_symndx;
5321
5322
0
      r_symndx = ELF32_R_SYM (rel->r_info);
5323
0
      r_type = ELF32_R_TYPE (rel->r_info);
5324
5325
0
      if (r_symndx < symtab_hdr->sh_info)
5326
0
  h = NULL;
5327
0
      else
5328
0
  {
5329
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
5330
0
    while (h->root.type == bfd_link_hash_indirect
5331
0
     || h->root.type == bfd_link_hash_warning)
5332
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
5333
0
  }
5334
5335
0
      r_type = sh_elf_optimized_tls_reloc (info, r_type, h == NULL);
5336
0
      if (! bfd_link_pic (info)
5337
0
    && r_type == R_SH_TLS_IE_32
5338
0
    && h != NULL
5339
0
    && h->root.type != bfd_link_hash_undefined
5340
0
    && h->root.type != bfd_link_hash_undefweak
5341
0
    && (h->dynindx == -1
5342
0
        || h->def_regular))
5343
0
  r_type = R_SH_TLS_LE_32;
5344
5345
0
      if (htab->fdpic_p)
5346
0
  switch (r_type)
5347
0
    {
5348
0
    case R_SH_GOTOFFFUNCDESC:
5349
0
    case R_SH_GOTOFFFUNCDESC20:
5350
0
    case R_SH_FUNCDESC:
5351
0
    case R_SH_GOTFUNCDESC:
5352
0
    case R_SH_GOTFUNCDESC20:
5353
0
      if (h != NULL)
5354
0
        {
5355
0
    if (h->dynindx == -1)
5356
0
      switch (ELF_ST_VISIBILITY (h->other))
5357
0
        {
5358
0
        case STV_INTERNAL:
5359
0
        case STV_HIDDEN:
5360
0
          break;
5361
0
        default:
5362
0
          bfd_elf_link_record_dynamic_symbol (info, h);
5363
0
          break;
5364
0
        }
5365
0
        }
5366
0
      break;
5367
0
    }
5368
5369
      /* Some relocs require a global offset table.  */
5370
0
      if (htab->root.sgot == NULL)
5371
0
  {
5372
0
    switch (r_type)
5373
0
      {
5374
0
      case R_SH_DIR32:
5375
        /* This may require an rofixup.  */
5376
0
        if (!htab->fdpic_p)
5377
0
    break;
5378
        /* Fall through.  */
5379
0
      case R_SH_GOTPLT32:
5380
0
      case R_SH_GOT32:
5381
0
      case R_SH_GOT20:
5382
0
      case R_SH_GOTOFF:
5383
0
      case R_SH_GOTOFF20:
5384
0
      case R_SH_FUNCDESC:
5385
0
      case R_SH_GOTFUNCDESC:
5386
0
      case R_SH_GOTFUNCDESC20:
5387
0
      case R_SH_GOTOFFFUNCDESC:
5388
0
      case R_SH_GOTOFFFUNCDESC20:
5389
0
      case R_SH_GOTPC:
5390
0
      case R_SH_TLS_GD_32:
5391
0
      case R_SH_TLS_LD_32:
5392
0
      case R_SH_TLS_IE_32:
5393
0
        if (htab->root.dynobj == NULL)
5394
0
    htab->root.dynobj = abfd;
5395
0
        if (!create_got_section (htab->root.dynobj, info))
5396
0
    return false;
5397
0
        break;
5398
5399
0
      default:
5400
0
        break;
5401
0
      }
5402
0
  }
5403
5404
0
      switch (r_type)
5405
0
  {
5406
    /* This relocation describes the C++ object vtable hierarchy.
5407
       Reconstruct it for later use during GC.  */
5408
0
  case R_SH_GNU_VTINHERIT:
5409
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
5410
0
      return false;
5411
0
    break;
5412
5413
    /* This relocation describes which C++ vtable entries are actually
5414
       used.  Record for later use during GC.  */
5415
0
  case R_SH_GNU_VTENTRY:
5416
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
5417
0
      return false;
5418
0
    break;
5419
5420
0
  case R_SH_TLS_IE_32:
5421
0
    if (bfd_link_pic (info))
5422
0
      info->flags |= DF_STATIC_TLS;
5423
5424
    /* FALLTHROUGH */
5425
0
  force_got:
5426
0
  case R_SH_TLS_GD_32:
5427
0
  case R_SH_GOT32:
5428
0
  case R_SH_GOT20:
5429
0
  case R_SH_GOTFUNCDESC:
5430
0
  case R_SH_GOTFUNCDESC20:
5431
0
    switch (r_type)
5432
0
      {
5433
0
      default:
5434
0
        got_type = GOT_NORMAL;
5435
0
        break;
5436
0
      case R_SH_TLS_GD_32:
5437
0
        got_type = GOT_TLS_GD;
5438
0
        break;
5439
0
      case R_SH_TLS_IE_32:
5440
0
        got_type = GOT_TLS_IE;
5441
0
        break;
5442
0
      case R_SH_GOTFUNCDESC:
5443
0
      case R_SH_GOTFUNCDESC20:
5444
0
        got_type = GOT_FUNCDESC;
5445
0
        break;
5446
0
      }
5447
5448
0
    if (h != NULL)
5449
0
      {
5450
0
        h->got.refcount += 1;
5451
0
        old_got_type = sh_elf_hash_entry (h)->got_type;
5452
0
      }
5453
0
    else
5454
0
      {
5455
0
        bfd_signed_vma *local_got_refcounts;
5456
5457
        /* This is a global offset table entry for a local
5458
     symbol.  */
5459
0
        local_got_refcounts = elf_local_got_refcounts (abfd);
5460
0
        if (local_got_refcounts == NULL)
5461
0
    {
5462
0
      bfd_size_type size;
5463
5464
0
      size = symtab_hdr->sh_info;
5465
0
      size *= sizeof (bfd_signed_vma);
5466
0
      size += symtab_hdr->sh_info;
5467
0
      local_got_refcounts = ((bfd_signed_vma *)
5468
0
           bfd_zalloc (abfd, size));
5469
0
      if (local_got_refcounts == NULL)
5470
0
        return false;
5471
0
      elf_local_got_refcounts (abfd) = local_got_refcounts;
5472
0
      sh_elf_local_got_type (abfd)
5473
0
        = (char *) (local_got_refcounts + symtab_hdr->sh_info);
5474
0
    }
5475
0
        local_got_refcounts[r_symndx] += 1;
5476
0
        old_got_type = sh_elf_local_got_type (abfd) [r_symndx];
5477
0
      }
5478
5479
    /* If a TLS symbol is accessed using IE at least once,
5480
       there is no point to use dynamic model for it.  */
5481
0
    if (old_got_type != got_type && old_got_type != GOT_UNKNOWN
5482
0
        && (old_got_type != GOT_TLS_GD || got_type != GOT_TLS_IE))
5483
0
      {
5484
0
        if (old_got_type == GOT_TLS_IE && got_type == GOT_TLS_GD)
5485
0
    got_type = GOT_TLS_IE;
5486
0
        else
5487
0
    {
5488
0
      if ((old_got_type == GOT_FUNCDESC || got_type == GOT_FUNCDESC)
5489
0
          && (old_got_type == GOT_NORMAL || got_type == GOT_NORMAL))
5490
0
        _bfd_error_handler
5491
          /* xgettext:c-format */
5492
0
          (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
5493
0
           abfd, h->root.root.string);
5494
0
      else if (old_got_type == GOT_FUNCDESC
5495
0
         || got_type == GOT_FUNCDESC)
5496
0
        _bfd_error_handler
5497
          /* xgettext:c-format */
5498
0
          (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
5499
0
           abfd, h->root.root.string);
5500
0
      else
5501
0
        _bfd_error_handler
5502
          /* xgettext:c-format */
5503
0
          (_("%pB: `%s' accessed both as normal and thread local symbol"),
5504
0
           abfd, h->root.root.string);
5505
0
      return false;
5506
0
    }
5507
0
      }
5508
5509
0
    if (old_got_type != got_type)
5510
0
      {
5511
0
        if (h != NULL)
5512
0
    sh_elf_hash_entry (h)->got_type = got_type;
5513
0
        else
5514
0
    sh_elf_local_got_type (abfd) [r_symndx] = got_type;
5515
0
      }
5516
5517
0
    break;
5518
5519
0
  case R_SH_TLS_LD_32:
5520
0
    sh_elf_hash_table(info)->tls_ldm_got.refcount += 1;
5521
0
    break;
5522
5523
0
  case R_SH_FUNCDESC:
5524
0
  case R_SH_GOTOFFFUNCDESC:
5525
0
  case R_SH_GOTOFFFUNCDESC20:
5526
0
    if (rel->r_addend)
5527
0
      {
5528
0
        _bfd_error_handler
5529
0
    (_("%pB: Function descriptor relocation with non-zero addend"),
5530
0
     abfd);
5531
0
        return false;
5532
0
      }
5533
5534
0
    if (h == NULL)
5535
0
      {
5536
0
        union gotref *local_funcdesc;
5537
5538
        /* We need a function descriptor for a local symbol.  */
5539
0
        local_funcdesc = sh_elf_local_funcdesc (abfd);
5540
0
        if (local_funcdesc == NULL)
5541
0
    {
5542
0
      bfd_size_type size;
5543
5544
0
      size = symtab_hdr->sh_info * sizeof (union gotref);
5545
0
      local_funcdesc = (union gotref *) bfd_zalloc (abfd, size);
5546
0
      if (local_funcdesc == NULL)
5547
0
        return false;
5548
0
      sh_elf_local_funcdesc (abfd) = local_funcdesc;
5549
0
    }
5550
0
        local_funcdesc[r_symndx].refcount += 1;
5551
5552
0
        if (r_type == R_SH_FUNCDESC)
5553
0
    {
5554
0
      if (!bfd_link_pic (info))
5555
0
        htab->srofixup->size += 4;
5556
0
      else
5557
0
        htab->root.srelgot->size += sizeof (Elf32_External_Rela);
5558
0
    }
5559
0
      }
5560
0
    else
5561
0
      {
5562
0
        sh_elf_hash_entry (h)->funcdesc.refcount++;
5563
0
        if (r_type == R_SH_FUNCDESC)
5564
0
    sh_elf_hash_entry (h)->abs_funcdesc_refcount++;
5565
5566
        /* If there is a function descriptor reference, then
5567
     there should not be any non-FDPIC references.  */
5568
0
        old_got_type = sh_elf_hash_entry (h)->got_type;
5569
0
        if (old_got_type != GOT_FUNCDESC && old_got_type != GOT_UNKNOWN)
5570
0
    {
5571
0
      if (old_got_type == GOT_NORMAL)
5572
0
        _bfd_error_handler
5573
          /* xgettext:c-format */
5574
0
          (_("%pB: `%s' accessed both as normal and FDPIC symbol"),
5575
0
           abfd, h->root.root.string);
5576
0
      else
5577
0
        _bfd_error_handler
5578
          /* xgettext:c-format */
5579
0
          (_("%pB: `%s' accessed both as FDPIC and thread local symbol"),
5580
0
           abfd, h->root.root.string);
5581
0
    }
5582
0
      }
5583
0
    break;
5584
5585
0
  case R_SH_GOTPLT32:
5586
    /* If this is a local symbol, we resolve it directly without
5587
       creating a procedure linkage table entry.  */
5588
5589
0
    if (h == NULL
5590
0
        || h->forced_local
5591
0
        || ! bfd_link_pic (info)
5592
0
        || info->symbolic
5593
0
        || h->dynindx == -1)
5594
0
      goto force_got;
5595
5596
0
    h->needs_plt = 1;
5597
0
    h->plt.refcount += 1;
5598
0
    ((struct elf_sh_link_hash_entry *) h)->gotplt_refcount += 1;
5599
5600
0
    break;
5601
5602
0
  case R_SH_PLT32:
5603
    /* This symbol requires a procedure linkage table entry.  We
5604
       actually build the entry in adjust_dynamic_symbol,
5605
       because this might be a case of linking PIC code which is
5606
       never referenced by a dynamic object, in which case we
5607
       don't need to generate a procedure linkage table entry
5608
       after all.  */
5609
5610
    /* If this is a local symbol, we resolve it directly without
5611
       creating a procedure linkage table entry.  */
5612
0
    if (h == NULL)
5613
0
      continue;
5614
5615
0
    if (h->forced_local)
5616
0
      break;
5617
5618
0
    h->needs_plt = 1;
5619
0
    h->plt.refcount += 1;
5620
0
    break;
5621
5622
0
  case R_SH_DIR32:
5623
0
  case R_SH_REL32:
5624
0
    if (h != NULL && ! bfd_link_pic (info))
5625
0
      {
5626
0
        h->non_got_ref = 1;
5627
0
        h->plt.refcount += 1;
5628
0
      }
5629
5630
    /* If we are creating a shared library, and this is a reloc
5631
       against a global symbol, or a non PC relative reloc
5632
       against a local symbol, then we need to copy the reloc
5633
       into the shared library.  However, if we are linking with
5634
       -Bsymbolic, we do not need to copy a reloc against a
5635
       global symbol which is defined in an object we are
5636
       including in the link (i.e., DEF_REGULAR is set).  At
5637
       this point we have not seen all the input files, so it is
5638
       possible that DEF_REGULAR is not set now but will be set
5639
       later (it is never cleared).  We account for that
5640
       possibility below by storing information in the
5641
       dyn_relocs field of the hash table entry. A similar
5642
       situation occurs when creating shared libraries and symbol
5643
       visibility changes render the symbol local.
5644
5645
       If on the other hand, we are creating an executable, we
5646
       may need to keep relocations for symbols satisfied by a
5647
       dynamic library if we manage to avoid copy relocs for the
5648
       symbol.  */
5649
0
    if ((bfd_link_pic (info)
5650
0
         && (sec->flags & SEC_ALLOC) != 0
5651
0
         && (r_type != R_SH_REL32
5652
0
       || (h != NULL
5653
0
           && (! info->symbolic
5654
0
         || h->root.type == bfd_link_hash_defweak
5655
0
         || !h->def_regular))))
5656
0
        || (! bfd_link_pic (info)
5657
0
      && (sec->flags & SEC_ALLOC) != 0
5658
0
      && h != NULL
5659
0
      && (h->root.type == bfd_link_hash_defweak
5660
0
          || !h->def_regular)))
5661
0
      {
5662
0
        struct elf_dyn_relocs *p;
5663
0
        struct elf_dyn_relocs **head;
5664
5665
0
        if (htab->root.dynobj == NULL)
5666
0
    htab->root.dynobj = abfd;
5667
5668
        /* When creating a shared object, we must copy these
5669
     reloc types into the output file.  We create a reloc
5670
     section in dynobj and make room for this reloc.  */
5671
0
        if (sreloc == NULL)
5672
0
    {
5673
0
      sreloc = _bfd_elf_make_dynamic_reloc_section
5674
0
        (sec, htab->root.dynobj, 2, abfd, /*rela?*/ true);
5675
5676
0
      if (sreloc == NULL)
5677
0
        return false;
5678
0
    }
5679
5680
        /* If this is a global symbol, we count the number of
5681
     relocations we need for this symbol.  */
5682
0
        if (h != NULL)
5683
0
    head = &h->dyn_relocs;
5684
0
        else
5685
0
    {
5686
      /* Track dynamic relocs needed for local syms too.  */
5687
0
      asection *s;
5688
0
      void *vpp;
5689
0
      Elf_Internal_Sym *isym;
5690
5691
0
      isym = bfd_sym_from_r_symndx (&htab->root.sym_cache,
5692
0
            abfd, r_symndx);
5693
0
      if (isym == NULL)
5694
0
        return false;
5695
5696
0
      s = bfd_section_from_elf_index (abfd, isym->st_shndx);
5697
0
      if (s == NULL)
5698
0
        s = sec;
5699
5700
0
      vpp = &elf_section_data (s)->local_dynrel;
5701
0
      head = (struct elf_dyn_relocs **) vpp;
5702
0
    }
5703
5704
0
        p = *head;
5705
0
        if (p == NULL || p->sec != sec)
5706
0
    {
5707
0
      size_t amt = sizeof (*p);
5708
0
      p = bfd_alloc (htab->root.dynobj, amt);
5709
0
      if (p == NULL)
5710
0
        return false;
5711
0
      p->next = *head;
5712
0
      *head = p;
5713
0
      p->sec = sec;
5714
0
      p->count = 0;
5715
0
      p->pc_count = 0;
5716
0
    }
5717
5718
0
        p->count += 1;
5719
0
        if (r_type == R_SH_REL32)
5720
0
    p->pc_count += 1;
5721
0
      }
5722
5723
    /* Allocate the fixup regardless of whether we need a relocation.
5724
       If we end up generating the relocation, we'll unallocate the
5725
       fixup.  */
5726
0
    if (htab->fdpic_p && !bfd_link_pic (info)
5727
0
        && r_type == R_SH_DIR32
5728
0
        && (sec->flags & SEC_ALLOC) != 0)
5729
0
      htab->srofixup->size += 4;
5730
0
    break;
5731
5732
0
  case R_SH_TLS_LE_32:
5733
0
    if (bfd_link_dll (info))
5734
0
      {
5735
0
        _bfd_error_handler
5736
0
    (_("%pB: TLS local exec code cannot be linked into shared objects"),
5737
0
     abfd);
5738
0
        return false;
5739
0
      }
5740
5741
0
    break;
5742
5743
0
  case R_SH_TLS_LDO_32:
5744
    /* Nothing to do.  */
5745
0
    break;
5746
5747
0
  default:
5748
0
    break;
5749
0
  }
5750
0
    }
5751
5752
0
  return true;
5753
0
}
5754
5755
#ifndef sh_elf_set_mach_from_flags
5756
static unsigned int sh_ef_bfd_table[] = { EF_SH_BFD_TABLE };
5757
5758
static bool
5759
sh_elf_set_mach_from_flags (bfd *abfd)
5760
1.25k
{
5761
1.25k
  flagword flags = elf_elfheader (abfd)->e_flags & EF_SH_MACH_MASK;
5762
5763
1.25k
  if (flags >= ARRAY_SIZE (sh_ef_bfd_table))
5764
20
    return false;
5765
5766
1.23k
  if (sh_ef_bfd_table[flags] == 0)
5767
15
    return false;
5768
5769
1.22k
  bfd_default_set_arch_mach (abfd, bfd_arch_sh, sh_ef_bfd_table[flags]);
5770
5771
1.22k
  return true;
5772
1.23k
}
5773
5774
5775
/* Reverse table lookup for sh_ef_bfd_table[].
5776
   Given a bfd MACH value from archures.c
5777
   return the equivalent ELF flags from the table.
5778
   Return -1 if no match is found.  */
5779
5780
int
5781
sh_elf_get_flags_from_mach (unsigned long mach)
5782
0
{
5783
0
  int i = ARRAY_SIZE (sh_ef_bfd_table) - 1;
5784
5785
0
  for (; i>0; i--)
5786
0
    if (sh_ef_bfd_table[i] == mach)
5787
0
      return i;
5788
5789
  /* shouldn't get here */
5790
0
  BFD_FAIL();
5791
5792
0
  return -1;
5793
0
}
5794
#endif /* not sh_elf_set_mach_from_flags */
5795
5796
#ifndef sh_elf_copy_private_data
5797
/* Copy backend specific data from one object module to another */
5798
5799
static bool
5800
sh_elf_copy_private_data (bfd * ibfd, bfd * obfd)
5801
0
{
5802
0
  if (! is_sh_elf (ibfd))
5803
0
    return true;
5804
5805
0
  if (! _bfd_elf_copy_private_bfd_data (ibfd, obfd))
5806
0
    return false;
5807
5808
0
  return sh_elf_set_mach_from_flags (obfd);
5809
0
}
5810
#endif /* not sh_elf_copy_private_data */
5811
5812
#ifndef sh_elf_merge_private_data
5813
5814
/* This function returns the ELF architecture number that
5815
   corresponds to the given arch_sh* flags.  */
5816
5817
int
5818
sh_find_elf_flags (unsigned int arch_set)
5819
0
{
5820
0
  extern unsigned long sh_get_bfd_mach_from_arch_set (unsigned int);
5821
0
  unsigned long bfd_mach = sh_get_bfd_mach_from_arch_set (arch_set);
5822
5823
0
  return sh_elf_get_flags_from_mach (bfd_mach);
5824
0
}
5825
5826
/* Merge the architecture type of two BFD files, such that the
5827
   resultant architecture supports all the features required
5828
   by the two input BFDs.
5829
   If the input BFDs are multually incompatible - i.e. one uses
5830
   DSP while the other uses FPU - or there is no known architecture
5831
   that fits the requirements then an error is emitted.  */
5832
5833
static bool
5834
sh_merge_bfd_arch (bfd *ibfd, struct bfd_link_info *info)
5835
0
{
5836
0
  bfd *obfd = info->output_bfd;
5837
0
  unsigned int old_arch, new_arch, merged_arch;
5838
5839
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
5840
0
    return false;
5841
5842
0
  old_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (obfd));
5843
0
  new_arch = sh_get_arch_up_from_bfd_mach (bfd_get_mach (ibfd));
5844
5845
0
  merged_arch = SH_MERGE_ARCH_SET (old_arch, new_arch);
5846
5847
0
  if (!SH_VALID_CO_ARCH_SET (merged_arch))
5848
0
    {
5849
0
      _bfd_error_handler
5850
  /* xgettext:c-format */
5851
0
  (_("%pB: uses %s instructions while previous modules "
5852
0
     "use %s instructions"),
5853
0
   ibfd,
5854
0
   SH_ARCH_SET_HAS_DSP (new_arch) ? "dsp" : "floating point",
5855
0
   SH_ARCH_SET_HAS_DSP (new_arch) ? "floating point" : "dsp");
5856
0
      bfd_set_error (bfd_error_bad_value);
5857
0
      return false;
5858
0
    }
5859
0
  else if (!SH_VALID_ARCH_SET (merged_arch))
5860
0
    {
5861
0
      _bfd_error_handler
5862
  /* xgettext:c-format */
5863
0
  (_("internal error: merge of architecture '%s' with "
5864
0
     "architecture '%s' produced unknown architecture"),
5865
0
   bfd_printable_name (obfd),
5866
0
   bfd_printable_name (ibfd));
5867
0
      bfd_set_error (bfd_error_bad_value);
5868
0
      return false;
5869
0
    }
5870
5871
0
  bfd_default_set_arch_mach (obfd, bfd_arch_sh,
5872
0
           sh_get_bfd_mach_from_arch_set (merged_arch));
5873
5874
0
  return true;
5875
0
}
5876
5877
/* This routine initialises the elf flags when required and
5878
   calls sh_merge_bfd_arch() to check dsp/fpu compatibility.  */
5879
5880
static bool
5881
sh_elf_merge_private_data (bfd *ibfd, struct bfd_link_info *info)
5882
0
{
5883
0
  bfd *obfd = info->output_bfd;
5884
5885
  /* FIXME: What should be checked when linking shared libraries?  */
5886
0
  if ((ibfd->flags & DYNAMIC) != 0)
5887
0
    return true;
5888
5889
0
  if (! is_sh_elf (ibfd))
5890
0
    return true;
5891
5892
0
  if (! elf_flags_init (obfd))
5893
0
    {
5894
      /* This happens when ld starts out with a 'blank' output file.  */
5895
0
      elf_flags_init (obfd) = true;
5896
0
      elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
5897
0
      sh_elf_set_mach_from_flags (obfd);
5898
0
      if (elf_elfheader (obfd)->e_flags & EF_SH_FDPIC)
5899
0
  elf_elfheader (obfd)->e_flags &= ~EF_SH_PIC;
5900
0
    }
5901
5902
0
  if (! sh_merge_bfd_arch (ibfd, info))
5903
0
    {
5904
0
      _bfd_error_handler (_("%pB: uses instructions which are incompatible "
5905
0
          "with instructions used in previous modules"),
5906
0
        ibfd);
5907
0
      bfd_set_error (bfd_error_bad_value);
5908
0
      return false;
5909
0
    }
5910
5911
0
  elf_elfheader (obfd)->e_flags &= ~EF_SH_MACH_MASK;
5912
0
  elf_elfheader (obfd)->e_flags |=
5913
0
    sh_elf_get_flags_from_mach (bfd_get_mach (obfd));
5914
5915
0
  if (fdpic_object_p (ibfd) != fdpic_object_p (obfd))
5916
0
    {
5917
0
      _bfd_error_handler (_("%pB: attempt to mix FDPIC and non-FDPIC objects"),
5918
0
        ibfd);
5919
0
      bfd_set_error (bfd_error_bad_value);
5920
0
      return false;
5921
0
    }
5922
5923
0
  return true;
5924
0
}
5925
#endif /* not sh_elf_merge_private_data */
5926
5927
/* Override the generic function because we need to store sh_elf_obj_tdata
5928
   as the specific tdata.  We set also the machine architecture from flags
5929
   here.  */
5930
5931
static bool
5932
sh_elf_object_p (bfd *abfd)
5933
1.25k
{
5934
1.25k
  if (! sh_elf_set_mach_from_flags (abfd))
5935
35
    return false;
5936
5937
1.22k
  return (((elf_elfheader (abfd)->e_flags & EF_SH_FDPIC) != 0)
5938
1.22k
    == fdpic_object_p (abfd));
5939
1.25k
}
5940
5941
/* Finish up dynamic symbol handling.  We set the contents of various
5942
   dynamic sections here.  */
5943
5944
static bool
5945
sh_elf_finish_dynamic_symbol (bfd *output_bfd, struct bfd_link_info *info,
5946
            struct elf_link_hash_entry *h,
5947
            Elf_Internal_Sym *sym)
5948
0
{
5949
0
  struct elf_sh_link_hash_table *htab;
5950
5951
0
  htab = sh_elf_hash_table (info);
5952
5953
0
  if (h->plt.offset != (bfd_vma) -1)
5954
0
    {
5955
0
      asection *splt;
5956
0
      asection *sgotplt;
5957
0
      asection *srelplt;
5958
5959
0
      bfd_vma plt_index;
5960
0
      bfd_vma got_offset;
5961
0
      Elf_Internal_Rela rel;
5962
0
      bfd_byte *loc;
5963
0
      const struct elf_sh_plt_info *plt_info;
5964
5965
      /* This symbol has an entry in the procedure linkage table.  Set
5966
   it up.  */
5967
5968
0
      BFD_ASSERT (h->dynindx != -1);
5969
5970
0
      splt = htab->root.splt;
5971
0
      sgotplt = htab->root.sgotplt;
5972
0
      srelplt = htab->root.srelplt;
5973
0
      BFD_ASSERT (splt != NULL && sgotplt != NULL && srelplt != NULL);
5974
5975
      /* Get the index in the procedure linkage table which
5976
   corresponds to this symbol.  This is the index of this symbol
5977
   in all the symbols for which we are making plt entries.  The
5978
   first entry in the procedure linkage table is reserved.  */
5979
0
      plt_index = get_plt_index (htab->plt_info, h->plt.offset);
5980
5981
0
      plt_info = htab->plt_info;
5982
0
      if (plt_info->short_plt != NULL && plt_index <= MAX_SHORT_PLT)
5983
0
  plt_info = plt_info->short_plt;
5984
5985
      /* Get the offset into the .got table of the entry that
5986
   corresponds to this function.  */
5987
0
      if (htab->fdpic_p)
5988
  /* The offset must be relative to the GOT symbol, twelve bytes
5989
     before the end of .got.plt.  Each descriptor is eight
5990
     bytes.  */
5991
0
  got_offset = plt_index * 8 + 12 - sgotplt->size;
5992
0
      else
5993
  /* Each .got entry is 4 bytes.  The first three are
5994
     reserved.  */
5995
0
  got_offset = (plt_index + 3) * 4;
5996
5997
#ifdef GOT_BIAS
5998
      if (bfd_link_pic (info))
5999
  got_offset -= GOT_BIAS;
6000
#endif
6001
6002
      /* Fill in the entry in the procedure linkage table.  */
6003
0
      memcpy (splt->contents + h->plt.offset,
6004
0
        plt_info->symbol_entry,
6005
0
        plt_info->symbol_entry_size);
6006
6007
0
      if (bfd_link_pic (info) || htab->fdpic_p)
6008
0
  {
6009
0
    if (plt_info->symbol_fields.got20)
6010
0
      {
6011
0
        bfd_reloc_status_type r;
6012
0
        r = install_movi20_field (output_bfd, got_offset,
6013
0
          splt->owner, splt, splt->contents,
6014
0
          h->plt.offset
6015
0
          + plt_info->symbol_fields.got_entry);
6016
0
        BFD_ASSERT (r == bfd_reloc_ok);
6017
0
      }
6018
0
    else
6019
0
      install_plt_field (output_bfd, false, got_offset,
6020
0
             (splt->contents
6021
0
        + h->plt.offset
6022
0
        + plt_info->symbol_fields.got_entry));
6023
0
  }
6024
0
      else
6025
0
  {
6026
0
    BFD_ASSERT (!plt_info->symbol_fields.got20);
6027
6028
0
    install_plt_field (output_bfd, false,
6029
0
           (sgotplt->output_section->vma
6030
0
            + sgotplt->output_offset
6031
0
            + got_offset),
6032
0
           (splt->contents
6033
0
            + h->plt.offset
6034
0
            + plt_info->symbol_fields.got_entry));
6035
0
    if (htab->root.target_os == is_vxworks)
6036
0
      {
6037
0
        unsigned int reachable_plts, plts_per_4k;
6038
0
        int distance;
6039
6040
        /* Divide the PLT into groups.  The first group contains
6041
     REACHABLE_PLTS entries and the other groups contain
6042
     PLTS_PER_4K entries.  Entries in the first group can
6043
     branch directly to .plt; those in later groups branch
6044
     to the last element of the previous group.  */
6045
        /* ??? It would be better to create multiple copies of
6046
     the common resolver stub.  */
6047
0
        reachable_plts = ((4096
6048
0
         - plt_info->plt0_entry_size
6049
0
         - (plt_info->symbol_fields.plt + 4))
6050
0
        / plt_info->symbol_entry_size) + 1;
6051
0
        plts_per_4k = (4096 / plt_info->symbol_entry_size);
6052
0
        if (plt_index < reachable_plts)
6053
0
    distance = -(h->plt.offset
6054
0
           + plt_info->symbol_fields.plt);
6055
0
        else
6056
0
    distance = -(((plt_index - reachable_plts) % plts_per_4k + 1)
6057
0
           * plt_info->symbol_entry_size);
6058
6059
        /* Install the 'bra' with this offset.  */
6060
0
        bfd_put_16 (output_bfd,
6061
0
        0xa000 | (0x0fff & ((distance - 4) / 2)),
6062
0
        (splt->contents
6063
0
         + h->plt.offset
6064
0
         + plt_info->symbol_fields.plt));
6065
0
      }
6066
0
    else
6067
0
      install_plt_field (output_bfd, true,
6068
0
             splt->output_section->vma + splt->output_offset,
6069
0
             (splt->contents
6070
0
        + h->plt.offset
6071
0
        + plt_info->symbol_fields.plt));
6072
0
  }
6073
6074
      /* Make got_offset relative to the start of .got.plt.  */
6075
#ifdef GOT_BIAS
6076
      if (bfd_link_pic (info))
6077
  got_offset += GOT_BIAS;
6078
#endif
6079
0
      if (htab->fdpic_p)
6080
0
  got_offset = plt_index * 8;
6081
6082
0
      if (plt_info->symbol_fields.reloc_offset != MINUS_ONE)
6083
0
  install_plt_field (output_bfd, false,
6084
0
         plt_index * sizeof (Elf32_External_Rela),
6085
0
         (splt->contents
6086
0
          + h->plt.offset
6087
0
          + plt_info->symbol_fields.reloc_offset));
6088
6089
      /* Fill in the entry in the global offset table.  */
6090
0
      bfd_put_32 (output_bfd,
6091
0
      (splt->output_section->vma
6092
0
       + splt->output_offset
6093
0
       + h->plt.offset
6094
0
       + plt_info->symbol_resolve_offset),
6095
0
      sgotplt->contents + got_offset);
6096
0
      if (htab->fdpic_p)
6097
0
  bfd_put_32 (output_bfd,
6098
0
        sh_elf_osec_to_segment (output_bfd, splt->output_section),
6099
0
        sgotplt->contents + got_offset + 4);
6100
6101
      /* Fill in the entry in the .rela.plt section.  */
6102
0
      rel.r_offset = (sgotplt->output_section->vma
6103
0
          + sgotplt->output_offset
6104
0
          + got_offset);
6105
0
      if (htab->fdpic_p)
6106
0
  rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_FUNCDESC_VALUE);
6107
0
      else
6108
0
  rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_JMP_SLOT);
6109
0
      rel.r_addend = 0;
6110
#ifdef GOT_BIAS
6111
      rel.r_addend = GOT_BIAS;
6112
#endif
6113
0
      loc = srelplt->contents + plt_index * sizeof (Elf32_External_Rela);
6114
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6115
6116
0
      if (htab->root.target_os == is_vxworks && !bfd_link_pic (info))
6117
0
  {
6118
    /* Create the .rela.plt.unloaded relocations for this PLT entry.
6119
       Begin by pointing LOC to the first such relocation.  */
6120
0
    loc = (htab->srelplt2->contents
6121
0
     + (plt_index * 2 + 1) * sizeof (Elf32_External_Rela));
6122
6123
    /* Create a .rela.plt.unloaded R_SH_DIR32 relocation
6124
       for the PLT entry's pointer to the .got.plt entry.  */
6125
0
    rel.r_offset = (splt->output_section->vma
6126
0
        + splt->output_offset
6127
0
        + h->plt.offset
6128
0
        + plt_info->symbol_fields.got_entry);
6129
0
    rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6130
0
    rel.r_addend = got_offset;
6131
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6132
0
    loc += sizeof (Elf32_External_Rela);
6133
6134
    /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for
6135
       the .got.plt entry, which initially points to .plt.  */
6136
0
    rel.r_offset = (sgotplt->output_section->vma
6137
0
        + sgotplt->output_offset
6138
0
        + got_offset);
6139
0
    rel.r_info = ELF32_R_INFO (htab->root.hplt->indx, R_SH_DIR32);
6140
0
    rel.r_addend = 0;
6141
0
    bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6142
0
  }
6143
6144
0
      if (!h->def_regular)
6145
0
  {
6146
    /* Mark the symbol as undefined, rather than as defined in
6147
       the .plt section.  Leave the value alone.  */
6148
0
    sym->st_shndx = SHN_UNDEF;
6149
0
  }
6150
0
    }
6151
6152
0
  if (h->got.offset != (bfd_vma) -1
6153
0
      && sh_elf_hash_entry (h)->got_type != GOT_TLS_GD
6154
0
      && sh_elf_hash_entry (h)->got_type != GOT_TLS_IE
6155
0
      && sh_elf_hash_entry (h)->got_type != GOT_FUNCDESC)
6156
0
    {
6157
0
      asection *sgot;
6158
0
      asection *srelgot;
6159
0
      Elf_Internal_Rela rel;
6160
0
      bfd_byte *loc;
6161
6162
      /* This symbol has an entry in the global offset table.  Set it
6163
   up.  */
6164
6165
0
      sgot = htab->root.sgot;
6166
0
      srelgot = htab->root.srelgot;
6167
0
      BFD_ASSERT (sgot != NULL && srelgot != NULL);
6168
6169
0
      rel.r_offset = (sgot->output_section->vma
6170
0
          + sgot->output_offset
6171
0
          + (h->got.offset &~ (bfd_vma) 1));
6172
6173
      /* If this is a static link, or it is a -Bsymbolic link and the
6174
   symbol is defined locally or was forced to be local because
6175
   of a version file, we just want to emit a RELATIVE reloc.
6176
   The entry in the global offset table will already have been
6177
   initialized in the relocate_section function.  */
6178
0
      if (bfd_link_pic (info)
6179
0
    && (h->root.type == bfd_link_hash_defined
6180
0
        || h->root.type == bfd_link_hash_defweak)
6181
0
    && SYMBOL_REFERENCES_LOCAL (info, h))
6182
0
  {
6183
0
    if (htab->fdpic_p)
6184
0
      {
6185
0
        asection *sec = h->root.u.def.section;
6186
0
        int dynindx
6187
0
    = elf_section_data (sec->output_section)->dynindx;
6188
6189
0
        rel.r_info = ELF32_R_INFO (dynindx, R_SH_DIR32);
6190
0
        rel.r_addend = (h->root.u.def.value
6191
0
            + h->root.u.def.section->output_offset);
6192
0
      }
6193
0
    else
6194
0
      {
6195
0
        rel.r_info = ELF32_R_INFO (0, R_SH_RELATIVE);
6196
0
        rel.r_addend = (h->root.u.def.value
6197
0
            + h->root.u.def.section->output_section->vma
6198
0
            + h->root.u.def.section->output_offset);
6199
0
      }
6200
0
  }
6201
0
      else
6202
0
  {
6203
0
    bfd_put_32 (output_bfd, (bfd_vma) 0, sgot->contents + h->got.offset);
6204
0
    rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_GLOB_DAT);
6205
0
    rel.r_addend = 0;
6206
0
  }
6207
6208
0
      loc = srelgot->contents;
6209
0
      loc += srelgot->reloc_count++ * sizeof (Elf32_External_Rela);
6210
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6211
0
    }
6212
6213
0
  if (h->needs_copy)
6214
0
    {
6215
0
      asection *s;
6216
0
      Elf_Internal_Rela rel;
6217
0
      bfd_byte *loc;
6218
6219
      /* This symbol needs a copy reloc.  Set it up.  */
6220
6221
0
      BFD_ASSERT (h->dynindx != -1
6222
0
      && (h->root.type == bfd_link_hash_defined
6223
0
          || h->root.type == bfd_link_hash_defweak));
6224
6225
0
      s = bfd_get_linker_section (htab->root.dynobj, ".rela.bss");
6226
0
      BFD_ASSERT (s != NULL);
6227
6228
0
      rel.r_offset = (h->root.u.def.value
6229
0
          + h->root.u.def.section->output_section->vma
6230
0
          + h->root.u.def.section->output_offset);
6231
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_SH_COPY);
6232
0
      rel.r_addend = 0;
6233
0
      loc = s->contents + s->reloc_count++ * sizeof (Elf32_External_Rela);
6234
0
      bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6235
0
    }
6236
6237
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  On VxWorks,
6238
     _GLOBAL_OFFSET_TABLE_ is not absolute: it is relative to the
6239
     ".got" section.  */
6240
0
  if (h == htab->root.hdynamic
6241
0
      || (htab->root.target_os != is_vxworks && h == htab->root.hgot))
6242
0
    sym->st_shndx = SHN_ABS;
6243
6244
0
  return true;
6245
0
}
6246
6247
/* Finish up the dynamic sections.  */
6248
6249
static bool
6250
sh_elf_finish_dynamic_sections (bfd *output_bfd, struct bfd_link_info *info,
6251
        bfd_byte *buf ATTRIBUTE_UNUSED)
6252
0
{
6253
0
  struct elf_sh_link_hash_table *htab;
6254
0
  asection *sgotplt;
6255
0
  asection *sdyn;
6256
6257
0
  htab = sh_elf_hash_table (info);
6258
0
  if (htab == NULL)
6259
0
    return false;
6260
6261
0
  sgotplt = htab->root.sgotplt;
6262
0
  sdyn = bfd_get_linker_section (htab->root.dynobj, ".dynamic");
6263
6264
0
  if (htab->root.dynamic_sections_created)
6265
0
    {
6266
0
      asection *splt;
6267
0
      Elf32_External_Dyn *dyncon, *dynconend;
6268
6269
0
      BFD_ASSERT (sgotplt != NULL && sdyn != NULL);
6270
6271
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
6272
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
6273
0
      for (; dyncon < dynconend; dyncon++)
6274
0
  {
6275
0
    Elf_Internal_Dyn dyn;
6276
0
    asection *s;
6277
6278
0
    bfd_elf32_swap_dyn_in (htab->root.dynobj, dyncon, &dyn);
6279
6280
0
    switch (dyn.d_tag)
6281
0
      {
6282
0
      default:
6283
0
#ifdef OBJ_MAYBE_ELF_VXWORKS
6284
0
        if (htab->root.target_os == is_vxworks
6285
0
      && elf_vxworks_finish_dynamic_entry (output_bfd, &dyn))
6286
0
    bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6287
0
#endif /* OBJ_MAYBE_ELF_VXWORKS */
6288
0
        break;
6289
6290
0
      case DT_PLTGOT:
6291
0
        BFD_ASSERT (htab->root.hgot != NULL);
6292
0
        s = htab->root.hgot->root.u.def.section;
6293
0
        dyn.d_un.d_ptr = htab->root.hgot->root.u.def.value
6294
0
    + s->output_section->vma + s->output_offset;
6295
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6296
0
        break;
6297
6298
0
      case DT_JMPREL:
6299
0
        s = htab->root.srelplt;
6300
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
6301
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6302
0
        break;
6303
6304
0
      case DT_PLTRELSZ:
6305
0
        s = htab->root.srelplt;
6306
0
        dyn.d_un.d_val = s->size;
6307
0
        bfd_elf32_swap_dyn_out (output_bfd, &dyn, dyncon);
6308
0
        break;
6309
0
      }
6310
0
  }
6311
6312
      /* Fill in the first entry in the procedure linkage table.  */
6313
0
      splt = htab->root.splt;
6314
0
      if (splt && splt->size > 0 && htab->plt_info->plt0_entry)
6315
0
  {
6316
0
    unsigned int i;
6317
6318
0
    memcpy (splt->contents,
6319
0
      htab->plt_info->plt0_entry,
6320
0
      htab->plt_info->plt0_entry_size);
6321
0
    for (i = 0; i < ARRAY_SIZE (htab->plt_info->plt0_got_fields); i++)
6322
0
      if (htab->plt_info->plt0_got_fields[i] != MINUS_ONE)
6323
0
        install_plt_field (output_bfd, false,
6324
0
         (sgotplt->output_section->vma
6325
0
          + sgotplt->output_offset
6326
0
          + (i * 4)),
6327
0
         (splt->contents
6328
0
          + htab->plt_info->plt0_got_fields[i]));
6329
6330
0
    if (htab->root.target_os == is_vxworks)
6331
0
      {
6332
        /* Finalize the .rela.plt.unloaded contents.  */
6333
0
        Elf_Internal_Rela rel;
6334
0
        bfd_byte *loc;
6335
6336
        /* Create a .rela.plt.unloaded R_SH_DIR32 relocation for the
6337
     first PLT entry's pointer to _GLOBAL_OFFSET_TABLE_ + 8.  */
6338
0
        loc = htab->srelplt2->contents;
6339
0
        rel.r_offset = (splt->output_section->vma
6340
0
            + splt->output_offset
6341
0
            + htab->plt_info->plt0_got_fields[2]);
6342
0
        rel.r_info = ELF32_R_INFO (htab->root.hgot->indx, R_SH_DIR32);
6343
0
        rel.r_addend = 8;
6344
0
        bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
6345
0
        loc += sizeof (Elf32_External_Rela);
6346
6347
        /* Fix up the remaining .rela.plt.unloaded relocations.
6348
     They may have the wrong symbol index for _G_O_T_ or
6349
     _P_L_T_ depending on the order in which symbols were
6350
     output.  */
6351
0
        while (loc < htab->srelplt2->contents + htab->srelplt2->size)
6352
0
    {
6353
      /* The PLT entry's pointer to the .got.plt slot.  */
6354
0
      bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
6355
0
      rel.r_info = ELF32_R_INFO (htab->root.hgot->indx,
6356
0
               R_SH_DIR32);
6357
0
      bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6358
0
      loc += sizeof (Elf32_External_Rela);
6359
6360
      /* The .got.plt slot's pointer to .plt.  */
6361
0
      bfd_elf32_swap_reloc_in (output_bfd, loc, &rel);
6362
0
      rel.r_info = ELF32_R_INFO (htab->root.hplt->indx,
6363
0
               R_SH_DIR32);
6364
0
      bfd_elf32_swap_reloc_out (output_bfd, &rel, loc);
6365
0
      loc += sizeof (Elf32_External_Rela);
6366
0
    }
6367
0
      }
6368
6369
    /* UnixWare sets the entsize of .plt to 4, although that doesn't
6370
       really seem like the right value.  */
6371
0
    elf_section_data (splt->output_section)->this_hdr.sh_entsize = 4;
6372
0
  }
6373
0
    }
6374
6375
  /* Fill in the first three entries in the global offset table.  */
6376
0
  if (sgotplt && sgotplt->size > 0 && !htab->fdpic_p)
6377
0
    {
6378
0
      if (sdyn == NULL)
6379
0
  bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents);
6380
0
      else
6381
0
  bfd_put_32 (output_bfd,
6382
0
        sdyn->output_section->vma + sdyn->output_offset,
6383
0
        sgotplt->contents);
6384
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 4);
6385
0
      bfd_put_32 (output_bfd, (bfd_vma) 0, sgotplt->contents + 8);
6386
0
    }
6387
6388
0
  if (sgotplt && sgotplt->size > 0)
6389
0
    elf_section_data (sgotplt->output_section)->this_hdr.sh_entsize = 4;
6390
6391
  /* At the very end of the .rofixup section is a pointer to the GOT.  */
6392
0
  if (htab->fdpic_p && htab->srofixup != NULL)
6393
0
    {
6394
0
      struct elf_link_hash_entry *hgot = htab->root.hgot;
6395
0
      bfd_vma got_value = hgot->root.u.def.value
6396
0
  + hgot->root.u.def.section->output_section->vma
6397
0
  + hgot->root.u.def.section->output_offset;
6398
6399
0
      sh_elf_add_rofixup (output_bfd, htab->srofixup, got_value);
6400
6401
      /* Make sure we allocated and generated the same number of fixups.  */
6402
0
      BFD_ASSERT (htab->srofixup->reloc_count * 4 == htab->srofixup->size);
6403
0
    }
6404
6405
0
  if (htab->srelfuncdesc)
6406
0
    BFD_ASSERT (htab->srelfuncdesc->reloc_count * sizeof (Elf32_External_Rela)
6407
0
    == htab->srelfuncdesc->size);
6408
6409
0
  if (htab->root.srelgot)
6410
0
    BFD_ASSERT (htab->root.srelgot->reloc_count * sizeof (Elf32_External_Rela)
6411
0
    == htab->root.srelgot->size);
6412
6413
0
  return true;
6414
0
}
6415
6416
static enum elf_reloc_type_class
6417
sh_elf_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
6418
       const asection *rel_sec ATTRIBUTE_UNUSED,
6419
       const Elf_Internal_Rela *rela)
6420
0
{
6421
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
6422
0
    {
6423
0
    case R_SH_RELATIVE:
6424
0
      return reloc_class_relative;
6425
0
    case R_SH_JMP_SLOT:
6426
0
      return reloc_class_plt;
6427
0
    case R_SH_COPY:
6428
0
      return reloc_class_copy;
6429
0
    default:
6430
0
      return reloc_class_normal;
6431
0
    }
6432
0
}
6433
6434
#if !defined SH_TARGET_ALREADY_DEFINED
6435
/* Support for Linux core dump NOTE sections.  */
6436
6437
static bool
6438
elf32_shlin_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
6439
0
{
6440
0
  int offset;
6441
0
  unsigned int size;
6442
6443
0
  switch (note->descsz)
6444
0
    {
6445
0
      default:
6446
0
  return false;
6447
6448
0
      case 168:   /* Linux/SH */
6449
  /* pr_cursig */
6450
0
  elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
6451
6452
  /* pr_pid */
6453
0
  elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
6454
6455
  /* pr_reg */
6456
0
  offset = 72;
6457
0
  size = 92;
6458
6459
0
  break;
6460
0
    }
6461
6462
  /* Make a ".reg/999" section.  */
6463
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg",
6464
0
            size, note->descpos + offset);
6465
0
}
6466
6467
static bool
6468
elf32_shlin_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
6469
0
{
6470
0
  switch (note->descsz)
6471
0
    {
6472
0
      default:
6473
0
  return false;
6474
6475
0
      case 124:   /* Linux/SH elf_prpsinfo */
6476
0
  elf_tdata (abfd)->core->program
6477
0
   = _bfd_elfcore_strndup (abfd, note->descdata + 28, 16);
6478
0
  elf_tdata (abfd)->core->command
6479
0
   = _bfd_elfcore_strndup (abfd, note->descdata + 44, 80);
6480
0
    }
6481
6482
  /* Note that for some reason, a spurious space is tacked
6483
     onto the end of the args in some (at least one anyway)
6484
     implementations, so strip it off if it exists.  */
6485
6486
0
  {
6487
0
    char *command = elf_tdata (abfd)->core->command;
6488
0
    int n = strlen (command);
6489
6490
0
    if (0 < n && command[n - 1] == ' ')
6491
0
      command[n - 1] = '\0';
6492
0
  }
6493
6494
0
  return true;
6495
0
}
6496
#endif /* not SH_TARGET_ALREADY_DEFINED */
6497
6498
6499
/* Return address for Ith PLT stub in section PLT, for relocation REL
6500
   or (bfd_vma) -1 if it should not be included.  */
6501
6502
static bfd_vma
6503
sh_elf_plt_sym_val (bfd_vma i, const asection *plt,
6504
        const arelent *rel ATTRIBUTE_UNUSED)
6505
0
{
6506
0
  const struct elf_sh_plt_info *plt_info;
6507
6508
0
  plt_info = get_plt_info (plt->owner, (plt->owner->flags & DYNAMIC) != 0);
6509
0
  return plt->vma + get_plt_offset (plt_info, i);
6510
0
}
6511
6512
/* Decide whether to attempt to turn absptr or lsda encodings in
6513
   shared libraries into pcrel within the given input section.  */
6514
6515
static bool
6516
sh_elf_use_relative_eh_frame (bfd *input_bfd ATTRIBUTE_UNUSED,
6517
            struct bfd_link_info *info,
6518
            asection *eh_frame_section ATTRIBUTE_UNUSED)
6519
0
{
6520
0
  struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
6521
6522
  /* We can't use PC-relative encodings in FDPIC binaries, in general.  */
6523
0
  if (htab->fdpic_p)
6524
0
    return false;
6525
6526
0
  return true;
6527
0
}
6528
6529
/* Adjust the contents of an eh_frame_hdr section before they're output.  */
6530
6531
static bfd_byte
6532
sh_elf_encode_eh_address (bfd *abfd,
6533
        struct bfd_link_info *info,
6534
        asection *osec, bfd_vma offset,
6535
        asection *loc_sec, bfd_vma loc_offset,
6536
        bfd_vma *encoded)
6537
0
{
6538
0
  struct elf_sh_link_hash_table *htab = sh_elf_hash_table (info);
6539
0
  struct elf_link_hash_entry *h;
6540
6541
0
  if (!htab->fdpic_p)
6542
0
    return _bfd_elf_encode_eh_address (abfd, info, osec, offset, loc_sec,
6543
0
               loc_offset, encoded);
6544
6545
0
  h = htab->root.hgot;
6546
0
  BFD_ASSERT (h && h->root.type == bfd_link_hash_defined);
6547
6548
0
  if (! h || (sh_elf_osec_to_segment (abfd, osec)
6549
0
        == sh_elf_osec_to_segment (abfd, loc_sec->output_section)))
6550
0
    return _bfd_elf_encode_eh_address (abfd, info, osec, offset,
6551
0
               loc_sec, loc_offset, encoded);
6552
6553
0
  BFD_ASSERT (sh_elf_osec_to_segment (abfd, osec)
6554
0
        == (sh_elf_osec_to_segment
6555
0
      (abfd, h->root.u.def.section->output_section)));
6556
6557
0
  *encoded = osec->vma + offset
6558
0
    - (h->root.u.def.value
6559
0
       + h->root.u.def.section->output_section->vma
6560
0
       + h->root.u.def.section->output_offset);
6561
6562
0
  return DW_EH_PE_datarel | DW_EH_PE_sdata4;
6563
0
}
6564
6565
#if !defined SH_TARGET_ALREADY_DEFINED
6566
#define TARGET_BIG_SYM    sh_elf32_vec
6567
#define TARGET_BIG_NAME   "elf32-sh"
6568
#define TARGET_LITTLE_SYM sh_elf32_le_vec
6569
#define TARGET_LITTLE_NAME  "elf32-shl"
6570
#endif
6571
6572
#define ELF_ARCH    bfd_arch_sh
6573
#define ELF_TARGET_ID   SH_ELF_DATA
6574
#define ELF_MACHINE_CODE  EM_SH
6575
#ifdef __QNXTARGET__
6576
#define ELF_MAXPAGESIZE   0x1000
6577
#else
6578
#define ELF_MAXPAGESIZE   0x80
6579
#endif
6580
6581
#define elf_symbol_leading_char '_'
6582
6583
#define bfd_elf32_bfd_reloc_type_lookup sh_elf_reloc_type_lookup
6584
#define bfd_elf32_bfd_reloc_name_lookup \
6585
          sh_elf_reloc_name_lookup
6586
#define elf_info_to_howto   sh_elf_info_to_howto
6587
#define bfd_elf32_bfd_relax_section sh_elf_relax_section
6588
#define elf_backend_relocate_section  sh_elf_relocate_section
6589
#define bfd_elf32_bfd_get_relocated_section_contents \
6590
          sh_elf_get_relocated_section_contents
6591
#define bfd_elf32_mkobject    sh_elf_mkobject
6592
#define elf_backend_object_p    sh_elf_object_p
6593
#define bfd_elf32_bfd_copy_private_bfd_data \
6594
          sh_elf_copy_private_data
6595
#define bfd_elf32_bfd_merge_private_bfd_data \
6596
          sh_elf_merge_private_data
6597
6598
#define elf_backend_gc_mark_hook  sh_elf_gc_mark_hook
6599
#define elf_backend_check_relocs  sh_elf_check_relocs
6600
#define elf_backend_copy_indirect_symbol \
6601
          sh_elf_copy_indirect_symbol
6602
#define elf_backend_create_dynamic_sections \
6603
          sh_elf_create_dynamic_sections
6604
#define bfd_elf32_bfd_link_hash_table_create \
6605
          sh_elf_link_hash_table_create
6606
#define elf_backend_adjust_dynamic_symbol \
6607
          sh_elf_adjust_dynamic_symbol
6608
#define elf_backend_early_size_sections sh_elf_early_size_sections
6609
#define elf_backend_late_size_sections  sh_elf_late_size_sections
6610
#define elf_backend_omit_section_dynsym sh_elf_omit_section_dynsym
6611
#define elf_backend_finish_dynamic_symbol \
6612
          sh_elf_finish_dynamic_symbol
6613
#define elf_backend_finish_dynamic_sections \
6614
          sh_elf_finish_dynamic_sections
6615
#define elf_backend_reloc_type_class  sh_elf_reloc_type_class
6616
#define elf_backend_plt_sym_val   sh_elf_plt_sym_val
6617
#define elf_backend_can_make_relative_eh_frame \
6618
          sh_elf_use_relative_eh_frame
6619
#define elf_backend_can_make_lsda_relative_eh_frame \
6620
          sh_elf_use_relative_eh_frame
6621
#define elf_backend_encode_eh_address \
6622
          sh_elf_encode_eh_address
6623
6624
#define elf_backend_stack_align   8
6625
#define elf_backend_can_gc_sections 1
6626
#define elf_backend_can_refcount  1
6627
#define elf_backend_want_got_plt  1
6628
#define elf_backend_plt_readonly  1
6629
#define elf_backend_want_plt_sym  0
6630
#define elf_backend_got_header_size 12
6631
#define elf_backend_dtrel_excludes_plt  1
6632
6633
#define elf_backend_linux_prpsinfo32_ugid16 true
6634
6635
#if !defined SH_TARGET_ALREADY_DEFINED
6636
6637
#include "elf32-target.h"
6638
6639
/* NetBSD support.  */
6640
#undef  TARGET_BIG_SYM
6641
#define TARGET_BIG_SYM      sh_elf32_nbsd_vec
6642
#undef  TARGET_BIG_NAME
6643
#define TARGET_BIG_NAME     "elf32-sh-nbsd"
6644
#undef  TARGET_LITTLE_SYM
6645
#define TARGET_LITTLE_SYM   sh_elf32_nbsd_le_vec
6646
#undef  TARGET_LITTLE_NAME
6647
#define TARGET_LITTLE_NAME    "elf32-shl-nbsd"
6648
#undef  ELF_MAXPAGESIZE
6649
#define ELF_MAXPAGESIZE     0x10000
6650
#undef  ELF_COMMONPAGESIZE
6651
#undef  elf_symbol_leading_char
6652
#define elf_symbol_leading_char   0
6653
#undef  elf32_bed
6654
#define elf32_bed     elf32_sh_nbsd_bed
6655
6656
#include "elf32-target.h"
6657
6658
6659
/* Linux support.  */
6660
#undef  TARGET_BIG_SYM
6661
#define TARGET_BIG_SYM      sh_elf32_linux_be_vec
6662
#undef  TARGET_BIG_NAME
6663
#define TARGET_BIG_NAME     "elf32-shbig-linux"
6664
#undef  TARGET_LITTLE_SYM
6665
#define TARGET_LITTLE_SYM   sh_elf32_linux_vec
6666
#undef  TARGET_LITTLE_NAME
6667
#define TARGET_LITTLE_NAME    "elf32-sh-linux"
6668
#undef  ELF_COMMONPAGESIZE
6669
#define ELF_COMMONPAGESIZE    0x1000
6670
6671
#undef  elf_backend_grok_prstatus
6672
#define elf_backend_grok_prstatus elf32_shlin_grok_prstatus
6673
#undef  elf_backend_grok_psinfo
6674
#define elf_backend_grok_psinfo   elf32_shlin_grok_psinfo
6675
#undef  elf32_bed
6676
#define elf32_bed     elf32_sh_lin_bed
6677
6678
#include "elf32-target.h"
6679
6680
6681
/* FDPIC support.  */
6682
#undef  TARGET_BIG_SYM
6683
#define TARGET_BIG_SYM      sh_elf32_fdpic_be_vec
6684
#undef  TARGET_BIG_NAME
6685
#define TARGET_BIG_NAME     "elf32-shbig-fdpic"
6686
#undef  TARGET_LITTLE_SYM
6687
#define TARGET_LITTLE_SYM   sh_elf32_fdpic_le_vec
6688
#undef  TARGET_LITTLE_NAME
6689
#define TARGET_LITTLE_NAME    "elf32-sh-fdpic"
6690
6691
#undef  elf32_bed
6692
#define elf32_bed     elf32_sh_fd_bed
6693
6694
#include "elf32-target.h"
6695
6696
#ifdef OBJ_MAYBE_ELF_VXWORKS
6697
6698
/* VxWorks support.  */
6699
#undef  TARGET_BIG_SYM
6700
#define TARGET_BIG_SYM      sh_elf32_vxworks_vec
6701
#undef  TARGET_BIG_NAME
6702
#define TARGET_BIG_NAME     "elf32-sh-vxworks"
6703
#undef  TARGET_LITTLE_SYM
6704
#define TARGET_LITTLE_SYM   sh_elf32_vxworks_le_vec
6705
#undef  TARGET_LITTLE_NAME
6706
#define TARGET_LITTLE_NAME    "elf32-shl-vxworks"
6707
#undef  elf32_bed
6708
#define elf32_bed     elf32_sh_vxworks_bed
6709
6710
#undef  elf_backend_want_plt_sym
6711
#define elf_backend_want_plt_sym  1
6712
#undef  elf_symbol_leading_char
6713
#define elf_symbol_leading_char   '_'
6714
#define elf_backend_want_got_underscore 1
6715
#undef  elf_backend_grok_prstatus
6716
#undef  elf_backend_grok_psinfo
6717
#undef  elf_backend_add_symbol_hook
6718
#define elf_backend_add_symbol_hook elf_vxworks_add_symbol_hook
6719
#undef  elf_backend_link_output_symbol_hook
6720
#define elf_backend_link_output_symbol_hook \
6721
          elf_vxworks_link_output_symbol_hook
6722
#undef  elf_backend_emit_relocs
6723
#define elf_backend_emit_relocs   elf_vxworks_emit_relocs
6724
#undef  elf_backend_final_write_processing
6725
#define elf_backend_final_write_processing \
6726
          elf_vxworks_final_write_processing
6727
#undef  ELF_MAXPAGESIZE
6728
#define ELF_MAXPAGESIZE     0x1000
6729
#undef  ELF_COMMONPAGESIZE
6730
6731
#undef  ELF_TARGET_OS
6732
#define ELF_TARGET_OS     is_vxworks
6733
6734
#include "elf32-target.h"
6735
6736
#endif /* OBJ_MAYBE_ELF_VXWORKS */
6737
6738
#endif /* not SH_TARGET_ALREADY_DEFINED */