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-kvx.c
Line
Count
Source
1
#line 1 "elfnn-kvx.c"
2
/* KVX-specific support for 32-bit ELF.
3
   Copyright (C) 2009-2026 Free Software Foundation, Inc.
4
   Contributed by Kalray SA.
5
6
   This file is part of BFD, the Binary File Descriptor library.
7
8
   This program is free software; you can redistribute it and/or modify
9
   it under the terms of the GNU General Public License as published by
10
   the Free Software Foundation; either version 3 of the License, or
11
   (at your option) any later version.
12
13
   This program is distributed in the hope that it will be useful,
14
   but WITHOUT ANY WARRANTY; without even the implied warranty of
15
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
   GNU General Public License for more details.
17
18
   You should have received a copy of the GNU General Public License
19
   along with this program; see the file COPYING3. If not,
20
   see <http://www.gnu.org/licenses/>.  */
21
22
#include "sysdep.h"
23
#include "bfd.h"
24
#include "libiberty.h"
25
#include "libbfd.h"
26
#include "elf-bfd.h"
27
#include "bfdlink.h"
28
#include "objalloc.h"
29
#include "elf/kvx.h"
30
#include "elfxx-kvx.h"
31
32
0
#define ARCH_SIZE 32
33
34
#if ARCH_SIZE == 64
35
#define LOG_FILE_ALIGN  3
36
#endif
37
38
#if ARCH_SIZE == 32
39
0
#define LOG_FILE_ALIGN  2
40
#endif
41
42
#define IS_KVX_TLS_RELOC(R_TYPE)      \
43
0
  ((R_TYPE) == BFD_RELOC_KVX_S37_TLS_LE_LO10  \
44
0
   || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_LE_UP27  \
45
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LE_LO10  \
46
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LE_UP27  \
47
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LE_EX6  \
48
0
   || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_DTPOFF_LO10  \
49
0
   || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_DTPOFF_UP27  \
50
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_DTPOFF_LO10  \
51
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_DTPOFF_UP27  \
52
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_DTPOFF_EX6  \
53
0
   || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_IE_LO10  \
54
0
   || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_IE_UP27  \
55
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_IE_LO10  \
56
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_IE_UP27  \
57
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_IE_EX6  \
58
0
   || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_GD_LO10  \
59
0
   || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_GD_UP27  \
60
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_GD_LO10  \
61
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_GD_UP27  \
62
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_GD_EX6  \
63
0
   || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_LD_LO10  \
64
0
   || (R_TYPE) == BFD_RELOC_KVX_S37_TLS_LD_UP27  \
65
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LD_LO10  \
66
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LD_UP27  \
67
0
   || (R_TYPE) == BFD_RELOC_KVX_S43_TLS_LD_EX6  \
68
0
   )
69
70
0
#define IS_KVX_TLS_RELAX_RELOC(R_TYPE) 0
71
72
0
#define ELIMINATE_COPY_RELOCS 0
73
74
/* Return size of a relocation entry.  HTAB is the bfd's
75
   elf_kvx_link_hash_entry.  */
76
0
#define RELOC_SIZE(HTAB) (sizeof (Elf32_External_Rela))
77
78
/* GOT Entry size - 8 bytes in ELF64 and 4 bytes in ELF32.  */
79
0
#define GOT_ENTRY_SIZE                  (ARCH_SIZE / 8)
80
0
#define PLT_ENTRY_SIZE                  (32)
81
82
0
#define PLT_SMALL_ENTRY_SIZE            (4*4)
83
84
/* Encoding of the nop instruction */
85
0
#define INSN_NOP 0x00f0037f
86
87
#define kvx_compute_jump_table_size(htab)   \
88
0
  (((htab)->root.srelplt == NULL) ? 0      \
89
0
   : (htab)->root.srelplt->reloc_count * GOT_ENTRY_SIZE)
90
91
static const bfd_byte elf32_kvx_small_plt0_entry[PLT_ENTRY_SIZE] =
92
{
93
 /* FIXME KVX: no first entry, not used yet */
94
  0
95
};
96
97
/* Per function entry in a procedure linkage table looks like this
98
   if the distance between the PLTGOT and the PLT is < 4GB use
99
   these PLT entries.  */
100
static const bfd_byte elf32_kvx_small_plt_entry[PLT_SMALL_ENTRY_SIZE] =
101
{
102
  0x10, 0x00, 0xc4, 0x0f,       /* get $r16 = $pc     ;; */
103
#if ARCH_SIZE == 32
104
  0x10, 0x00, 0x40, 0xb0,       /* lwz $r16 = 0[$r16]   ;; */
105
#else
106
  0x10, 0x00, 0x40, 0xb8,       /* ld $r16 = 0[$r16] ;; */
107
#endif
108
  0x00, 0x00, 0x00, 0x18,       /* upper 27 bits for LSU */
109
  0x10, 0x00, 0xd8, 0x0f, /* igoto $r16          ;; */
110
};
111
112
/* Long stub use 43bits format of make. */
113
static const uint32_t elf32_kvx_long_branch_stub[] =
114
{
115
  0xe0400000,      /* make $r16 = LO10<emm43> EX6<imm43> */
116
  0x00000000,      /* UP27<imm43> ;; */
117
  0x0fd80010,      /* igoto "r16  ;; */
118
};
119
120
#define elf_info_to_howto               elf32_kvx_info_to_howto
121
#define elf_info_to_howto_rel           elf32_kvx_info_to_howto
122
123
7
#define KVX_ELF_ABI_VERSION   0
124
125
/* In case we're on a 32-bit machine, construct a 64-bit "-1" value.  */
126
#define ALL_ONES (~ (bfd_vma) 0)
127
128
/* Indexed by the bfd interal reloc enumerators.
129
   Therefore, the table needs to be synced with BFD_RELOC_KVX_*
130
   in reloc.c.   */
131
132
#define KVX_KV3_V1_KV3_V2_KV4_V1
133
#include "elfxx-kvx-relocs.h"
134
#undef KVX_KV3_V1_KV3_V2_KV4_V1
135
136
/* Given HOWTO, return the bfd internal relocation enumerator.  */
137
138
static bfd_reloc_code_real_type
139
elf32_kvx_bfd_reloc_from_howto (reloc_howto_type *howto)
140
0
{
141
0
  const int size = (int) ARRAY_SIZE (elf_kvx_howto_table);
142
0
  const ptrdiff_t offset = howto - elf_kvx_howto_table;
143
144
0
  if (offset >= 0 && offset < size)
145
0
    return BFD_RELOC_KVX_RELOC_START + offset + 1;
146
147
0
  return BFD_RELOC_KVX_RELOC_START + 1;
148
0
}
149
150
/* Given R_TYPE, return the bfd internal relocation enumerator.  */
151
152
static bfd_reloc_code_real_type
153
elf32_kvx_bfd_reloc_from_type (bfd *abfd ATTRIBUTE_UNUSED, unsigned int r_type)
154
392
{
155
392
  static bool initialized_p = false;
156
  /* Indexed by R_TYPE, values are offsets in the howto_table.  */
157
392
  static unsigned int offsets[R_KVX_end];
158
159
392
  if (!initialized_p)
160
4
    {
161
4
      unsigned int i;
162
163
336
      for (i = 0; i < ARRAY_SIZE (elf_kvx_howto_table); ++i)
164
332
  offsets[elf_kvx_howto_table[i].type] = i;
165
166
4
      initialized_p = true;
167
4
    }
168
169
  /* PR 17512: file: b371e70a.  */
170
392
  if (r_type >= R_KVX_end)
171
33
    {
172
33
      bfd_set_error (bfd_error_bad_value);
173
33
      return BFD_RELOC_KVX_RELOC_END;
174
33
    }
175
176
359
  return (BFD_RELOC_KVX_RELOC_START + 1) + offsets[r_type];
177
392
}
178
179
struct elf_kvx_reloc_map
180
{
181
  bfd_reloc_code_real_type from;
182
  bfd_reloc_code_real_type to;
183
};
184
185
/* Map bfd generic reloc to KVX-specific reloc.  */
186
static const struct elf_kvx_reloc_map elf_kvx_reloc_map[] =
187
{
188
  {BFD_RELOC_NONE, BFD_RELOC_KVX_NONE},
189
190
  /* Basic data relocations.  */
191
  {BFD_RELOC_CTOR, BFD_RELOC_KVX_32},
192
  {BFD_RELOC_64, BFD_RELOC_KVX_64},
193
  {BFD_RELOC_32, BFD_RELOC_KVX_32},
194
  {BFD_RELOC_16, BFD_RELOC_KVX_16},
195
  {BFD_RELOC_8,  BFD_RELOC_KVX_8},
196
197
  {BFD_RELOC_64_PCREL, BFD_RELOC_KVX_64_PCREL},
198
  {BFD_RELOC_32_PCREL, BFD_RELOC_KVX_32_PCREL},
199
};
200
201
/* Given the bfd internal relocation enumerator in CODE, return the
202
   corresponding howto entry.  */
203
204
static reloc_howto_type *
205
elf32_kvx_howto_from_bfd_reloc (bfd_reloc_code_real_type code)
206
392
{
207
392
  unsigned int i;
208
209
  /* Convert bfd generic reloc to KVX-specific reloc.  */
210
392
  if (code < BFD_RELOC_KVX_RELOC_START || code > BFD_RELOC_KVX_RELOC_END)
211
0
    for (i = 0; i < ARRAY_SIZE (elf_kvx_reloc_map) ; i++)
212
0
      if (elf_kvx_reloc_map[i].from == code)
213
0
  {
214
0
    code = elf_kvx_reloc_map[i].to;
215
0
    break;
216
0
  }
217
218
392
  if (code > BFD_RELOC_KVX_RELOC_START && code < BFD_RELOC_KVX_RELOC_END)
219
359
      return &elf_kvx_howto_table[code - (BFD_RELOC_KVX_RELOC_START + 1)];
220
221
33
  return NULL;
222
392
}
223
224
static reloc_howto_type *
225
elf32_kvx_howto_from_type (bfd *abfd, unsigned int r_type)
226
392
{
227
392
  bfd_reloc_code_real_type val;
228
392
  reloc_howto_type *howto;
229
230
392
#if ARCH_SIZE == 32
231
392
  if (r_type > 256)
232
0
    {
233
0
      bfd_set_error (bfd_error_bad_value);
234
0
      return NULL;
235
0
    }
236
392
#endif
237
238
392
  val = elf32_kvx_bfd_reloc_from_type (abfd, r_type);
239
392
  howto = elf32_kvx_howto_from_bfd_reloc (val);
240
241
392
  if (howto != NULL)
242
359
    return howto;
243
244
33
  bfd_set_error (bfd_error_bad_value);
245
33
  return NULL;
246
392
}
247
248
static bool
249
elf32_kvx_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
250
       Elf_Internal_Rela *elf_reloc)
251
392
{
252
392
  unsigned int r_type;
253
254
392
  r_type = ELF32_R_TYPE (elf_reloc->r_info);
255
392
  bfd_reloc->howto = elf32_kvx_howto_from_type (abfd, r_type);
256
257
392
  if (bfd_reloc->howto == NULL)
258
33
    {
259
      /* xgettext:c-format */
260
33
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
261
33
        abfd, r_type);
262
33
      return false;
263
33
    }
264
359
  return true;
265
392
}
266
267
static reloc_howto_type *
268
elf32_kvx_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
269
           bfd_reloc_code_real_type code)
270
0
{
271
0
  reloc_howto_type *howto = elf32_kvx_howto_from_bfd_reloc (code);
272
273
0
  if (howto != NULL)
274
0
    return howto;
275
276
0
  bfd_set_error (bfd_error_bad_value);
277
0
  return NULL;
278
0
}
279
280
static reloc_howto_type *
281
elf32_kvx_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
282
           const char *r_name)
283
0
{
284
0
  unsigned int i;
285
286
0
  for (i = 0; i < ARRAY_SIZE (elf_kvx_howto_table); ++i)
287
0
    if (elf_kvx_howto_table[i].name != NULL
288
0
  && strcasecmp (elf_kvx_howto_table[i].name, r_name) == 0)
289
0
      return &elf_kvx_howto_table[i];
290
291
0
  return NULL;
292
0
}
293
294
#define TARGET_LITTLE_SYM               kvx_elf32_vec
295
#define TARGET_LITTLE_NAME              "elf32-kvx"
296
297
/* The linker script knows the section names for placement.
298
   The entry_names are used to do simple name mangling on the stubs.
299
   Given a function name, and its type, the stub can be found. The
300
   name can be changed. The only requirement is the %s be present.  */
301
0
#define STUB_ENTRY_NAME   "__%s_veneer"
302
303
/* The name of the dynamic interpreter.  This is put in the .interp
304
   section.  */
305
0
#define ELF_DYNAMIC_INTERPRETER     "/lib/ld.so.1"
306
307
308
/* PCREL 27 is signed-extended and scaled by 4 */
309
#define KVX_MAX_FWD_CALL_OFFSET \
310
0
  (((1 << 26) - 1) << 2)
311
#define KVX_MAX_BWD_CALL_OFFSET \
312
0
  (-((1 << 26) << 2))
313
314
/* Check that the destination of the call is within the PCREL27
315
   range. */
316
static int
317
kvx_valid_call_p (bfd_vma value, bfd_vma place)
318
0
{
319
0
  bfd_signed_vma offset = (bfd_signed_vma) (value - place);
320
0
  return (offset <= KVX_MAX_FWD_CALL_OFFSET
321
0
    && offset >= KVX_MAX_BWD_CALL_OFFSET);
322
0
}
323
324
/* Section name for stubs is the associated section name plus this
325
   string.  */
326
0
#define STUB_SUFFIX ".stub"
327
328
enum elf_kvx_stub_type
329
{
330
  kvx_stub_none,
331
  kvx_stub_long_branch,
332
};
333
334
struct elf_kvx_stub_hash_entry
335
{
336
  /* Base hash table entry structure.  */
337
  struct bfd_hash_entry root;
338
339
  /* The stub section.  */
340
  asection *stub_sec;
341
342
  /* Offset within stub_sec of the beginning of this stub.  */
343
  bfd_vma stub_offset;
344
345
  /* Given the symbol's value and its section we can determine its final
346
     value when building the stubs (so the stub knows where to jump).  */
347
  bfd_vma target_value;
348
  asection *target_section;
349
350
  enum elf_kvx_stub_type stub_type;
351
352
  /* The symbol table entry, if any, that this was derived from.  */
353
  struct elf_kvx_link_hash_entry *h;
354
355
  /* Destination symbol type */
356
  unsigned char st_type;
357
358
  /* Where this stub is being called from, or, in the case of combined
359
     stub sections, the first input section in the group.  */
360
  asection *id_sec;
361
362
  /* The name for the local symbol at the start of this stub.  The
363
     stub name in the hash table has to be unique; this does not, so
364
     it can be friendlier.  */
365
  char *output_name;
366
};
367
368
/* Used to build a map of a section.  This is required for mixed-endian
369
   code/data.  */
370
371
typedef struct elf_elf_section_map
372
{
373
  bfd_vma vma;
374
  char type;
375
}
376
elf_kvx_section_map;
377
378
379
typedef struct _kvx_elf_section_data
380
{
381
  struct bfd_elf_section_data elf;
382
  unsigned int mapcount;
383
  unsigned int mapsize;
384
  elf_kvx_section_map *map;
385
}
386
_kvx_elf_section_data;
387
388
#define elf_kvx_section_data(sec) \
389
  ((_kvx_elf_section_data *) elf_section_data (sec))
390
391
struct elf_kvx_local_symbol
392
{
393
  unsigned int got_type;
394
  bfd_signed_vma got_refcount;
395
  bfd_vma got_offset;
396
};
397
398
struct elf_kvx_obj_tdata
399
{
400
  struct elf_obj_tdata root;
401
402
  /* local symbol descriptors */
403
  struct elf_kvx_local_symbol *locals;
404
405
  /* Zero to warn when linking objects with incompatible enum sizes.  */
406
  int no_enum_size_warning;
407
408
  /* Zero to warn when linking objects with incompatible wchar_t sizes.  */
409
  int no_wchar_size_warning;
410
};
411
412
#define elf_kvx_tdata(bfd)        \
413
0
  ((struct elf_kvx_obj_tdata *) (bfd)->tdata.any)
414
415
0
#define elf_kvx_locals(bfd) (elf_kvx_tdata (bfd)->locals)
416
417
#define is_kvx_elf(bfd)       \
418
0
  (bfd_get_flavour (bfd) == bfd_target_elf_flavour  \
419
0
   && elf_tdata (bfd) != NULL        \
420
0
   && elf_object_id (bfd) == KVX_ELF_DATA)
421
422
static bool
423
elf32_kvx_mkobject (bfd *abfd)
424
23.8k
{
425
23.8k
  return bfd_elf_allocate_object (abfd, sizeof (struct elf_kvx_obj_tdata));
426
23.8k
}
427
428
#define elf_kvx_hash_entry(ent) \
429
0
  ((struct elf_kvx_link_hash_entry *)(ent))
430
431
0
#define GOT_UNKNOWN    0
432
0
#define GOT_NORMAL     1
433
434
0
#define GOT_TLS_GD     2
435
0
#define GOT_TLS_IE     4
436
0
#define GOT_TLS_LD     8
437
438
/* KVX ELF linker hash entry.  */
439
struct elf_kvx_link_hash_entry
440
{
441
  struct elf_link_hash_entry root;
442
443
  /* Since PLT entries have variable size, we need to record the
444
     index into .got.plt instead of recomputing it from the PLT
445
     offset.  */
446
  bfd_signed_vma plt_got_offset;
447
448
  /* Bit mask representing the type of GOT entry(s) if any required by
449
     this symbol.  */
450
  unsigned int got_type;
451
452
  /* A pointer to the most recently used stub hash entry against this
453
     symbol.  */
454
  struct elf_kvx_stub_hash_entry *stub_cache;
455
};
456
457
/* Get the KVX elf linker hash table from a link_info structure.  */
458
#define elf_kvx_hash_table(info)          \
459
0
  ((struct elf_kvx_link_hash_table *) ((info)->hash))
460
461
#define kvx_stub_hash_lookup(table, string, create, copy)   \
462
0
  ((struct elf_kvx_stub_hash_entry *)       \
463
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
464
465
/* KVX ELF linker hash table.  */
466
struct elf_kvx_link_hash_table
467
{
468
  /* The main hash table.  */
469
  struct elf_link_hash_table root;
470
471
  /* Nonzero to force PIC branch veneers.  */
472
  int pic_veneer;
473
474
  /* The number of bytes in the initial entry in the PLT.  */
475
  bfd_size_type plt_header_size;
476
477
  /* The number of bytes in the subsequent PLT etries.  */
478
  bfd_size_type plt_entry_size;
479
480
  /* The bytes of the subsequent PLT entry.  */
481
  const bfd_byte *plt_entry;
482
483
  /* Short-cuts to get to dynamic linker sections.  */
484
  asection *sdynbss;
485
  asection *srelbss;
486
487
  /* Small local sym cache.  */
488
  struct sym_cache sym_cache;
489
490
  /* For convenience in allocate_dynrelocs.  */
491
  bfd *obfd;
492
493
  /* The amount of space used by the reserved portion of the sgotplt
494
     section, plus whatever space is used by the jump slots.  */
495
  bfd_vma sgotplt_jump_table_size;
496
497
  /* The stub hash table.  */
498
  struct bfd_hash_table stub_hash_table;
499
500
  /* Linker stub bfd.  */
501
  bfd *stub_bfd;
502
503
  /* Linker call-backs.  */
504
  asection *(*add_stub_section) (const char *, asection *);
505
  void (*layout_sections_again) (void);
506
507
  /* Array to keep track of which stub sections have been created, and
508
     information on stub grouping.  */
509
  struct map_stub
510
  {
511
    /* This is the section to which stubs in the group will be
512
       attached.  */
513
    asection *link_sec;
514
    /* The stub section.  */
515
    asection *stub_sec;
516
  } *stub_group;
517
518
  /* Assorted information used by elf32_kvx_size_stubs.  */
519
  unsigned int bfd_count;
520
  unsigned int top_index;
521
  asection **input_list;
522
};
523
524
/* Create an entry in an KVX ELF linker hash table.  */
525
526
static struct bfd_hash_entry *
527
elf32_kvx_link_hash_newfunc (struct bfd_hash_entry *entry,
528
           struct bfd_hash_table *table,
529
           const char *string)
530
0
{
531
0
  struct elf_kvx_link_hash_entry *ret =
532
0
    (struct elf_kvx_link_hash_entry *) entry;
533
534
  /* Allocate the structure if it has not already been allocated by a
535
     subclass.  */
536
0
  if (ret == NULL)
537
0
    ret = bfd_hash_allocate (table,
538
0
           sizeof (struct elf_kvx_link_hash_entry));
539
0
  if (ret == NULL)
540
0
    return (struct bfd_hash_entry *) ret;
541
542
  /* Call the allocation method of the superclass.  */
543
0
  ret = ((struct elf_kvx_link_hash_entry *)
544
0
   _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
545
0
             table, string));
546
0
  if (ret != NULL)
547
0
    {
548
0
      ret->got_type = GOT_UNKNOWN;
549
0
      ret->plt_got_offset = (bfd_vma) - 1;
550
0
      ret->stub_cache = NULL;
551
0
    }
552
553
0
  return (struct bfd_hash_entry *) ret;
554
0
}
555
556
/* Initialize an entry in the stub hash table.  */
557
558
static struct bfd_hash_entry *
559
stub_hash_newfunc (struct bfd_hash_entry *entry,
560
       struct bfd_hash_table *table, const char *string)
561
0
{
562
  /* Allocate the structure if it has not already been allocated by a
563
     subclass.  */
564
0
  if (entry == NULL)
565
0
    {
566
0
      entry = bfd_hash_allocate (table,
567
0
         sizeof (struct
568
0
           elf_kvx_stub_hash_entry));
569
0
      if (entry == NULL)
570
0
  return entry;
571
0
    }
572
573
  /* Call the allocation method of the superclass.  */
574
0
  entry = bfd_hash_newfunc (entry, table, string);
575
0
  if (entry != NULL)
576
0
    {
577
0
      struct elf_kvx_stub_hash_entry *eh;
578
579
      /* Initialize the local fields.  */
580
0
      eh = (struct elf_kvx_stub_hash_entry *) entry;
581
0
      eh->stub_sec = NULL;
582
0
      eh->stub_offset = 0;
583
0
      eh->target_value = 0;
584
0
      eh->target_section = NULL;
585
0
      eh->stub_type = kvx_stub_none;
586
0
      eh->h = NULL;
587
0
      eh->id_sec = NULL;
588
0
    }
589
590
0
  return entry;
591
0
}
592
593
/* Copy the extra info we tack onto an elf_link_hash_entry.  */
594
595
static void
596
elf32_kvx_copy_indirect_symbol (struct bfd_link_info *info,
597
        struct elf_link_hash_entry *dir,
598
        struct elf_link_hash_entry *ind)
599
0
{
600
0
  struct elf_kvx_link_hash_entry *edir, *eind;
601
602
0
  edir = (struct elf_kvx_link_hash_entry *) dir;
603
0
  eind = (struct elf_kvx_link_hash_entry *) ind;
604
605
0
  if (ind->root.type == bfd_link_hash_indirect)
606
0
    {
607
      /* Copy over PLT info.  */
608
0
      if (dir->got.refcount <= 0)
609
0
  {
610
0
    edir->got_type = eind->got_type;
611
0
    eind->got_type = GOT_UNKNOWN;
612
0
  }
613
0
    }
614
615
0
  _bfd_elf_link_hash_copy_indirect (info, dir, ind);
616
0
}
617
618
/* Destroy a KVX elf linker hash table.  */
619
620
static void
621
elf32_kvx_link_hash_table_free (bfd *obfd)
622
0
{
623
0
  struct elf_kvx_link_hash_table *ret
624
0
    = (struct elf_kvx_link_hash_table *) obfd->link.hash;
625
626
0
  bfd_hash_table_free (&ret->stub_hash_table);
627
0
  _bfd_elf_link_hash_table_free (obfd);
628
0
}
629
630
/* Create a KVX elf linker hash table.  */
631
632
static struct bfd_link_hash_table *
633
elf32_kvx_link_hash_table_create (bfd *abfd)
634
0
{
635
0
  struct elf_kvx_link_hash_table *ret;
636
0
  bfd_size_type amt = sizeof (struct elf_kvx_link_hash_table);
637
638
0
  ret = bfd_zmalloc (amt);
639
0
  if (ret == NULL)
640
0
    return NULL;
641
642
0
  if (!_bfd_elf_link_hash_table_init
643
0
      (&ret->root, abfd, elf32_kvx_link_hash_newfunc,
644
0
       sizeof (struct elf_kvx_link_hash_entry)))
645
0
    {
646
0
      free (ret);
647
0
      return NULL;
648
0
    }
649
650
0
  ret->plt_header_size = PLT_ENTRY_SIZE;
651
0
  ret->plt_entry_size = PLT_SMALL_ENTRY_SIZE;
652
0
  ret->plt_entry = elf32_kvx_small_plt_entry;
653
654
0
  ret->obfd = abfd;
655
656
0
  if (!bfd_hash_table_init (&ret->stub_hash_table, stub_hash_newfunc,
657
0
          sizeof (struct elf_kvx_stub_hash_entry)))
658
0
    {
659
0
      _bfd_elf_link_hash_table_free (abfd);
660
0
      return NULL;
661
0
    }
662
663
0
  ret->root.root.hash_table_free = elf32_kvx_link_hash_table_free;
664
665
0
  return &ret->root.root;
666
0
}
667
668
static bfd_reloc_status_type
669
kvx_relocate (unsigned int r_type, bfd *input_bfd, asection *input_section,
670
        bfd_vma offset, bfd_vma value)
