Coverage Report

Created: 2026-11-21 09:00

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