671
0
{
672
0
  reloc_howto_type *howto;
673
674
0
  howto = elf32_kvx_howto_from_type (input_bfd, r_type);
675
0
  r_type = elf32_kvx_bfd_reloc_from_type (input_bfd, r_type);
676
0
  return _bfd_kvx_elf_put_addend (input_bfd,
677
0
          input_section->contents + offset, r_type,
678
0
          howto, value);
679
0
}
680
681
/* Determine the type of stub needed, if any, for a call.  */
682
683
static enum elf_kvx_stub_type
684
kvx_type_of_stub (asection *input_sec,
685
      const Elf_Internal_Rela *rel,
686
      asection *sym_sec,
687
      unsigned char st_type,
688
      bfd_vma destination)
689
0
{
690
0
  bfd_vma location;
691
0
  bfd_signed_vma branch_offset;
692
0
  unsigned int r_type;
693
0
  enum elf_kvx_stub_type stub_type = kvx_stub_none;
694
695
0
  if (st_type != STT_FUNC
696
0
      && (sym_sec == input_sec))
697
0
    return stub_type;
698
699
  /* Determine where the call point is.  */
700
0
  location = (input_sec->output_offset
701
0
        + input_sec->output_section->vma + rel->r_offset);
702
703
0
  branch_offset = (bfd_signed_vma) (destination - location);
704
705
0
  r_type = ELF32_R_TYPE (rel->r_info);
706
707
  /* We don't want to redirect any old unconditional jump in this way,
708
     only one which is being used for a sibcall, where it is
709
     acceptable for the R16 and R17 registers to be clobbered.  */
710
0
  if (r_type == R_KVX_PCREL27
711
0
      && (branch_offset > KVX_MAX_FWD_CALL_OFFSET
712
0
    || branch_offset < KVX_MAX_BWD_CALL_OFFSET))
713
0
    {
714
0
      stub_type = kvx_stub_long_branch;
715
0
    }
716
717
0
  return stub_type;
718
0
}
719
720
/* Build a name for an entry in the stub hash table.  */
721
722
static char *
723
elf32_kvx_stub_name (const asection *input_section,
724
         const asection *sym_sec,
725
         const struct elf_kvx_link_hash_entry *hash,
726
         const Elf_Internal_Rela *rel)
727
0
{
728
0
  char *stub_name;
729
0
  bfd_size_type len;
730
731
0
  if (hash)
732
0
    {
733
0
      len = 8 + 1 + strlen (hash->root.root.root.string) + 1 + 16 + 1;
734
0
      stub_name = bfd_malloc (len);
735
0
      if (stub_name != NULL)
736
0
  snprintf (stub_name, len, "%08x_%s+%" PRIx64 "x",
737
0
      (unsigned int) input_section->id,
738
0
      hash->root.root.root.string,
739
0
      (uint64_t) rel->r_addend);
740
0
    }
741
0
  else
742
0
    {
743
0
      len = 8 + 1 + 8 + 1 + 8 + 1 + 16 + 1;
744
0
      stub_name = bfd_malloc (len);
745
0
      if (stub_name != NULL)
746
0
  snprintf (stub_name, len, "%08x_%x:%x+%" PRIx64 "x",
747
0
      (unsigned int) input_section->id,
748
0
      (unsigned int) sym_sec->id,
749
0
      (unsigned int) ELF32_R_SYM (rel->r_info),
750
0
      (uint64_t) rel->r_addend);
751
0
    }
752
753
0
  return stub_name;
754
0
}
755
756
/* Return true if symbol H should be hashed in the `.gnu.hash' section.  For
757
   executable PLT slots where the executable never takes the address of those
758
   functions, the function symbols are not added to the hash table.  */
759
760
static bool
761
elf_kvx_hash_symbol (struct elf_link_hash_entry *h)
762
0
{
763
0
  if (h->plt.offset != (bfd_vma) -1
764
0
      && !h->def_regular
765
0
      && !h->pointer_equality_needed)
766
0
    return false;
767
768
0
  return _bfd_elf_hash_symbol (h);
769
0
}
770
771
772
/* Look up an entry in the stub hash.  Stub entries are cached because
773
   creating the stub name takes a bit of time.  */
774
775
static struct elf_kvx_stub_hash_entry *
776
elf32_kvx_get_stub_entry (const asection *input_section,
777
        const asection *sym_sec,
778
        struct elf_link_hash_entry *hash,
779
        const Elf_Internal_Rela *rel,
780
        struct elf_kvx_link_hash_table *htab)
781
0
{
782
0
  struct elf_kvx_stub_hash_entry *stub_entry;
783
0
  struct elf_kvx_link_hash_entry *h =
784
0
    (struct elf_kvx_link_hash_entry *) hash;
785
0
  const asection *id_sec;
786
787
0
  if ((input_section->flags & SEC_CODE) == 0)
788
0
    return NULL;
789
790
  /* If this input section is part of a group of sections sharing one
791
     stub section, then use the id of the first section in the group.
792
     Stub names need to include a section id, as there may well be
793
     more than one stub used to reach say, printf, and we need to
794
     distinguish between them.  */
795
0
  id_sec = htab->stub_group[input_section->id].link_sec;
796
797
0
  if (h != NULL && h->stub_cache != NULL
798
0
      && h->stub_cache->h == h && h->stub_cache->id_sec == id_sec)
799
0
    {
800
0
      stub_entry = h->stub_cache;
801
0
    }
802
0
  else
803
0
    {
804
0
      char *stub_name;
805
806
0
      stub_name = elf32_kvx_stub_name (id_sec, sym_sec, h, rel);
807
0
      if (stub_name == NULL)
808
0
  return NULL;
809
810
0
      stub_entry = kvx_stub_hash_lookup (&htab->stub_hash_table,
811
0
           stub_name, false, false);
812
0
      if (h != NULL)
813
0
  h->stub_cache = stub_entry;
814
815
0
      free (stub_name);
816
0
    }
817
818
0
  return stub_entry;
819
0
}
820
821
822
/* Create a stub section.  */
823
824
static asection *
825
_bfd_kvx_create_stub_section (asection *section,
826
            struct elf_kvx_link_hash_table *htab)
827
828
0
{
829
0
  size_t namelen;
830
0
  bfd_size_type len;
831
0
  char *s_name;
832
833
0
  namelen = strlen (section->name);
834
0
  len = namelen + sizeof (STUB_SUFFIX);
835
0
  s_name = bfd_alloc (htab->stub_bfd, len);
836
0
  if (s_name == NULL)
837
0
    return NULL;
838
839
0
  memcpy (s_name, section->name, namelen);
840
0
  memcpy (s_name + namelen, STUB_SUFFIX, sizeof (STUB_SUFFIX));
841
0
  return (*htab->add_stub_section) (s_name, section);
842
0
}
843
844
845
/* Find or create a stub section for a link section.
846
847
   Fix or create the stub section used to collect stubs attached to
848
   the specified link section.  */
849
850
static asection *
851
_bfd_kvx_get_stub_for_link_section (asection *link_section,
852
            struct elf_kvx_link_hash_table *htab)
853
0
{
854
0
  if (htab->stub_group[link_section->id].stub_sec == NULL)
855
0
    htab->stub_group[link_section->id].stub_sec
856
0
      = _bfd_kvx_create_stub_section (link_section, htab);
857
0
  return htab->stub_group[link_section->id].stub_sec;
858
0
}
859
860
861
/* Find or create a stub section in the stub group for an input
862
   section.  */
863
864
static asection *
865
_bfd_kvx_create_or_find_stub_sec (asection *section,
866
          struct elf_kvx_link_hash_table *htab)
867
0
{
868
0
  asection *link_sec = htab->stub_group[section->id].link_sec;
869
0
  return _bfd_kvx_get_stub_for_link_section (link_sec, htab);
870
0
}
871
872
873
/* Add a new stub entry in the stub group associated with an input
874
   section to the stub hash.  Not all fields of the new stub entry are
875
   initialised.  */
876
877
static struct elf_kvx_stub_hash_entry *
878
_bfd_kvx_add_stub_entry_in_group (const char *stub_name,
879
          asection *section,
880
          struct elf_kvx_link_hash_table *htab)
881
0
{
882
0
  asection *link_sec;
883
0
  asection *stub_sec;
884
0
  struct elf_kvx_stub_hash_entry *stub_entry;
885
886
0
  link_sec = htab->stub_group[section->id].link_sec;
887
0
  stub_sec = _bfd_kvx_create_or_find_stub_sec (section, htab);
888
889
  /* Enter this entry into the linker stub hash table.  */
890
0
  stub_entry = kvx_stub_hash_lookup (&htab->stub_hash_table, stub_name,
891
0
             true, false);
892
0
  if (stub_entry == NULL)
893
0
    {
894
      /* xgettext:c-format */
895
0
      _bfd_error_handler (_("%pB: cannot create stub entry %s"),
896
0
        section->owner, stub_name);
897
0
      return NULL;
898
0
    }
899
900
0
  stub_entry->stub_sec = stub_sec;
901
0
  stub_entry->stub_offset = 0;
902
0
  stub_entry->id_sec = link_sec;
903
904
0
  return stub_entry;
905
0
}
906
907
static bool
908
kvx_build_one_stub (struct bfd_hash_entry *gen_entry,
909
        void *in_arg)
910
0
{
911
0
  struct elf_kvx_stub_hash_entry *stub_entry;
912
0
  asection *stub_sec;
913
0
  bfd *stub_bfd;
914
0
  bfd_byte *loc;
915
0
  bfd_vma sym_value;
916
0
  unsigned int template_size;
917
0
  const uint32_t *template;
918
0
  unsigned int i;
919
0
  struct bfd_link_info *info;
920
921
  /* Massage our args to the form they really have.  */
922
0
  stub_entry = (struct elf_kvx_stub_hash_entry *) gen_entry;
923
924
0
  info = (struct bfd_link_info *) in_arg;
925
926
  /* Fail if the target section could not be assigned to an output
927
     section.  The user should fix his linker script.  */
928
0
  if (stub_entry->target_section->output_section == NULL
929
0
      && info->non_contiguous_regions)
930
0
    info->callbacks->fatal (_("%P: Could not assign '%pA' to an output section. "
931
0
            "Retry without "
932
0
            "--enable-non-contiguous-regions.\n"),
933
0
          stub_entry->target_section);
934
935
0
  stub_sec = stub_entry->stub_sec;
936
937
  /* Make a note of the offset within the stubs for this entry.  */
938
0
  stub_entry->stub_offset = stub_sec->size;
939
0
  loc = stub_sec->contents + stub_entry->stub_offset;
940
941
0
  stub_bfd = stub_sec->owner;
942
943
  /* This is the address of the stub destination.  */
944
0
  sym_value = (stub_entry->target_value
945
0
         + stub_entry->target_section->output_offset
946
0
         + stub_entry->target_section->output_section->vma);
947
948
0
  switch (stub_entry->stub_type)
949
0
    {
950
0
    case kvx_stub_long_branch:
951
0
      template = elf32_kvx_long_branch_stub;
952
0
      template_size = sizeof (elf32_kvx_long_branch_stub);
953
0
      break;
954
0
    default:
955
0
      abort ();
956
0
    }
957
958
0
  for (i = 0; i < (template_size / sizeof template[0]); i++)
959
0
    {
960
0
      bfd_putl32 (template[i], loc);
961
0
      loc += 4;
962
0
    }
963
964
0
  stub_sec->size += template_size;
965
966
0
  switch (stub_entry->stub_type)
967
0
    {
968
0
    case kvx_stub_long_branch:
969
      /* The stub uses a make insn with 43bits immediate.
970
   We need to apply 3 relocations:
971
   BFD_RELOC_KVX_S43_LO10,
972
   BFD_RELOC_KVX_S43_UP27,
973
   BFD_RELOC_KVX_S43_EX6.  */
974
0
      if (kvx_relocate (R_KVX_S43_LO10, stub_bfd, stub_sec,
975
0
      stub_entry->stub_offset, sym_value) != bfd_reloc_ok)
976
0
  BFD_FAIL ();
977
0
      if (kvx_relocate (R_KVX_S43_EX6, stub_bfd, stub_sec,
978
0
      stub_entry->stub_offset, sym_value) != bfd_reloc_ok)
979
0
  BFD_FAIL ();
980
0
      if (kvx_relocate (R_KVX_S43_UP27, stub_bfd, stub_sec,
981
0
      stub_entry->stub_offset + 4, sym_value) != bfd_reloc_ok)
982
0
  BFD_FAIL ();
983
0
      break;
984
0
    default:
985
0
      abort ();
986
0
    }
987
988
0
  return true;
989
0
}
990
991
/* As above, but don't actually build the stub.  Just bump offset so
992
   we know stub section sizes.  */
993
994
static bool
995
kvx_size_one_stub (struct bfd_hash_entry *gen_entry,
996
       void *in_arg ATTRIBUTE_UNUSED)
997
0
{
998
0
  struct elf_kvx_stub_hash_entry *stub_entry;
999
0
  int size;
1000
1001
  /* Massage our args to the form they really have.  */
1002
0
  stub_entry = (struct elf_kvx_stub_hash_entry *) gen_entry;
1003
1004
0
  switch (stub_entry->stub_type)
1005
0
    {
1006
0
    case kvx_stub_long_branch:
1007
0
      size = sizeof (elf32_kvx_long_branch_stub);
1008
0
      break;
1009
0
    default:
1010
0
      abort ();
1011
0
    }
1012
1013
0
  stub_entry->stub_sec->size += size;
1014
0
  return true;
1015
0
}
1016
1017
/* External entry points for sizing and building linker stubs.  */
1018
1019
/* Set up various things so that we can make a list of input sections
1020
   for each output section included in the link.  Returns -1 on error,
1021
   0 when no stubs will be needed, and 1 on success.  */
1022
1023
int
1024
elf32_kvx_setup_section_lists (bfd *output_bfd,
1025
             struct bfd_link_info *info)
1026
0
{
1027
0
  bfd *input_bfd;
1028
0
  unsigned int bfd_count;
1029
0
  unsigned int top_id, top_index;
1030
0
  asection *section;
1031
0
  asection **input_list, **list;
1032
0
  bfd_size_type amt;
1033
0
  struct elf_kvx_link_hash_table *htab =
1034
0
    elf_kvx_hash_table (info);
1035
1036
0
  if (!is_elf_hash_table ((const struct bfd_link_hash_table *)htab))
1037
0
    return 0;
1038
1039
  /* Count the number of input BFDs and find the top input section id.  */
1040
0
  for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
1041
0
       input_bfd != NULL; input_bfd = input_bfd->link.next)
1042
0
    {
1043
0
      bfd_count += 1;
1044
0
      for (section = input_bfd->sections;
1045
0
     section != NULL; section = section->next)
1046
0
  {
1047
0
    if (top_id < section->id)
1048
0
      top_id = section->id;
1049
0
  }
1050
0
    }
1051
0
  htab->bfd_count = bfd_count;
1052
1053
0
  amt = sizeof (struct map_stub) * (top_id + 1);
1054
0
  htab->stub_group = bfd_zmalloc (amt);
1055
0
  if (htab->stub_group == NULL)
1056
0
    return -1;
1057
1058
  /* We can't use output_bfd->section_count here to find the top output
1059
     section index as some sections may have been removed, and
1060
     _bfd_strip_section_from_output doesn't renumber the indices.  */
1061
0
  for (section = output_bfd->sections, top_index = 0;
1062
0
       section != NULL; section = section->next)
1063
0
    {
1064
0
      if (top_index < section->index)
1065
0
  top_index = section->index;
1066
0
    }
1067
1068
0
  htab->top_index = top_index;
1069
0
  amt = sizeof (asection *) * (top_index + 1);
1070
0
  input_list = bfd_malloc (amt);
1071
0
  htab->input_list = input_list;
1072
0
  if (input_list == NULL)
1073
0
    return -1;
1074
1075
  /* For sections we aren't interested in, mark their entries with a
1076
     value we can check later.  */
1077
0
  list = input_list + top_index;
1078
0
  do
1079
0
    *list = bfd_abs_section_ptr;
1080
0
  while (list-- != input_list);
1081
1082
0
  for (section = output_bfd->sections;
1083
0
       section != NULL; section = section->next)
1084
0
    {
1085
0
      if ((section->flags & SEC_CODE) != 0)
1086
0
  input_list[section->index] = NULL;
1087
0
    }
1088
1089
0
  return 1;
1090
0
}
1091
1092
/* Used by elf32_kvx_next_input_section and group_sections.  */
1093
0
#define PREV_SEC(sec) (htab->stub_group[(sec)->id].link_sec)
1094
1095
/* The linker repeatedly calls this function for each input section,
1096
   in the order that input sections are linked into output sections.
1097
   Build lists of input sections to determine groupings between which
1098
   we may insert linker stubs.  */
1099
1100
void
1101
elf32_kvx_next_input_section (struct bfd_link_info *info, asection *isec)
1102
0
{
1103
0
  struct elf_kvx_link_hash_table *htab =
1104
0
    elf_kvx_hash_table (info);
1105
1106
0
  if (isec->output_section->index <= htab->top_index)
1107
0
    {
1108
0
      asection **list = htab->input_list + isec->output_section->index;
1109
1110
0
      if (*list != bfd_abs_section_ptr)
1111
0
  {
1112
    /* Steal the link_sec pointer for our list.  */
1113
    /* This happens to make the list in reverse order,
1114
       which is what we want.  */
1115
0
    PREV_SEC (isec) = *list;
1116
0
    *list = isec;
1117
0
  }
1118
0
    }
1119
0
}
1120
1121
/* See whether we can group stub sections together.  Grouping stub
1122
   sections may result in fewer stubs.  More importantly, we need to
1123
   put all .init* and .fini* stubs at the beginning of the .init or
1124
   .fini output sections respectively, because glibc splits the
1125
   _init and _fini functions into multiple parts.  Putting a stub in
1126
   the middle of a function is not a good idea.  */
1127
1128
static void
1129
group_sections (struct elf_kvx_link_hash_table *htab,
1130
    bfd_size_type stub_group_size,
1131
    bool stubs_always_after_branch)
1132
0
{
1133
0
  asection **list = htab->input_list;
1134
1135
0
  do
1136
0
    {
1137
0
      asection *tail = *list;
1138
0
      asection *head;
1139
1140
0
      if (tail == bfd_abs_section_ptr)
1141
0
  continue;
1142
1143
      /* Reverse the list: we must avoid placing stubs at the
1144
   beginning of the section because the beginning of the text
1145
   section may be required for an interrupt vector in bare metal
1146
   code.  */
1147
0
#define NEXT_SEC PREV_SEC
1148
0
      head = NULL;
1149
0
      while (tail != NULL)
1150
0
  {
1151
    /* Pop from tail.  */
1152
0
    asection *item = tail;
1153
0
    tail = PREV_SEC (item);
1154
1155
    /* Push on head.  */
1156
0
    NEXT_SEC (item) = head;
1157
0
    head = item;
1158
0
  }
1159
1160
0
      while (head != NULL)
1161
0
  {
1162
0
    asection *curr;
1163
0
    asection *next;
1164
0
    bfd_vma stub_group_start = head->output_offset;
1165
0
    bfd_vma end_of_next;
1166
1167
0
    curr = head;
1168
0
    while (NEXT_SEC (curr) != NULL)
1169
0
      {
1170
0
        next = NEXT_SEC (curr);
1171
0
        end_of_next = next->output_offset + next->size;
1172
0
        if (end_of_next - stub_group_start >= stub_group_size)
1173
    /* End of NEXT is too far from start, so stop.  */
1174
0
    break;
1175
        /* Add NEXT to the group.  */
1176
0
        curr = next;
1177
0
      }
1178
1179
    /* OK, the size from the start to the start of CURR is less
1180
       than stub_group_size and thus can be handled by one stub
1181
       section.  (Or the head section is itself larger than
1182
       stub_group_size, in which case we may be toast.)
1183
       We should really be keeping track of the total size of
1184
       stubs added here, as stubs contribute to the final output
1185
       section size.  */
1186
0
    do
1187
0
      {
1188
0
        next = NEXT_SEC (head);
1189
        /* Set up this stub group.  */
1190
0
        htab->stub_group[head->id].link_sec = curr;
1191
0
      }
1192
0
    while (head != curr && (head = next) != NULL);
1193
1194
    /* But wait, there's more!  Input sections up to stub_group_size
1195
       bytes after the stub section can be handled by it too.  */
1196
0
    if (!stubs_always_after_branch)
1197
0
      {
1198
0
        stub_group_start = curr->output_offset + curr->size;
1199
1200
0
        while (next != NULL)
1201
0
    {
1202
0
      end_of_next = next->output_offset + next->size;
1203
0
      if (end_of_next - stub_group_start >= stub_group_size)
1204
        /* End of NEXT is too far from stubs, so stop.  */
1205
0
        break;
1206
      /* Add NEXT to the stub group.  */
1207
0
      head = next;
1208
0
      next = NEXT_SEC (head);
1209
0
      htab->stub_group[head->id].link_sec = curr;
1210
0
    }
1211
0
      }
1212
0
    head = next;
1213
0
  }
1214
0
    }
1215
0
  while (list++ != htab->input_list + htab->top_index);
1216
1217
0
  free (htab->input_list);
1218
0
}
1219
1220
static void
1221
_bfd_kvx_resize_stubs (struct elf_kvx_link_hash_table *htab)
1222
0
{
1223
0
  asection *section;
1224
1225
  /* OK, we've added some stubs.  Find out the new size of the
1226
     stub sections.  */
1227
0
  for (section = htab->stub_bfd->sections;
1228
0
       section != NULL; section = section->next)
1229
0
    {
1230
      /* Ignore non-stub sections.  */
1231
0
      if (!strstr (section->name, STUB_SUFFIX))
1232
0
  continue;
1233
0
      section->size = 0;
1234
0
    }
1235
1236
0
  bfd_hash_traverse (&htab->stub_hash_table, kvx_size_one_stub, htab);
1237
0
}
1238
1239
/* Determine and set the size of the stub section for a final link.
1240
1241
   The basic idea here is to examine all the relocations looking for
1242
   PC-relative calls to a target that is unreachable with a 27bits
1243
   immediate (found in call and goto).  */
1244
1245
bool
1246
elf32_kvx_size_stubs (bfd *output_bfd,
1247
         bfd *stub_bfd,
1248
         struct bfd_link_info *info,
1249
         bfd_signed_vma group_size,
1250
         asection * (*add_stub_section) (const char *,
1251
                 asection *),
1252
         void (*layout_sections_again) (void))
1253
0
{
1254
0
  bfd_size_type stub_group_size;
1255
0
  bool stubs_always_before_branch;
1256
0
  bool stub_changed = false;
1257
0
  struct elf_kvx_link_hash_table *htab = elf_kvx_hash_table (info);
1258
1259
  /* Propagate mach to stub bfd, because it may not have been
1260
     finalized when we created stub_bfd.  */
1261
0
  bfd_set_arch_mach (stub_bfd, bfd_get_arch (output_bfd),
1262
0
         bfd_get_mach (output_bfd));
1263
1264
  /* Stash our params away.  */
1265
0
  htab->stub_bfd = stub_bfd;
1266
0
  htab->add_stub_section = add_stub_section;
1267
0
  htab->layout_sections_again = layout_sections_again;
1268
0
  stubs_always_before_branch = group_size < 0;
1269
0
  if (group_size < 0)
1270
0
    stub_group_size = -group_size;
1271
0
  else
1272
0
    stub_group_size = group_size;
1273
1274
0
  if (stub_group_size == 1)
1275
0
    {
1276
      /* Default values.  */
1277
      /* KVX branch range is +-256MB. The value used is 1MB less.  */
1278
0
      stub_group_size = 255 * 1024 * 1024;
1279
0
    }
1280
1281
0
  group_sections (htab, stub_group_size, stubs_always_before_branch);
1282
1283
0
  (*htab->layout_sections_again) ();
1284
1285
0
  while (1)
1286
0
    {
1287
0
      bfd *input_bfd;
1288
1289
0
      for (input_bfd = info->input_bfds;
1290
0
     input_bfd != NULL; input_bfd = input_bfd->link.next)
1291
0
  {
1292
0
    Elf_Internal_Shdr *symtab_hdr;
1293
0
    asection *section;
1294
0
    Elf_Internal_Sym *local_syms = NULL;
1295
1296
0
    if (!is_kvx_elf (input_bfd)
1297
0
        || (input_bfd->flags & BFD_LINKER_CREATED) != 0)
1298
0
      continue;
1299
1300
    /* We'll need the symbol table in a second.  */
1301
0
    symtab_hdr = &elf_symtab_hdr (input_bfd);
1302
0
    if (symtab_hdr->sh_info == 0)
1303
0
      continue;
1304
1305
    /* Walk over each section attached to the input bfd.  */
1306
0
    for (section = input_bfd->sections;
1307
0
         section != NULL; section = section->next)
1308
0
      {
1309
0
        Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
1310
1311
        /* If there aren't any relocs, then there's nothing more
1312
     to do.  */
1313
0
        if ((section->flags & SEC_RELOC) == 0
1314
0
      || section->reloc_count == 0
1315
0
      || (section->flags & SEC_CODE) == 0)
1316
0
    continue;
1317
1318
        /* If this section is a link-once section that will be
1319
     discarded, then don't create any stubs.  */
1320
0
        if (section->output_section == NULL
1321
0
      || section->output_section->owner != output_bfd)
1322
0
    continue;
1323
1324
        /* Get the relocs.  */
1325
0
        internal_relocs
1326
0
    = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
1327
0
               info->keep_memory);
1328
0
        if (internal_relocs == NULL)
1329
0
    goto error_ret_free_local;
1330
1331
        /* Now examine each relocation.  */
1332
0
        irela = internal_relocs;
1333
0
        irelaend = irela + section->reloc_count;
1334
0
        for (; irela < irelaend; irela++)
1335
0
    {
1336
0
      unsigned int r_type, r_indx;
1337
0
      enum elf_kvx_stub_type stub_type;
1338
0
      struct elf_kvx_stub_hash_entry *stub_entry;
1339
0
      asection *sym_sec;
1340
0
      bfd_vma sym_value;
1341
0
      bfd_vma destination;
1342
0
      struct elf_kvx_link_hash_entry *hash;
1343
0
      const char *sym_name;
1344
0
      char *stub_name;
1345
0
      const asection *id_sec;
1346
0
      unsigned char st_type;
1347
0
      bfd_size_type len;
1348
1349
0
      r_type = ELF32_R_TYPE (irela->r_info);
1350
0
      r_indx = ELF32_R_SYM (irela->r_info);
1351
1352
0
      if (r_type >= (unsigned int) R_KVX_end)
1353
0
        {
1354
0
          bfd_set_error (bfd_error_bad_value);
1355
0
        error_ret_free_internal:
1356
0
          if (elf_section_data (section)->relocs == NULL)
1357
0
      free (internal_relocs);
1358
0
          goto error_ret_free_local;
1359
0
        }
1360
1361
      /* Only look for stubs on unconditional branch and
1362
         branch and link instructions.  */
1363
      /* This catches CALL and GOTO insn */
1364
0
      if (r_type != (unsigned int) R_KVX_PCREL27)
1365
0
        continue;
1366
1367
      /* Now determine the call target, its name, value,
1368
         section.  */
1369
0
      sym_sec = NULL;
1370
0
      sym_value = 0;
1371
0
      destination = 0;
1372
0
      hash = NULL;
1373
0
      sym_name = NULL;
1374
0
      if (r_indx < symtab_hdr->sh_info)
1375
0
        {
1376
          /* It's a local symbol.  */
1377
0
          Elf_Internal_Sym *sym;
1378
0
          Elf_Internal_Shdr *hdr;
1379
1380
0
          if (local_syms == NULL)
1381
0
      {
1382
0
        local_syms
1383
0
          = (Elf_Internal_Sym *) symtab_hdr->contents;
1384
0
        if (local_syms == NULL)
1385
0
          local_syms
1386
0
            = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1387
0
                  symtab_hdr->sh_info, 0,
1388
0
                  NULL, NULL, NULL);
1389
0
        if (local_syms == NULL)
1390
0
          goto error_ret_free_internal;
1391
0
      }
1392
1393
0
          sym = local_syms + r_indx;
1394
0
          hdr = elf_elfsections (input_bfd)[sym->st_shndx];
1395
0
          sym_sec = hdr->bfd_section;
1396
0
          if (!sym_sec)
1397
      /* This is an undefined symbol.  It can never
1398
         be resolved.  */
1399
0
      continue;
1400
1401
0
          if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
1402
0
      sym_value = sym->st_value;
1403
0
          destination = (sym_value + irela->r_addend
1404
0
             + sym_sec->output_offset
1405
0
             + sym_sec->output_section->vma);
1406
0
          st_type = ELF_ST_TYPE (sym->st_info);
1407
0
          sym_name
1408
0
      = bfd_elf_string_from_elf_section (input_bfd,
1409
0
                 symtab_hdr->sh_link,
1410
0
                 sym->st_name);
1411
0
        }
1412
0
      else
1413
0
        {
1414
0
          int e_indx;
1415
1416
0
          e_indx = r_indx - symtab_hdr->sh_info;
1417
0
          hash = ((struct elf_kvx_link_hash_entry *)
1418
0
            elf_sym_hashes (input_bfd)[e_indx]);
1419
1420
0
          while (hash->root.root.type == bfd_link_hash_indirect
1421
0
           || hash->root.root.type == bfd_link_hash_warning)
1422
0
      hash = ((struct elf_kvx_link_hash_entry *)
1423
0
        hash->root.root.u.i.link);
1424
1425
0
          if (hash->root.root.type == bfd_link_hash_defined
1426
0
        || hash->root.root.type == bfd_link_hash_defweak)
1427
0
      {
1428
0
        struct elf_kvx_link_hash_table *globals =
1429
0
          elf_kvx_hash_table (info);
1430
0
        sym_sec = hash->root.root.u.def.section;
1431
0
        sym_value = hash->root.root.u.def.value;
1432
        /* For a destination in a shared library,
1433
           use the PLT stub as target address to
1434
           decide whether a branch stub is
1435
           needed.  */
1436
0
        if (globals->root.splt != NULL && hash != NULL
1437
0
            && hash->root.plt.offset != (bfd_vma) - 1)
1438
0
          {
1439
0
            sym_sec = globals->root.splt;
1440
0
            sym_value = hash->root.plt.offset;
1441
0
            if (sym_sec->output_section != NULL)
1442
0
        destination = (sym_value
1443
0
                 + sym_sec->output_offset
1444
0
                 + sym_sec->output_section->vma);
1445
0
          }
1446
0
        else if (sym_sec->output_section != NULL)
1447
0
          destination = (sym_value + irela->r_addend
1448
0
             + sym_sec->output_offset
1449
0
             + sym_sec->output_section->vma);
1450
0
      }
1451
0
          else if (hash->root.root.type == bfd_link_hash_undefined
1452
0
             || (hash->root.root.type
1453
0
           == bfd_link_hash_undefweak))
1454
0
      {
1455
        /* For a shared library, use the PLT stub as
1456
           target address to decide whether a long
1457
           branch stub is needed.
1458
           For absolute code, they cannot be handled.  */
1459
0
        struct elf_kvx_link_hash_table *globals =
1460
0
          elf_kvx_hash_table (info);
1461
1462
0
        if (globals->root.splt != NULL && hash != NULL
1463
0
            && hash->root.plt.offset != (bfd_vma) - 1)
1464
0
          {
1465
0
            sym_sec = globals->root.splt;
1466
0
            sym_value = hash->root.plt.offset;
1467
0
            if (sym_sec->output_section != NULL)
1468
0
        destination = (sym_value
1469
0
                 + sym_sec->output_offset
1470
0
                 + sym_sec->output_section->vma);
1471
0
          }
1472
0
        else
1473
0
          continue;
1474
0
      }
1475
0
          else
1476
0
      {
1477
0
        bfd_set_error (bfd_error_bad_value);
1478
0
        goto error_ret_free_internal;
1479
0
      }
1480
0
          st_type = ELF_ST_TYPE (hash->root.type);
1481
0
          sym_name = hash->root.root.root.string;
1482
0
        }
1483
1484
      /* Determine what (if any) linker stub is needed.  */
1485
0
      stub_type = kvx_type_of_stub (section, irela, sym_sec,
1486
0
            st_type, destination);
1487
0
      if (stub_type == kvx_stub_none)
1488
0
        continue;
1489
1490
      /* Support for grouping stub sections.  */
1491
0
      id_sec = htab->stub_group[section->id].link_sec;
1492
1493
      /* Get the name of this stub.  */
1494
0
      stub_name = elf32_kvx_stub_name (id_sec, sym_sec, hash,
1495
0
              irela);
1496
0
      if (!stub_name)
1497
0
        goto error_ret_free_internal;
1498
1499
0
      stub_entry =
1500
0
        kvx_stub_hash_lookup (&htab->stub_hash_table,
1501
0
           stub_name, false, false);
1502
0
      if (stub_entry != NULL)
1503
0
        {
1504
          /* The proper stub has already been created.  */
1505
0
          free (stub_name);
1506
          /* Always update this stub's target since it may have
1507
       changed after layout.  */
1508
0
          stub_entry->target_value = sym_value + irela->r_addend;
1509
0
          continue;
1510
0
        }
1511
1512
0
      stub_entry = _bfd_kvx_add_stub_entry_in_group
1513
0
        (stub_name, section, htab);
1514
0
      if (stub_entry == NULL)
1515
0
        {
1516
0
          free (stub_name);
1517
0
          goto error_ret_free_internal;
1518
0
        }
1519
1520
0
      stub_entry->target_value = sym_value + irela->r_addend;
1521
0
      stub_entry->target_section = sym_sec;
1522
0
      stub_entry->stub_type = stub_type;
1523
0
      stub_entry->h = hash;
1524
0
      stub_entry->st_type = st_type;
1525
1526
0
      if (sym_name == NULL)
1527
0
        sym_name = "unnamed";
1528
0
      len = sizeof (STUB_ENTRY_NAME) + strlen (sym_name);
1529
0
      stub_entry->output_name = bfd_alloc (htab->stub_bfd, len);
1530
0
      if (stub_entry->output_name == NULL)
1531
0
        {
1532
0
          free (stub_name);
1533
0
          goto error_ret_free_internal;
1534
0
        }
1535
1536
0
      snprintf (stub_entry->output_name, len, STUB_ENTRY_NAME,
1537
0
          sym_name);
1538
1539
0
      stub_changed = true;
1540
0
    }
1541
1542
        /* We're done with the internal relocs, free them.  */
1543
0
        if (elf_section_data (section)->relocs == NULL)
1544
0
    free (internal_relocs);
1545
0
      }
1546
0
  }
1547
1548
0
      if (!stub_changed)
1549
0
  break;
1550
1551
0
      _bfd_kvx_resize_stubs (htab);
1552
1553
      /* Ask the linker to do its stuff.  */
1554
0
      (*htab->layout_sections_again) ();
1555
0
      stub_changed = false;
1556
0
    }
1557
1558
0
  return true;
1559
1560
0
error_ret_free_local:
1561
0
  return false;
1562
1563
0
}
1564
1565
/* Build all the stubs associated with the current output file.  The
1566
   stubs are kept in a hash table attached to the main linker hash
1567
   table.  We also set up the .plt entries for statically linked PIC
1568
   functions here.  This function is called via kvx_elf_finish in the
1569
   linker.  */
1570
1571
bool
1572
elf32_kvx_build_stubs (struct bfd_link_info *info)
1573
0
{
1574
0
  asection *stub_sec;
1575
0
  struct bfd_hash_table *table;
1576
0
  struct elf_kvx_link_hash_table *htab;
1577
1578
0
  htab = elf_kvx_hash_table (info);
1579
1580
0
  for (stub_sec = htab->stub_bfd->sections;
1581
0
       stub_sec != NULL; stub_sec = stub_sec->next)
1582
0
    {
1583
0
      bfd_size_type size;
1584
1585
      /* Ignore non-stub sections.  */
1586
0
      if (!strstr (stub_sec->name, STUB_SUFFIX))
1587
0
  continue;
1588
1589
      /* Allocate memory to hold the linker stubs.  */
1590
0
      size = stub_sec->size;
1591
0
      stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
1592
0
      if (stub_sec->contents == NULL && size != 0)
1593
0
  return false;
1594
0
      stub_sec->alloced = 1;
1595
0
      stub_sec->size = 0;
1596
0
    }
1597
1598
  /* Build the stubs as directed by the stub hash table.  */
1599
0
  table = &htab->stub_hash_table;
1600
0
  bfd_hash_traverse (table, kvx_build_one_stub, info);
1601
1602
0
  return true;
1603
0
}
1604
1605
static bfd_vma
1606
kvx_calculate_got_entry_vma (struct elf_link_hash_entry *h,
1607
         struct elf_kvx_link_hash_table
1608
         *globals, struct bfd_link_info *info,
1609
         bfd_vma value, bfd *output_bfd,
1610
         bool *unresolved_reloc_p)
1611
0
{
1612
0
  bfd_vma off = (bfd_vma) - 1;
1613
0
  asection *basegot = globals->root.sgot;
1614
0
  bool dyn = globals->root.dynamic_sections_created;
1615
1616
0
  if (h != NULL)
1617
0
    {
1618
0
      BFD_ASSERT (basegot != NULL);
1619
0
      off = h->got.offset;
1620
0
      BFD_ASSERT (off != (bfd_vma) - 1);
1621
0
      if (!WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, bfd_link_pic (info), h)
1622
0
    || (bfd_link_pic (info)
1623
0
        && SYMBOL_REFERENCES_LOCAL (info, h))
1624
0
    || (ELF_ST_VISIBILITY (h->other)
1625
0
        && h->root.type == bfd_link_hash_undefweak))
1626
0
  {
1627
    /* This is actually a static link, or it is a -Bsymbolic link
1628
       and the symbol is defined locally.  We must initialize this
1629
       entry in the global offset table.  Since the offset must
1630
       always be a multiple of 8 (4 in the case of ILP32), we use
1631
       the least significant bit to record whether we have
1632
       initialized it already.
1633
       When doing a dynamic link, we create a .rel(a).got relocation
1634
       entry to initialize the value.  This is done in the
1635
       finish_dynamic_symbol routine.  */
1636
0
    if ((off & 1) != 0)
1637
0
      off &= ~1;
1638
0
    else
1639
0
      {
1640
0
        bfd_put_32 (output_bfd, value, basegot->contents + off);
1641
0
        h->got.offset |= 1;
1642
0
      }
1643
0
  }
1644
0
      else
1645
0
  *unresolved_reloc_p = false;
1646
0
    }
1647
1648
0
  return off;
1649
0
}
1650
1651
static unsigned int
1652
kvx_reloc_got_type (bfd_reloc_code_real_type r_type)
1653
0
{
1654
0
  switch (r_type)
1655
0
    {
1656
      /* Extracted with:
1657
   awk 'match ($0, /HOWTO.*R_(KVX.*_GOT(OFF)?(64)?_.*),/,ary) \
1658
   {print "case BFD_RELOC_" ary[1] ":";}' elfxx-kvxc.def  */
1659
0
    case BFD_RELOC_KVX_S37_GOTOFF_LO10:
1660
0
    case BFD_RELOC_KVX_S37_GOTOFF_UP27:
1661
1662
0
    case BFD_RELOC_KVX_S37_GOT_LO10:
1663
0
    case BFD_RELOC_KVX_S37_GOT_UP27:
1664
1665
0
    case BFD_RELOC_KVX_S43_GOTOFF_LO10:
1666
0
    case BFD_RELOC_KVX_S43_GOTOFF_UP27:
1667
0
    case BFD_RELOC_KVX_S43_GOTOFF_EX6:
1668
1669
0
    case BFD_RELOC_KVX_S43_GOT_LO10:
1670
0
    case BFD_RELOC_KVX_S43_GOT_UP27:
1671
0
    case BFD_RELOC_KVX_S43_GOT_EX6:
1672
0
      return GOT_NORMAL;
1673
1674
0
    case BFD_RELOC_KVX_S37_TLS_GD_LO10:
1675
0
    case BFD_RELOC_KVX_S37_TLS_GD_UP27:
1676
0
    case BFD_RELOC_KVX_S43_TLS_GD_LO10:
1677
0
    case BFD_RELOC_KVX_S43_TLS_GD_UP27:
1678
0
    case BFD_RELOC_KVX_S43_TLS_GD_EX6:
1679
0
      return GOT_TLS_GD;
1680
1681
0
    case BFD_RELOC_KVX_S37_TLS_LD_LO10:
1682
0
    case BFD_RELOC_KVX_S37_TLS_LD_UP27:
1683
0
    case BFD_RELOC_KVX_S43_TLS_LD_LO10:
1684
0
    case BFD_RELOC_KVX_S43_TLS_LD_UP27:
1685
0
    case BFD_RELOC_KVX_S43_TLS_LD_EX6:
1686
0
      return GOT_TLS_LD;
1687
1688
0
    case BFD_RELOC_KVX_S37_TLS_IE_LO10:
1689
0
    case BFD_RELOC_KVX_S37_TLS_IE_UP27:
1690
0
    case BFD_RELOC_KVX_S43_TLS_IE_LO10:
1691
0
    case BFD_RELOC_KVX_S43_TLS_IE_UP27:
1692
0
    case BFD_RELOC_KVX_S43_TLS_IE_EX6:
1693
0
      return GOT_TLS_IE;
1694
1695
0
    default:
1696
0
      break;
1697
0
    }
1698
0
  return GOT_UNKNOWN;
1699
0
}
1700
1701
static bool
1702
kvx_can_relax_tls (bfd *input_bfd ATTRIBUTE_UNUSED,
1703
           struct bfd_link_info *info ATTRIBUTE_UNUSED,
1704
           bfd_reloc_code_real_type r_type ATTRIBUTE_UNUSED,
1705
           struct elf_link_hash_entry *h ATTRIBUTE_UNUSED,
1706
           unsigned long r_symndx ATTRIBUTE_UNUSED)
1707
0
{
1708
0
  if (! IS_KVX_TLS_RELAX_RELOC (r_type))
1709
0
    return false;
1710
1711
  /* Relaxing hook. Disabled on KVX. */
1712
  /* See elfnn-aarch64.c */
1713
0
  return true;
1714
0
}
1715
1716
/* Given the relocation code R_TYPE, return the relaxed bfd reloc
1717
   enumerator.  */
1718
1719
static bfd_reloc_code_real_type
1720
kvx_tls_transition (bfd *input_bfd,
1721
      struct bfd_link_info *info,
1722
      unsigned int r_type,
1723
      struct elf_link_hash_entry *h,
1724
      unsigned long r_symndx)
1725
0
{
1726
0
  bfd_reloc_code_real_type bfd_r_type
1727
0
    = elf32_kvx_bfd_reloc_from_type (input_bfd, r_type);
1728
1729
0
  if (! kvx_can_relax_tls (input_bfd, info, bfd_r_type, h, r_symndx))
1730
0
    return bfd_r_type;
1731
1732
0
  return bfd_r_type;
1733
0
}
1734
1735
/* Return the base VMA address which should be subtracted from real addresses
1736
   when resolving R_KVX_*_TLS_GD_* and R_KVX_*_TLS_LD_* relocation.  */
1737
1738
static bfd_vma
1739
dtpoff_base (struct bfd_link_info *info)
1740
0
{
1741
  /* If tls_sec is NULL, we should have signalled an error already.  */
1742
0
  BFD_ASSERT (elf_hash_table (info)->tls_sec != NULL);
1743
0
  return elf_hash_table (info)->tls_sec->vma;
1744
0
}
1745
1746
/* Return the base VMA address which should be subtracted from real addresses
1747
   when resolving R_KVX_*_TLS_IE_* and R_KVX_*_TLS_LE_* relocations.  */
1748
1749
static bfd_vma
1750
tpoff_base (struct bfd_link_info *info)
1751
0
{
1752
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
1753
1754
  /* If tls_sec is NULL, we should have signalled an error already.  */
1755
0
  BFD_ASSERT (htab->tls_sec != NULL);
1756
1757
0
  bfd_vma base = align_power ((bfd_vma) 0,
1758
0
            htab->tls_sec->alignment_power);
1759
0
  return htab->tls_sec->vma - base;
1760
0
}
1761
1762
static bfd_vma *
1763
symbol_got_offset_ref (bfd *input_bfd, struct elf_link_hash_entry *h,
1764
           unsigned long r_symndx)
1765
0
{
1766
  /* Calculate the address of the GOT entry for symbol
1767
     referred to in h.  */
1768
0
  if (h != NULL)
1769
0
    return &h->got.offset;
1770
0
  else
1771
0
    {
1772
      /* local symbol */
1773
0
      struct elf_kvx_local_symbol *l;
1774
1775
0
      l = elf_kvx_locals (input_bfd);
1776
0
      return &l[r_symndx].got_offset;
1777
0
    }
1778
0
}
1779
1780
static void
1781
symbol_got_offset_mark (bfd *input_bfd, struct elf_link_hash_entry *h,
1782
      unsigned long r_symndx)
1783
0
{
1784
0
  bfd_vma *p;
1785
0
  p = symbol_got_offset_ref (input_bfd, h, r_symndx);
1786
0
  *p |= 1;
1787
0
}
1788
1789
static int
1790
symbol_got_offset_mark_p (bfd *input_bfd, struct elf_link_hash_entry *h,
1791
        unsigned long r_symndx)
1792
0
{
1793
0
  bfd_vma value;
1794
0
  value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
1795
0
  return value & 1;
1796
0
}
1797
1798
static bfd_vma
1799
symbol_got_offset (bfd *input_bfd, struct elf_link_hash_entry *h,
1800
       unsigned long r_symndx)
1801
0
{
1802
0
  bfd_vma value;
1803
0
  value = * symbol_got_offset_ref (input_bfd, h, r_symndx);
1804
0
  value &= ~1;
1805
0
  return value;
1806
0
}
1807
1808
/* N_ONES produces N one bits, without overflowing machine arithmetic.  */
1809
0
#define N_ONES(n) (((((bfd_vma) 1 << ((n) -1)) - 1) << 1) | 1)
1810
1811
/* This is a copy/paste + modification from
1812
   reloc.c:_bfd_relocate_contents. Relocations are applied to 32bits
1813
   words, so all overflow checks will overflow for values above
1814
   32bits.  */
1815
static bfd_reloc_status_type
1816
check_signed_overflow (enum complain_overflow complain_on_overflow,
1817
           bfd_reloc_code_real_type bfd_r_type, bfd *input_bfd,
1818
           bfd_vma relocation)
1819
0
{
1820
0
  bfd_reloc_status_type flag = bfd_reloc_ok;
1821
0
  bfd_vma addrmask, fieldmask, signmask, ss;
1822
0
  bfd_vma a, b, sum;
1823
0
  bfd_vma x = 0;
1824
1825
  /* These usually come from howto struct. As we don't check for
1826
     values fitting in bitfields or in subpart of words, we set all
1827
     these to values to check as if the field is starting from first
1828
     bit.  */
1829
0
  unsigned int rightshift = 0;
1830
0
  unsigned int bitpos = 0;
1831
0
  unsigned int bitsize = 0;
1832
0
  bfd_vma src_mask = -1;
1833
1834
  /* Only regular symbol relocations are checked here. Others
1835
     relocations (GOT, TLS) could be checked if the need is
1836
     confirmed. At the moment, we keep previous behavior
1837
     (ie. unchecked) for those. */
1838
0
  switch (bfd_r_type)
1839
0
    {
1840
0
    case BFD_RELOC_KVX_S37_LO10:
1841
0
    case BFD_RELOC_KVX_S37_UP27:
1842
0
      bitsize = 37;
1843
0
      break;
1844
1845
0
    case BFD_RELOC_KVX_S32_LO5:
1846
0
    case BFD_RELOC_KVX_S32_UP27:
1847
0
      bitsize = 32;
1848
0
      break;
1849
1850
0
    case BFD_RELOC_KVX_S43_LO10:
1851
0
    case BFD_RELOC_KVX_S43_UP27:
1852
0
    case BFD_RELOC_KVX_S43_EX6:
1853
0
      bitsize = 43;
1854
0
      break;
1855
1856
0
    case BFD_RELOC_KVX_S64_LO10:
1857
0
    case BFD_RELOC_KVX_S64_UP27:
1858
0
    case BFD_RELOC_KVX_S64_EX27:
1859
0
      bitsize = 64;
1860
0
      break;
1861
1862
0
    default:
1863
0
      return bfd_reloc_ok;
1864
0
    }
1865
1866
  /* direct copy/paste from reloc.c below */
1867
1868
  /* Get the values to be added together.  For signed and unsigned
1869
     relocations, we assume that all values should be truncated to
1870
     the size of an address.  For bitfields, all the bits matter.
1871
     See also bfd_check_overflow.  */
1872
0
  fieldmask = N_ONES (bitsize);
1873
0
  signmask = ~fieldmask;
1874
0
  addrmask = (N_ONES (bfd_arch_bits_per_address (input_bfd))
1875
0
        | (fieldmask << rightshift));
1876
0
  a = (relocation & addrmask) >> rightshift;
1877
0
  b = (x & src_mask & addrmask) >> bitpos;
1878
0
  addrmask >>= rightshift;
1879
1880
0
  switch (complain_on_overflow)
1881
0
    {
1882
0
    case complain_overflow_signed:
1883
      /* If any sign bits are set, all sign bits must be set.
1884
   That is, A must be a valid negative address after
1885
   shifting.  */
1886
0
      signmask = ~(fieldmask >> 1);
1887
      /* Fall thru */
1888
1889
0
    case complain_overflow_bitfield:
1890
      /* Much like the signed check, but for a field one bit
1891
   wider.  We allow a bitfield to represent numbers in the
1892
   range -2**n to 2**n-1, where n is the number of bits in the
1893
   field.  Note that when bfd_vma is 32 bits, a 32-bit reloc
1894
   can't overflow, which is exactly what we want.  */
1895
0
      ss = a & signmask;
1896
0
      if (ss != 0 && ss != (addrmask & signmask))
1897
0
  flag = bfd_reloc_overflow;
1898
1899
      /* We only need this next bit of code if the sign bit of B
1900
   is below the sign bit of A.  This would only happen if
1901
   SRC_MASK had fewer bits than BITSIZE.  Note that if
1902
   SRC_MASK has more bits than BITSIZE, we can get into
1903
   trouble; we would need to verify that B is in range, as
1904
   we do for A above.  */
1905
0
      ss = ((~src_mask) >> 1) & src_mask;
1906
0
      ss >>= bitpos;
1907
1908
      /* Set all the bits above the sign bit.  */
1909
0
      b = (b ^ ss) - ss;
1910
1911
      /* Now we can do the addition.  */
1912
0
      sum = a + b;
1913
1914
      /* See if the result has the correct sign.  Bits above the
1915
   sign bit are junk now; ignore them.  If the sum is
1916
   positive, make sure we did not have all negative inputs;
1917
   if the sum is negative, make sure we did not have all
1918
   positive inputs.  The test below looks only at the sign
1919
   bits, and it really just
1920
   SIGN (A) == SIGN (B) && SIGN (A) != SIGN (SUM)
1921
1922
   We mask with addrmask here to explicitly allow an address
1923
   wrap-around.  The Linux kernel relies on it, and it is
1924
   the only way to write assembler code which can run when
1925
   loaded at a location 0x80000000 away from the location at
1926
   which it is linked.  */
1927
0
      if (((~(a ^ b)) & (a ^ sum)) & signmask & addrmask)
1928
0
  flag = bfd_reloc_overflow;
1929
0
      break;
1930
1931
0
    case complain_overflow_unsigned:
1932
      /* Checking for an unsigned overflow is relatively easy:
1933
   trim the addresses and add, and trim the result as well.
1934
   Overflow is normally indicated when the result does not
1935
   fit in the field.  However, we also need to consider the
1936
   case when, e.g., fieldmask is 0x7fffffff or smaller, an
1937
   input is 0x80000000, and bfd_vma is only 32 bits; then we
1938
   will get sum == 0, but there is an overflow, since the
1939
   inputs did not fit in the field.  Instead of doing a
1940
   separate test, we can check for this by or-ing in the
1941
   operands when testing for the sum overflowing its final
1942
   field.  */
1943
0
      sum = (a + b) & addrmask;
1944
0
      if ((a | b | sum) & signmask)
1945
0
  flag = bfd_reloc_overflow;
1946
0
      break;
1947
1948
0
    default:
1949
0
      abort ();
1950
0
    }
1951
0
  return flag;
1952
0
}
1953
1954
/* Perform a relocation as part of a final link.  */
1955
static bfd_reloc_status_type
1956
elf32_kvx_final_link_relocate (reloc_howto_type *howto,
1957
             bfd *input_bfd,
1958
             bfd *output_bfd,
1959
             asection *input_section,
1960
             bfd_byte *contents,
1961
             Elf_Internal_Rela *rel,
1962
             bfd_vma value,
1963
             struct bfd_link_info *info,
1964
             asection *sym_sec,
1965
             struct elf_link_hash_entry *h,
1966
             bool *unresolved_reloc_p,
1967
             bool save_addend,
1968
             bfd_vma *saved_addend,
1969
             const Elf_Internal_Sym *sym)
1970
0
{
1971
0
  Elf_Internal_Shdr *symtab_hdr;
1972
0
  unsigned int r_type = howto->type;
1973
0
  bfd_reloc_code_real_type bfd_r_type
1974
0
    = elf32_kvx_bfd_reloc_from_howto (howto);
1975
0
  bfd_reloc_code_real_type new_bfd_r_type;
1976
0
  unsigned long r_symndx;
1977
0
  bfd_byte *hit_data = contents + rel->r_offset;
1978
0
  bfd_vma place, off;
1979
0
  bfd_vma addend;
1980
0
  struct elf_kvx_link_hash_table *globals;
1981
0
  bool weak_undef_p;
1982
0
  asection *base_got;
1983
0
  bfd_reloc_status_type rret = bfd_reloc_ok;
1984
0
  bool resolved_to_zero;
1985
0
  globals = elf_kvx_hash_table (info);
1986
1987
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
1988
1989
0
  BFD_ASSERT (is_kvx_elf (input_bfd));
1990
1991
0
  r_symndx = ELF32_R_SYM (rel->r_info);
1992
1993
  /* It is possible to have linker relaxations on some TLS access
1994
     models.  Update our information here.  */
1995
0
  new_bfd_r_type = kvx_tls_transition (input_bfd, info, r_type, h, r_symndx);
1996
0
  if (new_bfd_r_type != bfd_r_type)
1997
0
    {
1998
0
      bfd_r_type = new_bfd_r_type;
1999
0
      howto = elf32_kvx_howto_from_bfd_reloc (bfd_r_type);
2000
0
      BFD_ASSERT (howto != NULL);
2001
0
      r_type = howto->type;
2002
0
    }
2003
2004
0
  place = input_section->output_section->vma
2005
0
    + input_section->output_offset + rel->r_offset;
2006
2007
  /* Get addend, accumulating the addend for consecutive relocs
2008
     which refer to the same offset.  */
2009
0
  addend = saved_addend ? *saved_addend : 0;
2010
0
  addend += rel->r_addend;
2011
2012
0
  weak_undef_p = (h ? h->root.type == bfd_link_hash_undefweak
2013
0
      : bfd_is_und_section (sym_sec));
2014
0
  resolved_to_zero = (h != NULL
2015
0
          && UNDEFWEAK_NO_DYNAMIC_RELOC (info, h));
2016
2017
0
  switch (bfd_r_type)
2018
0
    {
2019
0
    case BFD_RELOC_KVX_32:
2020
#if ARCH_SIZE == 64
2021
    case BFD_RELOC_KVX_32:
2022
#endif
2023
0
    case BFD_RELOC_KVX_S37_LO10:
2024
0
    case BFD_RELOC_KVX_S37_UP27:
2025
2026
0
    case BFD_RELOC_KVX_S32_LO5:
2027
0
    case BFD_RELOC_KVX_S32_UP27:
2028
2029
0
    case BFD_RELOC_KVX_S43_LO10:
2030
0
    case BFD_RELOC_KVX_S43_UP27:
2031
0
    case BFD_RELOC_KVX_S43_EX6:
2032
2033
0
    case BFD_RELOC_KVX_S64_LO10:
2034
0
    case BFD_RELOC_KVX_S64_UP27:
2035
0
    case BFD_RELOC_KVX_S64_EX27:
2036
      /* When generating a shared library or PIE, these relocations
2037
   are copied into the output file to be resolved at run time.  */
2038
0
      if (bfd_link_pic (info)
2039
0
    && (input_section->flags & SEC_ALLOC)
2040
0
    && (h == NULL
2041
0
        || (ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2042
0
      && !resolved_to_zero)
2043
0
        || h->root.type != bfd_link_hash_undefweak))
2044
0
  {
2045
0
    Elf_Internal_Rela outrel;
2046
0
    bfd_byte *loc;
2047
0
    bool skip, relocate;
2048
0
    asection *sreloc;
2049
2050
0
    *unresolved_reloc_p = false;
2051
2052
0
    skip = false;
2053
0
    relocate = false;
2054
2055
0
    outrel.r_addend = addend;
2056
0
    outrel.r_offset =
2057
0
      _bfd_elf_section_offset (output_bfd, info, input_section,
2058
0
             rel->r_offset);
2059
0
    if (outrel.r_offset == (bfd_vma) - 1)
2060
0
      skip = true;
2061
0
    else if (outrel.r_offset == (bfd_vma) - 2)
2062
0
      {
2063
0
        skip = true;
2064
0
        relocate = true;
2065
0
      }
2066
2067
0
    outrel.r_offset += (input_section->output_section->vma
2068
0
            + input_section->output_offset);
2069
2070
0
    if (skip)
2071
0
      memset (&outrel, 0, sizeof outrel);
2072
0
    else if (h != NULL
2073
0
       && h->dynindx != -1
2074
0
       && (!bfd_link_pic (info) || !info->symbolic
2075
0
           || !h->def_regular))
2076
0
      outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
2077
0
    else if (bfd_r_type == BFD_RELOC_KVX_32
2078
0
       || bfd_r_type == BFD_RELOC_KVX_64)
2079
0
      {
2080
0
        int symbol;
2081
2082
        /* On SVR4-ish systems, the dynamic loader cannot
2083
     relocate the text and data segments independently,
2084
     so the symbol does not matter.  */
2085
0
        symbol = 0;
2086
0
        outrel.r_info = ELF32_R_INFO (symbol, R_KVX_RELATIVE);
2087
0
        outrel.r_addend += value;
2088
0
      }
2089
0
    else if (bfd_link_pic (info) && info->symbolic)
2090
0
      {
2091
0
        goto skip_because_pic;
2092
0
      }
2093
0
    else
2094
0
      {
2095
        /* We may endup here from bad input code trying to
2096
     insert relocation on symbols within code.  We do not
2097
     want that currently, and such code should use GOT +
2098
     KVX_32/64 reloc that translate in KVX_RELATIVE.  */
2099
0
        const char *name;
2100
0
        if (h && h->root.root.string)
2101
0
    name = h->root.root.string;
2102
0
        else
2103
0
    name = bfd_elf_sym_name (input_bfd, symtab_hdr, sym,
2104
0
           NULL);
2105
2106
0
        (*_bfd_error_handler)
2107
    /* xgettext:c-format */
2108
0
    (_("%pB(%pA+%#" PRIx64 "): "
2109
0
       "unresolvable %s relocation in section `%s'"),
2110
0
     input_bfd, input_section, (uint64_t) rel->r_offset, howto->name,
2111
0
     name);
2112
0
        return bfd_reloc_notsupported;
2113
0
      }
2114
2115
0
    sreloc = elf_section_data (input_section)->sreloc;
2116
0
    if (sreloc == NULL || sreloc->contents == NULL)
2117
0
      return bfd_reloc_notsupported;
2118
2119
0
    loc = sreloc->contents + sreloc->reloc_count++ * RELOC_SIZE (globals);
2120
0
    bfd_elf32_swap_reloca_out (output_bfd, &outrel, loc);
2121
2122
0
    if (sreloc->reloc_count * RELOC_SIZE (globals) > sreloc->size)
2123
0
      {
2124
        /* Sanity to check that we have previously allocated
2125
     sufficient space in the relocation section for the
2126
     number of relocations we actually want to emit.  */
2127
0
        abort ();
2128
0
      }
2129
2130
    /* If this reloc is against an external symbol, we do not want to
2131
       fiddle with the addend.  Otherwise, we need to include the symbol
2132
       value so that it becomes an addend for the dynamic reloc.  */
2133
0
    if (!relocate)
2134
0
      return bfd_reloc_ok;
2135
2136
0
    rret = check_signed_overflow (complain_overflow_signed, bfd_r_type,
2137
0
          input_bfd, value + addend);
2138
0
    if (rret != bfd_reloc_ok)
2139
0
      return rret;
2140
2141
0
    return _bfd_final_link_relocate (howto, input_bfd, input_section,
2142
0
             contents, rel->r_offset, value,
2143
0
             addend);
2144
0
  }
2145
2146
0
    skip_because_pic:
2147
0
      rret = check_signed_overflow (complain_overflow_signed, bfd_r_type,
2148
0
            input_bfd, value + addend);
2149
0
      if (rret != bfd_reloc_ok)
2150
0
  return rret;
2151
2152
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
2153
0
               contents, rel->r_offset, value,
2154
0
               addend);
2155
0
      break;
2156
2157
0
    case BFD_RELOC_KVX_PCREL17:
2158
0
    case BFD_RELOC_KVX_PCREL27:
2159
0
      {
2160
  /* BCU insn are always first in a bundle, so there is no need
2161
     to correct the address using offset within bundle.  */
2162
2163
0
  asection *splt = globals->root.splt;
2164
0
  bool via_plt_p =
2165
0
    splt != NULL && h != NULL && h->plt.offset != (bfd_vma) - 1;
2166
2167
  /* A call to an undefined weak symbol is converted to a jump to
2168
     the next instruction unless a PLT entry will be created.
2169
     The jump to the next instruction is optimized as a NOP.
2170
     Do the same for local undefined symbols.  */
2171
0
  if (weak_undef_p && ! via_plt_p)
2172
0
    {
2173
0
      bfd_putl32 (INSN_NOP, hit_data);
2174
0
      return bfd_reloc_ok;
2175
0
    }
2176
2177
  /* If the call goes through a PLT entry, make sure to
2178
     check distance to the right destination address.  */
2179
0
  if (via_plt_p)
2180
0
    value = (splt->output_section->vma
2181
0
       + splt->output_offset + h->plt.offset);
2182
2183
  /* Check if a stub has to be inserted because the destination
2184
     is too far away.  */
2185
0
  struct elf_kvx_stub_hash_entry *stub_entry = NULL;
2186
2187
  /* If the target symbol is global and marked as a function the
2188
     relocation applies a function call or a tail call.  In this
2189
     situation we can veneer out of range branches.  The veneers
2190
     use R16 and R17 hence cannot be used arbitrary out of range
2191
     branches that occur within the body of a function.  */
2192
2193
  /* Check if a stub has to be inserted because the destination
2194
     is too far away.  */
2195
0
  if (! kvx_valid_call_p (value, place))
2196
0
    {
2197
      /* The target is out of reach, so redirect the branch to
2198
         the local stub for this function.  */
2199
0
      stub_entry = elf32_kvx_get_stub_entry (input_section,
2200
0
               sym_sec, h,
2201
0
               rel, globals);
2202
0
      if (stub_entry != NULL)
2203
0
        value = (stub_entry->stub_offset
2204
0
           + stub_entry->stub_sec->output_offset
2205
0
           + stub_entry->stub_sec->output_section->vma);
2206
      /* We have redirected the destination to stub entry address,
2207
         so ignore any addend record in the original rela entry.  */
2208
0
      addend = 0;
2209
0
    }
2210
0
      }
2211
0
      *unresolved_reloc_p = false;
2212
2213
      /* FALLTHROUGH */
2214
2215
      /* PCREL 32 are used in dwarf2 table for exception handling */
2216
0
    case BFD_RELOC_KVX_32_PCREL:
2217
0
    case BFD_RELOC_KVX_S64_PCREL_LO10:
2218
0
    case BFD_RELOC_KVX_S64_PCREL_UP27:
2219
0
    case BFD_RELOC_KVX_S64_PCREL_EX27:
2220
0
    case BFD_RELOC_KVX_S37_PCREL_LO10:
2221
0
    case BFD_RELOC_KVX_S37_PCREL_UP27:
2222
0
    case BFD_RELOC_KVX_S43_PCREL_LO10:
2223
0
    case BFD_RELOC_KVX_S43_PCREL_UP27:
2224
0
    case BFD_RELOC_KVX_S43_PCREL_EX6:
2225
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
2226
0
               contents, rel->r_offset, value,
2227
0
               addend);
2228
0
      break;
2229
2230
0
    case BFD_RELOC_KVX_S37_TLS_LE_LO10:
2231
0
    case BFD_RELOC_KVX_S37_TLS_LE_UP27:
2232
2233
0
    case BFD_RELOC_KVX_S43_TLS_LE_LO10:
2234
0
    case BFD_RELOC_KVX_S43_TLS_LE_UP27:
2235
0
    case BFD_RELOC_KVX_S43_TLS_LE_EX6:
2236
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
2237
0
               contents, rel->r_offset,
2238
0
               value - tpoff_base (info), addend);
2239
0
      break;
2240
2241
0
    case BFD_RELOC_KVX_S37_TLS_DTPOFF_LO10:
2242
0
    case BFD_RELOC_KVX_S37_TLS_DTPOFF_UP27:
2243
2244
0
    case BFD_RELOC_KVX_S43_TLS_DTPOFF_LO10:
2245
0
    case BFD_RELOC_KVX_S43_TLS_DTPOFF_UP27:
2246
0
    case BFD_RELOC_KVX_S43_TLS_DTPOFF_EX6:
2247
0
      return _bfd_final_link_relocate (howto, input_bfd, input_section,
2248
0
               contents, rel->r_offset,
2249
0
               value - dtpoff_base (info), addend);
2250
2251
0
    case BFD_RELOC_KVX_S37_TLS_GD_UP27:
2252
0
    case BFD_RELOC_KVX_S37_TLS_GD_LO10:
2253
2254
0
    case BFD_RELOC_KVX_S43_TLS_GD_UP27:
2255
0
    case BFD_RELOC_KVX_S43_TLS_GD_EX6:
2256
0
    case BFD_RELOC_KVX_S43_TLS_GD_LO10:
2257
2258
0
    case BFD_RELOC_KVX_S37_TLS_IE_UP27:
2259
0
    case BFD_RELOC_KVX_S37_TLS_IE_LO10:
2260
2261
0
    case BFD_RELOC_KVX_S43_TLS_IE_UP27:
2262
0
    case BFD_RELOC_KVX_S43_TLS_IE_EX6:
2263
0
    case BFD_RELOC_KVX_S43_TLS_IE_LO10:
2264
2265
0
    case BFD_RELOC_KVX_S37_TLS_LD_UP27:
2266
0
    case BFD_RELOC_KVX_S37_TLS_LD_LO10:
2267
2268
0
    case BFD_RELOC_KVX_S43_TLS_LD_UP27:
2269
0
    case BFD_RELOC_KVX_S43_TLS_LD_EX6:
2270
0
    case BFD_RELOC_KVX_S43_TLS_LD_LO10:
2271
2272
0
      if (globals->root.sgot == NULL)
2273
0
  return bfd_reloc_notsupported;
2274
0
      value = symbol_got_offset (input_bfd, h, r_symndx);
2275
2276
0
      _bfd_final_link_relocate (howto, input_bfd, input_section,
2277
0
        contents, rel->r_offset, value, addend);
2278
0
      *unresolved_reloc_p = false;
2279
0
      break;
2280
2281
0
    case BFD_RELOC_KVX_S37_GOTADDR_UP27:
2282
0
    case BFD_RELOC_KVX_S37_GOTADDR_LO10:
2283
2284
0
    case BFD_RELOC_KVX_S43_GOTADDR_UP27:
2285
0
    case BFD_RELOC_KVX_S43_GOTADDR_EX6:
2286
0
    case BFD_RELOC_KVX_S43_GOTADDR_LO10:
2287
2288
0
    case BFD_RELOC_KVX_S64_GOTADDR_UP27:
2289
0
    case BFD_RELOC_KVX_S64_GOTADDR_EX27:
2290
0
    case BFD_RELOC_KVX_S64_GOTADDR_LO10:
2291
0
      {
2292
0
  if (globals->root.sgot == NULL)
2293
0
    BFD_ASSERT (h != NULL);
2294
2295
0
  value = globals->root.sgot->output_section->vma
2296
0
    + globals->root.sgot->output_offset;
2297
2298
0
  return _bfd_final_link_relocate (howto, input_bfd, input_section,
2299
0
           contents, rel->r_offset, value,
2300
0
           addend);
2301
0
      }
2302
0
      break;
2303
2304
0
    case BFD_RELOC_KVX_S37_GOTOFF_LO10:
2305
0
    case BFD_RELOC_KVX_S37_GOTOFF_UP27:
2306
2307
0
    case BFD_RELOC_KVX_32_GOTOFF:
2308
0
    case BFD_RELOC_KVX_64_GOTOFF:
2309
2310
0
    case BFD_RELOC_KVX_S43_GOTOFF_LO10:
2311
0
    case BFD_RELOC_KVX_S43_GOTOFF_UP27:
2312
0
    case BFD_RELOC_KVX_S43_GOTOFF_EX6:
2313
2314
0
      {
2315
0
  asection *basegot = globals->root.sgot;
2316
  /* BFD_ASSERT(h == NULL); */
2317
0
  BFD_ASSERT(globals->root.sgot != NULL);
2318
0
  value -= basegot->output_section->vma + basegot->output_offset;
2319
0
  return _bfd_final_link_relocate (howto, input_bfd, input_section,
2320
0
           contents, rel->r_offset, value,
2321
0
           addend);
2322
0
      }
2323
0
      break;
2324
2325
0
    case BFD_RELOC_KVX_S37_GOT_LO10:
2326
0
    case BFD_RELOC_KVX_S37_GOT_UP27:
2327
2328
0
    case BFD_RELOC_KVX_32_GOT:
2329
0
    case BFD_RELOC_KVX_64_GOT:
2330
2331
0
    case BFD_RELOC_KVX_S43_GOT_LO10:
2332
0
    case BFD_RELOC_KVX_S43_GOT_UP27:
2333
0
    case BFD_RELOC_KVX_S43_GOT_EX6:
2334
2335
0
      if (globals->root.sgot == NULL)
2336
0
  BFD_ASSERT (h != NULL);
2337
2338
0
      if (h != NULL)
2339
0
  {
2340
0
    value = kvx_calculate_got_entry_vma (h, globals, info, value,
2341
0
                 output_bfd,
2342
0
                 unresolved_reloc_p);
2343
#ifdef UGLY_DEBUG
2344
    printf("GOT_LO/HI for %s, value %x\n", h->root.root.string, value);
2345
#endif
2346
2347
0
    return _bfd_final_link_relocate (howto, input_bfd, input_section,
2348
0
             contents, rel->r_offset, value,
2349
0
             addend);
2350
0
  }
2351
0
      else
2352
0
  {
2353
#ifdef UGLY_DEBUG
2354
    printf("GOT_LO/HI with h NULL, initial value %x\n", value);
2355
#endif
2356
0
    struct elf_kvx_local_symbol *locals = elf_kvx_locals (input_bfd);
2357
2358
0
    if (locals == NULL)
2359
0
      {
2360
0
        int howto_index = bfd_r_type - BFD_RELOC_KVX_RELOC_START;
2361
0
        _bfd_error_handler
2362
    /* xgettext:c-format */
2363
0
    (_("%pB: local symbol descriptor table be NULL when applying "
2364
0
       "relocation %s against local symbol"),
2365
0
     input_bfd, elf_kvx_howto_table[howto_index].name);
2366
0
        abort ();
2367
0
      }
2368
2369
0
    off = symbol_got_offset (input_bfd, h, r_symndx);
2370
0
    base_got = globals->root.sgot;
2371
0
    bfd_vma got_entry_addr = (base_got->output_section->vma
2372
0
            + base_got->output_offset + off);
2373
2374
0
    if (!symbol_got_offset_mark_p (input_bfd, h, r_symndx))
2375
0
      {
2376
0
        bfd_put_64 (output_bfd, value, base_got->contents + off);
2377
2378
0
        if (bfd_link_pic (info))
2379
0
    {
2380
0
      asection *s;
2381
0
      Elf_Internal_Rela outrel;
2382
2383
      /* For PIC executables and shared libraries we need
2384
         to relocate the GOT entry at run time.  */
2385
0
      s = globals->root.srelgot;
2386
0
      if (s == NULL)
2387
0
        abort ();
2388
2389
0
      outrel.r_offset = got_entry_addr;
2390
0
      outrel.r_info = ELF32_R_INFO (0, R_KVX_RELATIVE);
2391
0
      outrel.r_addend = value;
2392
0
      _bfd_elf_append_rela (output_bfd, s, &outrel);
2393
0
    }
2394
2395
0
        symbol_got_offset_mark (input_bfd, h, r_symndx);
2396
0
      }
2397
2398
    /* Update the relocation value to GOT entry addr as we have
2399
       transformed the direct data access into an indirect data
2400
       access through GOT.  */
2401
0
    value = got_entry_addr;
2402
2403
0
    return _bfd_final_link_relocate (howto, input_bfd, input_section,
2404
0
             contents, rel->r_offset, off, 0);
2405
0
  }
2406
0
      break;
2407
2408
0
    default:
2409
0
      return bfd_reloc_notsupported;
2410
0
    }
2411
2412
0
  if (saved_addend)
2413
0
    *saved_addend = value;
2414
2415
  /* Only apply the final relocation in a sequence.  */
2416
0
  if (save_addend)
2417
0
    return bfd_reloc_continue;
2418
2419
0
  return _bfd_kvx_elf_put_addend (input_bfd, hit_data, bfd_r_type,
2420
0
          howto, value);
2421
0
}
2422
2423
2424
2425
/* Relocate a KVX ELF section.  */
2426
2427
static int
2428
elf32_kvx_relocate_section (struct bfd_link_info *info,
2429
          bfd *input_bfd,
2430
          asection *input_section,
2431
          bfd_byte *contents,
2432
          Elf_Internal_Rela *relocs,
2433
          Elf_Internal_Sym *local_syms,
2434
          asection **local_sections)
2435
0
{
2436
0
  Elf_Internal_Shdr *symtab_hdr;
2437
0
  struct elf_link_hash_entry **sym_hashes;
2438
0
  Elf_Internal_Rela *rel;
2439
0
  Elf_Internal_Rela *relend;
2440
0
  const char *name;
2441
0
  struct elf_kvx_link_hash_table *globals;
2442
0
  bool save_addend = false;
2443
0
  bfd_vma addend = 0;
2444
2445
0
  globals = elf_kvx_hash_table (info);
2446
2447
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
2448
0
  sym_hashes = elf_sym_hashes (input_bfd);
2449
2450
0
  rel = relocs;
2451
0
  relend = relocs + input_section->reloc_count;
2452
0
  for (; rel < relend; rel++)
2453
0
    {
2454
0
      unsigned int r_type;
2455
0
      bfd_reloc_code_real_type bfd_r_type;
2456
0
      reloc_howto_type *howto;
2457
0
      unsigned long r_symndx;
2458
0
      const Elf_Internal_Sym *sym;
2459
0
      asection *sec;
2460
0
      struct elf_link_hash_entry *h;
2461
0
      bfd_vma relocation;
2462
0
      bfd_reloc_status_type r;
2463
0
      arelent bfd_reloc;
2464
0
      char sym_type;
2465
0
      bool unresolved_reloc = false;
2466
0
      char *error_message = NULL;
2467
2468
0
      r_symndx = ELF32_R_SYM (rel->r_info);
2469
0
      r_type = ELF32_R_TYPE (rel->r_info);
2470
2471
0
      bfd_reloc.howto = elf32_kvx_howto_from_type (input_bfd, r_type);
2472
0
      howto = bfd_reloc.howto;
2473
2474
0
      if (howto == NULL)
2475
0
  return _bfd_unrecognized_reloc (input_bfd, input_section, r_type);
2476
2477
0
      bfd_r_type = elf32_kvx_bfd_reloc_from_howto (howto);
2478
2479
0
      h = NULL;
2480
0
      sym = NULL;
2481
0
      sec = NULL;
2482
2483
0
      if (r_symndx < symtab_hdr->sh_info) /* A local symbol. */
2484
0
  {
2485
0
    sym = local_syms + r_symndx;
2486
0
    sym_type = ELF32_ST_TYPE (sym->st_info);
2487
0
    sec = local_sections[r_symndx];
2488
2489
    /* An object file might have a reference to a local
2490
       undefined symbol.  This is a draft object file, but we
2491
       should at least do something about it.  */
2492
0
    if (r_type != R_KVX_NONE
2493
0
        && r_type != R_KVX_S37_GOTADDR_LO10
2494
0
        && r_type != R_KVX_S37_GOTADDR_UP27
2495
0
        && r_type != R_KVX_S64_GOTADDR_LO10
2496
0
        && r_type != R_KVX_S64_GOTADDR_UP27
2497
0
        && r_type != R_KVX_S64_GOTADDR_EX27
2498
0
        && r_type != R_KVX_S43_GOTADDR_LO10
2499
0
        && r_type != R_KVX_S43_GOTADDR_UP27
2500
0
        && r_type != R_KVX_S43_GOTADDR_EX6
2501
0
        && bfd_is_und_section (sec)
2502
0
        && ELF_ST_BIND (sym->st_info) != STB_WEAK)
2503
0
      (*info->callbacks->undefined_symbol)
2504
0
        (info, bfd_elf_string_from_elf_section
2505
0
         (input_bfd, symtab_hdr->sh_link, sym->st_name),
2506
0
         input_bfd, input_section, rel->r_offset, true);
2507
2508
0
    relocation = _bfd_elf_rela_local_sym (info->output_bfd,
2509
0
            sym, &sec, rel);
2510
0
  }
2511
0
      else
2512
0
  {
2513
0
    bool warned, ignored;
2514
2515
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
2516
0
           r_symndx, symtab_hdr, sym_hashes,
2517
0
           h, sec, relocation,
2518
0
           unresolved_reloc, warned, ignored);
2519
2520
0
    sym_type = h->type;
2521
0
  }
2522
2523
0
      if (sec != NULL && discarded_section (sec))
2524
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
2525
0
           rel, 1, relend, R_KVX_NONE,
2526
0
           howto, 0, contents);
2527
2528
0
      if (bfd_link_relocatable (info))
2529
0
  continue;
2530
2531
0
      if (h != NULL)
2532
0
  name = h->root.root.string;
2533
0
      else
2534
0
  {
2535
0
    name = (bfd_elf_string_from_elf_section
2536
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name));
2537
0
    if (name == NULL || *name == '\0')
2538
0
      name = bfd_section_name (sec);
2539
0
  }
2540
2541
0
      if (r_symndx != 0
2542
0
    && r_type != R_KVX_NONE
2543
0
    && (h == NULL
2544
0
        || h->root.type == bfd_link_hash_defined
2545
0
        || h->root.type == bfd_link_hash_defweak)
2546
0
    && IS_KVX_TLS_RELOC (bfd_r_type) != (sym_type == STT_TLS))
2547
0
  {
2548
0
    (*_bfd_error_handler)
2549
0
      ((sym_type == STT_TLS
2550
        /* xgettext:c-format */
2551
0
        ? _("%pB(%pA+%#" PRIx64 "): %s used with TLS symbol %s")
2552
        /* xgettext:c-format */
2553
0
        : _("%pB(%pA+%#" PRIx64 "): %s used with non-TLS symbol %s")),
2554
0
       input_bfd,
2555
0
       input_section, (uint64_t) rel->r_offset, howto->name, name);
2556
0
  }
2557
2558
      /* Original aarch64 has relaxation handling for TLS here. */
2559
0
      r = bfd_reloc_continue;
2560
2561
      /* There may be multiple consecutive relocations for the
2562
   same offset.  In that case we are supposed to treat the
2563
   output of each relocation as the addend for the next.  */
2564
0
      if (rel + 1 < relend
2565
0
    && rel->r_offset == rel[1].r_offset
2566
0
    && ELF32_R_TYPE (rel[1].r_info) != R_KVX_NONE)
2567
2568
0
  save_addend = true;
2569
0
      else
2570
0
  save_addend = false;
2571
2572
0
      if (r == bfd_reloc_continue)
2573
0
  r = elf32_kvx_final_link_relocate (howto, input_bfd, info->output_bfd,
2574
0
             input_section, contents, rel,
2575
0
             relocation, info, sec,
2576
0
             h, &unresolved_reloc,
2577
0
             save_addend, &addend, sym);
2578
2579
0
      switch (elf32_kvx_bfd_reloc_from_type (input_bfd, r_type))
2580
0
  {
2581
0
  case BFD_RELOC_KVX_S37_TLS_GD_LO10:
2582
0
  case BFD_RELOC_KVX_S37_TLS_GD_UP27:
2583
2584
0
  case BFD_RELOC_KVX_S43_TLS_GD_LO10:
2585
0
  case BFD_RELOC_KVX_S43_TLS_GD_UP27:
2586
0
  case BFD_RELOC_KVX_S43_TLS_GD_EX6:
2587
2588
0
  case BFD_RELOC_KVX_S37_TLS_LD_LO10:
2589
0
  case BFD_RELOC_KVX_S37_TLS_LD_UP27:
2590
2591
0
  case BFD_RELOC_KVX_S43_TLS_LD_LO10:
2592
0
  case BFD_RELOC_KVX_S43_TLS_LD_UP27:
2593
0
  case BFD_RELOC_KVX_S43_TLS_LD_EX6:
2594
2595
0
    if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
2596
0
      {
2597
0
        bool need_relocs = false;
2598
0
        bfd_byte *loc;
2599
0
        int indx;
2600
0
        bfd_vma off;
2601
2602
0
        off = symbol_got_offset (input_bfd, h, r_symndx);
2603
0
        indx = h && h->dynindx != -1 ? h->dynindx : 0;
2604
2605
0
        need_relocs =
2606
0
    (bfd_link_pic (info) || indx != 0) &&
2607
0
    (h == NULL
2608
0
     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2609
0
     || h->root.type != bfd_link_hash_undefweak);
2610
2611
0
        BFD_ASSERT (globals->root.srelgot != NULL);
2612
2613
0
        if (need_relocs)
2614
0
    {
2615
0
      Elf_Internal_Rela rela;
2616
0
      rela.r_info = ELF32_R_INFO (indx, R_KVX_64_DTPMOD);
2617
0
      rela.r_addend = 0;
2618
0
      rela.r_offset = globals->root.sgot->output_section->vma +
2619
0
        globals->root.sgot->output_offset + off;
2620
2621
0
      loc = globals->root.srelgot->contents;
2622
0
      loc += globals->root.srelgot->reloc_count++
2623
0
        * RELOC_SIZE (htab);
2624
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
2625
2626
0
      bfd_reloc_code_real_type real_type =
2627
0
        elf32_kvx_bfd_reloc_from_type (input_bfd, r_type);
2628
2629
0
      if (real_type == BFD_RELOC_KVX_S37_TLS_LD_LO10
2630
0
          || real_type == BFD_RELOC_KVX_S37_TLS_LD_UP27
2631
0
          || real_type == BFD_RELOC_KVX_S43_TLS_LD_LO10
2632
0
          || real_type == BFD_RELOC_KVX_S43_TLS_LD_UP27
2633
0
          || real_type == BFD_RELOC_KVX_S43_TLS_LD_EX6)
2634
0
        {
2635
          /* For local dynamic, don't generate DTPOFF in any case.
2636
       Initialize the DTPOFF slot into zero, so we get module
2637
       base address when invoke runtime TLS resolver.  */
2638
0
          bfd_put_32 (info->output_bfd, 0,
2639
0
          globals->root.sgot->contents + off
2640
0
          + GOT_ENTRY_SIZE);
2641
0
        }
2642
0
      else if (indx == 0)
2643
0
        {
2644
0
          bfd_put_32 (info->output_bfd,
2645
0
          relocation - dtpoff_base (info),
2646
0
          globals->root.sgot->contents + off
2647
0
          + GOT_ENTRY_SIZE);
2648
0
        }
2649
0
      else
2650
0
        {
2651
          /* This TLS symbol is global. We emit a
2652
       relocation to fixup the tls offset at load
2653
       time.  */
2654
0
          rela.r_info =
2655
0
      ELF32_R_INFO (indx, R_KVX_64_DTPOFF);
2656
0
          rela.r_addend = 0;
2657
0
          rela.r_offset =
2658
0
      (globals->root.sgot->output_section->vma
2659
0
       + globals->root.sgot->output_offset + off
2660
0
       + GOT_ENTRY_SIZE);
2661
2662
0
          loc = globals->root.srelgot->contents;
2663
0
          loc += globals->root.srelgot->reloc_count++
2664
0
      * RELOC_SIZE (globals);
2665
0
          bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
2666
0
          bfd_put_32 (info->output_bfd, 0,
2667
0
          globals->root.sgot->contents + off
2668
0
          + GOT_ENTRY_SIZE);
2669
0
        }
2670
0
    }
2671
0
        else
2672
0
    {
2673
0
      bfd_put_32 (info->output_bfd, (bfd_vma) 1,
2674
0
            globals->root.sgot->contents + off);
2675
0
      bfd_put_32 (info->output_bfd,
2676
0
            relocation - dtpoff_base (info),
2677
0
            globals->root.sgot->contents + off
2678
0
            + GOT_ENTRY_SIZE);
2679
0
    }
2680
2681
0
        symbol_got_offset_mark (input_bfd, h, r_symndx);
2682
0
      }
2683
0
    break;
2684
2685
0
  case BFD_RELOC_KVX_S37_TLS_IE_LO10:
2686
0
  case BFD_RELOC_KVX_S37_TLS_IE_UP27:
2687
2688
0
  case BFD_RELOC_KVX_S43_TLS_IE_LO10:
2689
0
  case BFD_RELOC_KVX_S43_TLS_IE_UP27:
2690
0
  case BFD_RELOC_KVX_S43_TLS_IE_EX6:
2691
0
    if (! symbol_got_offset_mark_p (input_bfd, h, r_symndx))
2692
0
      {
2693
0
        bool need_relocs = false;
2694
0
        bfd_byte *loc;
2695
0
        int indx;
2696
0
        bfd_vma off;
2697
2698
0
        off = symbol_got_offset (input_bfd, h, r_symndx);
2699
2700
0
        indx = h && h->dynindx != -1 ? h->dynindx : 0;
2701
2702
0
        need_relocs =
2703
0
    (bfd_link_pic (info) || indx != 0) &&
2704
0
    (h == NULL
2705
0
     || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
2706
0
     || h->root.type != bfd_link_hash_undefweak);
2707
2708
0
        BFD_ASSERT (globals->root.srelgot != NULL);
2709
2710
0
        if (need_relocs)
2711
0
    {
2712
0
      Elf_Internal_Rela rela;
2713
2714
0
      if (indx == 0)
2715
0
        rela.r_addend = relocation - dtpoff_base (info);
2716
0
      else
2717
0
        rela.r_addend = 0;
2718
2719
0
      rela.r_info = ELF32_R_INFO (indx, R_KVX_64_TPOFF);
2720
0
      rela.r_offset = globals->root.sgot->output_section->vma +
2721
0
        globals->root.sgot->output_offset + off;
2722
2723
0
      loc = globals->root.srelgot->contents;
2724
0
      loc += globals->root.srelgot->reloc_count++
2725
0
        * RELOC_SIZE (htab);
2726
2727
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
2728
2729
0
      bfd_put_32 (info->output_bfd, rela.r_addend,
2730
0
            globals->root.sgot->contents + off);
2731
0
    }
2732
0
        else
2733
0
    bfd_put_32 (info->output_bfd, relocation - tpoff_base (info),
2734
0
          globals->root.sgot->contents + off);
2735
2736
0
        symbol_got_offset_mark (input_bfd, h, r_symndx);
2737
0
      }
2738
0
    break;
2739
2740
0
  default:
2741
0
    break;
2742
0
  }
2743
2744
      /* Dynamic relocs are not propagated for SEC_DEBUGGING sections
2745
   because such sections are not SEC_ALLOC and thus ld.so will
2746
   not process them.  */
2747
0
      if (unresolved_reloc
2748
0
    && !((input_section->flags & SEC_DEBUGGING) != 0
2749
0
         && h->def_dynamic)
2750
0
    && _bfd_elf_section_offset (info->output_bfd, info, input_section,
2751
0
              +rel->r_offset) != (bfd_vma) - 1)
2752
0
  {
2753
0
    (*_bfd_error_handler)
2754
      /* xgettext:c-format */
2755
0
      (_("%pB(%pA+%#" PRIx64 "): "
2756
0
         "unresolvable %s relocation against symbol `%s'"),
2757
0
       input_bfd, input_section, (uint64_t) rel->r_offset, howto->name,
2758
0
       h->root.root.string);
2759
0
    return false;
2760
0
  }
2761
2762
0
      if (r != bfd_reloc_ok && r != bfd_reloc_continue)
2763
0
  {
2764
0
    switch (r)
2765
0
      {
2766
0
      case bfd_reloc_overflow:
2767
0
        (*info->callbacks->reloc_overflow)
2768
0
    (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
2769
0
     input_bfd, input_section, rel->r_offset);
2770
2771
        /* Original aarch64 code had a check for alignement correctness */
2772
0
        break;
2773
2774
0
      case bfd_reloc_undefined:
2775
0
        (*info->callbacks->undefined_symbol)
2776
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
2777
0
        break;
2778
2779
0
      case bfd_reloc_outofrange:
2780
0
        error_message = _("out of range");
2781
0
        goto common_error;
2782
2783
0
      case bfd_reloc_notsupported:
2784
0
        error_message = _("unsupported relocation");
2785
0
        goto common_error;
2786
2787
0
      case bfd_reloc_dangerous:
2788
        /* error_message should already be set.  */
2789
0
        goto common_error;
2790
2791
0
      default:
2792
0
        error_message = _("unknown error");
2793
        /* Fall through.  */
2794
2795
0
      common_error:
2796
0
        BFD_ASSERT (error_message != NULL);
2797
0
        (*info->callbacks->reloc_dangerous)
2798
0
    (info, error_message, input_bfd, input_section, rel->r_offset);
2799
0
        break;
2800
0
      }
2801
0
  }
2802
2803
0
      if (!save_addend)
2804
0
  addend = 0;
2805
0
    }
2806
2807
0
  return true;
2808
0
}
2809
2810
/* Set the right machine number.  */
2811
2812
static bool
2813
elf32_kvx_object_p (bfd *abfd)
2814
261
{
2815
  /* must be coherent with default arch in cpu-kvx.c */
2816
261
  int e_set = bfd_mach_kv3_1;
2817
2818
261
  if (elf_elfheader (abfd)->e_machine == EM_KVX)
2819
261
    {
2820
261
      int e_core = elf_elfheader (abfd)->e_flags & ELF_KVX_CORE_MASK;
2821
261
      switch(e_core)
2822
261
  {
2823
#if ARCH_SIZE == 64
2824
  case ELF_KVX_CORE_KV3_1 : e_set = bfd_mach_kv3_1_64; break;
2825
  case ELF_KVX_CORE_KV3_2 : e_set = bfd_mach_kv3_2_64; break;
2826
  case ELF_KVX_CORE_KV4_1 : e_set = bfd_mach_kv4_1_64; break;
2827
#else
2828
18
  case ELF_KVX_CORE_KV3_1 : e_set = bfd_mach_kv3_1; break;
2829
9
  case ELF_KVX_CORE_KV3_2 : e_set = bfd_mach_kv3_2; break;
2830
4
  case ELF_KVX_CORE_KV4_1 : e_set = bfd_mach_kv4_1; break;
2831
0
#endif
2832
230
  default:
2833
230
    (*_bfd_error_handler)(_("%s: Bad ELF id: `%d'"),
2834
230
        abfd->filename, e_core);
2835
261
  }
2836
261
    }
2837
261
  return bfd_default_set_arch_mach (abfd, bfd_arch_kvx, e_set);
2838
261
}
2839
2840
/* Function to keep KVX specific flags in the ELF header.  */
2841
2842
static bool
2843
elf32_kvx_set_private_flags (bfd *abfd, flagword flags)
2844
0
{
2845
0
  if (elf_flags_init (abfd) && elf_elfheader (abfd)->e_flags != flags)
2846
0
    {
2847
0
    }
2848
0
  else
2849
0
    {
2850
0
      elf_elfheader (abfd)->e_flags = flags;
2851
0
      elf_flags_init (abfd) = true;
2852
0
    }
2853
2854
0
  return true;
2855
0
}
2856
2857
/* Merge backend specific data from an object file to the output
2858
   object file when linking.  */
2859
2860
static bool
2861
elf32_kvx_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
2862
0
{
2863
0
  bfd *obfd = info->output_bfd;
2864
0
  flagword out_flags;
2865
0
  flagword in_flags;
2866
0
  bool flags_compatible = true;
2867
0
  asection *sec;
2868
2869
  /* Check if we have the same endianess.  */
2870
0
  if (!_bfd_generic_verify_endian_match (ibfd, info))
2871
0
    return false;
2872
2873
0
  if (!is_kvx_elf (ibfd))
2874
0
    return true;
2875
2876
  /* The input BFD must have had its flags initialised.  */
2877
  /* The following seems bogus to me -- The flags are initialized in
2878
     the assembler but I don't think an elf_flags_init field is
2879
     written into the object.  */
2880
  /* BFD_ASSERT (elf_flags_init (ibfd)); */
2881
2882
0
  if (bfd_get_arch_size (ibfd) != bfd_get_arch_size (obfd))
2883
0
    {
2884
0
      const char *msg;
2885
2886
0
      if (bfd_get_arch_size (ibfd) == 32
2887
0
    && bfd_get_arch_size (obfd) == 64)
2888
0
  msg = _("%s: compiled as 32-bit object and %s is 64-bit");
2889
0
      else if (bfd_get_arch_size (ibfd) == 64
2890
0
         && bfd_get_arch_size (obfd) == 32)
2891
0
  msg = _("%s: compiled as 64-bit object and %s is 32-bit");
2892
0
      else
2893
0
  msg = _("%s: object size does not match that of target %s");
2894
2895
0
      (*_bfd_error_handler) (msg, bfd_get_filename (ibfd),
2896
0
           bfd_get_filename (obfd));
2897
0
      bfd_set_error (bfd_error_wrong_format);
2898
0
      return false;
2899
0
    }
2900
2901
0
  in_flags = elf_elfheader (ibfd)->e_flags;
2902
0
  out_flags = elf_elfheader (obfd)->e_flags;
2903
2904
0
  if (!elf_flags_init (obfd))
2905
0
    {
2906
      /* If the input is the default architecture and had the default
2907
   flags then do not bother setting the flags for the output
2908
   architecture, instead allow future merges to do this.  If no
2909
   future merges ever set these flags then they will retain their
2910
   uninitialised values, which surprise surprise, correspond
2911
   to the default values.  */
2912
0
      if (bfd_get_arch_info (ibfd)->the_default
2913
0
    && elf_elfheader (ibfd)->e_flags == 0)
2914
0
  return true;
2915
2916
0
      elf_flags_init (obfd) = true;
2917
0
      elf_elfheader (obfd)->e_flags = in_flags;
2918
2919
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2920
0
    && bfd_get_arch_info (obfd)->the_default)
2921
0
  return bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2922
0
          bfd_get_mach (ibfd));
2923
2924
0
      return true;
2925
0
    }
2926
2927
  /* Identical flags must be compatible.  */
2928
0
  if (in_flags == out_flags)
2929
0
    return true;
2930
2931
  /* Check to see if the input BFD actually contains any sections.  If
2932
     not, its flags may not have been initialised either, but it
2933
     cannot actually cause any incompatiblity.  Do not short-circuit
2934
     dynamic objects; their section list may be emptied by
2935
     elf_link_add_object_symbols.
2936
2937
     Also check to see if there are no code sections in the input.
2938
     In this case there is no need to check for code specific flags.
2939
     XXX - do we need to worry about floating-point format compatability
2940
     in data sections ?  */
2941
0
  if (!(ibfd->flags & DYNAMIC))
2942
0
    {
2943
0
      bool null_input_bfd = true;
2944
0
      bool only_data_sections = true;
2945
2946
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
2947
0
  {
2948
0
    if ((bfd_section_flags (sec)
2949
0
         & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
2950
0
        == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
2951
0
      only_data_sections = false;
2952
2953
0
    null_input_bfd = false;
2954
0
    break;
2955
0
  }
2956
2957
0
      if (null_input_bfd || only_data_sections)
2958
0
  return true;
2959
0
    }
2960
0
  return flags_compatible;
2961
0
}
2962
2963
/* Display the flags field.  */
2964
2965
static bool
2966
elf32_kvx_print_private_bfd_data (bfd *abfd, void *ptr)
2967
10
{
2968
10
  FILE *file = (FILE *) ptr;
2969
10
  unsigned long flags;
2970
2971
10
  BFD_ASSERT (abfd != NULL && ptr != NULL);
2972
2973
  /* Print normal ELF private data.  */
2974
10
  _bfd_elf_print_private_bfd_data (abfd, ptr);
2975
2976
10
  flags = elf_elfheader (abfd)->e_flags;
2977
  /* Ignore init flag - it may not be set, despite the flags field
2978
     containing valid data.  */
2979
2980
  /* xgettext:c-format */
2981
10
  fprintf (file, _("Private flags = 0x%lx : "), elf_elfheader (abfd)->e_flags);
2982
10
  if((flags & ELF_KVX_ABI_64B_ADDR_BIT) == ELF_KVX_ABI_64B_ADDR_BIT)
2983
0
    {
2984
0
      if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV3_1))
2985
0
  fprintf (file, _("Coolidge (kv3) V1 64 bits"));
2986
0
      else if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV3_2))
2987
0
  fprintf (file, _("Coolidge (kv3) V2 64 bits"));
2988
0
      else if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV4_1))
2989
0
  fprintf (file, _("Coolidge (kv4) V1 64 bits"));
2990
0
    }
2991
10
  else
2992
10
    {
2993
10
      if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV3_1))
2994
0
  fprintf (file, _("Coolidge (kv3) V1 32 bits"));
2995
10
      else if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV3_2))
2996
0
  fprintf (file, _("Coolidge (kv3) V2 32 bits"));
2997
10
      else if (ELF_KVX_CHECK_CORE(flags,ELF_KVX_CORE_KV4_1))
2998
0
  fprintf (file, _("Coolidge (kv4) V1 32 bits"));
2999
10
    }
3000
3001
10
  fputc ('\n', file);
3002
3003
10
  return true;
3004
10
}
3005
3006
/* Adjust a symbol defined by a dynamic object and referenced by a
3007
   regular object.  The current definition is in some section of the
3008
   dynamic object, but we're not including those sections.  We have to
3009
   change the definition to something the rest of the link can
3010
   understand.  */
3011
3012
static bool
3013
elf32_kvx_adjust_dynamic_symbol (struct bfd_link_info *info,
3014
         struct elf_link_hash_entry *h)
3015
0
{
3016
0
  struct elf_kvx_link_hash_table *htab;
3017
0
  asection *s;
3018
3019
  /* If this is a function, put it in the procedure linkage table.  We
3020
     will fill in the contents of the procedure linkage table later,
3021
     when we know the address of the .got section.  */
3022
0
  if (h->type == STT_FUNC || h->needs_plt)
3023
0
    {
3024
0
      if (h->plt.refcount <= 0
3025
0
    || ((SYMBOL_CALLS_LOCAL (info, h)
3026
0
         || (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3027
0
       && h->root.type == bfd_link_hash_undefweak))))
3028
0
  {
3029
    /* This case can occur if we saw a CALL26 reloc in
3030
       an input file, but the symbol wasn't referred to
3031
       by a dynamic object or all references were
3032
       garbage collected. In which case we can end up
3033
       resolving.  */
3034
0
    h->plt.offset = (bfd_vma) - 1;
3035
0
    h->needs_plt = 0;
3036
0
  }
3037
3038
0
      return true;
3039
0
    }
3040
0
  else
3041
    /* Otherwise, reset to -1.  */
3042
0
    h->plt.offset = (bfd_vma) - 1;
3043
3044
3045
  /* If this is a weak symbol, and there is a real definition, the
3046
     processor independent code will have arranged for us to see the
3047
     real definition first, and we can just use the same value.  */
3048
0
  if (h->is_weakalias)
3049
0
    {
3050
0
      struct elf_link_hash_entry *def = weakdef (h);
3051
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
3052
0
      h->root.u.def.section = def->root.u.def.section;
3053
0
      h->root.u.def.value = def->root.u.def.value;
3054
0
      if (ELIMINATE_COPY_RELOCS || info->nocopyreloc)
3055
0
  h->non_got_ref = def->non_got_ref;
3056
0
      return true;
3057
0
    }
3058
3059
  /* If we are creating a shared library, we must presume that the
3060
     only references to the symbol are via the global offset table.
3061
     For such cases we need not do anything here; the relocations will
3062
     be handled correctly by relocate_section.  */
3063
0
  if (bfd_link_pic (info))
3064
0
    return true;
3065
3066
  /* If there are no references to this symbol that do not use the
3067
     GOT, we don't need to generate a copy reloc.  */
3068
0
  if (!h->non_got_ref)
3069
0
    return true;
3070
3071
  /* If -z nocopyreloc was given, we won't generate them either.  */
3072
0
  if (info->nocopyreloc)
3073
0
    {
3074
0
      h->non_got_ref = 0;
3075
0
      return true;
3076
0
    }
3077
3078
  /* We must allocate the symbol in our .dynbss section, which will
3079
     become part of the .bss section of the executable.  There will be
3080
     an entry for this symbol in the .dynsym section.  The dynamic
3081
     object will contain position independent code, so all references
3082
     from the dynamic object to this symbol will go through the global
3083
     offset table.  The dynamic linker will use the .dynsym entry to
3084
     determine the address it must put in the global offset table, so
3085
     both the dynamic object and the regular object will refer to the
3086
     same memory location for the variable.  */
3087
3088
0
  htab = elf_kvx_hash_table (info);
3089
3090
  /* We must generate a R_KVX_COPY reloc to tell the dynamic linker
3091
     to copy the initial value out of the dynamic object and into the
3092
     runtime process image.  */
3093
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0 && h->size != 0)
3094
0
    {
3095
0
      htab->srelbss->size += RELOC_SIZE (htab);
3096
0
      h->needs_copy = 1;
3097
0
    }
3098
3099
0
  s = htab->sdynbss;
3100
3101
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
3102
0
}
3103
3104
static bool
3105
elf32_kvx_allocate_local_symbols (bfd *abfd, unsigned number)
3106
0
{
3107
0
  struct elf_kvx_local_symbol *locals;
3108
0
  locals = elf_kvx_locals (abfd);
3109
0
  if (locals == NULL)
3110
0
    {
3111
0
      locals = (struct elf_kvx_local_symbol *)
3112
0
  bfd_zalloc (abfd, number * sizeof (struct elf_kvx_local_symbol));
3113
0
      if (locals == NULL)
3114
0
  return false;
3115
0
      elf_kvx_locals (abfd) = locals;
3116
0
    }
3117
0
  return true;
3118
0
}
3119
3120
/* Create the .got section to hold the global offset table.  */
3121
3122
static bool
3123
kvx_elf_create_got_section (bfd *abfd, struct bfd_link_info *info)
3124
0
{
3125
0
  elf_backend_data *bed = get_elf_backend_data (abfd);
3126
0
  flagword flags;
3127
0
  asection *s;
3128
0
  struct elf_link_hash_entry *h;
3129
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
3130
3131
  /* This function may be called more than once.  */
3132
0
  s = bfd_get_linker_section (abfd, ".got");
3133
0
  if (s != NULL)
3134
0
    return true;
3135
3136
0
  flags = bed->dynamic_sec_flags;
3137
3138
0
  s = bfd_make_section_anyway_with_flags (abfd,
3139
0
            (bed->rela_plts_and_copies_p
3140
0
             ? ".rela.got" : ".rel.got"),
3141
0
            (bed->dynamic_sec_flags
3142
0
             | SEC_READONLY));
3143
0
  if (s == NULL
3144
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
3145
3146
0
    return false;
3147
0
  htab->srelgot = s;
3148
3149
0
  s = bfd_make_section_anyway_with_flags (abfd, ".got", flags);
3150
0
  if (s == NULL
3151
0
      || !bfd_set_section_alignment (s, bed->s->log_file_align))
3152
0
    return false;
3153
0
  htab->sgot = s;
3154
0
  htab->sgot->size += GOT_ENTRY_SIZE;
3155
3156
0
  if (bed->want_got_sym)
3157
0
    {
3158
      /* Define the symbol _GLOBAL_OFFSET_TABLE_ at the start of the .got
3159
   (or .got.plt) section.  We don't do this in the linker script
3160
   because we don't want to define the symbol if we are not creating
3161
   a global offset table.  */
3162
0
      h = _bfd_elf_define_linkage_sym (abfd, info, s,
3163
0
               "_GLOBAL_OFFSET_TABLE_");
3164
0
      elf_hash_table (info)->hgot = h;
3165
0
      if (h == NULL)
3166
0
  return false;
3167
0
    }
3168
3169
0
  if (bed->want_got_plt)
3170
0
    {
3171
0
      s = bfd_make_section_anyway_with_flags (abfd, ".got.plt", flags);
3172
0
      if (s == NULL
3173
0
    || !bfd_set_section_alignment (s,
3174
0
           bed->s->log_file_align))
3175
0
  return false;
3176
0
      htab->sgotplt = s;
3177
0
    }
3178
3179
  /* The first bit of the global offset table is the header.  */
3180
0
  s->size += bed->got_header_size;
3181
3182
0
  return true;
3183
0
}
3184
3185
/* Look through the relocs for a section during the first phase.  */
3186
3187
static bool
3188
elf32_kvx_check_relocs (bfd *abfd, struct bfd_link_info *info,
3189
          asection *sec, const Elf_Internal_Rela *relocs)
3190
0
{
3191
0
  Elf_Internal_Shdr *symtab_hdr;
3192
0
  struct elf_link_hash_entry **sym_hashes;
3193
0
  const Elf_Internal_Rela *rel;
3194
0
  const Elf_Internal_Rela *rel_end;
3195
0
  asection *sreloc;
3196
3197
0
  struct elf_kvx_link_hash_table *htab;
3198
3199
0
  if (bfd_link_relocatable (info))
3200
0
    return true;
3201
3202
0
  BFD_ASSERT (is_kvx_elf (abfd));
3203
3204
0
  htab = elf_kvx_hash_table (info);
3205
0
  sreloc = NULL;
3206
3207
0
  symtab_hdr = &elf_symtab_hdr (abfd);
3208
0
  sym_hashes = elf_sym_hashes (abfd);
3209
3210
0
  rel_end = relocs + sec->reloc_count;
3211
0
  for (rel = relocs; rel < rel_end; rel++)
3212
0
    {
3213
0
      unsigned int r_symndx;
3214
0
      unsigned int r_type;
3215
0
      bfd_reloc_code_real_type bfd_r_type;
3216
0
      struct elf_link_hash_entry *h;
3217
0
      const Elf_Internal_Sym *isym = NULL;
3218
3219
0
      r_symndx = ELF32_R_SYM (rel->r_info);
3220
0
      r_type = ELF32_R_TYPE (rel->r_info);
3221
3222
0
      if (r_symndx < symtab_hdr->sh_info)
3223
0
  {
3224
    /* A local symbol.  */
3225
0
    isym = bfd_sym_from_r_symndx (&htab->sym_cache, abfd, r_symndx);
3226
0
    if (isym == NULL)
3227
0
      return false;
3228
3229
0
    h = NULL;
3230
0
  }
3231
0
      else
3232
0
  {
3233
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
3234
0
    while (h->root.type == bfd_link_hash_indirect
3235
0
     || h->root.type == bfd_link_hash_warning)
3236
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
3237
0
  }
3238
3239
      /* Could be done earlier, if h were already available.  */
3240
0
      bfd_r_type = kvx_tls_transition (abfd, info, r_type, h, r_symndx);
3241
3242
0
      if (h != NULL)
3243
0
  {
3244
    /* Create the ifunc sections for static executables.  If we
3245
       never see an indirect function symbol nor we are building
3246
       a static executable, those sections will be empty and
3247
       won't appear in output.  */
3248
0
    switch (bfd_r_type)
3249
0
      {
3250
0
      default:
3251
0
        break;
3252
0
      }
3253
3254
    /* It is referenced by a non-shared object. */
3255
0
    h->ref_regular = 1;
3256
0
  }
3257
3258
0
      switch (bfd_r_type)
3259
0
  {
3260
3261
0
  case BFD_RELOC_KVX_S43_LO10:
3262
0
  case BFD_RELOC_KVX_S43_UP27:
3263
0
  case BFD_RELOC_KVX_S43_EX6:
3264
3265
0
  case BFD_RELOC_KVX_S37_LO10:
3266
0
  case BFD_RELOC_KVX_S37_UP27:
3267
3268
0
  case BFD_RELOC_KVX_S64_LO10:
3269
0
  case BFD_RELOC_KVX_S64_UP27:
3270
0
  case BFD_RELOC_KVX_S64_EX27:
3271
3272
0
  case BFD_RELOC_KVX_32:
3273
0
  case BFD_RELOC_KVX_64:
3274
3275
    /* We don't need to handle relocs into sections not going into
3276
       the "real" output.  */
3277
0
    if ((sec->flags & SEC_ALLOC) == 0)
3278
0
      break;
3279
3280
0
    if (h != NULL)
3281
0
      {
3282
0
        if (!bfd_link_pic (info))
3283
0
    h->non_got_ref = 1;
3284
3285
0
        h->plt.refcount += 1;
3286
0
        h->pointer_equality_needed = 1;
3287
0
      }
3288
3289
    /* No need to do anything if we're not creating a shared
3290
       object.  */
3291
0
    if (! bfd_link_pic (info))
3292
0
      break;
3293
3294
0
    {
3295
0
      struct elf_dyn_relocs *p;
3296
0
      struct elf_dyn_relocs **head;
3297
3298
      /* We must copy these reloc types into the output file.
3299
         Create a reloc section in dynobj and make room for
3300
         this reloc.  */
3301
0
      if (sreloc == NULL)
3302
0
        {
3303
0
    if (htab->root.dynobj == NULL
3304
0
        && !_bfd_elf_link_dynobj (info))
3305
0
      return false;
3306
3307
0
    sreloc = _bfd_elf_make_dynamic_reloc_section
3308
0
      (sec, htab->root.dynobj, LOG_FILE_ALIGN, abfd, /*rela? */ true);
3309
3310
0
    if (sreloc == NULL)
3311
0
      return false;
3312
0
        }
3313
3314
      /* If this is a global symbol, we count the number of
3315
         relocations we need for this symbol.  */
3316
0
      if (h != NULL)
3317
0
        {
3318
0
    head = &h->dyn_relocs;
3319
0
        }
3320
0
      else
3321
0
        {
3322
    /* Track dynamic relocs needed for local syms too.  */
3323
0
    asection *s;
3324
0
    void **vpp;
3325
3326
0
    s = bfd_section_from_elf_index (abfd, isym->st_shndx);
3327
0
    if (s == NULL)
3328
0
      s = sec;
3329
3330
    /* Beware of type punned pointers vs strict aliasing
3331
       rules.  */
3332
0
    vpp = &(elf_section_data (s)->local_dynrel);
3333
0
    head = (struct elf_dyn_relocs **) vpp;
3334
0
        }
3335
3336
0
      p = *head;
3337
0
      if (p == NULL || p->sec != sec)
3338
0
        {
3339
0
    bfd_size_type amt = sizeof *p;
3340
0
    p = ((struct elf_dyn_relocs *)
3341
0
         bfd_zalloc (htab->root.dynobj, amt));
3342
0
    if (p == NULL)
3343
0
      return false;
3344
0
    p->next = *head;
3345
0
    *head = p;
3346
0
    p->sec = sec;
3347
0
        }
3348
3349
0
      p->count += 1;
3350
3351
0
    }
3352
0
    break;
3353
3354
0
  case BFD_RELOC_KVX_S37_GOT_LO10:
3355
0
  case BFD_RELOC_KVX_S37_GOT_UP27:
3356
3357
0
  case BFD_RELOC_KVX_S37_GOTOFF_LO10:
3358
0
  case BFD_RELOC_KVX_S37_GOTOFF_UP27:
3359
3360
0
  case BFD_RELOC_KVX_S43_GOT_LO10:
3361
0
  case BFD_RELOC_KVX_S43_GOT_UP27:
3362
0
  case BFD_RELOC_KVX_S43_GOT_EX6:
3363
3364
0
  case BFD_RELOC_KVX_S43_GOTOFF_LO10:
3365
0
  case BFD_RELOC_KVX_S43_GOTOFF_UP27:
3366
0
  case BFD_RELOC_KVX_S43_GOTOFF_EX6:
3367
3368
0
  case BFD_RELOC_KVX_S37_TLS_GD_LO10:
3369
0
  case BFD_RELOC_KVX_S37_TLS_GD_UP27:
3370
3371
0
  case BFD_RELOC_KVX_S43_TLS_GD_LO10:
3372
0
  case BFD_RELOC_KVX_S43_TLS_GD_UP27:
3373
0
  case BFD_RELOC_KVX_S43_TLS_GD_EX6:
3374
3375
0
  case BFD_RELOC_KVX_S37_TLS_IE_LO10:
3376
0
  case BFD_RELOC_KVX_S37_TLS_IE_UP27:
3377
3378
0
  case BFD_RELOC_KVX_S43_TLS_IE_LO10:
3379
0
  case BFD_RELOC_KVX_S43_TLS_IE_UP27:
3380
0
  case BFD_RELOC_KVX_S43_TLS_IE_EX6:
3381
3382
0
  case BFD_RELOC_KVX_S37_TLS_LD_LO10:
3383
0
  case BFD_RELOC_KVX_S37_TLS_LD_UP27:
3384
3385
0
  case BFD_RELOC_KVX_S43_TLS_LD_LO10:
3386
0
  case BFD_RELOC_KVX_S43_TLS_LD_UP27:
3387
0
  case BFD_RELOC_KVX_S43_TLS_LD_EX6:
3388
0
    {
3389
0
      unsigned got_type;
3390
0
      unsigned old_got_type;
3391
3392
0
      got_type = kvx_reloc_got_type (bfd_r_type);
3393
3394
0
      if (h)
3395
0
        {
3396
0
    h->got.refcount += 1;
3397
0
    old_got_type = elf_kvx_hash_entry (h)->got_type;
3398
0
        }
3399
0
      else
3400
0
        {
3401
0
    struct elf_kvx_local_symbol *locals;
3402
3403
0
    if (!elf32_kvx_allocate_local_symbols
3404
0
        (abfd, symtab_hdr->sh_info))
3405
0
      return false;
3406
3407
0
    locals = elf_kvx_locals (abfd);
3408
0
    BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
3409
0
    locals[r_symndx].got_refcount += 1;
3410
0
    old_got_type = locals[r_symndx].got_type;
3411
0
        }
3412
3413
      /* We will already have issued an error message if there
3414
         is a TLS/non-TLS mismatch, based on the symbol type.
3415
         So just combine any TLS types needed.  */
3416
0
      if (old_got_type != GOT_UNKNOWN && old_got_type != GOT_NORMAL
3417
0
    && got_type != GOT_NORMAL)
3418
0
        got_type |= old_got_type;
3419
3420
      /* If the symbol is accessed by both IE and GD methods, we
3421
         are able to relax.  Turn off the GD flag, without
3422
         messing up with any other kind of TLS types that may be
3423
         involved.  */
3424
      /* Disabled untested and unused TLS */
3425
      /* if ((got_type & GOT_TLS_IE) && GOT_TLS_GD_ANY_P (got_type)) */
3426
      /*   got_type &= ~ (GOT_TLSDESC_GD | GOT_TLS_GD); */
3427
3428
0
      if (old_got_type != got_type)
3429
0
        {
3430
0
    if (h != NULL)
3431
0
      elf_kvx_hash_entry (h)->got_type = got_type;
3432
0
    else
3433
0
      {
3434
0
        struct elf_kvx_local_symbol *locals;
3435
0
        locals = elf_kvx_locals (abfd);
3436
0
        BFD_ASSERT (r_symndx < symtab_hdr->sh_info);
3437
0
        locals[r_symndx].got_type = got_type;
3438
0
      }
3439
0
        }
3440
3441
0
      if (htab->root.dynobj == NULL
3442
0
    && !_bfd_elf_link_dynobj (info))
3443
0
        return false;
3444
0
      if (! kvx_elf_create_got_section (htab->root.dynobj, info))
3445
0
        return false;
3446
0
      break;
3447
0
    }
3448
3449
0
  case BFD_RELOC_KVX_S64_GOTADDR_LO10:
3450
0
  case BFD_RELOC_KVX_S64_GOTADDR_UP27:
3451
0
  case BFD_RELOC_KVX_S64_GOTADDR_EX27:
3452
3453
0
  case BFD_RELOC_KVX_S43_GOTADDR_LO10:
3454
0
  case BFD_RELOC_KVX_S43_GOTADDR_UP27:
3455
0
  case BFD_RELOC_KVX_S43_GOTADDR_EX6:
3456
3457
0
  case BFD_RELOC_KVX_S37_GOTADDR_LO10:
3458
0
  case BFD_RELOC_KVX_S37_GOTADDR_UP27:
3459
3460
0
    if (htab->root.dynobj == NULL
3461
0
        && !_bfd_elf_link_dynobj (info))
3462
0
      return false;
3463
0
    if (! kvx_elf_create_got_section (htab->root.dynobj, info))
3464
0
      return false;
3465
0
    break;
3466
3467
0
  case BFD_RELOC_KVX_PCREL27:
3468
0
  case BFD_RELOC_KVX_PCREL17:
3469
    /* If this is a local symbol then we resolve it
3470
       directly without creating a PLT entry.  */
3471
0
    if (h == NULL)
3472
0
      continue;
3473
3474
0
    h->needs_plt = 1;
3475
0
    if (h->plt.refcount <= 0)
3476
0
      h->plt.refcount = 1;
3477
0
    else
3478
0
      h->plt.refcount += 1;
3479
0
    break;
3480
3481
0
  default:
3482
0
    break;
3483
0
  }
3484
0
    }
3485
3486
0
  return true;
3487
0
}
3488
3489
static bool
3490
elf32_kvx_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
3491
7
{
3492
7
  Elf_Internal_Ehdr *i_ehdrp; /* ELF file header, internal form.  */
3493
3494
7
  if (!_bfd_elf_init_file_header (abfd, link_info))
3495
0
    return false;
3496
3497
7
  i_ehdrp = elf_elfheader (abfd);
3498
7
  i_ehdrp->e_ident[EI_ABIVERSION] = KVX_ELF_ABI_VERSION;
3499
7
  return true;
3500
7
}
3501
3502
static enum elf_reloc_type_class
3503
elf32_kvx_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
3504
        const asection *rel_sec ATTRIBUTE_UNUSED,
3505
        const Elf_Internal_Rela *rela)
3506
0
{
3507
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
3508
0
    {
3509
0
    case R_KVX_RELATIVE:
3510
0
      return reloc_class_relative;
3511
0
    case R_KVX_JMP_SLOT:
3512
0
      return reloc_class_plt;
3513
0
    case R_KVX_COPY:
3514
0
      return reloc_class_copy;
3515
0
    default:
3516
0
      return reloc_class_normal;
3517
0
    }
3518
0
}
3519
3520
/* A structure used to record a list of sections, independently
3521
   of the next and prev fields in the asection structure.  */
3522
typedef struct section_list
3523
{
3524
  asection *sec;
3525
  struct section_list *next;
3526
  struct section_list *prev;
3527
}
3528
section_list;
3529
3530
typedef struct
3531
{
3532
  void *finfo;
3533
  struct bfd_link_info *info;
3534
  asection *sec;
3535
  int sec_shndx;
3536
  int (*func) (void *, const char *, Elf_Internal_Sym *,
3537
         asection *, struct elf_link_hash_entry *);
3538
} output_arch_syminfo;
3539
3540
/* Output a single local symbol for a generated stub.  */
3541
3542
static bool
3543
elf32_kvx_output_stub_sym (output_arch_syminfo *osi, const char *name,
3544
             bfd_vma offset, bfd_vma size)
3545
0
{
3546
0
  Elf_Internal_Sym sym;
3547
3548
0
  sym.st_value = (osi->sec->output_section->vma
3549
0
      + osi->sec->output_offset + offset);
3550
0
  sym.st_size = size;
3551
0
  sym.st_other = 0;
3552
0
  sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FUNC);
3553
0
  sym.st_shndx = osi->sec_shndx;
3554
0
  return osi->func (osi->finfo, name, &sym, osi->sec, NULL) == 1;
3555
0
}
3556
3557
static bool
3558
kvx_map_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
3559
0
{
3560
0
  struct elf_kvx_stub_hash_entry *stub_entry;
3561
0
  asection *stub_sec;
3562
0
  bfd_vma addr;
3563
0
  char *stub_name;
3564
0
  output_arch_syminfo *osi;
3565
3566
  /* Massage our args to the form they really have.  */
3567
0
  stub_entry = (struct elf_kvx_stub_hash_entry *) gen_entry;
3568
0
  osi = (output_arch_syminfo *) in_arg;
3569
3570
0
  stub_sec = stub_entry->stub_sec;
3571
3572
  /* Ensure this stub is attached to the current section being
3573
     processed.  */
3574
0
  if (stub_sec != osi->sec)
3575
0
    return true;
3576
3577
0
  addr = (bfd_vma) stub_entry->stub_offset;
3578
3579
0
  stub_name = stub_entry->output_name;
3580
3581
0
  switch (stub_entry->stub_type)
3582
0
    {
3583
0
    case kvx_stub_long_branch:
3584
0
      if (!elf32_kvx_output_stub_sym
3585
0
    (osi, stub_name, addr, sizeof (elf32_kvx_long_branch_stub)))
3586
0
  return false;
3587
0
      break;
3588
3589
0
    default:
3590
0
      abort ();
3591
0
    }
3592
3593
0
  return true;
3594
0
}
3595
3596
/* Output mapping symbols for linker generated sections.  */
3597
3598
static bool
3599
elf32_kvx_output_arch_local_syms (struct bfd_link_info *info,
3600
          void *finfo,
3601
          int (*func) (void *, const char *,
3602
                 Elf_Internal_Sym *,
3603
                 asection *,
3604
                 struct elf_link_hash_entry *))
3605
0
{
3606
0
  output_arch_syminfo osi;
3607
0
  struct elf_kvx_link_hash_table *htab;
3608
3609
0
  htab = elf_kvx_hash_table (info);
3610
3611
0
  osi.finfo = finfo;
3612
0
  osi.info = info;
3613
0
  osi.func = func;
3614
3615
  /* Long calls stubs.  */
3616
0
  if (htab->stub_bfd && htab->stub_bfd->sections)
3617
0
    {
3618
0
      asection *stub_sec;
3619
3620
0
      for (stub_sec = htab->stub_bfd->sections;
3621
0
     stub_sec != NULL; stub_sec = stub_sec->next)
3622
0
  {
3623
    /* Ignore non-stub sections.  */
3624
0
    if (!strstr (stub_sec->name, STUB_SUFFIX))
3625
0
      continue;
3626
3627
0
    osi.sec = stub_sec;
3628
3629
0
    osi.sec_shndx = _bfd_elf_section_from_bfd_section
3630
0
      (info->output_bfd, osi.sec->output_section);
3631
3632
0
    bfd_hash_traverse (&htab->stub_hash_table, kvx_map_one_stub,
3633
0
           &osi);
3634
0
  }
3635
0
    }
3636
3637
  /* Finally, output mapping symbols for the PLT.  */
3638
0
  if (!htab->root.splt || htab->root.splt->size == 0)
3639
0
    return true;
3640
3641
0
  osi.sec_shndx = _bfd_elf_section_from_bfd_section
3642
0
    (info->output_bfd, htab->root.splt->output_section);
3643
0
  osi.sec = htab->root.splt;
3644
3645
0
  return true;
3646
3647
0
}
3648
3649
/* Allocate target specific section data.  */
3650
3651
static bool
3652
elf32_kvx_new_section_hook (bfd *abfd, asection *sec)
3653
2.76k
{
3654
2.76k
  _kvx_elf_section_data *sdata;
3655
3656
2.76k
  sdata = bfd_zalloc (abfd, sizeof (*sdata));
3657
2.76k
  if (sdata == NULL)
3658
0
    return false;
3659
2.76k
  sec->used_by_bfd = sdata;
3660
3661
2.76k
  return _bfd_elf_new_section_hook (abfd, sec);
3662
2.76k
}
3663
3664
/* Create dynamic sections. This is different from the ARM backend in that
3665
   the got, plt, gotplt and their relocation sections are all created in the
3666
   standard part of the bfd elf backend.  */
3667
3668
static bool
3669
elf32_kvx_create_dynamic_sections (bfd *dynobj,
3670
           struct bfd_link_info *info)
3671
0
{
3672
0
  struct elf_kvx_link_hash_table *htab;
3673
3674
  /* We need to create .got section.  */
3675
0
  if (!kvx_elf_create_got_section (dynobj, info))
3676
0
    return false;
3677
3678
0
  if (!_bfd_elf_create_dynamic_sections (dynobj, info))
3679
0
    return false;
3680
3681
0
  htab = elf_kvx_hash_table (info);
3682
0
  htab->sdynbss = bfd_get_linker_section (dynobj, ".dynbss");
3683
0
  if (!bfd_link_pic (info))
3684
0
    htab->srelbss = bfd_get_linker_section (dynobj, ".rela.bss");
3685
3686
0
  if (!htab->sdynbss || (!bfd_link_pic (info) && !htab->srelbss))
3687
0
    abort ();
3688
3689
0
  return true;
3690
0
}
3691
3692
3693
/* Allocate space in .plt, .got and associated reloc sections for
3694
   dynamic relocs.  */
3695
3696
static bool
3697
elf32_kvx_allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
3698
0
{
3699
0
  struct bfd_link_info *info;
3700
0
  struct elf_kvx_link_hash_table *htab;
3701
0
  struct elf_dyn_relocs *p;
3702
3703
  /* An example of a bfd_link_hash_indirect symbol is versioned
3704
     symbol. For example: __gxx_personality_v0(bfd_link_hash_indirect)
3705
     -> __gxx_personality_v0(bfd_link_hash_defined)
3706
3707
     There is no need to process bfd_link_hash_indirect symbols here
3708
     because we will also be presented with the concrete instance of
3709
     the symbol and elf32_kvx_copy_indirect_symbol () will have been
3710
     called to copy all relevant data from the generic to the concrete
3711
     symbol instance.  */
3712
0
  if (h->root.type == bfd_link_hash_indirect)
3713
0
    return true;
3714
3715
0
  if (h->root.type == bfd_link_hash_warning)
3716
0
    h = (struct elf_link_hash_entry *) h->root.u.i.link;
3717
3718
0
  info = (struct bfd_link_info *) inf;
3719
0
  htab = elf_kvx_hash_table (info);
3720
3721
0
  if (htab->root.dynamic_sections_created && h->plt.refcount > 0)
3722
0
    {
3723
      /* Make sure this symbol is output as a dynamic symbol.
3724
   Undefined weak syms won't yet be marked as dynamic.  */
3725
0
      if (h->dynindx == -1 && !h->forced_local)
3726
0
  {
3727
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
3728
0
      return false;
3729
0
  }
3730
3731
0
      if (bfd_link_pic (info) || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
3732
0
  {
3733
0
    asection *s = htab->root.splt;
3734
3735
    /* If this is the first .plt entry, make room for the special
3736
       first entry.  */
3737
0
    if (s->size == 0)
3738
0
      s->size += htab->plt_header_size;
3739
3740
0
    h->plt.offset = s->size;
3741
3742
    /* If this symbol is not defined in a regular file, and we are
3743
       not generating a shared library, then set the symbol to this
3744
       location in the .plt.  This is required to make function
3745
       pointers compare as equal between the normal executable and
3746
       the shared library.  */
3747
0
    if (!bfd_link_pic (info) && !h->def_regular)
3748
0
      {
3749
0
        h->root.u.def.section = s;
3750
0
        h->root.u.def.value = h->plt.offset;
3751
0
      }
3752
3753
    /* Make room for this entry. For now we only create the
3754
       small model PLT entries. We later need to find a way
3755
       of relaxing into these from the large model PLT entries.  */
3756
0
    s->size += PLT_SMALL_ENTRY_SIZE;
3757
3758
    /* We also need to make an entry in the .got.plt section, which
3759
       will be placed in the .got section by the linker script.  */
3760
0
    htab->root.sgotplt->size += GOT_ENTRY_SIZE;
3761
3762
    /* We also need to make an entry in the .rela.plt section.  */
3763
0
    htab->root.srelplt->size += RELOC_SIZE (htab);
3764
3765
    /* We need to ensure that all GOT entries that serve the PLT
3766
       are consecutive with the special GOT slots [0] [1] and
3767
       [2]. Any addtional relocations must be placed after the
3768
       PLT related entries.  We abuse the reloc_count such that
3769
       during sizing we adjust reloc_count to indicate the
3770
       number of PLT related reserved entries.  In subsequent
3771
       phases when filling in the contents of the reloc entries,
3772
       PLT related entries are placed by computing their PLT
3773
       index (0 .. reloc_count). While other none PLT relocs are
3774
       placed at the slot indicated by reloc_count and
3775
       reloc_count is updated.  */
3776
3777
0
    htab->root.srelplt->reloc_count++;
3778
0
  }
3779
0
      else
3780
0
  {
3781
0
    h->plt.offset = (bfd_vma) - 1;
3782
0
    h->needs_plt = 0;
3783
0
  }
3784
0
    }
3785
0
  else
3786
0
    {
3787
0
      h->plt.offset = (bfd_vma) - 1;
3788
0
      h->needs_plt = 0;
3789
0
    }
3790
3791
0
  if (h->got.refcount > 0)
3792
0
    {
3793
0
      bool dyn;
3794
0
      unsigned got_type = elf_kvx_hash_entry (h)->got_type;
3795
3796
0
      h->got.offset = (bfd_vma) - 1;
3797
3798
0
      dyn = htab->root.dynamic_sections_created;
3799
3800
      /* Make sure this symbol is output as a dynamic symbol.
3801
   Undefined weak syms won't yet be marked as dynamic.  */
3802
0
      if (dyn && h->dynindx == -1 && !h->forced_local)
3803
0
  {
3804
0
    if (!bfd_elf_link_record_dynamic_symbol (info, h))
3805
0
      return false;
3806
0
  }
3807
3808
0
      if (got_type == GOT_UNKNOWN)
3809
0
  {
3810
0
    (*_bfd_error_handler)
3811
0
      (_("relocation against `%s' has faulty GOT type "),
3812
0
       (h) ? h->root.root.string : "a local symbol");
3813
0
    bfd_set_error (bfd_error_bad_value);
3814
0
    return false;
3815
0
  }
3816
0
      else if (got_type == GOT_NORMAL)
3817
0
  {
3818
0
    h->got.offset = htab->root.sgot->size;
3819
0
    htab->root.sgot->size += GOT_ENTRY_SIZE;
3820
0
    if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3821
0
         || h->root.type != bfd_link_hash_undefweak)
3822
0
        && (bfd_link_pic (info)
3823
0
      || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3824
0
      {
3825
0
        htab->root.srelgot->size += RELOC_SIZE (htab);
3826
0
      }
3827
0
  }
3828
0
      else
3829
0
  {
3830
0
    int indx;
3831
3832
    /* Any of these will require 2 GOT slots because
3833
     * they use __tls_get_addr() */
3834
0
    if (got_type & (GOT_TLS_GD | GOT_TLS_LD))
3835
0
      {
3836
0
        h->got.offset = htab->root.sgot->size;
3837
0
        htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
3838
0
      }
3839
3840
0
    if (got_type & GOT_TLS_IE)
3841
0
      {
3842
0
        h->got.offset = htab->root.sgot->size;
3843
0
        htab->root.sgot->size += GOT_ENTRY_SIZE;
3844
0
      }
3845
3846
0
    indx = h && h->dynindx != -1 ? h->dynindx : 0;
3847
0
    if ((ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
3848
0
         || h->root.type != bfd_link_hash_undefweak)
3849
0
        && (bfd_link_pic (info)
3850
0
      || indx != 0
3851
0
      || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, h)))
3852
0
      {
3853
        /* Only the GD case requires 2 relocations. */
3854
0
        if (got_type & GOT_TLS_GD)
3855
0
    htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
3856
3857
        /* LD needs a DTPMOD reloc, IE needs a DTPOFF. */
3858
0
        if (got_type & (GOT_TLS_LD | GOT_TLS_IE))
3859
0
    htab->root.srelgot->size += RELOC_SIZE (htab);
3860
0
      }
3861
0
  }
3862
0
    }
3863
0
  else
3864
0
    {
3865
0
      h->got.offset = (bfd_vma) - 1;
3866
0
    }
3867
3868
0
  if (h->dyn_relocs == NULL)
3869
0
    return true;
3870
3871
  /* In the shared -Bsymbolic case, discard space allocated for
3872
     dynamic pc-relative relocs against symbols which turn out to be
3873
     defined in regular objects.  For the normal shared case, discard
3874
     space for pc-relative relocs that have become local due to symbol
3875
     visibility changes.  */
3876
3877
0
  if (bfd_link_pic (info))
3878
0
    {
3879
      /* Relocs that use pc_count are those that appear on a call
3880
   insn, or certain REL relocs that can generated via assembly.
3881
   We want calls to protected symbols to resolve directly to the
3882
   function rather than going via the plt.  If people want
3883
   function pointer comparisons to work as expected then they
3884
   should avoid writing weird assembly.  */
3885
0
      if (SYMBOL_CALLS_LOCAL (info, h))
3886
0
  {
3887
0
    struct elf_dyn_relocs **pp;
3888
3889
0
    for (pp = &h->dyn_relocs; (p = *pp) != NULL;)
3890
0
      {
3891
0
        p->count -= p->pc_count;
3892
0
        p->pc_count = 0;
3893
0
        if (p->count == 0)
3894
0
    *pp = p->next;
3895
0
        else
3896
0
    pp = &p->next;
3897
0
      }
3898
0
  }
3899
3900
      /* Also discard relocs on undefined weak syms with non-default
3901
   visibility.  */
3902
0
      if (h->dyn_relocs != NULL && h->root.type == bfd_link_hash_undefweak)
3903
0
  {
3904
0
    if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
3905
0
        || UNDEFWEAK_NO_DYNAMIC_RELOC (info, h))
3906
0
      h->dyn_relocs = NULL;
3907
3908
    /* Make sure undefined weak symbols are output as a dynamic
3909
       symbol in PIEs.  */
3910
0
    else if (h->dynindx == -1
3911
0
       && !h->forced_local
3912
0
       && !bfd_elf_link_record_dynamic_symbol (info, h))
3913
0
      return false;
3914
0
  }
3915
3916
0
    }
3917
0
  else if (ELIMINATE_COPY_RELOCS)
3918
0
    {
3919
      /* For the non-shared case, discard space for relocs against
3920
   symbols which turn out to need copy relocs or are not
3921
   dynamic.  */
3922
3923
0
      if (!h->non_got_ref
3924
0
    && ((h->def_dynamic
3925
0
         && !h->def_regular)
3926
0
        || (htab->root.dynamic_sections_created
3927
0
      && (h->root.type == bfd_link_hash_undefweak
3928
0
          || h->root.type == bfd_link_hash_undefined))))
3929
0
  {
3930
    /* Make sure this symbol is output as a dynamic symbol.
3931
       Undefined weak syms won't yet be marked as dynamic.  */
3932
0
    if (h->dynindx == -1
3933
0
        && !h->forced_local
3934
0
        && !bfd_elf_link_record_dynamic_symbol (info, h))
3935
0
      return false;
3936
3937
    /* If that succeeded, we know we'll be keeping all the
3938
       relocs.  */
3939
0
    if (h->dynindx != -1)
3940
0
      goto keep;
3941
0
  }
3942
3943
0
      h->dyn_relocs = NULL;
3944
3945
0
    keep:;
3946
0
    }
3947
3948
  /* Finally, allocate space.  */
3949
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
3950
0
    {
3951
0
      asection *sreloc;
3952
3953
0
      sreloc = elf_section_data (p->sec)->sreloc;
3954
3955
0
      BFD_ASSERT (sreloc != NULL);
3956
3957
0
      sreloc->size += p->count * RELOC_SIZE (htab);
3958
0
    }
3959
3960
0
  return true;
3961
0
}
3962
3963
/* Find any dynamic relocs that apply to read-only sections.  */
3964
3965
static bool
3966
kvx_readonly_dynrelocs (struct elf_link_hash_entry * h, void * inf)
3967
0
{
3968
0
  struct elf_dyn_relocs * p;
3969
3970
0
  for (p = h->dyn_relocs; p != NULL; p = p->next)
3971
0
    {
3972
0
      asection *s = p->sec;
3973
3974
0
      if (s != NULL && (s->flags & SEC_READONLY) != 0)
3975
0
  {
3976
0
    struct bfd_link_info *info = (struct bfd_link_info *) inf;
3977
3978
0
    info->flags |= DF_TEXTREL;
3979
0
    info->callbacks->minfo (_("%pB: dynamic relocation against `%pT' in "
3980
0
            "read-only section `%pA'\n"),
3981
0
          s->owner, h->root.root.string, s);
3982
3983
    /* Not an error, just cut short the traversal.  */
3984
0
    return false;
3985
0
  }
3986
0
    }
3987
0
  return true;
3988
0
}
3989
3990
/* This is the most important function of all . Innocuosly named
3991
   though !  */
3992
static bool
3993
elf32_kvx_late_size_sections (struct bfd_link_info *info)
3994
0
{
3995
0
  struct elf_kvx_link_hash_table *htab;
3996
0
  bfd *dynobj;
3997
0
  asection *s;
3998
0
  bool relocs;
3999
0
  bfd *ibfd;
4000
4001
0
  htab = elf_kvx_hash_table ((info));
4002
0
  dynobj = htab->root.dynobj;
4003
0
  if (dynobj == NULL)
4004
0
    return true;
4005
4006
0
  if (htab->root.dynamic_sections_created)
4007
0
    {
4008
0
      if (bfd_link_executable (info) && !info->nointerp)
4009
0
  {
4010
0
    s = htab->root.interp;
4011
0
    if (s == NULL)
4012
0
      abort ();
4013
0
    s->size = sizeof ELF_DYNAMIC_INTERPRETER;
4014
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
4015
0
    s->alloced = 1;
4016
0
  }
4017
0
    }
4018
4019
  /* Set up .got offsets for local syms, and space for local dynamic
4020
     relocs.  */
4021
0
  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
4022
0
    {
4023
0
      struct elf_kvx_local_symbol *locals = NULL;
4024
0
      Elf_Internal_Shdr *symtab_hdr;
4025
0
      asection *srel;
4026
0
      unsigned int i;
4027
4028
0
      if (!is_kvx_elf (ibfd))
4029
0
  continue;
4030
4031
0
      for (s = ibfd->sections; s != NULL; s = s->next)
4032
0
  {
4033
0
    struct elf_dyn_relocs *p;
4034
4035
0
    for (p = (struct elf_dyn_relocs *)
4036
0
     (elf_section_data (s)->local_dynrel); p != NULL; p = p->next)
4037
0
      {
4038
0
        if (!bfd_is_abs_section (p->sec)
4039
0
      && bfd_is_abs_section (p->sec->output_section))
4040
0
    {
4041
      /* Input section has been discarded, either because
4042
         it is a copy of a linkonce section or due to
4043
         linker script /DISCARD/, so we'll be discarding
4044
         the relocs too.  */
4045
0
    }
4046
0
        else if (p->count != 0)
4047
0
    {
4048
0
      srel = elf_section_data (p->sec)->sreloc;
4049
0
      srel->size += p->count * RELOC_SIZE (htab);
4050
0
      if ((p->sec->output_section->flags & SEC_READONLY) != 0)
4051
0
        info->flags |= DF_TEXTREL;
4052
0
    }
4053
0
      }
4054
0
  }
4055
4056
0
      locals = elf_kvx_locals (ibfd);
4057
0
      if (!locals)
4058
0
  continue;
4059
4060
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
4061
0
      srel = htab->root.srelgot;
4062
0
      for (i = 0; i < symtab_hdr->sh_info; i++)
4063
0
  {
4064
0
    locals[i].got_offset = (bfd_vma) - 1;
4065
0
    if (locals[i].got_refcount > 0)
4066
0
      {
4067
0
        unsigned got_type = locals[i].got_type;
4068
0
        if (got_type & (GOT_TLS_GD | GOT_TLS_LD))
4069
0
    {
4070
0
      locals[i].got_offset = htab->root.sgot->size;
4071
0
      htab->root.sgot->size += GOT_ENTRY_SIZE * 2;
4072
0
    }
4073
4074
0
        if (got_type & (GOT_NORMAL | GOT_TLS_IE ))
4075
0
    {
4076
0
      locals[i].got_offset = htab->root.sgot->size;
4077
0
      htab->root.sgot->size += GOT_ENTRY_SIZE;
4078
0
    }
4079
4080
0
        if (got_type == GOT_UNKNOWN)
4081
0
    {
4082
0
    }
4083
4084
0
        if (bfd_link_pic (info))
4085
0
    {
4086
0
      if (got_type & GOT_TLS_GD)
4087
0
        htab->root.srelgot->size += RELOC_SIZE (htab) * 2;
4088
4089
0
      if (got_type & GOT_TLS_IE
4090
0
          || got_type & GOT_TLS_LD
4091
0
          || got_type & GOT_NORMAL)
4092
0
        htab->root.srelgot->size += RELOC_SIZE (htab);
4093
0
    }
4094
0
      }
4095
0
    else
4096
0
      {
4097
0
        locals[i].got_refcount = (bfd_vma) - 1;
4098
0
      }
4099
0
  }
4100
0
    }
4101
4102
4103
  /* Allocate global sym .plt and .got entries, and space for global
4104
     sym dynamic relocs.  */
4105
0
  elf_link_hash_traverse (&htab->root, elf32_kvx_allocate_dynrelocs,
4106
0
        info);
4107
4108
  /* For every jump slot reserved in the sgotplt, reloc_count is
4109
     incremented.  However, when we reserve space for TLS descriptors,
4110
     it's not incremented, so in order to compute the space reserved
4111
     for them, it suffices to multiply the reloc count by the jump
4112
     slot size.  */
4113
4114
0
  if (htab->root.srelplt)
4115
0
    htab->sgotplt_jump_table_size = kvx_compute_jump_table_size (htab);
4116
4117
  /* We now have determined the sizes of the various dynamic sections.
4118
     Allocate memory for them.  */
4119
0
  relocs = false;
4120
0
  for (s = dynobj->sections; s != NULL; s = s->next)
4121
0
    {
4122
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
4123
0
  continue;
4124
4125
0
      if (s == htab->root.splt
4126
0
    || s == htab->root.sgot
4127
0
    || s == htab->root.sgotplt
4128
0
    || s == htab->root.iplt
4129
0
    || s == htab->root.igotplt || s == htab->sdynbss)
4130
0
  {
4131
    /* Strip this section if we don't need it; see the
4132
       comment below.  */
4133
0
  }
4134
0
      else if (startswith (bfd_section_name (s), ".rela"))
4135
0
  {
4136
0
    if (s->size != 0 && s != htab->root.srelplt)
4137
0
      relocs = true;
4138
4139
    /* We use the reloc_count field as a counter if we need
4140
       to copy relocs into the output file.  */
4141
0
    if (s != htab->root.srelplt)
4142
0
      s->reloc_count = 0;
4143
0
  }
4144
0
      else
4145
0
  {
4146
    /* It's not one of our sections, so don't allocate space.  */
4147
0
    continue;
4148
0
  }
4149
4150
0
      if (s->size == 0)
4151
0
  {
4152
    /* If we don't need this section, strip it from the
4153
       output file.  This is mostly to handle .rela.bss and
4154
       .rela.plt.  We must create both sections in
4155
       create_dynamic_sections, because they must be created
4156
       before the linker maps input sections to output
4157
       sections.  The linker does that before
4158
       adjust_dynamic_symbol is called, and it is that
4159
       function which decides whether anything needs to go
4160
       into these sections.  */
4161
4162
0
    s->flags |= SEC_EXCLUDE;
4163
0
    continue;
4164
0
  }
4165
4166
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
4167
0
  continue;
4168
4169
      /* Allocate memory for the section contents.  We use bfd_zalloc
4170
   here in case unused entries are not reclaimed before the
4171
   section's contents are written out.  This should not happen,
4172
   but this way if it does, we get a R_KVX_NONE reloc instead
4173
   of garbage.  */
4174
0
      s->contents = (bfd_byte *) bfd_zalloc (dynobj, s->size);
4175
0
      if (s->contents == NULL)
4176
0
  return false;
4177
0
      s->alloced = 1;
4178
0
    }
4179
4180
0
  if (htab->root.dynamic_sections_created)
4181
0
    {
4182
      /* Add some entries to the .dynamic section.  We fill in the
4183
   values later, in elf32_kvx_finish_dynamic_sections, but we
4184
   must add the entries now so that we get the correct size for
4185
   the .dynamic section.  The DT_DEBUG entry is filled in by the
4186
   dynamic linker and used by the debugger.  */
4187
0
#define add_dynamic_entry(TAG, VAL)     \
4188
0
      _bfd_elf_add_dynamic_entry (info, TAG, VAL)
4189
4190
0
      if (bfd_link_executable (info))
4191
0
  {
4192
0
    if (!add_dynamic_entry (DT_DEBUG, 0))
4193
0
      return false;
4194
0
  }
4195
4196
0
      if (htab->root.splt->size != 0)
4197
0
  {
4198
0
    if (!add_dynamic_entry (DT_PLTGOT, 0)
4199
0
        || !add_dynamic_entry (DT_PLTRELSZ, 0)
4200
0
        || !add_dynamic_entry (DT_PLTREL, DT_RELA)
4201
0
        || !add_dynamic_entry (DT_JMPREL, 0))
4202
0
      return false;
4203
0
  }
4204
4205
0
      if (relocs)
4206
0
  {
4207
0
    if (!add_dynamic_entry (DT_RELA, 0)
4208
0
        || !add_dynamic_entry (DT_RELASZ, 0)
4209
0
        || !add_dynamic_entry (DT_RELAENT, RELOC_SIZE (htab)))
4210
0
      return false;
4211
4212
    /* If any dynamic relocs apply to a read-only section,
4213
       then we need a DT_TEXTREL entry.  */
4214
0
    if ((info->flags & DF_TEXTREL) == 0)
4215
0
      elf_link_hash_traverse (&htab->root, kvx_readonly_dynrelocs,
4216
0
            info);
4217
4218
0
    if ((info->flags & DF_TEXTREL) != 0)
4219
0
      {
4220
0
        if (!add_dynamic_entry (DT_TEXTREL, 0))
4221
0
    return false;
4222
0
      }
4223
0
  }
4224
0
    }
4225
0
#undef add_dynamic_entry
4226
4227
0
  return true;
4228
0
}
4229
4230
static inline void
4231
elf_kvx_update_plt_entry (bfd *output_bfd,
4232
        bfd_reloc_code_real_type r_type,
4233
        bfd_byte *plt_entry, bfd_vma value)
4234
0
{
4235
0
  reloc_howto_type *howto = elf32_kvx_howto_from_bfd_reloc (r_type);
4236
0
  BFD_ASSERT(howto != NULL);
4237
0
  _bfd_kvx_elf_put_addend (output_bfd, plt_entry, r_type, howto, value);
4238
0
}
4239
4240
static void
4241
elf32_kvx_create_small_pltn_entry (struct elf_link_hash_entry *h,
4242
           struct elf_kvx_link_hash_table *htab,
4243
           bfd *output_bfd)
4244
0
{
4245
0
  bfd_byte *plt_entry;
4246
0
  bfd_vma plt_index;
4247
0
  bfd_vma got_offset;
4248
0
  bfd_vma gotplt_entry_address;
4249
0
  bfd_vma plt_entry_address;
4250
0
  Elf_Internal_Rela rela;
4251
0
  bfd_byte *loc;
4252
0
  asection *plt, *gotplt, *relplt;
4253
4254
0
  plt = htab->root.splt;
4255
0
  gotplt = htab->root.sgotplt;
4256
0
  relplt = htab->root.srelplt;
4257
4258
  /* Get the index in the procedure linkage table which
4259
     corresponds to this symbol.  This is the index of this symbol
4260
     in all the symbols for which we are making plt entries.  The
4261
     first entry in the procedure linkage table is reserved.
4262
4263
     Get the offset into the .got table of the entry that
4264
     corresponds to this function.  Each .got entry is GOT_ENTRY_SIZE
4265
     bytes. The first three are reserved for the dynamic linker.
4266
4267
     For static executables, we don't reserve anything.  */
4268
4269
0
  if (plt == htab->root.splt)
4270
0
    {
4271
0
      plt_index = (h->plt.offset - htab->plt_header_size) / htab->plt_entry_size;
4272
0
      got_offset = (plt_index + 3) * GOT_ENTRY_SIZE;
4273
0
    }
4274
0
  else
4275
0
    {
4276
0
      plt_index = h->plt.offset / htab->plt_entry_size;
4277
0
      got_offset = plt_index * GOT_ENTRY_SIZE;
4278
0
    }
4279
4280
0
  plt_entry = plt->contents + h->plt.offset;
4281
0
  plt_entry_address = plt->output_section->vma
4282
0
    + plt->output_offset + h->plt.offset;
4283
0
  gotplt_entry_address = gotplt->output_section->vma +
4284
0
    gotplt->output_offset + got_offset;
4285
4286
  /* Copy in the boiler-plate for the PLTn entry.  */
4287
0
  memcpy (plt_entry, elf32_kvx_small_plt_entry, PLT_SMALL_ENTRY_SIZE);
4288
4289
  /* Patch the loading of the GOT entry, relative to the PLT entry
4290
     address. */
4291
4292
  /* Use 37bits offset for both 32 and 64bits mode.
4293
     Fill the LO10 of of lw $r9 = 0[$r14].  */
4294
0
  elf_kvx_update_plt_entry(output_bfd, BFD_RELOC_KVX_S37_LO10,
4295
0
         plt_entry+4,
4296
0
         gotplt_entry_address - plt_entry_address);
4297
4298
  /* Fill the UP27 of of lw $r9 = 0[$r14].  */
4299
0
  elf_kvx_update_plt_entry(output_bfd, BFD_RELOC_KVX_S37_UP27,
4300
0
         plt_entry+8,
4301
0
         gotplt_entry_address - plt_entry_address);
4302
4303
0
  rela.r_offset = gotplt_entry_address;
4304
4305
  /* Fill in the entry in the .rela.plt section.  */
4306
0
  rela.r_info = ELF32_R_INFO (h->dynindx, R_KVX_JMP_SLOT);
4307
0
  rela.r_addend = 0;
4308
4309
  /* Compute the relocation entry to used based on PLT index and do
4310
     not adjust reloc_count. The reloc_count has already been adjusted
4311
     to account for this entry.  */
4312
0
  loc = relplt->contents + plt_index * RELOC_SIZE (htab);
4313
0
  bfd_elf32_swap_reloca_out (output_bfd, &rela, loc);
4314
0
}
4315
4316
/* Size sections even though they're not dynamic.  We use it to setup
4317
   _TLS_MODULE_BASE_, if needed.  */
4318
4319
static bool
4320
elf32_kvx_early_size_sections (struct bfd_link_info *info)
4321
0
{
4322
0
  asection *tls_sec;
4323
4324
0
  if (bfd_link_relocatable (info))
4325
0
    return true;
4326
4327
0
  tls_sec = elf_hash_table (info)->tls_sec;
4328
4329
0
  if (tls_sec)
4330
0
    {
4331
0
      struct elf_link_hash_entry *tlsbase;
4332
4333
0
      tlsbase = elf_link_hash_lookup (elf_hash_table (info),
4334
0
              "_TLS_MODULE_BASE_", true, true, false);
4335
4336
0
      if (tlsbase)
4337
0
  {
4338
0
    struct bfd_link_hash_entry *h = NULL;
4339
0
    elf_backend_data *bed = get_elf_backend_data (info->output_bfd);
4340
4341
0
    if (!(_bfd_generic_link_add_one_symbol
4342
0
    (info, info->output_bfd, "_TLS_MODULE_BASE_", BSF_LOCAL,
4343
0
     tls_sec, 0, NULL, false, bed->collect, &h)))
4344
0
      return false;
4345
4346
0
    tlsbase->type = STT_TLS;
4347
0
    tlsbase = (struct elf_link_hash_entry *) h;
4348
0
    tlsbase->def_regular = 1;
4349
0
    tlsbase->other = STV_HIDDEN;
4350
0
    (*bed->elf_backend_hide_symbol) (info, tlsbase, true);
4351
0
  }
4352
0
    }
4353
4354
0
  return true;
4355
0
}
4356
4357
/* Finish up dynamic symbol handling.  We set the contents of various
4358
   dynamic sections here.  */
4359
static bool
4360
elf32_kvx_finish_dynamic_symbol (struct bfd_link_info *info,
4361
         struct elf_link_hash_entry *h,
4362
         Elf_Internal_Sym *sym)
4363
0
{
4364
0
  struct elf_kvx_link_hash_table *htab;
4365
0
  htab = elf_kvx_hash_table (info);
4366
4367
0
  if (h->plt.offset != (bfd_vma) - 1)
4368
0
    {
4369
0
      asection *plt = NULL, *gotplt = NULL, *relplt = NULL;
4370
4371
      /* This symbol has an entry in the procedure linkage table.  Set
4372
   it up.  */
4373
4374
0
      if (htab->root.splt != NULL)
4375
0
  {
4376
0
    plt = htab->root.splt;
4377
0
    gotplt = htab->root.sgotplt;
4378
0
    relplt = htab->root.srelplt;
4379
0
  }
4380
4381
      /* This symbol has an entry in the procedure linkage table.  Set
4382
   it up.  */
4383
0
      if ((h->dynindx == -1
4384
0
     && !((h->forced_local || bfd_link_executable (info))
4385
0
    && h->def_regular
4386
0
    && h->type == STT_GNU_IFUNC))
4387
0
    || plt == NULL
4388
0
    || gotplt == NULL
4389
0
    || relplt == NULL)
4390
0
  abort ();
4391
4392
0
      elf32_kvx_create_small_pltn_entry (h, htab, info->output_bfd);
4393
0
      if (!h->def_regular)
4394
0
  {
4395
    /* Mark the symbol as undefined, rather than as defined in
4396
       the .plt section.  */
4397
0
    sym->st_shndx = SHN_UNDEF;
4398
    /* If the symbol is weak we need to clear the value.
4399
       Otherwise, the PLT entry would provide a definition for
4400
       the symbol even if the symbol wasn't defined anywhere,
4401
       and so the symbol would never be NULL.  Leave the value if
4402
       there were any relocations where pointer equality matters
4403
       (this is a clue for the dynamic linker, to make function
4404
       pointer comparisons work between an application and shared
4405
       library).  */
4406
0
    if (!h->ref_regular_nonweak || !h->pointer_equality_needed)
4407
0
      sym->st_value = 0;
4408
0
  }
4409
0
    }
4410
4411
0
  if (h->got.offset != (bfd_vma) - 1
4412
0
      && elf_kvx_hash_entry (h)->got_type == GOT_NORMAL)
4413
0
    {
4414
0
      Elf_Internal_Rela rela;
4415
0
      bfd_byte *loc;
4416
4417
      /* This symbol has an entry in the global offset table.  Set it
4418
   up.  */
4419
0
      if (htab->root.sgot == NULL || htab->root.srelgot == NULL)
4420
0
  abort ();
4421
4422
0
      rela.r_offset = (htab->root.sgot->output_section->vma
4423
0
           + htab->root.sgot->output_offset
4424
0
           + (h->got.offset & ~(bfd_vma) 1));
4425
4426
#ifdef UGLY_DEBUG
4427
      printf("setting rela at offset 0x%x(0x%x + 0x%x + 0x%x) for %s\n",
4428
       rela.r_offset,
4429
       htab->root.sgot->output_section->vma,
4430
       htab->root.sgot->output_offset,
4431
       h->got.offset,
4432
       h->root.root.string);
4433
#endif
4434
4435
0
      if (bfd_link_pic (info) && SYMBOL_REFERENCES_LOCAL (info, h))
4436
0
  {
4437
0
    if (!h->def_regular)
4438
0
      return false;
4439
4440
    /* in case of PLT related GOT entry, it is not clear who is
4441
       supposed to set the LSB of GOT entry...
4442
       kvx_calculate_got_entry_vma() would be a good candidate,
4443
       but it is not called currently
4444
       So we are commenting it ATM.  */
4445
    // BFD_ASSERT ((h->got.offset & 1) != 0);
4446
0
    rela.r_info = ELF32_R_INFO (0, R_KVX_RELATIVE);
4447
0
    rela.r_addend = (h->root.u.def.value
4448
0
         + h->root.u.def.section->output_section->vma
4449
0
         + h->root.u.def.section->output_offset);
4450
0
  }
4451
0
      else
4452
0
  {
4453
0
    BFD_ASSERT ((h->got.offset & 1) == 0);
4454
0
    bfd_put_32 (info->output_bfd, 0,
4455
0
          htab->root.sgot->contents + h->got.offset);
4456
0
    rela.r_info = ELF32_R_INFO (h->dynindx, R_KVX_GLOB_DAT);
4457
0
    rela.r_addend = 0;
4458
0
  }
4459
4460
0
      loc = htab->root.srelgot->contents;
4461
0
      loc += htab->root.srelgot->reloc_count++ * RELOC_SIZE (htab);
4462
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
4463
0
    }
4464
4465
0
  if (h->needs_copy)
4466
0
    {
4467
0
      Elf_Internal_Rela rela;
4468
0
      bfd_byte *loc;
4469
4470
      /* This symbol needs a copy reloc.  Set it up.  */
4471
4472
0
      if (h->dynindx == -1
4473
0
    || (h->root.type != bfd_link_hash_defined
4474
0
        && h->root.type != bfd_link_hash_defweak)
4475
0
    || htab->srelbss == NULL)
4476
0
  abort ();
4477
4478
0
      rela.r_offset = (h->root.u.def.value
4479
0
           + h->root.u.def.section->output_section->vma
4480
0
           + h->root.u.def.section->output_offset);
4481
0
      rela.r_info = ELF32_R_INFO (h->dynindx, R_KVX_COPY);
4482
0
      rela.r_addend = 0;
4483
0
      loc = htab->srelbss->contents;
4484
0
      loc += htab->srelbss->reloc_count++ * RELOC_SIZE (htab);
4485
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rela, loc);
4486
0
    }
4487
4488
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  SYM may
4489
     be NULL for local symbols.  */
4490
0
  if (sym != NULL
4491
0
      && (h == elf_hash_table (info)->hdynamic
4492
0
    || h == elf_hash_table (info)->hgot))
4493
0
    sym->st_shndx = SHN_ABS;
4494
4495
0
  return true;
4496
0
}
4497
4498
static void
4499
elf32_kvx_init_small_plt0_entry (bfd *output_bfd ATTRIBUTE_UNUSED,
4500
         struct elf_kvx_link_hash_table *htab)
4501
0
{
4502
0
  memcpy (htab->root.splt->contents, elf32_kvx_small_plt0_entry,
4503
0
    PLT_ENTRY_SIZE);
4504
0
  elf_section_data (htab->root.splt->output_section)->this_hdr.sh_entsize =
4505
0
    PLT_ENTRY_SIZE;
4506
0
}
4507
4508
static bool
4509
elf32_kvx_finish_dynamic_sections (struct bfd_link_info *info,
4510
           bfd_byte *buf ATTRIBUTE_UNUSED)
4511
0
{
4512
0
  struct elf_kvx_link_hash_table *htab;
4513
0
  bfd *dynobj;
4514
0
  asection *sdyn;
4515
4516
0
  htab = elf_kvx_hash_table (info);
4517
0
  dynobj = htab->root.dynobj;
4518
0
  sdyn = bfd_get_linker_section (dynobj, ".dynamic");
4519
4520
0
  if (htab->root.dynamic_sections_created)
4521
0
    {
4522
0
      Elf32_External_Dyn *dyncon, *dynconend;
4523
4524
0
      if (sdyn == NULL || htab->root.sgot == NULL)
4525
0
  abort ();
4526
4527
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
4528
0
      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
4529
0
      for (; dyncon < dynconend; dyncon++)
4530
0
  {
4531
0
    Elf_Internal_Dyn dyn;
4532
0
    asection *s;
4533
4534
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &dyn);
4535
4536
0
    switch (dyn.d_tag)
4537
0
      {
4538
0
      default:
4539
0
        continue;
4540
4541
0
      case DT_PLTGOT:
4542
0
        s = htab->root.sgotplt;
4543
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4544
0
        break;
4545
4546
0
      case DT_JMPREL:
4547
0
        s = htab->root.srelplt;
4548
0
        dyn.d_un.d_ptr = s->output_section->vma + s->output_offset;
4549
0
        break;
4550
4551
0
      case DT_PLTRELSZ:
4552
0
        s = htab->root.srelplt;
4553
0
        dyn.d_un.d_val = s->size;
4554
0
        break;
4555
4556
0
      case DT_RELASZ:
4557
        /* The procedure linkage table relocs (DT_JMPREL) should
4558
     not be included in the overall relocs (DT_RELA).
4559
     Therefore, we override the DT_RELASZ entry here to
4560
     make it not include the JMPREL relocs.  Since the
4561
     linker script arranges for .rela.plt to follow all
4562
     other relocation sections, we don't have to worry
4563
     about changing the DT_RELA entry.  */
4564
0
        if (htab->root.srelplt != NULL)
4565
0
    {
4566
0
      s = htab->root.srelplt;
4567
0
      dyn.d_un.d_val -= s->size;
4568
0
    }
4569
0
        break;
4570
0
      }
4571
4572
0
    bfd_elf32_swap_dyn_out (info->output_bfd, &dyn, dyncon);
4573
0
  }
4574
4575
0
    }
4576
4577
  /* Fill in the special first entry in the procedure linkage table.  */
4578
0
  if (htab->root.splt && htab->root.splt->size > 0)
4579
0
    {
4580
0
      elf32_kvx_init_small_plt0_entry (info->output_bfd, htab);
4581
4582
0
      elf_section_data (htab->root.splt->output_section)->
4583
0
  this_hdr.sh_entsize = htab->plt_entry_size;
4584
0
    }
4585
4586
0
  if (htab->root.sgotplt)
4587
0
    {
4588
0
      if (bfd_is_abs_section (htab->root.sgotplt->output_section))
4589
0
  {
4590
0
    (*_bfd_error_handler)
4591
0
      (_("discarded output section: `%pA'"), htab->root.sgotplt);
4592
0
    return false;
4593
0
  }
4594
4595
      /* Fill in the first three entries in the global offset table.  */
4596
0
      if (htab->root.sgotplt->size > 0)
4597
0
  {
4598
0
    bfd_put_32 (info->output_bfd, 0, htab->root.sgotplt->contents);
4599
4600
    /* Write GOT[1] and GOT[2], needed for the dynamic linker.  */
4601
0
    bfd_put_32 (info->output_bfd, 0,
4602
0
          htab->root.sgotplt->contents + GOT_ENTRY_SIZE);
4603
0
    bfd_put_32 (info->output_bfd, 0,
4604
0
          htab->root.sgotplt->contents + GOT_ENTRY_SIZE * 2);
4605
0
  }
4606
4607
0
      if (htab->root.sgot)
4608
0
  {
4609
0
    if (htab->root.sgot->size > 0)
4610
0
      {
4611
0
        bfd_vma addr =
4612
0
    sdyn ? sdyn->output_section->vma + sdyn->output_offset : 0;
4613
0
        bfd_put_32 (info->output_bfd, addr, htab->root.sgot->contents);
4614
0
      }
4615
0
  }
4616
4617
0
      elf_section_data (htab->root.sgotplt->output_section)->
4618
0
  this_hdr.sh_entsize = GOT_ENTRY_SIZE;
4619
0
    }
4620
4621
0
  if (htab->root.sgot && htab->root.sgot->size > 0)
4622
0
    elf_section_data (htab->root.sgot->output_section)->this_hdr.sh_entsize
4623
0
      = GOT_ENTRY_SIZE;
4624
4625
0
  return true;
4626
0
}
4627
4628
/* Return address for Ith PLT stub in section PLT, for relocation REL
4629
   or (bfd_vma) -1 if it should not be included.  */
4630
4631
static bfd_vma
4632
elf32_kvx_plt_sym_val (bfd_vma i, const asection *plt,
4633
           const arelent *rel ATTRIBUTE_UNUSED)
4634
0
{
4635
0
  return plt->vma + PLT_ENTRY_SIZE + i * PLT_SMALL_ENTRY_SIZE;
4636
0
}
4637
4638
#define ELF_ARCH      bfd_arch_kvx
4639
#define ELF_TARGET_ID     KVX_ELF_DATA
4640
#define ELF_MACHINE_CODE    EM_KVX
4641
#define ELF_MAXPAGESIZE     0x10000
4642
#define ELF_MINPAGESIZE     0x1000
4643
#define ELF_COMMONPAGESIZE    0x1000
4644
4645
#define bfd_elf32_bfd_link_hash_table_create    \
4646
  elf32_kvx_link_hash_table_create
4647
4648
#define bfd_elf32_bfd_merge_private_bfd_data  \
4649
  elf32_kvx_merge_private_bfd_data
4650
4651
#define bfd_elf32_bfd_print_private_bfd_data  \
4652
  elf32_kvx_print_private_bfd_data
4653
4654
#define bfd_elf32_bfd_reloc_type_lookup   \
4655
  elf32_kvx_reloc_type_lookup
4656
4657
#define bfd_elf32_bfd_reloc_name_lookup   \
4658
  elf32_kvx_reloc_name_lookup
4659
4660
#define bfd_elf32_bfd_set_private_flags   \
4661
  elf32_kvx_set_private_flags
4662
4663
#define bfd_elf32_mkobject      \
4664
  elf32_kvx_mkobject
4665
4666
#define bfd_elf32_new_section_hook    \
4667
  elf32_kvx_new_section_hook
4668
4669
#define elf_backend_adjust_dynamic_symbol \
4670
  elf32_kvx_adjust_dynamic_symbol
4671
4672
#define elf_backend_early_size_sections   \
4673
  elf32_kvx_early_size_sections
4674
4675
#define elf_backend_check_relocs    \
4676
  elf32_kvx_check_relocs
4677
4678
#define elf_backend_copy_indirect_symbol  \
4679
  elf32_kvx_copy_indirect_symbol
4680
4681
/* Create .dynbss, and .rela.bss sections in DYNOBJ, and set up shortcuts
4682
   to them in our hash.  */
4683
#define elf_backend_create_dynamic_sections \
4684
  elf32_kvx_create_dynamic_sections
4685
4686
#define elf_backend_init_index_section    \
4687
  _bfd_elf_init_2_index_sections
4688
4689
#define elf_backend_finish_dynamic_sections \
4690
  elf32_kvx_finish_dynamic_sections
4691
4692
#define elf_backend_finish_dynamic_symbol \
4693
  elf32_kvx_finish_dynamic_symbol
4694
4695
#define elf_backend_object_p      \
4696
  elf32_kvx_object_p
4697
4698
#define elf_backend_output_arch_local_syms      \
4699
  elf32_kvx_output_arch_local_syms
4700
4701
#define elf_backend_plt_sym_val     \
4702
  elf32_kvx_plt_sym_val
4703
4704
#define elf_backend_init_file_header    \
4705
  elf32_kvx_init_file_header
4706
4707
#define elf_backend_init_process_headers  \
4708
  elf32_kvx_init_process_headers
4709
4710
#define elf_backend_relocate_section    \
4711
  elf32_kvx_relocate_section
4712
4713
#define elf_backend_reloc_type_class    \
4714
  elf32_kvx_reloc_type_class
4715
4716
#define elf_backend_late_size_sections  \
4717
  elf32_kvx_late_size_sections
4718
4719
#define elf_backend_can_refcount       1
4720
#define elf_backend_can_gc_sections    1
4721
#define elf_backend_plt_readonly       1
4722
#define elf_backend_want_got_plt       1
4723
#define elf_backend_want_plt_sym       0
4724
#define elf_backend_want_stub_bfd      1
4725
#define elf_backend_may_use_rel_p      0
4726
#define elf_backend_may_use_rela_p     1
4727
#define elf_backend_default_use_rela_p 1
4728
#define elf_backend_rela_normal        1
4729
#define elf_backend_got_header_size (GOT_ENTRY_SIZE * 3)
4730
#define elf_backend_default_execstack  0
4731
#define elf_backend_extern_protected_data 1
4732
#define elf_backend_hash_symbol elf_kvx_hash_symbol
4733
4734
#include "elf32-target.h"