Coverage Report

Created: 2026-07-25 10:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-arc.c
Line
Count
Source
1
/* ARC-specific support for 32-bit ELF
2
   Copyright (C) 1994-2026 Free Software Foundation, Inc.
3
   Contributed by Cupertino Miranda (cmiranda@synopsys.com).
4
5
   This file is part of BFD, the Binary File Descriptor library.
6
7
   This program is free software; you can redistribute it and/or modify
8
   it under the terms of the GNU General Public License as published by
9
   the Free Software Foundation; either version 3 of the License, or
10
   (at your option) any later version.
11
12
   This program is distributed in the hope that it will be useful,
13
   but WITHOUT ANY WARRANTY; without even the implied warranty of
14
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
   GNU General Public License for more details.
16
17
   You should have received a copy of the GNU General Public License
18
   along with this program; if not, write to the Free Software
19
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include "bfd.h"
24
#include "libbfd.h"
25
#include "elf-bfd.h"
26
#include "elf/arc.h"
27
#include "libiberty.h"
28
#include "opcode/arc-func.h"
29
#include "opcode/arc.h"
30
#include "arc-plt.h"
31
32
#define FEATURE_LIST_NAME bfd_feature_list
33
#define CONFLICT_LIST bfd_conflict_list
34
#include "opcode/arc-attrs.h"
35
36
/* #define ARC_ENABLE_DEBUG 1  */
37
#ifdef ARC_ENABLE_DEBUG
38
static const char *
39
name_for_global_symbol (struct elf_link_hash_entry *h)
40
{
41
  static char *local_str = "(local)";
42
  if (h == NULL)
43
    return local_str;
44
  return h->root.root.string;
45
}
46
#define ARC_DEBUG(fmt, args...) fprintf (stderr, fmt, ##args)
47
#else
48
#define ARC_DEBUG(...)
49
#endif
50
51
52
#define ADD_RELA(BFD, SECTION, OFFSET, SYM_IDX, TYPE, ADDEND)   \
53
0
  {                 \
54
0
    struct elf_link_hash_table *_htab = elf_hash_table (info);    \
55
0
    Elf_Internal_Rela _rel;           \
56
0
    bfd_byte * _loc;              \
57
0
                  \
58
0
    if (_htab->dynamic_sections_created)       \
59
0
      {                 \
60
0
  BFD_ASSERT (_htab->srel##SECTION &&_htab->srel##SECTION->contents); \
61
0
  _loc = _htab->srel##SECTION->contents       \
62
0
    + ((_htab->srel##SECTION->reloc_count)     \
63
0
       * sizeof (Elf32_External_Rela));       \
64
0
  _htab->srel##SECTION->reloc_count++;       \
65
0
  _rel.r_addend = ADDEND;           \
66
0
  _rel.r_offset = (_htab->s##SECTION)->output_section->vma \
67
0
    + (_htab->s##SECTION)->output_offset + OFFSET;   \
68
0
  BFD_ASSERT ((long) SYM_IDX != -1);        \
69
0
  _rel.r_info = ELF32_R_INFO (SYM_IDX, TYPE);     \
70
0
  bfd_elf32_swap_reloca_out (BFD, &_rel, _loc);     \
71
0
      }                 \
72
0
  }
73
74
#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
75
      case VALUE: \
76
  return "R_" #TYPE; \
77
  break;
78
79
static ATTRIBUTE_UNUSED const char *
80
reloc_type_to_name (unsigned int type)
81
0
{
82
0
  switch (type)
83
0
    {
84
0
#include "elf/arc-reloc.def"
85
0
86
0
    default:
87
0
      return "UNKNOWN";
88
0
      break;
89
0
    }
90
0
}
91
92
#undef ARC_RELOC_HOWTO
93
94
/* Try to minimize the amount of space occupied by relocation tables
95
   on the ROM (not that the ROM won't be swamped by other ELF overhead).  */
96
97
#define USE_REL 1
98
99
/* Similar with bfd_get_32 but taking into account the
100
   middle-endianess of the ARC CPUs.  Only to be used in code
101
   sections.  */
102
103
static bfd_vma
104
bfd_get_32_me (bfd * abfd,const unsigned char * data)
105
0
{
106
0
  bfd_vma value = 0;
107
108
0
  if (bfd_big_endian (abfd))
109
0
    value = bfd_get_32 (abfd, data);
110
0
  else
111
0
    {
112
0
      value = ((bfd_get_8 (abfd, data) & 255) << 16);
113
0
      value |= ((bfd_get_8 (abfd, data + 1) & 255) << 24);
114
0
      value |= (bfd_get_8 (abfd, data + 2) & 255);
115
0
      value |= ((bfd_get_8 (abfd, data + 3) & 255) << 8);
116
0
    }
117
118
0
  return value;
119
0
}
120
121
static void
122
bfd_put_32_me (bfd *abfd, bfd_vma value,unsigned char *data)
123
0
{
124
0
  bfd_put_16 (abfd, (value & 0xffff0000) >> 16, data);
125
0
  bfd_put_16 (abfd, value & 0xffff, data + 2);
126
0
}
127
128
static ATTRIBUTE_UNUSED bool
129
is_reloc_PC_relative (reloc_howto_type *howto)
130
0
{
131
0
  return strstr (howto->name, "PC") != NULL;
132
0
}
133
134
static bool
135
is_reloc_SDA_relative (reloc_howto_type *howto)
136
0
{
137
0
  return strstr (howto->name, "SDA") != NULL;
138
0
}
139
140
static bool
141
is_reloc_for_GOT (reloc_howto_type * howto)
142
0
{
143
0
  if (strstr (howto->name, "TLS") != NULL)
144
0
    return false;
145
0
  return strstr (howto->name, "GOT") != NULL;
146
0
}
147
148
static bool
149
is_reloc_for_PLT (reloc_howto_type * howto)
150
0
{
151
0
  return strstr (howto->name, "PLT") != NULL;
152
0
}
153
154
static bool
155
is_reloc_for_TLS (reloc_howto_type *howto)
156
0
{
157
0
  return strstr (howto->name, "TLS") != NULL;
158
0
}
159
160
struct arc_relocation_data
161
{
162
  bfd_signed_vma reloc_offset;
163
  bfd_signed_vma reloc_addend;
164
  bfd_signed_vma got_offset_value;
165
166
  bfd_signed_vma sym_value;
167
  asection *sym_section;
168
169
  reloc_howto_type *howto;
170
171
  asection *input_section;
172
173
  bfd_signed_vma sdata_begin_symbol_vma;
174
  bool sdata_begin_symbol_vma_set;
175
  bfd_signed_vma got_symbol_vma;
176
177
  bool should_relocate;
178
179
  const char *symbol_name;
180
};
181
182
/* ARC ELF linker hash entry.  */
183
struct elf_arc_link_hash_entry
184
{
185
  struct elf_link_hash_entry root;
186
187
  struct got_entry *got_ents;
188
};
189
190
191
/* Should be included at this location due to static declarations
192
   defined before this point.  */
193
#include "arc-got.h"
194
195
0
#define arc_bfd_get_8(A,B,C) bfd_get_8(A,B)
196
0
#define arc_bfd_get_16(A,B,C) bfd_get_16(A,B)
197
0
#define arc_bfd_get_32(A,B,C) bfd_get_32(A,B)
198
0
#define arc_bfd_put_8(A,B,C,D) bfd_put_8(A,B,C)
199
0
#define arc_bfd_put_16(A,B,C,D) bfd_put_16(A,B,C)
200
0
#define arc_bfd_put_32(A,B,C,D) bfd_put_32(A,B,C)
201
202
203
static bfd_reloc_status_type
204
arc_elf_reloc (bfd *abfd ATTRIBUTE_UNUSED,
205
         arelent *reloc_entry,
206
         asymbol *symbol_in,
207
         void *data ATTRIBUTE_UNUSED,
208
         asection *input_section,
209
         bfd *output_bfd,
210
         char ** error_message ATTRIBUTE_UNUSED)
211
0
{
212
0
  if (output_bfd != NULL)
213
0
    {
214
0
      reloc_entry->address += input_section->output_offset;
215
216
      /* In case of relocateable link and if the reloc is against a
217
   section symbol, the addend needs to be adjusted according to
218
   where the section symbol winds up in the output section.  */
219
0
      if ((symbol_in->flags & BSF_SECTION_SYM) && symbol_in->section)
220
0
  reloc_entry->addend += symbol_in->section->output_offset;
221
222
0
      return bfd_reloc_ok;
223
0
    }
224
225
0
  return bfd_reloc_continue;
226
0
}
227
228
229
#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
230
  TYPE = VALUE,
231
232
enum howto_list
233
{
234
#include "elf/arc-reloc.def"
235
  HOWTO_LIST_LAST
236
};
237
238
#undef ARC_RELOC_HOWTO
239
240
#define ARC_RELOC_HOWTO(TYPE, VALUE, RSIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
241
  [TYPE] = HOWTO (R_##TYPE, 0, RSIZE, BITSIZE, false, 0,    \
242
      complain_overflow_##OVERFLOW, arc_elf_reloc,    \
243
      "R_" #TYPE, false, 0, 0, false),
244
245
static struct reloc_howto_struct elf_arc_howto_table[] =
246
{
247
#include "elf/arc-reloc.def"
248
/* Example of what is generated by the preprocessor.  Currently kept as an
249
   example.
250
 HOWTO (R_ARC_NONE, // Type.
251
    0, // Rightshift.
252
    4, // Size.
253
    32, // Bitsize.
254
    false, // PC_relative.
255
    0, // Bitpos.
256
    complain_overflow_bitfield, // Complain_on_overflow.
257
    bfd_elf_generic_reloc, // Special_function.
258
    "R_ARC_NONE", // Name.
259
    true, // Partial_inplace.
260
    0, // Src_mask.
261
    0, // Dst_mask.
262
    false), // PCrel_offset.
263
*/
264
};
265
#undef ARC_RELOC_HOWTO
266
267
static void
268
arc_elf_howto_init (void)
269
2
{
270
2
#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
271
138
  elf_arc_howto_table[TYPE].pc_relative =       \
272
138
    (strstr (#FORMULA, " P ") != NULL || strstr (#FORMULA, " PDATA ") != NULL); \
273
138
  elf_arc_howto_table[TYPE].dst_mask = RELOC_FUNCTION(0, ~0);   \
274
  /* Only 32 bit data relocations should be marked as ME.  */    \
275
138
  if (strstr (#FORMULA, " ME ") != NULL)       \
276
138
    {                 \
277
82
      BFD_ASSERT (SIZE == 4);           \
278
82
    }
279
280
2
#include "elf/arc-reloc.def"
281
282
2
}
283
#undef ARC_RELOC_HOWTO
284
285
286
#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
287
  [TYPE] = VALUE,
288
289
const int howto_table_lookup[] =
290
{
291
#include "elf/arc-reloc.def"
292
};
293
294
#undef ARC_RELOC_HOWTO
295
296
static reloc_howto_type *
297
arc_elf_howto (unsigned int r_type)
298
344
{
299
344
  if (elf_arc_howto_table[R_ARC_32].dst_mask == 0)
300
2
    arc_elf_howto_init ();
301
344
  return &elf_arc_howto_table[r_type];
302
344
}
303
304
/* Map BFD reloc types to ARC ELF reloc types.  */
305
306
struct arc_reloc_map
307
{
308
  bfd_reloc_code_real_type  bfd_reloc_val;
309
  unsigned char       elf_reloc_val;
310
};
311
312
/* ARC ELF linker hash table.  */
313
struct elf_arc_link_hash_table
314
{
315
  struct elf_link_hash_table elf;
316
};
317
318
static struct bfd_hash_entry *
319
elf_arc_link_hash_newfunc (struct bfd_hash_entry *entry,
320
         struct bfd_hash_table *table,
321
         const char *string)
322
0
{
323
0
  struct elf_arc_link_hash_entry * ret =
324
0
    (struct elf_arc_link_hash_entry *) entry;
325
326
  /* Allocate the structure if it has not already been allocated by a
327
     subclass.  */
328
0
  if (ret == NULL)
329
0
    ret = (struct elf_arc_link_hash_entry *)
330
0
  bfd_hash_allocate (table, sizeof (struct elf_arc_link_hash_entry));
331
0
  if (ret == NULL)
332
0
    return (struct bfd_hash_entry *) ret;
333
334
  /* Call the allocation method of the superclass.  */
335
0
  ret = ((struct elf_arc_link_hash_entry *)
336
0
   _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
337
0
             table, string));
338
0
  if (ret != NULL)
339
0
    {
340
0
      ret->got_ents = NULL;
341
0
    }
342
343
0
  return (struct bfd_hash_entry *) ret;
344
0
}
345
346
/* Destroy an ARC ELF linker hash table.  */
347
static void
348
elf_arc_link_hash_table_free (bfd *obfd)
349
0
{
350
0
  _bfd_elf_link_hash_table_free (obfd);
351
0
}
352
353
/* Create an ARC ELF linker hash table.  */
354
355
static struct bfd_link_hash_table *
356
arc_elf_link_hash_table_create (bfd *abfd)
357
0
{
358
0
  struct elf_arc_link_hash_table *ret;
359
360
0
  ret = (struct elf_arc_link_hash_table *) bfd_zmalloc (sizeof (*ret));
361
0
  if (ret == NULL)
362
0
    return NULL;
363
364
0
  if (!_bfd_elf_link_hash_table_init (&ret->elf, abfd,
365
0
              elf_arc_link_hash_newfunc,
366
0
              sizeof (struct elf_arc_link_hash_entry)))
367
0
    {
368
0
      free (ret);
369
0
      return NULL;
370
0
    }
371
372
0
  ret->elf.root.hash_table_free = elf_arc_link_hash_table_free;
373
374
0
  return &ret->elf.root;
375
0
}
376
377
#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
378
  { BFD_RELOC_##TYPE, R_##TYPE },
379
380
/* Aliases.  */
381
#define BFD_RELOC_ARC_NONE  BFD_RELOC_NONE
382
#define BFD_RELOC_ARC_8   BFD_RELOC_8
383
#define BFD_RELOC_ARC_16  BFD_RELOC_16
384
#define BFD_RELOC_ARC_24  BFD_RELOC_24
385
#define BFD_RELOC_ARC_32  BFD_RELOC_32
386
#define BFD_RELOC_ARC_PC32  BFD_RELOC_32_PCREL
387
#define BFD_RELOC_ARC_PLT32 BFD_RELOC_32_PLT_PCREL
388
#define BFD_RELOC_ARC_GOTOFF  BFD_RELOC_32_GOTOFF
389
#define BFD_RELOC_ARC_GOTPC32 BFD_RELOC_32_GOT_PCREL
390
#define BFD_RELOC_ARC_COPY  BFD_RELOC_COPY
391
#define BFD_RELOC_ARC_GLOB_DAT  BFD_RELOC_GLOB_DAT
392
#define BFD_RELOC_ARC_JMP_SLOT  BFD_RELOC_JMP_SLOT
393
#define BFD_RELOC_ARC_RELATIVE  BFD_RELOC_RELATIVE
394
395
static const struct arc_reloc_map arc_reloc_map[] =
396
{
397
#include "elf/arc-reloc.def"
398
};
399
400
#undef ARC_RELOC_HOWTO
401
402
typedef ATTRIBUTE_UNUSED unsigned (*replace_func) (unsigned, int ATTRIBUTE_UNUSED);
403
404
#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
405
0
  case TYPE: \
406
0
    func = RELOC_FUNCTION; \
407
0
    break;
408
409
static replace_func
410
get_replace_function (bfd *abfd, unsigned int r_type)
411
0
{
412
0
  replace_func func = NULL;
413
414
0
  switch (r_type)
415
0
    {
416
0
      #include "elf/arc-reloc.def"
417
0
    }
418
419
0
  if (func == replace_bits24 && bfd_big_endian (abfd))
420
0
    func = replace_bits24_be;
421
422
0
  return func;
423
0
}
424
#undef ARC_RELOC_HOWTO
425
426
static reloc_howto_type *
427
arc_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
428
         bfd_reloc_code_real_type code)
429
0
{
430
0
  unsigned int i;
431
432
0
  for (i = ARRAY_SIZE (arc_reloc_map); i--;)
433
0
    {
434
0
      if (arc_reloc_map[i].bfd_reloc_val == code)
435
0
  return arc_elf_howto (arc_reloc_map[i].elf_reloc_val);
436
0
    }
437
438
0
  return NULL;
439
0
}
440
441
/* Function to set the ELF flag bits.  */
442
static bool
443
arc_elf_set_private_flags (bfd *abfd, flagword flags)
444
0
{
445
0
  elf_elfheader (abfd)->e_flags = flags;
446
0
  elf_flags_init (abfd) = true;
447
0
  return true;
448
0
}
449
450
/* Print private flags.  */
451
static bool
452
arc_elf_print_private_bfd_data (bfd *abfd, void * ptr)
453
366
{
454
366
  FILE *file = (FILE *) ptr;
455
366
  flagword flags;
456
457
366
  BFD_ASSERT (abfd != NULL && ptr != NULL);
458
459
  /* Print normal ELF private data.  */
460
366
  _bfd_elf_print_private_bfd_data (abfd, ptr);
461
462
366
  flags = elf_elfheader (abfd)->e_flags;
463
366
  fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
464
465
366
  switch (flags & EF_ARC_MACH_MSK)
466
366
    {
467
43
    case EF_ARC_CPU_ARCV2HS : fprintf (file, " -mcpu=ARCv2HS");    break;
468
123
    case EF_ARC_CPU_ARCV2EM : fprintf (file, " -mcpu=ARCv2EM");    break;
469
32
    case E_ARC_MACH_ARC600  : fprintf (file, " -mcpu=ARC600");     break;
470
0
    case E_ARC_MACH_ARC601  : fprintf (file, " -mcpu=ARC601");     break;
471
0
    case E_ARC_MACH_ARC700  : fprintf (file, " -mcpu=ARC700");     break;
472
168
    default:
473
168
      fprintf (file, "-mcpu=unknown");
474
168
      break;
475
366
    }
476
477
366
  switch (flags & EF_ARC_OSABI_MSK)
478
366
    {
479
67
    case E_ARC_OSABI_ORIG : fprintf (file, " (ABI:legacy)"); break;
480
0
    case E_ARC_OSABI_V2   : fprintf (file, " (ABI:v2)");     break;
481
11
    case E_ARC_OSABI_V3   : fprintf (file, " (ABI:v3)");     break;
482
0
    case E_ARC_OSABI_V4   : fprintf (file, " (ABI:v4)");     break;
483
288
    default:
484
288
      fprintf (file, " (ABI:unknown)");
485
288
      break;
486
366
    }
487
488
366
  fputc ('\n', file);
489
366
  return true;
490
366
}
491
492
/* Copy backend specific data from one object module to another.  */
493
494
static bool
495
arc_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
496
5
{
497
5
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
498
0
    return true;
499
500
5
  BFD_ASSERT (!elf_flags_init (obfd)
501
5
        || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);
502
503
5
  elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
504
5
  elf_flags_init (obfd) = true;
505
506
  /* Copy object attributes.  */
507
5
  _bfd_elf_copy_obj_attributes (ibfd, obfd);
508
509
5
  return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
510
5
}
511
512
static reloc_howto_type *
513
bfd_elf32_bfd_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED,
514
         const char *r_name)
515
0
{
516
0
  unsigned int i;
517
518
0
  for (i = 0; i < ARRAY_SIZE (elf_arc_howto_table); i++)
519
0
    if (elf_arc_howto_table[i].name != NULL
520
0
  && strcasecmp (elf_arc_howto_table[i].name, r_name) == 0)
521
0
      return arc_elf_howto (i);
522
523
0
  return NULL;
524
0
}
525
526
/* Set the howto pointer for an ARC ELF reloc.  */
527
528
static bool
529
arc_info_to_howto_rel (bfd * abfd,
530
           arelent * cache_ptr,
531
           Elf_Internal_Rela * dst)
532
349
{
533
349
  unsigned int r_type;
534
535
349
  r_type = ELF32_R_TYPE (dst->r_info);
536
349
  if (r_type >= (unsigned int) R_ARC_max)
537
5
    {
538
      /* xgettext:c-format */
539
5
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
540
5
        abfd, r_type);
541
5
      bfd_set_error (bfd_error_bad_value);
542
5
      return false;
543
5
    }
544
545
344
  cache_ptr->howto = arc_elf_howto (r_type);
546
344
  return true;
547
349
}
548
549
/* Extract CPU features from an NTBS.  */
550
551
static unsigned
552
arc_extract_features (const char *p)
553
0
{
554
0
  unsigned i, r = 0;
555
556
0
  if (!p)
557
0
    return 0;
558
559
0
  for (i = 0; i < ARRAY_SIZE (bfd_feature_list); i++)
560
0
    {
561
0
      const char *t = strstr (p, bfd_feature_list[i].attr);
562
0
      unsigned l = strlen (bfd_feature_list[i].attr);
563
0
      if ((t != NULL)
564
0
    && (t[l] == ','
565
0
        || t[l] == '\0'))
566
0
  r |= bfd_feature_list[i].feature;
567
0
    }
568
569
0
  return r;
570
0
}
571
572
/* Concatenate two strings.  s1 can be NULL but not
573
   s2.  */
574
575
static char *
576
arc_stralloc (char * s1, const char * s2)
577
0
{
578
0
  char *p;
579
580
  /* Only s1 can be null.  */
581
0
  BFD_ASSERT (s2);
582
583
0
  p = s1 ? concat (s1, ",", s2, NULL) : (char *)s2;
584
585
0
  return p;
586
0
}
587
588
/* Merge ARC object attributes from IBFD into OBFD.  Raise an error if
589
   there are conflicting attributes.  */
590
591
static bool
592
arc_elf_merge_attributes (bfd *ibfd, struct bfd_link_info *info)
593
0
{
594
0
  bfd *obfd = info->output_bfd;
595
0
  obj_attribute *in_attr;
596
0
  obj_attribute *out_attr;
597
0
  int i;
598
0
  bool result = true;
599
0
  const char *sec_name = get_elf_backend_data (ibfd)->obj_attrs_section;
600
0
  char *tagname = NULL;
601
602
  /* Skip the linker stubs file.  This preserves previous behavior
603
     of accepting unknown attributes in the first input file - but
604
     is that a bug?  */
605
0
  if (ibfd->flags & BFD_LINKER_CREATED)
606
0
    return true;
607
608
  /* Skip any input that hasn't attribute section.
609
     This enables to link object files without attribute section with
610
     any others.  */
611
0
  if (bfd_get_section_by_name (ibfd, sec_name) == NULL)
612
0
    return true;
613
614
0
  if (!elf_known_obj_attributes_proc (obfd)[0].i)
615
0
    {
616
      /* This is the first object.  Copy the attributes.  */
617
0
      _bfd_elf_copy_obj_attributes (ibfd, obfd);
618
619
0
      out_attr = elf_known_obj_attributes_proc (obfd);
620
621
      /* Use the Tag_null value to indicate the attributes have been
622
   initialized.  */
623
0
      out_attr[0].i = 1;
624
625
0
      return true;
626
0
    }
627
628
0
  in_attr = elf_known_obj_attributes_proc (ibfd);
629
0
  out_attr = elf_known_obj_attributes_proc (obfd);
630
631
0
  for (i = LEAST_KNOWN_OBJ_ATTRIBUTE; i < NUM_KNOWN_OBJ_ATTRIBUTES; i++)
632
0
    {
633
      /* Merge this attribute with existing attributes.  */
634
0
      switch (i)
635
0
  {
636
0
  case Tag_ARC_PCS_config:
637
0
    if (out_attr[i].i == 0)
638
0
      out_attr[i].i = in_attr[i].i;
639
0
    else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i)
640
0
      {
641
0
        const char *tagval[] = { "Absent", "Bare-metal/mwdt",
642
0
          "Bare-metal/newlib", "Linux/uclibc",
643
0
          "Linux/glibc" };
644
0
        BFD_ASSERT (in_attr[i].i < 5);
645
0
        BFD_ASSERT (out_attr[i].i < 5);
646
        /* It's sometimes ok to mix different configs, so this is only
647
     a warning.  */
648
0
        _bfd_error_handler
649
0
    (_("warning: %pB: conflicting platform configuration "
650
0
       "%s with %s"), ibfd,
651
0
     tagval[in_attr[i].i],
652
0
     tagval[out_attr[i].i]);
653
0
      }
654
0
    break;
655
656
0
  case Tag_ARC_CPU_base:
657
0
    if (out_attr[i].i == 0)
658
0
      out_attr[i].i = in_attr[i].i;
659
0
    else if (in_attr[i].i != 0 && out_attr[i].i != in_attr[i].i
660
0
       && ((out_attr[i].i + in_attr[i].i) < 6))
661
0
      {
662
0
        const char *tagval[] = { "Absent", "ARC6xx", "ARC7xx",
663
0
          "ARCEM", "ARCHS" };
664
0
        BFD_ASSERT (in_attr[i].i < 5);
665
0
        BFD_ASSERT (out_attr[i].i < 5);
666
        /* We cannot mix code for different CPUs.  */
667
0
        _bfd_error_handler
668
0
    (_("error: %pB: unable to merge CPU base attributes "
669
0
       "%s with %s"),
670
0
     obfd,
671
0
     tagval[in_attr[i].i],
672
0
     tagval[out_attr[i].i]);
673
0
        result = false;
674
0
        break;
675
0
      }
676
0
    else
677
0
      {
678
        /* The CPUs may be different, check if we can still mix
679
     the objects against the output choosen CPU.  */
680
0
        unsigned in_feature = 0;
681
0
        unsigned out_feature = 0;
682
0
        char *p1 = in_attr[Tag_ARC_ISA_config].s;
683
0
        char *p2 = out_attr[Tag_ARC_ISA_config].s;
684
0
        unsigned j;
685
0
        unsigned cpu_out;
686
0
        unsigned opcode_map[] = {0, ARC_OPCODE_ARC600, ARC_OPCODE_ARC700,
687
0
               ARC_OPCODE_ARCv2EM, ARC_OPCODE_ARCv2HS};
688
689
0
        BFD_ASSERT (in_attr[i].i < (sizeof (opcode_map)
690
0
            / sizeof (unsigned)));
691
0
        BFD_ASSERT (out_attr[i].i < (sizeof (opcode_map)
692
0
             / sizeof (unsigned)));
693
0
        cpu_out = opcode_map[out_attr[i].i];
694
695
0
        in_feature = arc_extract_features (p1);
696
0
        out_feature = arc_extract_features (p2);
697
698
        /* First, check if a feature is compatible with the
699
     output object chosen CPU.  */
700
0
        for (j = 0; j < ARRAY_SIZE (bfd_feature_list); j++)
701
0
    if (((in_feature | out_feature) & bfd_feature_list[j].feature)
702
0
        && (!(cpu_out & bfd_feature_list[j].cpus)))
703
0
      {
704
0
        _bfd_error_handler
705
0
          (_("error: %pB: unable to merge ISA extension attributes "
706
0
       "%s"),
707
0
           obfd, bfd_feature_list[j].name);
708
0
        result = false;
709
0
        break;
710
0
      }
711
        /* Second, if we have compatible features with the
712
     chosen CPU, check if they are compatible among
713
     them.  */
714
0
        for (j = 0; j < ARRAY_SIZE (bfd_conflict_list); j++)
715
0
    if (((in_feature | out_feature) & bfd_conflict_list[j])
716
0
        == bfd_conflict_list[j])
717
0
      {
718
0
        unsigned k;
719
0
        for (k = 0; k < ARRAY_SIZE (bfd_feature_list); k++)
720
0
          {
721
0
      if (in_feature &  bfd_feature_list[k].feature
722
0
          & bfd_conflict_list[j])
723
0
        p1 = (char *) bfd_feature_list[k].name;
724
0
      if (out_feature &  bfd_feature_list[k].feature
725
0
          & bfd_conflict_list[j])
726
0
        p2 = (char *) bfd_feature_list[k].name;
727
0
          }
728
0
        _bfd_error_handler
729
0
          (_("error: %pB: conflicting ISA extension attributes "
730
0
       "%s with %s"),
731
0
           obfd, p1, p2);
732
0
        result = false;
733
0
        break;
734
0
      }
735
        /* Everithing is alright.  */
736
0
        out_feature |= in_feature;
737
0
        p1 = NULL;
738
0
        for (j = 0; j < ARRAY_SIZE (bfd_feature_list); j++)
739
0
    if (out_feature & bfd_feature_list[j].feature)
740
0
      p1 = arc_stralloc (p1, bfd_feature_list[j].attr);
741
0
        if (p1)
742
0
    out_attr[Tag_ARC_ISA_config].s =
743
0
      _bfd_elf_attr_strdup (obfd, p1);
744
0
      }
745
    /* Fall through.  */
746
0
  case Tag_ARC_CPU_variation:
747
0
  case Tag_ARC_ISA_mpy_option:
748
0
  case Tag_ARC_ABI_osver:
749
    /* Use the largest value specified.  */
750
0
    if (in_attr[i].i > out_attr[i].i)
751
0
      out_attr[i].i = in_attr[i].i;
752
0
    break;
753
754
    /* The CPU name is given by the vendor, just choose an
755
       existing one if missing or different.  There are no fail
756
       criteria if they different or both missing.  */
757
0
  case Tag_ARC_CPU_name:
758
0
    if (!out_attr[i].s && in_attr[i].s)
759
0
      out_attr[i].s = _bfd_elf_attr_strdup (obfd, in_attr[i].s);
760
0
    break;
761
762
0
  case Tag_ARC_ABI_rf16:
763
0
    if (out_attr[i].i == 0)
764
0
      out_attr[i].i = in_attr[i].i;
765
0
    else if (out_attr[i].i != in_attr[i].i)
766
0
      {
767
        /* We cannot mix code with rf16 and without.  */
768
0
        _bfd_error_handler
769
0
    (_("error: %pB: cannot mix rf16 with full register set %pB"),
770
0
     obfd, ibfd);
771
0
        result = false;
772
0
      }
773
0
    break;
774
775
0
  case Tag_ARC_ABI_pic:
776
0
    tagname = "PIC";
777
    /* fall through */
778
0
  case Tag_ARC_ABI_sda:
779
0
    if (!tagname)
780
0
      tagname = "SDA";
781
    /* fall through */
782
0
  case Tag_ARC_ABI_tls:
783
0
    {
784
0
      const char *tagval[] = { "Absent", "MWDT", "GNU" };
785
786
0
      if (!tagname)
787
0
        tagname = "TLS";
788
789
0
      BFD_ASSERT (in_attr[i].i < 3);
790
0
      BFD_ASSERT (out_attr[i].i < 3);
791
0
      if (out_attr[i].i == 0)
792
0
        out_attr[i].i = in_attr[i].i;
793
0
      else if (out_attr[i].i != 0 && in_attr[i].i != 0
794
0
    && out_attr[i].i != in_attr[i].i)
795
0
        {
796
0
    _bfd_error_handler
797
0
      (_("error: %pB: conflicting attributes %s: %s with %s"),
798
0
       obfd, tagname,
799
0
       tagval[in_attr[i].i],
800
0
       tagval[out_attr[i].i]);
801
0
    result = false;
802
0
        }
803
0
      tagname = NULL;
804
0
      break;
805
0
    }
806
807
0
  case Tag_ARC_ABI_double_size:
808
0
    tagname = "Double size";
809
    /* fall through */
810
0
  case Tag_ARC_ABI_enumsize:
811
0
    if (!tagname)
812
0
      tagname = "Enum size";
813
    /* fall through */
814
0
  case Tag_ARC_ABI_exceptions:
815
0
    if (!tagname)
816
0
      tagname = "ABI exceptions";
817
818
0
    if (out_attr[i].i == 0)
819
0
      out_attr[i].i = in_attr[i].i;
820
0
    else if (out_attr[i].i != 0 && in_attr[i].i != 0
821
0
        && out_attr[i].i != in_attr[i].i)
822
0
      {
823
0
        _bfd_error_handler
824
0
    (_("error: %pB: conflicting attributes %s"),
825
0
     obfd, tagname);
826
0
        result = false;
827
0
      }
828
0
    break;
829
830
0
  case Tag_ARC_ISA_apex:
831
0
    break; /* Do nothing for APEX attributes.  */
832
833
0
  case Tag_ARC_ISA_config:
834
    /* It is handled in Tag_ARC_CPU_base.  */
835
0
    break;
836
837
0
  case Tag_ARC_ATR_version:
838
0
    if (out_attr[i].i == 0)
839
0
      out_attr[i].i = in_attr[i].i;
840
0
    break;
841
842
0
  default:
843
0
    result
844
0
      = result && _bfd_elf_merge_unknown_attribute_low (ibfd, obfd, i);
845
0
  }
846
847
      /* If out_attr was copied from in_attr then it won't have a type yet.  */
848
0
      if (in_attr[i].type && !out_attr[i].type)
849
0
  out_attr[i].type = in_attr[i].type;
850
0
    }
851
852
  /* Merge Tag_compatibility attributes and any common GNU ones.  */
853
0
  if (!_bfd_elf_merge_object_attributes (ibfd, info))
854
0
    return false;
855
856
  /* Check for any attributes not known on ARC.  */
857
0
  result &= _bfd_elf_merge_unknown_attribute_list (ibfd, obfd);
858
859
0
  return result;
860
0
}
861
862
/* Merge backend specific data from an object file to the output
863
   object file when linking.  */
864
865
static bool
866
arc_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
867
0
{
868
0
  bfd *obfd = info->output_bfd;
869
0
  unsigned short mach_ibfd;
870
0
  static unsigned short mach_obfd = EM_NONE;
871
0
  flagword out_flags;
872
0
  flagword in_flags;
873
0
  asection *sec;
874
875
   /* Check if we have the same endianess.  */
876
0
  if (! _bfd_generic_verify_endian_match (ibfd, info))
877
0
    return false;
878
879
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
880
0
    return true;
881
882
  /* Collect ELF flags.  */
883
0
  in_flags = elf_elfheader (ibfd)->e_flags & EF_ARC_MACH_MSK;
884
0
  out_flags = elf_elfheader (obfd)->e_flags & EF_ARC_MACH_MSK;
885
886
0
  if (!elf_flags_init (obfd)) /* First call, no flags set.  */
887
0
    {
888
0
      elf_flags_init (obfd) = true;
889
0
      out_flags = in_flags;
890
0
    }
891
892
0
  if (!arc_elf_merge_attributes (ibfd, info))
893
0
    return false;
894
895
  /* Check to see if the input BFD actually contains any sections.  Do
896
     not short-circuit dynamic objects; their section list may be
897
     emptied by elf_link_add_object_symbols.  */
898
0
  if (!(ibfd->flags & DYNAMIC))
899
0
    {
900
0
      bool null_input_bfd = true;
901
0
      bool only_data_sections = true;
902
903
0
      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
904
0
  {
905
0
    if ((bfd_section_flags (sec)
906
0
         & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
907
0
        == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
908
0
      only_data_sections = false;
909
910
0
    null_input_bfd = false;
911
0
  }
912
913
0
      if (null_input_bfd || only_data_sections)
914
0
  return true;
915
0
    }
916
917
  /* Complain about various flag/architecture mismatches.  */
918
0
  mach_ibfd = elf_elfheader (ibfd)->e_machine;
919
0
  if (mach_obfd == EM_NONE)
920
0
    {
921
0
      mach_obfd = mach_ibfd;
922
0
    }
923
0
  else
924
0
    {
925
0
      if (mach_ibfd != mach_obfd)
926
0
  {
927
    /* xgettext:c-format */
928
0
    _bfd_error_handler (_("error: attempting to link %pB "
929
0
        "with a binary %pB of different architecture"),
930
0
            ibfd, obfd);
931
0
    return false;
932
0
  }
933
0
      else if ((in_flags != out_flags)
934
         /* If we have object attributes, then we already
935
      checked the objects compatibility, skip it.  */
936
0
         && !bfd_elf_get_obj_attr_int (ibfd, OBJ_ATTR_PROC,
937
0
               Tag_ARC_CPU_base))
938
0
  {
939
0
    if (in_flags && out_flags)
940
0
      {
941
        /* Warn if different flags.  */
942
0
        _bfd_error_handler
943
    /* xgettext:c-format */
944
0
    (_("%pB: uses different e_flags (%#x) fields than "
945
0
       "previous modules (%#x)"),
946
0
     ibfd, in_flags, out_flags);
947
0
        return false;
948
0
      }
949
    /* MWDT doesnt set the eflags hence make sure we choose the
950
       eflags set by gcc.  */
951
0
    in_flags = in_flags > out_flags ? in_flags : out_flags;
952
0
  }
953
0
      else
954
0
  {
955
    /* Everything is correct; don't change the output flags.  */
956
0
    in_flags = out_flags;
957
0
  }
958
0
    }
959
960
  /* Update the flags.  */
961
0
  elf_elfheader (obfd)->e_flags = in_flags;
962
963
0
  if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
964
0
    {
965
0
      return bfd_set_arch_mach (obfd, bfd_arch_arc, bfd_get_mach (ibfd));
966
0
    }
967
968
0
  return true;
969
0
}
970
971
/* Return a best guess for the machine number based on the attributes.  */
972
973
static unsigned int
974
bfd_arc_get_mach_from_attributes (bfd * abfd)
975
242
{
976
242
  int arch = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC, Tag_ARC_CPU_base);
977
242
  unsigned e_machine = elf_elfheader (abfd)->e_machine;
978
979
242
  switch (arch)
980
242
    {
981
0
    case TAG_CPU_ARC6xx:
982
0
      return bfd_mach_arc_arc600;
983
0
    case TAG_CPU_ARC7xx:
984
0
      return bfd_mach_arc_arc700;
985
0
    case TAG_CPU_ARCEM:
986
0
    case TAG_CPU_ARCHS:
987
0
      return bfd_mach_arc_arcv2;
988
242
    default:
989
242
      break;
990
242
    }
991
242
  return (e_machine == EM_ARC_COMPACT)
992
242
    ? bfd_mach_arc_arc700 : bfd_mach_arc_arcv2;
993
242
}
994
995
/* Set the right machine number for an ARC ELF file.  */
996
static bool
997
arc_elf_object_p (bfd * abfd)
998
451
{
999
  /* Make sure this is initialised, or you'll have the potential of passing
1000
     garbage---or misleading values---into the call to
1001
     bfd_default_set_arch_mach ().  */
1002
451
  unsigned int    mach = bfd_mach_arc_arc700;
1003
451
  unsigned long   arch = elf_elfheader (abfd)->e_flags & EF_ARC_MACH_MSK;
1004
451
  unsigned    e_machine = elf_elfheader (abfd)->e_machine;
1005
1006
451
  if (e_machine == EM_ARC_COMPACT || e_machine == EM_ARC_COMPACT2)
1007
451
    {
1008
451
      switch (arch)
1009
451
  {
1010
38
  case E_ARC_MACH_ARC600:
1011
38
    mach = bfd_mach_arc_arc600;
1012
38
    break;
1013
4
  case E_ARC_MACH_ARC601:
1014
4
    mach = bfd_mach_arc_arc601;
1015
4
    break;
1016
1
  case E_ARC_MACH_ARC700:
1017
1
    mach = bfd_mach_arc_arc700;
1018
1
    break;
1019
43
  case EF_ARC_CPU_ARCV2HS:
1020
166
  case EF_ARC_CPU_ARCV2EM:
1021
166
    mach = bfd_mach_arc_arcv2;
1022
166
    break;
1023
242
  default:
1024
242
    mach = bfd_arc_get_mach_from_attributes (abfd);
1025
242
    break;
1026
451
  }
1027
451
    }
1028
0
  else
1029
0
    {
1030
0
      if (e_machine == EM_ARC)
1031
0
  {
1032
0
    _bfd_error_handler
1033
0
      (_("error: the ARC4 architecture is no longer supported"));
1034
0
    return false;
1035
0
  }
1036
0
      else
1037
0
  {
1038
0
    _bfd_error_handler
1039
0
      (_("warning: unset or old architecture flags; "
1040
0
         "use default machine"));
1041
0
  }
1042
0
    }
1043
1044
451
  return bfd_default_set_arch_mach (abfd, bfd_arch_arc, mach);
1045
451
}
1046
1047
/* The final processing done just before writing out an ARC ELF object file.
1048
   This gets the ARC architecture right based on the machine number.  */
1049
1050
static bool
1051
arc_elf_final_write_processing (bfd *abfd)
1052
5
{
1053
5
  unsigned long emf;
1054
1055
5
  switch (bfd_get_mach (abfd))
1056
5
    {
1057
1
    case bfd_mach_arc_arcv2:
1058
1
      emf = EM_ARC_COMPACT2;
1059
1
      break;
1060
4
    default:
1061
4
      emf = EM_ARC_COMPACT;
1062
4
      break;
1063
5
    }
1064
1065
5
  elf_elfheader (abfd)->e_machine = emf;
1066
1067
  /* Record whatever is the current syscall ABI version.  */
1068
5
  int osver = bfd_elf_get_obj_attr_int (abfd, OBJ_ATTR_PROC,
1069
5
          Tag_ARC_ABI_osver);
1070
5
  flagword e_flags = elf_elfheader (abfd)->e_flags;
1071
5
  if (osver)
1072
0
    e_flags = (e_flags & ~EF_ARC_OSABI_MSK) | ((osver & 0x0f) << 8);
1073
5
  else if ((e_flags & EF_ARC_OSABI_MSK) == 0)
1074
3
    e_flags |= E_ARC_OSABI_V3;
1075
1076
5
  elf_elfheader (abfd)->e_flags = e_flags;
1077
5
  return _bfd_elf_final_write_processing (abfd);
1078
5
}
1079
1080
#ifdef ARC_ENABLE_DEBUG
1081
#define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
1082
1083
static void
1084
debug_arc_reloc (struct arc_relocation_data reloc_data)
1085
{
1086
  ARC_DEBUG ("Reloc type=%s, should_relocate = %s\n",
1087
       reloc_data.howto->name,
1088
       reloc_data.should_relocate ? "true" : "false");
1089
  ARC_DEBUG ("  offset = 0x%x, addend = 0x%x\n",
1090
       (unsigned int) reloc_data.reloc_offset,
1091
       (unsigned int) reloc_data.reloc_addend);
1092
  ARC_DEBUG (" Symbol:\n");
1093
  ARC_DEBUG ("  value = 0x%08x\n",
1094
       (unsigned int) reloc_data.sym_value);
1095
  if (reloc_data.sym_section != NULL)
1096
    {
1097
      ARC_DEBUG (" Symbol Section:\n");
1098
      ARC_DEBUG ("  section name = %s, output_offset 0x%08x",
1099
     reloc_data.sym_section->name,
1100
     (unsigned int) reloc_data.sym_section->output_offset);
1101
      if (reloc_data.sym_section->output_section != NULL)
1102
  ARC_DEBUG (", output_section->vma = 0x%08x",
1103
       ((unsigned int) reloc_data.sym_section->output_section->vma));
1104
      ARC_DEBUG ("\n");
1105
      if (reloc_data.sym_section->owner
1106
    && reloc_data.sym_section->owner->filename)
1107
  ARC_DEBUG ("  file: %s\n", reloc_data.sym_section->owner->filename);
1108
    }
1109
  else
1110
    {
1111
      ARC_DEBUG ("  symbol section is NULL\n");
1112
    }
1113
1114
  ARC_DEBUG (" Input_section:\n");
1115
  if (reloc_data.input_section != NULL)
1116
    {
1117
      ARC_DEBUG ("  section name = %s, output_offset 0x%08x, output_section->vma = 0x%08x\n",
1118
     reloc_data.input_section->name,
1119
     (unsigned int) reloc_data.input_section->output_offset,
1120
     (unsigned int) reloc_data.input_section->output_section->vma);
1121
      ARC_DEBUG ("  changed_address = 0x%08x\n",
1122
     (unsigned int) (reloc_data.input_section->output_section->vma
1123
         + reloc_data.input_section->output_offset
1124
         + reloc_data.reloc_offset));
1125
      ARC_DEBUG ("  file: %s\n", reloc_data.input_section->owner->filename);
1126
    }
1127
  else
1128
    {
1129
      ARC_DEBUG ("  input section is NULL\n");
1130
    }
1131
}
1132
#else
1133
#define DEBUG_ARC_RELOC(A)
1134
#endif /* ARC_ENABLE_DEBUG */
1135
1136
static bfd_vma
1137
middle_endian_convert (bfd_vma insn, bool do_it)
1138
0
{
1139
0
  if (do_it)
1140
0
    {
1141
0
      insn
1142
0
  = ((insn & 0xffff0000) >> 16)
1143
0
    | ((insn & 0xffff) << 16);
1144
0
    }
1145
0
  return insn;
1146
0
}
1147
1148
/* This function is called for relocations that are otherwise marked as NOT
1149
   requiring overflow checks.  In here we perform non-standard checks of
1150
   the relocation value.  */
1151
1152
static inline bfd_reloc_status_type
1153
arc_special_overflow_checks (const struct arc_relocation_data reloc_data,
1154
           bfd_signed_vma relocation,
1155
           struct bfd_link_info *info ATTRIBUTE_UNUSED)
1156
0
{
1157
0
  switch (reloc_data.howto->type)
1158
0
    {
1159
0
    case R_ARC_NPS_CMEM16:
1160
0
      if (((relocation >> 16) & 0xffff) != NPS_CMEM_HIGH_VALUE)
1161
0
  {
1162
0
    if (reloc_data.reloc_addend == 0)
1163
0
      _bfd_error_handler
1164
        /* xgettext:c-format */
1165
0
        (_("%pB(%pA+%#" PRIx64 "): CMEM relocation to `%s' is invalid, "
1166
0
     "16 MSB should be %#x (value is %#" PRIx64 ")"),
1167
0
         reloc_data.input_section->owner,
1168
0
         reloc_data.input_section,
1169
0
         (uint64_t) reloc_data.reloc_offset,
1170
0
         reloc_data.symbol_name,
1171
0
         NPS_CMEM_HIGH_VALUE,
1172
0
         (uint64_t) relocation);
1173
0
    else
1174
0
      _bfd_error_handler
1175
        /* xgettext:c-format */
1176
0
        (_("%pB(%pA+%#" PRIx64 "): CMEM relocation to `%s+%#" PRIx64
1177
0
     "' is invalid, 16 MSB should be %#x (value is %#" PRIx64 ")"),
1178
0
         reloc_data.input_section->owner,
1179
0
         reloc_data.input_section,
1180
0
         (uint64_t) reloc_data.reloc_offset,
1181
0
         reloc_data.symbol_name,
1182
0
         (uint64_t) reloc_data.reloc_addend,
1183
0
         NPS_CMEM_HIGH_VALUE,
1184
0
         (uint64_t) relocation);
1185
0
    return bfd_reloc_overflow;
1186
0
  }
1187
0
      break;
1188
1189
0
    default:
1190
0
      break;
1191
0
    }
1192
1193
0
  return bfd_reloc_ok;
1194
0
}
1195
1196
#define ME(reloc) (reloc)
1197
1198
0
#define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
1199
0
          && (!bfd_big_endian (BFD)))
1200
1201
#define S ((bfd_signed_vma) (reloc_data.sym_value     \
1202
     + (reloc_data.sym_section->output_section != NULL ?    \
1203
        (reloc_data.sym_section->output_offset      \
1204
         + reloc_data.sym_section->output_section->vma) : 0)))
1205
#define L ((bfd_signed_vma) (reloc_data.sym_value     \
1206
     + (reloc_data.sym_section->output_section != NULL ?    \
1207
        (reloc_data.sym_section->output_offset      \
1208
        + reloc_data.sym_section->output_section->vma) : 0)))
1209
#define A (reloc_data.reloc_addend)
1210
#define B (0)
1211
#define G (reloc_data.got_offset_value)
1212
#define GOT (reloc_data.got_symbol_vma)
1213
#define GOT_BEGIN (htab->sgot->output_section->vma)
1214
1215
#define MES (0)
1216
  /* P: relative offset to PCL The offset should be to the
1217
    current location aligned to 32 bits.  */
1218
#define P ((bfd_signed_vma) (           \
1219
     (                \
1220
      (reloc_data.input_section->output_section != NULL ?   \
1221
       reloc_data.input_section->output_section->vma : 0)   \
1222
      + reloc_data.input_section->output_offset     \
1223
      + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0)))  \
1224
     & ~0x3))
1225
#define PDATA ((bfd_signed_vma) ( \
1226
      (reloc_data.input_section->output_section->vma \
1227
       + reloc_data.input_section->output_offset \
1228
       + (reloc_data.reloc_offset))))
1229
#define SECTSTART (bfd_signed_vma) (reloc_data.sym_section->output_section->vma \
1230
            + reloc_data.sym_section->output_offset)
1231
#define FINAL_SECTSTART \
1232
  (bfd_signed_vma) (reloc_data.sym_section->output_section->vma)
1233
#define JLI (bfd_signed_vma) (reloc_data.sym_section->output_section->vma)
1234
#define _SDA_BASE_ (bfd_signed_vma) (reloc_data.sdata_begin_symbol_vma)
1235
#define TLS_REL (bfd_signed_vma)(tls_sec->output_section->vma)
1236
#define TLS_TBSS (align_power (TCB_SIZE, tls_sec->alignment_power))
1237
1238
#define none (0)
1239
1240
#ifdef ARC_ENABLE_DEBUG
1241
#define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA, TYPE)      \
1242
  do                  \
1243
    {                 \
1244
      asection *sym_section = reloc_data.sym_section;     \
1245
      asection *input_section = reloc_data.input_section;   \
1246
      ARC_DEBUG ("RELOC_TYPE = " TYPE "\n");        \
1247
      ARC_DEBUG ("FORMULA = " FORMULA "\n");        \
1248
      ARC_DEBUG ("S = %#lx\n", S);          \
1249
      ARC_DEBUG ("A = %#lx\n", A);          \
1250
      ARC_DEBUG ("L = %lx\n", L);         \
1251
      if (sym_section->output_section != NULL)        \
1252
  ARC_DEBUG ("symbol_section->vma = %#lx\n",      \
1253
       sym_section->output_section->vma     \
1254
       + sym_section->output_offset);     \
1255
      else                \
1256
  ARC_DEBUG ("symbol_section->vma = NULL\n");     \
1257
      if (input_section->output_section != NULL)      \
1258
  ARC_DEBUG ("input_section->vma = %#lx\n",     \
1259
       input_section->output_section->vma     \
1260
       + input_section->output_offset);     \
1261
      else                \
1262
  ARC_DEBUG ("input_section->vma = NULL\n");      \
1263
      ARC_DEBUG ("PCL = %#lx\n", P);          \
1264
      ARC_DEBUG ("P = %#lx\n", P);          \
1265
      ARC_DEBUG ("G = %#lx\n", G);          \
1266
      ARC_DEBUG ("SDA_OFFSET = %#lx\n", _SDA_BASE_);      \
1267
      ARC_DEBUG ("SDA_SET = %d\n", reloc_data.sdata_begin_symbol_vma_set); \
1268
      ARC_DEBUG ("GOT_OFFSET = %#lx\n", GOT);       \
1269
      ARC_DEBUG ("relocation = %#08lx\n", relocation);      \
1270
      ARC_DEBUG ("before = %#08x\n", (unsigned) insn);      \
1271
      ARC_DEBUG ("data   = %08x (%u) (%d)\n", (unsigned) relocation,  \
1272
     (unsigned) relocation, (int) relocation);    \
1273
    }                 \
1274
  while (0)
1275
1276
#define PRINT_DEBUG_RELOC_INFO_AFTER        \
1277
  do                \
1278
    {               \
1279
      ARC_DEBUG ("after  = 0x%08x\n", (unsigned int) insn); \
1280
    }               \
1281
  while (0)
1282
1283
#else
1284
1285
#define PRINT_DEBUG_RELOC_INFO_BEFORE(...)
1286
#define PRINT_DEBUG_RELOC_INFO_AFTER
1287
1288
#endif /* ARC_ENABLE_DEBUG */
1289
1290
#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
1291
0
  case R_##TYPE:             \
1292
0
    {                 \
1293
0
      bfd_signed_vma bitsize ATTRIBUTE_UNUSED = BITSIZE;    \
1294
0
      relocation = FORMULA  ;           \
1295
0
      PRINT_DEBUG_RELOC_INFO_BEFORE (#FORMULA, #TYPE);      \
1296
0
      insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
1297
0
      insn = (* get_replace_function (abfd, TYPE)) (insn, relocation);  \
1298
0
      insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
1299
0
      PRINT_DEBUG_RELOC_INFO_AFTER;         \
1300
0
    }                 \
1301
0
    break;
1302
1303
static bfd_reloc_status_type
1304
arc_do_relocation (bfd_byte * contents,
1305
       struct arc_relocation_data reloc_data,
1306
       struct bfd_link_info *info)
1307
0
{
1308
0
  bfd_signed_vma relocation = 0;
1309
0
  bfd_vma insn;
1310
0
  bfd_vma orig_insn ATTRIBUTE_UNUSED;
1311
0
  bfd * abfd = reloc_data.input_section->owner;
1312
0
  struct elf_link_hash_table *htab ATTRIBUTE_UNUSED = elf_hash_table (info);
1313
0
  bfd_reloc_status_type flag;
1314
0
  asection *tls_sec = htab->tls_sec;
1315
1316
0
  if (!reloc_data.should_relocate)
1317
0
    return bfd_reloc_ok;
1318
1319
0
  switch (bfd_get_reloc_size (reloc_data.howto))
1320
0
    {
1321
0
    case 4:
1322
0
      insn = arc_bfd_get_32 (abfd,
1323
0
           contents + reloc_data.reloc_offset,
1324
0
           reloc_data.input_section);
1325
0
      break;
1326
0
    case 2:
1327
0
      insn = arc_bfd_get_16 (abfd,
1328
0
           contents + reloc_data.reloc_offset,
1329
0
           reloc_data.input_section);
1330
0
      break;
1331
0
    case 1:
1332
0
      insn = arc_bfd_get_8 (abfd,
1333
0
          contents + reloc_data.reloc_offset,
1334
0
          reloc_data.input_section);
1335
0
      break;
1336
0
    default:
1337
0
      insn = 0;
1338
0
      BFD_ASSERT (0);
1339
0
      break;
1340
0
    }
1341
1342
0
  orig_insn = insn;
1343
1344
  /* If we resolve a TLS relocation, make sure we do have a valid TLS
1345
     section.  */
1346
0
  switch (reloc_data.howto->type)
1347
0
    {
1348
0
    case R_ARC_TLS_LE_32:
1349
0
      if (tls_sec == NULL)
1350
0
  return bfd_reloc_notsupported;
1351
0
      break;
1352
1353
0
    default:
1354
0
      break;
1355
0
    }
1356
1357
1358
0
  switch (reloc_data.howto->type)
1359
0
    {
1360
0
#include "elf/arc-reloc.def"
1361
1362
0
    default:
1363
0
      BFD_ASSERT (0);
1364
0
      break;
1365
0
    }
1366
1367
  /* Check for relocation overflow.  */
1368
0
  if (reloc_data.howto->complain_on_overflow != complain_overflow_dont)
1369
0
    flag = bfd_check_overflow (reloc_data.howto->complain_on_overflow,
1370
0
             reloc_data.howto->bitsize,
1371
0
             reloc_data.howto->rightshift,
1372
0
             bfd_arch_bits_per_address (abfd),
1373
0
             relocation);
1374
0
  else
1375
0
    flag = arc_special_overflow_checks (reloc_data, relocation, info);
1376
1377
0
  if (flag != bfd_reloc_ok)
1378
0
    {
1379
0
      ARC_DEBUG ("Relocation overflows !\n");
1380
0
      DEBUG_ARC_RELOC (reloc_data);
1381
0
      ARC_DEBUG ("Relocation value = signed -> %d, unsigned -> %u"
1382
0
     ", hex -> (0x%08x)\n",
1383
0
    (int) relocation, (unsigned) relocation, (int) relocation);
1384
1385
0
      return flag;
1386
0
    }
1387
1388
  /* Write updated instruction back to memory.  */
1389
0
  switch (bfd_get_reloc_size (reloc_data.howto))
1390
0
    {
1391
0
    case 4:
1392
0
      arc_bfd_put_32 (abfd, insn,
1393
0
          contents + reloc_data.reloc_offset,
1394
0
          reloc_data.input_section);
1395
0
      break;
1396
0
    case 2:
1397
0
  arc_bfd_put_16 (abfd, insn,
1398
0
      contents + reloc_data.reloc_offset,
1399
0
      reloc_data.input_section);
1400
0
  break;
1401
0
    case 1:
1402
0
      arc_bfd_put_8 (abfd, insn,
1403
0
         contents + reloc_data.reloc_offset,
1404
0
         reloc_data.input_section);
1405
0
      break;
1406
0
    default:
1407
0
      ARC_DEBUG ("size = %d\n", reloc_data.howto->size);
1408
0
      BFD_ASSERT (0);
1409
0
      break;
1410
0
    }
1411
1412
0
  return bfd_reloc_ok;
1413
0
}
1414
#undef S
1415
#undef A
1416
#undef B
1417
#undef G
1418
#undef GOT
1419
#undef L
1420
#undef MES
1421
#undef P
1422
#undef SECTSTAR
1423
#undef SECTSTART
1424
#undef JLI
1425
#undef _SDA_BASE_
1426
#undef none
1427
1428
#undef ARC_RELOC_HOWTO
1429
1430
1431
/* Relocate an arc ELF section.
1432
   Function : elf_arc_relocate_section
1433
   Brief    : Relocate an arc section, by handling all the relocations
1434
       appearing in that section.
1435
   Args     : info      : Link information.
1436
        input_bfd     : The input bfd.
1437
        input_section : The section being relocated.
1438
        contents      : contents of the section being relocated.
1439
        relocs      : List of relocations in the section.
1440
        local_syms    : is a pointer to the swapped in local symbols.
1441
        local_section : is an array giving the section in the input file
1442
            corresponding to the st_shndx field of each
1443
            local symbol.  */
1444
static int
1445
elf_arc_relocate_section (struct bfd_link_info *  info,
1446
        bfd *       input_bfd,
1447
        asection *      input_section,
1448
        bfd_byte *      contents,
1449
        Elf_Internal_Rela *     relocs,
1450
        Elf_Internal_Sym *      local_syms,
1451
        asection **     local_sections)
1452
0
{
1453
0
  Elf_Internal_Shdr *    symtab_hdr;
1454
0
  struct elf_link_hash_entry **  sym_hashes;
1455
0
  Elf_Internal_Rela *    rel;
1456
0
  Elf_Internal_Rela *    wrel;
1457
0
  Elf_Internal_Rela *    relend;
1458
0
  struct elf_link_hash_table *   htab = elf_hash_table (info);
1459
1460
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
1461
0
  sym_hashes = elf_sym_hashes (input_bfd);
1462
1463
0
  rel = wrel = relocs;
1464
0
  relend = relocs + input_section->reloc_count;
1465
0
  for (; rel < relend; wrel++, rel++)
1466
0
    {
1467
0
      enum elf_arc_reloc_type r_type;
1468
0
      reloc_howto_type *howto;
1469
0
      unsigned long r_symndx;
1470
0
      struct elf_link_hash_entry *h;
1471
0
      Elf_Internal_Sym *sym;
1472
0
      asection *sec;
1473
0
      struct elf_link_hash_entry *h2;
1474
0
      const char *msg;
1475
0
      bool unresolved_reloc = false;
1476
1477
0
      struct arc_relocation_data reloc_data =
1478
0
      {
1479
0
  .reloc_offset = 0,
1480
0
  .reloc_addend = 0,
1481
0
  .got_offset_value = 0,
1482
0
  .sym_value = 0,
1483
0
  .sym_section = NULL,
1484
0
  .howto = NULL,
1485
0
  .input_section = NULL,
1486
0
  .sdata_begin_symbol_vma = 0,
1487
0
  .sdata_begin_symbol_vma_set = false,
1488
0
  .got_symbol_vma = 0,
1489
0
  .should_relocate = false
1490
0
      };
1491
1492
0
      r_type = ELF32_R_TYPE (rel->r_info);
1493
1494
0
      if (r_type >= (int) R_ARC_max)
1495
0
  {
1496
0
    bfd_set_error (bfd_error_bad_value);
1497
0
    return false;
1498
0
  }
1499
0
      howto = arc_elf_howto (r_type);
1500
1501
0
      r_symndx = ELF32_R_SYM (rel->r_info);
1502
1503
      /* If we are generating another .o file and the symbol in not
1504
   local, skip this relocation.  */
1505
0
      if (bfd_link_relocatable (info))
1506
0
  {
1507
    /* This is a relocateable link.  We don't have to change
1508
       anything, unless the reloc is against a section symbol,
1509
       in which case we have to adjust according to where the
1510
       section symbol winds up in the output section.  */
1511
1512
    /* Checks if this is a local symbol and thus the reloc
1513
       might (will??) be against a section symbol.  */
1514
0
    if (r_symndx < symtab_hdr->sh_info)
1515
0
      {
1516
0
        sym = local_syms + r_symndx;
1517
0
        if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1518
0
    {
1519
0
      sec = local_sections[r_symndx];
1520
1521
      /* For RELA relocs.  Just adjust the addend
1522
         value in the relocation entry.  */
1523
0
      rel->r_addend += sec->output_offset + sym->st_value;
1524
1525
0
      ARC_DEBUG ("local symbols reloc (section=%d %s) seen in %s\n",
1526
0
           (int) r_symndx, local_sections[r_symndx]->name,
1527
0
           __PRETTY_FUNCTION__);
1528
0
    }
1529
0
      }
1530
0
  }
1531
1532
0
      h2 = elf_link_hash_lookup (elf_hash_table (info), "__SDATA_BEGIN__",
1533
0
         false, false, true);
1534
1535
0
      if (!reloc_data.sdata_begin_symbol_vma_set
1536
0
    && h2 != NULL && h2->root.type != bfd_link_hash_undefined
1537
0
    && h2->root.u.def.section->output_section != NULL)
1538
  /* TODO: Verify this condition.  */
1539
0
  {
1540
0
    reloc_data.sdata_begin_symbol_vma =
1541
0
      (h2->root.u.def.value
1542
0
       + h2->root.u.def.section->output_section->vma);
1543
0
    reloc_data.sdata_begin_symbol_vma_set = true;
1544
0
  }
1545
1546
0
      reloc_data.input_section = input_section;
1547
0
      reloc_data.howto = howto;
1548
0
      reloc_data.reloc_offset = rel->r_offset;
1549
0
      reloc_data.reloc_addend = rel->r_addend;
1550
1551
      /* This is a final link.  */
1552
0
      h = NULL;
1553
0
      sym = NULL;
1554
0
      sec = NULL;
1555
1556
0
      if (r_symndx < symtab_hdr->sh_info) /* A local symbol.  */
1557
0
  {
1558
0
    sym = local_syms + r_symndx;
1559
0
    sec = local_sections[r_symndx];
1560
0
  }
1561
0
      else
1562
0
  {
1563
0
    bool warned, ignored;
1564
0
    bfd_vma relocation ATTRIBUTE_UNUSED;
1565
1566
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1567
0
           r_symndx, symtab_hdr, sym_hashes,
1568
0
           h, sec, relocation,
1569
0
           unresolved_reloc, warned, ignored);
1570
1571
    /* TODO: This code is repeated from below.  We should
1572
       clean it and remove duplications.
1573
       Sec is used check for discarded sections.
1574
       Need to redesign code below.  */
1575
1576
    /* Get the symbol's entry in the symtab.  */
1577
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1578
1579
0
    while (h->root.type == bfd_link_hash_indirect
1580
0
     || h->root.type == bfd_link_hash_warning)
1581
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
1582
1583
    /* If we have encountered a definition for this symbol.  */
1584
0
    if (h->root.type == bfd_link_hash_defined
1585
0
        || h->root.type == bfd_link_hash_defweak)
1586
0
      {
1587
0
        reloc_data.sym_value = h->root.u.def.value;
1588
0
        sec = h->root.u.def.section;
1589
0
      }
1590
0
  }
1591
1592
      /* Clean relocs for symbols in discarded sections.  */
1593
0
      if (sec != NULL && discarded_section (sec))
1594
0
  {
1595
0
    _bfd_clear_contents (howto, input_bfd, input_section,
1596
0
             contents, rel->r_offset);
1597
0
    rel->r_info = 0;
1598
0
    rel->r_addend = 0;
1599
1600
    /* For ld -r, remove relocations in debug sections against
1601
       sections defined in discarded sections.  Not done for
1602
       eh_frame editing code expects to be present.  */
1603
0
     if (bfd_link_relocatable (info)
1604
0
         && (input_section->flags & SEC_DEBUGGING))
1605
0
       wrel--;
1606
1607
0
    continue;
1608
0
  }
1609
1610
0
      if (bfd_link_relocatable (info))
1611
0
  {
1612
0
    if (wrel != rel)
1613
0
      *wrel = *rel;
1614
0
    continue;
1615
0
  }
1616
1617
0
      if (r_symndx < symtab_hdr->sh_info) /* A local symbol.  */
1618
0
  {
1619
0
    reloc_data.sym_value = sym->st_value;
1620
0
    reloc_data.sym_section = sec;
1621
0
    reloc_data.symbol_name =
1622
0
      bfd_elf_string_from_elf_section (input_bfd,
1623
0
               symtab_hdr->sh_link,
1624
0
               sym->st_name);
1625
1626
    /* Mergeable section handling.  */
1627
0
    if ((sec->flags & SEC_MERGE)
1628
0
        && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1629
0
      {
1630
0
        asection *msec;
1631
0
        msec = sec;
1632
0
        rel->r_addend = _bfd_elf_rel_local_sym (info->output_bfd, sym,
1633
0
                  &msec, rel->r_addend);
1634
0
        rel->r_addend -= (sec->output_section->vma
1635
0
        + sec->output_offset
1636
0
        + sym->st_value);
1637
0
        rel->r_addend += msec->output_section->vma + msec->output_offset;
1638
1639
0
        reloc_data.reloc_addend = rel->r_addend;
1640
0
      }
1641
1642
0
    BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
1643
0
    if (htab->sgot != NULL)
1644
0
      reloc_data.got_symbol_vma = htab->sgot->output_section->vma
1645
0
          + htab->sgot->output_offset;
1646
1647
0
    reloc_data.should_relocate = true;
1648
0
  }
1649
0
      else /* Global symbol.  */
1650
0
  {
1651
    /* FIXME: We should use the RELOC_FOR_GLOBAL_SYMBOL macro
1652
       (defined in elf-bfd.h) here.  */
1653
1654
    /* Get the symbol's entry in the symtab.  */
1655
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1656
1657
0
    while (h->root.type == bfd_link_hash_indirect
1658
0
     || h->root.type == bfd_link_hash_warning)
1659
0
    {
1660
0
      struct elf_arc_link_hash_entry *ah_old =
1661
0
        (struct elf_arc_link_hash_entry *) h;
1662
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
1663
0
      struct elf_arc_link_hash_entry *ah =
1664
0
        (struct elf_arc_link_hash_entry *) h;
1665
1666
0
      if (ah->got_ents == 0 && ah_old->got_ents != ah->got_ents)
1667
0
        ah->got_ents = ah_old->got_ents;
1668
0
    }
1669
1670
    /* TODO: Need to validate what was the intention.  */
1671
    /* BFD_ASSERT ((h->dynindx == -1) || (h->forced_local != 0)); */
1672
0
    reloc_data.symbol_name = h->root.root.string;
1673
1674
    /* If we have encountered a definition for this symbol.  */
1675
0
    if (h->root.type == bfd_link_hash_defined
1676
0
        || h->root.type == bfd_link_hash_defweak)
1677
0
      {
1678
0
        reloc_data.sym_value = h->root.u.def.value;
1679
0
        reloc_data.sym_section = h->root.u.def.section;
1680
1681
0
        reloc_data.should_relocate = true;
1682
1683
0
        if (is_reloc_for_GOT (howto) && !bfd_link_pic (info))
1684
0
    {
1685
0
      struct elf_arc_link_hash_entry *ah =
1686
0
        (struct elf_arc_link_hash_entry *) h;
1687
      /* TODO: Change it to use arc_do_relocation with
1688
        ARC_32 reloc.  Try to use ADD_RELA macro.  */
1689
0
      bfd_vma relocation =
1690
0
        reloc_data.sym_value + reloc_data.reloc_addend
1691
0
        + (reloc_data.sym_section->output_section != NULL ?
1692
0
      (reloc_data.sym_section->output_offset
1693
0
       + reloc_data.sym_section->output_section->vma)
1694
0
          : 0);
1695
1696
0
      BFD_ASSERT (ah->got_ents);
1697
0
      bfd_vma got_offset = ah->got_ents->offset;
1698
0
      bfd_put_32 (info->output_bfd, relocation,
1699
0
            htab->sgot->contents + got_offset);
1700
0
    }
1701
0
        if (is_reloc_for_PLT (howto) && h->plt.offset != (bfd_vma) -1)
1702
0
    {
1703
      /* TODO: This is repeated up here.  */
1704
0
      reloc_data.sym_value = h->plt.offset;
1705
0
      reloc_data.sym_section = htab->splt;
1706
0
    }
1707
0
      }
1708
0
    else if (h->root.type == bfd_link_hash_undefweak)
1709
0
      {
1710
        /* Is weak symbol and has no definition.  */
1711
0
        if (is_reloc_for_GOT (howto))
1712
0
    {
1713
0
      reloc_data.sym_value = h->root.u.def.value;
1714
0
      reloc_data.sym_section = htab->sgot;
1715
0
      reloc_data.should_relocate = true;
1716
0
    }
1717
0
        else if (is_reloc_for_PLT (howto)
1718
0
           && h->plt.offset != (bfd_vma) -1)
1719
0
    {
1720
      /* TODO: This is repeated up here.  */
1721
0
      reloc_data.sym_value = h->plt.offset;
1722
0
      reloc_data.sym_section = htab->splt;
1723
0
      reloc_data.should_relocate = true;
1724
0
    }
1725
0
        else
1726
0
    continue;
1727
0
      }
1728
0
    else
1729
0
      {
1730
0
        if (is_reloc_for_GOT (howto))
1731
0
    {
1732
0
      reloc_data.sym_value = h->root.u.def.value;
1733
0
      reloc_data.sym_section = htab->sgot;
1734
1735
0
      reloc_data.should_relocate = true;
1736
0
    }
1737
0
        else if (is_reloc_for_PLT (howto))
1738
0
    {
1739
      /* Fail if it is linking for PIE and the symbol is
1740
         undefined.  */
1741
0
      if (bfd_link_executable (info))
1742
0
        (*info->callbacks->undefined_symbol)
1743
0
          (info, h->root.root.string, input_bfd, input_section,
1744
0
           rel->r_offset, true);
1745
0
      reloc_data.sym_value = h->plt.offset;
1746
0
      reloc_data.sym_section = htab->splt;
1747
1748
0
      reloc_data.should_relocate = true;
1749
0
    }
1750
0
        else if (!bfd_link_pic (info) || bfd_link_executable (info))
1751
0
    (*info->callbacks->undefined_symbol)
1752
0
      (info, h->root.root.string, input_bfd, input_section,
1753
0
       rel->r_offset, true);
1754
0
      }
1755
1756
0
    BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
1757
0
    if (htab->sgot != NULL)
1758
0
      reloc_data.got_symbol_vma = htab->sgot->output_section->vma
1759
0
          + htab->sgot->output_offset;
1760
0
  }
1761
1762
0
      if ((is_reloc_for_GOT (howto)
1763
0
     || is_reloc_for_TLS (howto)))
1764
0
  {
1765
0
    reloc_data.should_relocate = true;
1766
1767
0
    struct got_entry **list
1768
0
      = get_got_entry_list_for_symbol (input_bfd, r_symndx, h);
1769
1770
0
    reloc_data.got_offset_value
1771
0
      = relocate_fix_got_relocs_for_got_info (list,
1772
0
                tls_type_for_reloc (howto),
1773
0
                info,
1774
0
                info->output_bfd,
1775
0
                r_symndx,
1776
0
                local_syms,
1777
0
                local_sections,
1778
0
                h,
1779
0
                &reloc_data);
1780
1781
0
    if (h == NULL)
1782
0
      {
1783
0
        create_got_dynrelocs_for_single_entry (
1784
0
      got_entry_for_type (list,
1785
0
              arc_got_entry_type_for_reloc (howto)),
1786
0
      info->output_bfd, info, NULL);
1787
0
      }
1788
0
  }
1789
1790
1791
0
#define IS_ARC_PCREL_TYPE(TYPE) \
1792
0
  (   (TYPE == R_ARC_PC32)      \
1793
0
   || (TYPE == R_ARC_32_PCREL))
1794
1795
0
      switch (r_type)
1796
0
  {
1797
0
    case R_ARC_32:
1798
0
    case R_ARC_32_ME:
1799
0
    case R_ARC_PC32:
1800
0
    case R_ARC_32_PCREL:
1801
0
      if (bfd_link_pic (info)
1802
0
    && (input_section->flags & SEC_ALLOC) != 0
1803
0
    && (!IS_ARC_PCREL_TYPE (r_type)
1804
0
        || (h != NULL
1805
0
      && h->dynindx != -1
1806
0
      && !h->def_regular
1807
0
      && (!info->symbolic || !h->def_regular))))
1808
0
        {
1809
0
    Elf_Internal_Rela outrel;
1810
0
    bfd_byte *loc;
1811
0
    bool skip = false;
1812
0
    bool relocate = false;
1813
0
    asection *sreloc = elf_section_data (input_section)->sreloc;
1814
1815
0
    BFD_ASSERT (sreloc != NULL);
1816
1817
0
    outrel.r_offset = _bfd_elf_section_offset (info->output_bfd,
1818
0
                 info,
1819
0
                 input_section,
1820
0
                 rel->r_offset);
1821
1822
0
    if (outrel.r_offset == (bfd_vma) -1)
1823
0
      skip = true;
1824
1825
0
    outrel.r_addend = rel->r_addend;
1826
0
    outrel.r_offset += (input_section->output_section->vma
1827
0
            + input_section->output_offset);
1828
1829
0
    if (skip)
1830
0
      {
1831
0
        memset (&outrel, 0, sizeof outrel);
1832
0
        relocate = false;
1833
0
      }
1834
0
    else if (h != NULL
1835
0
       && h->dynindx != -1
1836
0
       && (IS_ARC_PCREL_TYPE (r_type)
1837
0
           || !(bfd_link_executable (info)
1838
0
          || SYMBOLIC_BIND (info, h))
1839
0
           || ! h->def_regular))
1840
0
      {
1841
0
        BFD_ASSERT (h != NULL);
1842
0
        if ((input_section->flags & SEC_ALLOC) != 0)
1843
0
          relocate = false;
1844
0
        else
1845
0
          relocate = true;
1846
1847
0
        BFD_ASSERT (h->dynindx != -1);
1848
0
        outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
1849
0
      }
1850
0
    else
1851
0
      {
1852
        /* Handle local symbols, they either do not have a
1853
           global hash table entry (h == NULL), or are
1854
           forced local due to a version script
1855
           (h->forced_local), or the third condition is
1856
           legacy, it appears to say something like, for
1857
           links where we are pre-binding the symbols, or
1858
           there's not an entry for this symbol in the
1859
           dynamic symbol table, and it's a regular symbol
1860
           not defined in a shared object, then treat the
1861
           symbol as local, resolve it now.  */
1862
0
        relocate = true;
1863
        /* outrel.r_addend = 0; */
1864
0
        outrel.r_info = ELF32_R_INFO (0, R_ARC_RELATIVE);
1865
0
      }
1866
1867
0
    BFD_ASSERT (sreloc->contents != 0);
1868
1869
0
    loc = sreloc->contents;
1870
0
    loc += sreloc->reloc_count * sizeof (Elf32_External_Rela);
1871
0
    sreloc->reloc_count += 1;
1872
1873
0
    bfd_elf32_swap_reloca_out (info->output_bfd, &outrel, loc);
1874
1875
0
    if (!relocate)
1876
0
      continue;
1877
0
        }
1878
0
      break;
1879
0
    default:
1880
0
      break;
1881
0
  }
1882
1883
0
      if (is_reloc_SDA_relative (howto)
1884
0
    && !reloc_data.sdata_begin_symbol_vma_set)
1885
0
  {
1886
0
    _bfd_error_handler
1887
0
      ("error: linker symbol __SDATA_BEGIN__ not found");
1888
0
    bfd_set_error (bfd_error_bad_value);
1889
0
    return false;
1890
0
  }
1891
1892
0
      DEBUG_ARC_RELOC (reloc_data);
1893
1894
      /* Make sure we have with a dynamic linker.  In case of GOT and PLT
1895
   the sym_section should point to .got or .plt respectively.  */
1896
0
      if ((is_reloc_for_GOT (howto) || is_reloc_for_PLT (howto))
1897
0
    && reloc_data.sym_section == NULL)
1898
0
  {
1899
0
    _bfd_error_handler
1900
0
      (_("GOT and PLT relocations cannot be fixed with a non dynamic linker"));
1901
0
    bfd_set_error (bfd_error_bad_value);
1902
0
    return false;
1903
0
  }
1904
1905
0
      msg = NULL;
1906
0
      switch (arc_do_relocation (contents, reloc_data, info))
1907
0
  {
1908
0
  case bfd_reloc_ok:
1909
0
    continue; /* The reloc processing loop.  */
1910
1911
0
  case bfd_reloc_overflow:
1912
0
    (*info->callbacks->reloc_overflow)
1913
0
      (info, (h ? &h->root : NULL), reloc_data.symbol_name, howto->name, (bfd_vma) 0,
1914
0
       input_bfd, input_section, rel->r_offset);
1915
0
    break;
1916
1917
0
  case bfd_reloc_undefined:
1918
0
    (*info->callbacks->undefined_symbol)
1919
0
      (info, reloc_data.symbol_name, input_bfd, input_section, rel->r_offset, true);
1920
0
    break;
1921
1922
0
  case bfd_reloc_other:
1923
    /* xgettext:c-format */
1924
0
    msg = _("%pB(%pA): warning: unaligned access to symbol '%s' in the small data area");
1925
0
    break;
1926
1927
0
  case bfd_reloc_outofrange:
1928
    /* xgettext:c-format */
1929
0
    msg = _("%pB(%pA): internal error: out of range error");
1930
0
    break;
1931
1932
0
  case bfd_reloc_notsupported:
1933
    /* xgettext:c-format */
1934
0
    msg = _("%pB(%pA): internal error: unsupported relocation error");
1935
0
    break;
1936
1937
0
  case bfd_reloc_dangerous:
1938
    /* xgettext:c-format */
1939
0
    msg = _("%pB(%pA): internal error: dangerous relocation");
1940
0
    break;
1941
1942
0
  default:
1943
    /* xgettext:c-format */
1944
0
    msg = _("%pB(%pA): internal error: unknown error");
1945
0
    break;
1946
0
  }
1947
1948
0
      if (msg)
1949
0
  _bfd_error_handler (msg, input_bfd, input_section, reloc_data.symbol_name);
1950
0
      return false;
1951
0
    }
1952
1953
0
  if (wrel != rel)
1954
0
    {
1955
0
      Elf_Internal_Shdr *rel_hdr;
1956
0
      size_t deleted = rel - wrel;
1957
1958
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section->output_section);
1959
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
1960
0
      rel_hdr = _bfd_elf_single_rel_hdr (input_section);
1961
0
      rel_hdr->sh_size -= rel_hdr->sh_entsize * deleted;
1962
0
      input_section->reloc_count -= deleted;
1963
0
    }
1964
1965
0
  return true;
1966
0
}
1967
1968
#define elf_arc_hash_table(p) \
1969
0
  ((is_elf_hash_table ((p)->hash)          \
1970
0
    && elf_hash_table_id (elf_hash_table (p)) == ARC_ELF_DATA)   \
1971
0
   ? (struct elf_arc_link_hash_table *) (p)->hash : NULL)
1972
1973
static bool
1974
elf_arc_check_relocs (bfd *      abfd,
1975
          struct bfd_link_info *     info,
1976
          asection *     sec,
1977
          const Elf_Internal_Rela *  relocs)
1978
0
{
1979
0
  Elf_Internal_Shdr *   symtab_hdr;
1980
0
  struct elf_link_hash_entry ** sym_hashes;
1981
0
  const Elf_Internal_Rela * rel;
1982
0
  const Elf_Internal_Rela * rel_end;
1983
0
  bfd *       dynobj;
1984
0
  asection *      sreloc = NULL;
1985
0
  struct elf_link_hash_table *  htab = elf_hash_table (info);
1986
1987
0
  if (bfd_link_relocatable (info))
1988
0
    return true;
1989
1990
0
  if (htab->dynobj == NULL)
1991
0
    htab->dynobj = abfd;
1992
1993
0
  dynobj = (elf_hash_table (info))->dynobj;
1994
0
  symtab_hdr = &elf_symtab_hdr (abfd);
1995
0
  sym_hashes = elf_sym_hashes (abfd);
1996
1997
0
  rel_end = relocs + sec->reloc_count;
1998
0
  for (rel = relocs; rel < rel_end; rel++)
1999
0
    {
2000
0
      enum elf_arc_reloc_type r_type;
2001
0
      reloc_howto_type *howto;
2002
0
      unsigned long   r_symndx;
2003
0
      struct elf_link_hash_entry *h;
2004
2005
0
      r_type = ELF32_R_TYPE (rel->r_info);
2006
2007
0
      if (r_type >= (int) R_ARC_max)
2008
0
  {
2009
0
    bfd_set_error (bfd_error_bad_value);
2010
0
    return false;
2011
0
  }
2012
0
      howto = arc_elf_howto (r_type);
2013
2014
      /* Load symbol information.  */
2015
0
      r_symndx = ELF32_R_SYM (rel->r_info);
2016
0
      if (r_symndx < symtab_hdr->sh_info) /* Is a local symbol.  */
2017
0
  h = NULL;
2018
0
      else /* Global one.  */
2019
0
  {
2020
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
2021
0
    while (h->root.type == bfd_link_hash_indirect
2022
0
     || h->root.type == bfd_link_hash_warning)
2023
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
2024
0
  }
2025
2026
2027
0
      switch (r_type)
2028
0
  {
2029
0
  case R_ARC_32:
2030
0
  case R_ARC_32_ME:
2031
    /* During shared library creation, these relocs should not
2032
       appear in a shared library (as memory will be read only
2033
       and the dynamic linker can not resolve these.  However
2034
       the error should not occur for e.g. debugging or
2035
       non-readonly sections.  */
2036
0
    if (h != NULL
2037
0
        && (bfd_link_dll (info) && !bfd_link_pie (info))
2038
0
        && (sec->flags & SEC_ALLOC) != 0
2039
0
        && (sec->flags & SEC_READONLY) != 0
2040
0
        && ((sec->flags & SEC_CODE) != 0
2041
0
      || (sec->flags & SEC_DEBUGGING) != 0))
2042
0
      {
2043
0
        const char *name;
2044
0
        if (h)
2045
0
    name = h->root.root.string;
2046
0
        else
2047
0
    name = "UNKNOWN";
2048
0
        _bfd_error_handler
2049
        /* xgettext:c-format */
2050
0
        (_("%pB: relocation %s against `%s' can not be used"
2051
0
     " when making a shared object; recompile with -fPIC"),
2052
0
     abfd,
2053
0
     arc_elf_howto (r_type)->name,
2054
0
     name);
2055
0
        bfd_set_error (bfd_error_bad_value);
2056
0
        return false;
2057
0
      }
2058
2059
      /* In some cases we are not setting the 'non_got_ref'
2060
         flag, even though the relocations don't require a GOT
2061
         access.  We should extend the testing in this area to
2062
         ensure that no significant cases are being missed.  */
2063
0
      if (h)
2064
0
        h->non_got_ref = 1;
2065
      /* FALLTHROUGH */
2066
0
    case R_ARC_PC32:
2067
0
    case R_ARC_32_PCREL:
2068
0
      if ((bfd_link_pic (info))
2069
0
    && ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
2070
0
        || (h != NULL
2071
0
      && (!info->symbolic || !h->def_regular))))
2072
0
        {
2073
0
    if (sreloc == NULL)
2074
0
      {
2075
0
        if (info->dynamic
2076
0
      && ! htab->dynamic_sections_created
2077
0
      && ! bfd_elf_link_create_dynamic_sections (abfd, info))
2078
0
          return false;
2079
0
        sreloc = _bfd_elf_make_dynamic_reloc_section (sec, dynobj,
2080
0
                  2, abfd,
2081
                  /*rela*/
2082
0
                  true);
2083
2084
0
        if (sreloc == NULL)
2085
0
          return false;
2086
0
      }
2087
0
    sreloc->size += sizeof (Elf32_External_Rela);
2088
2089
0
        }
2090
0
    default:
2091
0
      break;
2092
0
  }
2093
2094
0
      if (is_reloc_for_PLT (howto))
2095
0
  {
2096
0
    if (h == NULL)
2097
0
      continue;
2098
0
    else
2099
0
      if (h->forced_local == 0)
2100
0
        h->needs_plt = 1;
2101
0
  }
2102
2103
      /* Add info to the symbol got_entry_list.  */
2104
0
      if (is_reloc_for_GOT (howto)
2105
0
    || is_reloc_for_TLS (howto))
2106
0
  {
2107
0
    if (bfd_link_dll (info) && !bfd_link_pie (info)
2108
0
        && (r_type == R_ARC_TLS_LE_32 || r_type == R_ARC_TLS_LE_S9))
2109
0
      {
2110
0
        const char *name;
2111
0
        if (h)
2112
0
    name = h->root.root.string;
2113
0
        else
2114
    /* bfd_elf_sym_name (abfd, symtab_hdr, isym, NULL);  */
2115
0
    name = "UNKNOWN";
2116
0
        _bfd_error_handler
2117
    /* xgettext:c-format */
2118
0
    (_("%pB: relocation %s against `%s' can not be used"
2119
0
       " when making a shared object; recompile with -fPIC"),
2120
0
       abfd,
2121
0
       arc_elf_howto (r_type)->name,
2122
0
       name);
2123
0
        bfd_set_error (bfd_error_bad_value);
2124
0
        return false;
2125
0
      }
2126
0
    if (! _bfd_elf_create_got_section (dynobj, info))
2127
0
      return false;
2128
2129
0
    arc_fill_got_info_for_reloc (
2130
0
      arc_got_entry_type_for_reloc (howto),
2131
0
      get_got_entry_list_for_symbol (abfd, r_symndx, h),
2132
0
      info,
2133
0
      h);
2134
0
  }
2135
0
    }
2136
2137
0
  return true;
2138
0
}
2139
2140
0
#define ELF_DYNAMIC_INTERPRETER  "/sbin/ld-uClibc.so"
2141
2142
static const struct plt_version_t *
2143
arc_get_plt_version (struct bfd_link_info *info)
2144
0
{
2145
0
  int i;
2146
2147
0
  for (i = 0; i < 1; i++)
2148
0
    {
2149
0
      ARC_DEBUG ("%d: size1 = %d, size2 = %d\n", i,
2150
0
     (int) plt_versions[i].entry_size,
2151
0
     (int) plt_versions[i].elem_size);
2152
0
    }
2153
2154
0
  if (bfd_get_mach (info->output_bfd) == bfd_mach_arc_arcv2)
2155
0
    {
2156
0
      if (bfd_link_pic (info))
2157
0
  return &(plt_versions[ELF_ARCV2_PIC]);
2158
0
      else
2159
0
  return &(plt_versions[ELF_ARCV2_ABS]);
2160
0
    }
2161
0
  else
2162
0
    {
2163
0
      if (bfd_link_pic (info))
2164
0
  return &(plt_versions[ELF_ARC_PIC]);
2165
0
      else
2166
0
  return &(plt_versions[ELF_ARC_ABS]);
2167
0
    }
2168
0
}
2169
2170
static bfd_vma
2171
add_symbol_to_plt (struct bfd_link_info *info)
2172
0
{
2173
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
2174
0
  bfd_vma ret;
2175
2176
0
  const struct plt_version_t *plt_data = arc_get_plt_version (info);
2177
2178
  /* If this is the first .plt entry, make room for the special first
2179
     entry.  */
2180
0
  if (htab->splt->size == 0)
2181
0
    htab->splt->size += plt_data->entry_size;
2182
2183
0
  ret = htab->splt->size;
2184
2185
0
  htab->splt->size += plt_data->elem_size;
2186
0
  ARC_DEBUG ("PLT_SIZE = %d\n", (int) htab->splt->size);
2187
2188
0
  htab->sgotplt->size += 4;
2189
0
  htab->srelplt->size += sizeof (Elf32_External_Rela);
2190
2191
0
  return ret;
2192
0
}
2193
2194
#define PLT_DO_RELOCS_FOR_ENTRY(ABFD, DS, RELOCS) \
2195
0
  plt_do_relocs_for_symbol (ABFD, DS, RELOCS, 0, 0)
2196
2197
static void
2198
plt_do_relocs_for_symbol (bfd *abfd,
2199
        struct elf_link_hash_table *htab,
2200
        const struct plt_reloc *reloc,
2201
        bfd_vma plt_offset,
2202
        bfd_vma symbol_got_offset)
2203
0
{
2204
0
  while (SYM_ONLY (reloc->symbol) != LAST_RELOC)
2205
0
    {
2206
0
      bfd_vma relocation = 0;
2207
2208
0
      switch (SYM_ONLY (reloc->symbol))
2209
0
  {
2210
0
    case SGOT:
2211
0
    relocation
2212
0
      = htab->sgotplt->output_section->vma
2213
0
        + htab->sgotplt->output_offset + symbol_got_offset;
2214
0
    break;
2215
0
  }
2216
0
      relocation += reloc->addend;
2217
2218
0
      if (IS_RELATIVE (reloc->symbol))
2219
0
  {
2220
0
    bfd_vma reloc_offset = reloc->offset;
2221
0
    reloc_offset -= (IS_INSN_32 (reloc->symbol)) ? 4 : 0;
2222
0
    reloc_offset -= (IS_INSN_24 (reloc->symbol)) ? 2 : 0;
2223
2224
0
    relocation -= htab->splt->output_section->vma
2225
0
       + htab->splt->output_offset
2226
0
       + plt_offset + reloc_offset;
2227
0
  }
2228
2229
      /* TODO: being ME is not a property of the relocation but of the
2230
   section of which is applying the relocation. */
2231
0
      if (IS_MIDDLE_ENDIAN (reloc->symbol) && !bfd_big_endian (abfd))
2232
0
  {
2233
0
    relocation
2234
0
      = ((relocation & 0xffff0000) >> 16)
2235
0
        | ((relocation & 0xffff) << 16);
2236
0
  }
2237
2238
0
      switch (reloc->size)
2239
0
  {
2240
0
    case 32:
2241
0
      bfd_put_32 (htab->splt->output_section->owner,
2242
0
      relocation,
2243
0
      htab->splt->contents + plt_offset + reloc->offset);
2244
0
      break;
2245
0
  }
2246
2247
0
      reloc = &(reloc[1]); /* Jump to next relocation.  */
2248
0
    }
2249
0
}
2250
2251
static void
2252
relocate_plt_for_symbol (bfd *output_bfd,
2253
       struct bfd_link_info *info,
2254
       struct elf_link_hash_entry *h)
2255
0
{
2256
0
  const struct plt_version_t *plt_data = arc_get_plt_version (info);
2257
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
2258
2259
0
  bfd_vma plt_index = (h->plt.offset  - plt_data->entry_size)
2260
0
          / plt_data->elem_size;
2261
0
  bfd_vma got_offset = (plt_index + 3) * 4;
2262
2263
0
  ARC_DEBUG ("arc_info: PLT_OFFSET = %#lx, PLT_ENTRY_VMA = %#lx, \
2264
0
GOT_ENTRY_OFFSET = %#lx, GOT_ENTRY_VMA = %#lx, for symbol %s\n",
2265
0
       (long) h->plt.offset,
2266
0
       (long) (htab->splt->output_section->vma
2267
0
         + htab->splt->output_offset
2268
0
         + h->plt.offset),
2269
0
       (long) got_offset,
2270
0
       (long) (htab->sgotplt->output_section->vma
2271
0
         + htab->sgotplt->output_offset
2272
0
         + got_offset),
2273
0
       h->root.root.string);
2274
2275
0
  {
2276
0
    bfd_vma i = 0;
2277
0
    uint16_t *ptr = (uint16_t *) plt_data->elem;
2278
2279
0
    for (i = 0; i < plt_data->elem_size/2; i++)
2280
0
      {
2281
0
  uint16_t data = ptr[i];
2282
0
  bfd_put_16 (output_bfd,
2283
0
        (bfd_vma) data,
2284
0
        htab->splt->contents + h->plt.offset + (i*2));
2285
0
      }
2286
0
  }
2287
2288
0
  plt_do_relocs_for_symbol (output_bfd, htab,
2289
0
          plt_data->elem_relocs,
2290
0
          h->plt.offset,
2291
0
          got_offset);
2292
2293
  /* Fill in the entry in the global offset table.  */
2294
0
  bfd_put_32 (output_bfd,
2295
0
        (bfd_vma) (htab->splt->output_section->vma
2296
0
       + htab->splt->output_offset),
2297
0
        htab->sgotplt->contents + got_offset);
2298
2299
  /* TODO: Fill in the entry in the .rela.plt section.  */
2300
0
  {
2301
0
    Elf_Internal_Rela rel;
2302
0
    bfd_byte *loc;
2303
2304
0
    rel.r_offset = (htab->sgotplt->output_section->vma
2305
0
        + htab->sgotplt->output_offset
2306
0
        + got_offset);
2307
0
    rel.r_addend = 0;
2308
2309
0
    BFD_ASSERT (h->dynindx != -1);
2310
0
    rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_JMP_SLOT);
2311
2312
0
    loc = htab->srelplt->contents;
2313
0
    loc += plt_index * sizeof (Elf32_External_Rela); /* relA */
2314
0
    bfd_elf32_swap_reloca_out (output_bfd, &rel, loc);
2315
0
  }
2316
0
}
2317
2318
static void
2319
relocate_plt_for_entry (bfd *abfd,
2320
      struct bfd_link_info *info)
2321
0
{
2322
0
  const struct plt_version_t *plt_data = arc_get_plt_version (info);
2323
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
2324
2325
0
  {
2326
0
    bfd_vma i = 0;
2327
0
    uint16_t *ptr = (uint16_t *) plt_data->entry;
2328
0
    for (i = 0; i < plt_data->entry_size/2; i++)
2329
0
      {
2330
0
  uint16_t data = ptr[i];
2331
0
  bfd_put_16 (abfd,
2332
0
        (bfd_vma) data,
2333
0
        htab->splt->contents + (i*2));
2334
0
      }
2335
0
  }
2336
0
  PLT_DO_RELOCS_FOR_ENTRY (abfd, htab, plt_data->entry_relocs);
2337
0
}
2338
2339
/* Desc : Adjust a symbol defined by a dynamic object and referenced
2340
   by a regular object.  The current definition is in some section of
2341
   the dynamic object, but we're not including those sections.  We
2342
   have to change the definition to something the rest of the link can
2343
   understand.  */
2344
2345
static bool
2346
elf_arc_adjust_dynamic_symbol (struct bfd_link_info *info,
2347
            struct elf_link_hash_entry *h)
2348
0
{
2349
0
  asection *s;
2350
0
  bfd *dynobj = (elf_hash_table (info))->dynobj;
2351
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
2352
2353
0
  if (h->type == STT_FUNC
2354
0
      || h->type == STT_GNU_IFUNC
2355
0
      || h->needs_plt == 1)
2356
0
    {
2357
0
      if (!bfd_link_pic (info) && !h->def_dynamic && !h->ref_dynamic)
2358
0
  {
2359
    /* This case can occur if we saw a PLT32 reloc in an input
2360
       file, but the symbol was never referred to by a dynamic
2361
       object.  In such a case, we don't actually need to build
2362
       a procedure linkage table, and we can just do a PC32
2363
       reloc instead.  */
2364
0
    BFD_ASSERT (h->needs_plt);
2365
0
    return true;
2366
0
  }
2367
2368
      /* Make sure this symbol is output as a dynamic symbol.  */
2369
0
      if (h->dynindx == -1 && !h->forced_local
2370
0
    && !bfd_elf_link_record_dynamic_symbol (info, h))
2371
0
  return false;
2372
2373
0
      if (bfd_link_pic (info)
2374
0
    || WILL_CALL_FINISH_DYNAMIC_SYMBOL (1, 0, h))
2375
0
  {
2376
0
    bfd_vma loc = add_symbol_to_plt (info);
2377
2378
0
    if (bfd_link_executable (info) && !h->def_regular)
2379
0
      {
2380
0
        h->root.u.def.section = htab->splt;
2381
0
        h->root.u.def.value = loc;
2382
0
      }
2383
0
    h->plt.offset = loc;
2384
0
  }
2385
0
      else
2386
0
  {
2387
0
    h->plt.offset = (bfd_vma) -1;
2388
0
    h->needs_plt = 0;
2389
0
  }
2390
0
      return true;
2391
0
    }
2392
2393
  /* If this is a weak symbol, and there is a real definition, the
2394
     processor independent code will have arranged for us to see the
2395
     real definition first, and we can just use the same value.  */
2396
0
  if (h->is_weakalias)
2397
0
    {
2398
0
      struct elf_link_hash_entry *def = weakdef (h);
2399
0
      BFD_ASSERT (def->root.type == bfd_link_hash_defined);
2400
0
      h->root.u.def.section = def->root.u.def.section;
2401
0
      h->root.u.def.value = def->root.u.def.value;
2402
0
      return true;
2403
0
    }
2404
2405
  /* This is a reference to a symbol defined by a dynamic object which
2406
     is not a function.  */
2407
2408
  /* If we are creating a shared library, we must presume that the
2409
     only references to the symbol are via the global offset table.
2410
     For such cases we need not do anything here; the relocations will
2411
     be handled correctly by relocate_section.  */
2412
0
  if (!bfd_link_executable (info))
2413
0
    return true;
2414
2415
  /* If there are no non-GOT references, we do not need a copy
2416
     relocation.  */
2417
0
  if (!h->non_got_ref)
2418
0
    return true;
2419
2420
  /* If -z nocopyreloc was given, we won't generate them either.  */
2421
0
  if (info->nocopyreloc)
2422
0
    {
2423
0
      h->non_got_ref = 0;
2424
0
      return true;
2425
0
    }
2426
2427
  /* We must allocate the symbol in our .dynbss section, which will
2428
     become part of the .bss section of the executable.  There will be
2429
     an entry for this symbol in the .dynsym section.  The dynamic
2430
     object will contain position independent code, so all references
2431
     from the dynamic object to this symbol will go through the global
2432
     offset table.  The dynamic linker will use the .dynsym entry to
2433
     determine the address it must put in the global offset table, so
2434
     both the dynamic object and the regular object will refer to the
2435
     same memory location for the variable.  */
2436
2437
0
  if (htab == NULL)
2438
0
    return false;
2439
2440
  /* We must generate a R_ARC_COPY reloc to tell the dynamic linker to
2441
     copy the initial value out of the dynamic object and into the
2442
     runtime process image.  We need to remember the offset into the
2443
     .rela.bss section we are going to use.  */
2444
0
  if ((h->root.u.def.section->flags & SEC_ALLOC) != 0)
2445
0
    {
2446
0
      struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info);
2447
2448
0
      BFD_ASSERT (arc_htab->elf.srelbss != NULL);
2449
0
      arc_htab->elf.srelbss->size += sizeof (Elf32_External_Rela);
2450
0
      h->needs_copy = 1;
2451
0
    }
2452
2453
  /* TODO: Move this also to arc_hash_table.  */
2454
0
  s = bfd_get_section_by_name (dynobj, ".dynbss");
2455
0
  BFD_ASSERT (s != NULL);
2456
2457
0
  return _bfd_elf_adjust_dynamic_copy (info, h, s);
2458
0
}
2459
2460
/* Function :  elf_arc_finish_dynamic_symbol
2461
   Brief    :  Finish up dynamic symbol handling.  We set the
2462
       contents of various dynamic sections here.
2463
   Args     :  output_bfd :
2464
         info   :
2465
         h    :
2466
         sym    :
2467
   Returns  : True/False as the return status.  */
2468
2469
static bool
2470
elf_arc_finish_dynamic_symbol (struct bfd_link_info *info,
2471
             struct elf_link_hash_entry *h,
2472
             Elf_Internal_Sym * sym)
2473
0
{
2474
0
  if (h->plt.offset != (bfd_vma) -1)
2475
0
    {
2476
0
      relocate_plt_for_symbol (info->output_bfd, info, h);
2477
2478
0
      if (!h->def_regular)
2479
0
  {
2480
    /* Mark the symbol as undefined, rather than as defined in
2481
       the .plt section.  Leave the value alone.  */
2482
0
    sym->st_shndx = SHN_UNDEF;
2483
0
  }
2484
0
    }
2485
2486
2487
  /* This function traverses list of GOT entries and
2488
     create respective dynamic relocs.  */
2489
  /* TODO: Make function to get list and not access the list directly.  */
2490
  /* TODO: Move function to relocate_section create this relocs eagerly.  */
2491
0
  struct elf_arc_link_hash_entry *ah =
2492
0
    (struct elf_arc_link_hash_entry *) h;
2493
0
  create_got_dynrelocs_for_got_info (&ah->got_ents,
2494
0
             info->output_bfd,
2495
0
             info,
2496
0
             h);
2497
2498
0
  if (h->needs_copy)
2499
0
    {
2500
0
      struct elf_arc_link_hash_table *arc_htab = elf_arc_hash_table (info);
2501
2502
0
      if (h->dynindx == -1
2503
0
    || (h->root.type != bfd_link_hash_defined
2504
0
        && h->root.type != bfd_link_hash_defweak)
2505
0
    || arc_htab->elf.srelbss == NULL)
2506
0
  abort ();
2507
2508
0
      bfd_vma rel_offset = (h->root.u.def.value
2509
0
          + h->root.u.def.section->output_section->vma
2510
0
          + h->root.u.def.section->output_offset);
2511
2512
0
      bfd_byte * loc = arc_htab->elf.srelbss->contents
2513
0
  + (arc_htab->elf.srelbss->reloc_count * sizeof (Elf32_External_Rela));
2514
0
      arc_htab->elf.srelbss->reloc_count++;
2515
2516
0
      Elf_Internal_Rela rel;
2517
0
      rel.r_addend = 0;
2518
0
      rel.r_offset = rel_offset;
2519
2520
0
      BFD_ASSERT (h->dynindx != -1);
2521
0
      rel.r_info = ELF32_R_INFO (h->dynindx, R_ARC_COPY);
2522
2523
0
      bfd_elf32_swap_reloca_out (info->output_bfd, &rel, loc);
2524
0
    }
2525
2526
  /* Mark _DYNAMIC and _GLOBAL_OFFSET_TABLE_ as absolute.  */
2527
0
  if (strcmp (h->root.root.string, "_DYNAMIC") == 0
2528
0
      || strcmp (h->root.root.string, "__DYNAMIC") == 0
2529
0
      || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
2530
0
    sym->st_shndx = SHN_ABS;
2531
2532
0
  return true;
2533
0
}
2534
2535
#define GET_SYMBOL_OR_SECTION(TAG, SYMBOL, SECTION)   \
2536
0
  case TAG:             \
2537
0
  if (SYMBOL != NULL)           \
2538
0
    h = elf_link_hash_lookup (elf_hash_table (info),    \
2539
0
            SYMBOL, false, false, true); \
2540
0
  else if (SECTION != NULL)         \
2541
0
    s = bfd_get_linker_section (dynobj, SECTION);   \
2542
0
  break;
2543
2544
2545
struct obfd_info_group {
2546
  bfd *output_bfd;
2547
  struct bfd_link_info *info;
2548
};
2549
2550
static bool
2551
arc_create_forced_local_got_entries_for_tls (struct bfd_hash_entry *bh,
2552
               void *data)
2553
0
{
2554
0
  struct elf_arc_link_hash_entry * h =
2555
0
    (struct elf_arc_link_hash_entry *) bh;
2556
0
  struct obfd_info_group *tmp = (struct obfd_info_group *) data;
2557
2558
0
  if (h->got_ents != NULL)
2559
0
    {
2560
0
      BFD_ASSERT (h);
2561
2562
0
      struct got_entry *list = h->got_ents;
2563
2564
0
      while (list != NULL)
2565
0
  {
2566
0
    create_got_dynrelocs_for_single_entry (list, tmp->output_bfd,
2567
0
      tmp->info,
2568
0
      (struct elf_link_hash_entry *) h);
2569
0
    list = list->next;
2570
0
  }
2571
0
    }
2572
2573
0
  return true;
2574
0
}
2575
2576
2577
/* Function :  elf_arc_finish_dynamic_sections
2578
   Brief    :  Finish up the dynamic sections handling.
2579
   Returns  : True/False as the return status.  */
2580
2581
static bool
2582
elf_arc_finish_dynamic_sections (struct bfd_link_info *info,
2583
         bfd_byte *buf ATTRIBUTE_UNUSED)
2584
0
{
2585
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
2586
0
  bfd *dynobj = (elf_hash_table (info))->dynobj;
2587
0
  asection *sdyn = bfd_get_linker_section (dynobj, ".dynamic");
2588
2589
0
  if (sdyn)
2590
0
    {
2591
0
      Elf32_External_Dyn *dyncon, *dynconend;
2592
2593
0
      dyncon = (Elf32_External_Dyn *) sdyn->contents;
2594
0
      dynconend
2595
0
  = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
2596
0
      for (; dyncon < dynconend; dyncon++)
2597
0
  {
2598
0
    Elf_Internal_Dyn internal_dyn;
2599
0
    bool do_it = false;
2600
2601
0
    struct elf_link_hash_entry *h = NULL;
2602
0
    asection   *s = NULL;
2603
2604
0
    bfd_elf32_swap_dyn_in (dynobj, dyncon, &internal_dyn);
2605
2606
0
    switch (internal_dyn.d_tag)
2607
0
      {
2608
0
        GET_SYMBOL_OR_SECTION (DT_INIT, info->init_function, NULL)
2609
0
        GET_SYMBOL_OR_SECTION (DT_FINI, info->fini_function, NULL)
2610
0
        GET_SYMBOL_OR_SECTION (DT_PLTGOT, NULL, ".plt")
2611
0
        GET_SYMBOL_OR_SECTION (DT_JMPREL, NULL, ".rela.plt")
2612
0
        GET_SYMBOL_OR_SECTION (DT_PLTRELSZ, NULL, ".rela.plt")
2613
0
        GET_SYMBOL_OR_SECTION (DT_VERSYM, NULL, ".gnu.version")
2614
0
        GET_SYMBOL_OR_SECTION (DT_VERDEF, NULL, ".gnu.version_d")
2615
0
        GET_SYMBOL_OR_SECTION (DT_VERNEED, NULL, ".gnu.version_r")
2616
0
        default:
2617
0
    break;
2618
0
      }
2619
2620
    /* In case the dynamic symbols should be updated with a symbol.  */
2621
0
    if (h != NULL
2622
0
        && (h->root.type == bfd_link_hash_defined
2623
0
      || h->root.type == bfd_link_hash_defweak))
2624
0
      {
2625
0
        asection       *asec_ptr;
2626
2627
0
        internal_dyn.d_un.d_val = h->root.u.def.value;
2628
0
        asec_ptr = h->root.u.def.section;
2629
0
        if (asec_ptr->output_section != NULL)
2630
0
    {
2631
0
      internal_dyn.d_un.d_val +=
2632
0
        (asec_ptr->output_section->vma
2633
0
         + asec_ptr->output_offset);
2634
0
    }
2635
0
        else
2636
0
    {
2637
      /* The symbol is imported from another shared
2638
         library and does not apply to this one.  */
2639
0
      internal_dyn.d_un.d_val = 0;
2640
0
    }
2641
0
        do_it = true;
2642
0
      }
2643
0
    else if (s != NULL) /* With a section information.  */
2644
0
      {
2645
0
        switch (internal_dyn.d_tag)
2646
0
    {
2647
0
      case DT_PLTGOT:
2648
0
      case DT_JMPREL:
2649
0
      case DT_VERSYM:
2650
0
      case DT_VERDEF:
2651
0
      case DT_VERNEED:
2652
0
        internal_dyn.d_un.d_ptr = (s->output_section->vma
2653
0
                 + s->output_offset);
2654
0
        do_it = true;
2655
0
        break;
2656
2657
0
      case DT_PLTRELSZ:
2658
0
        internal_dyn.d_un.d_val = s->size;
2659
0
        do_it = true;
2660
0
        break;
2661
2662
0
      default:
2663
0
        break;
2664
0
    }
2665
0
      }
2666
2667
0
    if (do_it)
2668
0
      bfd_elf32_swap_dyn_out (info->output_bfd, &internal_dyn, dyncon);
2669
0
  }
2670
2671
0
      if (htab->splt->size > 0)
2672
0
  {
2673
0
    relocate_plt_for_entry (info->output_bfd, info);
2674
0
  }
2675
2676
      /* TODO: Validate this.  */
2677
0
      if (htab->srelplt->output_section != bfd_abs_section_ptr)
2678
0
  elf_section_data (htab->srelplt->output_section)
2679
0
    ->this_hdr.sh_entsize = 12;
2680
0
    }
2681
2682
  /* Fill in the first three entries in the global offset table.  */
2683
0
  if (htab->sgot)
2684
0
    {
2685
0
      struct elf_link_hash_entry *h;
2686
0
      h = elf_link_hash_lookup (elf_hash_table (info), "_GLOBAL_OFFSET_TABLE_",
2687
0
         false, false, true);
2688
2689
0
  if (h != NULL && h->root.type != bfd_link_hash_undefined
2690
0
      && h->root.u.def.section != NULL)
2691
0
  {
2692
0
    asection *sec = h->root.u.def.section;
2693
2694
0
    if (sdyn == NULL)
2695
0
      bfd_put_32 (info->output_bfd, 0,
2696
0
      sec->contents);
2697
0
    else
2698
0
      bfd_put_32 (info->output_bfd,
2699
0
      sdyn->output_section->vma + sdyn->output_offset,
2700
0
      sec->contents);
2701
0
    bfd_put_32 (info->output_bfd, 0, sec->contents + 4);
2702
0
    bfd_put_32 (info->output_bfd, 0, sec->contents + 8);
2703
0
  }
2704
0
    }
2705
2706
0
  struct obfd_info_group group;
2707
0
  group.output_bfd = info->output_bfd;
2708
0
  group.info = info;
2709
0
  bfd_hash_traverse (&info->hash->table,
2710
0
         arc_create_forced_local_got_entries_for_tls, &group);
2711
2712
0
  return true;
2713
0
}
2714
2715
#define ADD_DYNAMIC_SYMBOL(NAME, TAG)         \
2716
0
  h =  elf_link_hash_lookup (elf_hash_table (info),     \
2717
0
           NAME, false, false, false);    \
2718
0
  if ((h != NULL && (h->ref_regular || h->def_regular)))   \
2719
0
    if (! _bfd_elf_add_dynamic_entry (info, TAG, 0))     \
2720
0
      return false;
2721
2722
/* Set the sizes of the dynamic sections.  */
2723
static bool
2724
elf_arc_late_size_sections (struct bfd_link_info *info)
2725
0
{
2726
0
  bfd *dynobj;
2727
0
  asection *s;
2728
0
  bool relocs_exist = false;
2729
0
  struct elf_link_hash_table *htab = elf_hash_table (info);
2730
2731
0
  dynobj = htab->dynobj;
2732
0
  if (dynobj == NULL)
2733
0
    return true;
2734
2735
0
  if (htab->dynamic_sections_created)
2736
0
    {
2737
0
      struct elf_link_hash_entry *h;
2738
2739
      /* Set the contents of the .interp section to the
2740
   interpreter.  */
2741
0
      if (bfd_link_executable (info) && !info->nointerp)
2742
0
  {
2743
0
    s = htab->interp;
2744
0
    BFD_ASSERT (s != NULL);
2745
0
    s->size = sizeof (ELF_DYNAMIC_INTERPRETER);
2746
0
    s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
2747
0
    s->alloced = 1;
2748
0
  }
2749
2750
      /* Add some entries to the .dynamic section.  We fill in some of
2751
   the values later, in elf_bfd_final_link, but we must add the
2752
   entries now so that we know the final size of the .dynamic
2753
   section.  Checking if the .init section is present.  We also
2754
   create DT_INIT and DT_FINI entries if the init_str has been
2755
   changed by the user.  */
2756
0
      ADD_DYNAMIC_SYMBOL (info->init_function, DT_INIT);
2757
0
      ADD_DYNAMIC_SYMBOL (info->fini_function, DT_FINI);
2758
0
    }
2759
0
  else
2760
0
    {
2761
      /* We may have created entries in the .rela.got section.
2762
   However, if we are not creating the dynamic sections, we will
2763
   not actually use these entries.  Reset the size of .rela.got,
2764
   which will cause it to get stripped from the output file
2765
   below.  */
2766
0
      if (htab->srelgot != NULL)
2767
0
  htab->srelgot->size = 0;
2768
0
    }
2769
2770
0
  for (s = dynobj->sections; s != NULL; s = s->next)
2771
0
    {
2772
0
      if ((s->flags & SEC_LINKER_CREATED) == 0)
2773
0
  continue;
2774
2775
0
      if (s == htab->splt
2776
0
    || s == htab->sgot
2777
0
    || s == htab->sgotplt
2778
0
    || s == htab->sdynbss)
2779
0
  {
2780
    /* Strip this section if we don't need it.  */
2781
0
  }
2782
0
      else if (startswith (s->name, ".rela"))
2783
0
  {
2784
0
    if (s->size != 0 && s != htab->srelplt)
2785
0
      relocs_exist = true;
2786
2787
    /* We use the reloc_count field as a counter if we need to
2788
       copy relocs into the output file.  */
2789
0
    s->reloc_count = 0;
2790
0
  }
2791
0
      else
2792
0
  {
2793
    /* It's not one of our sections, so don't allocate space.  */
2794
0
    continue;
2795
0
  }
2796
2797
0
      if (s->size == 0)
2798
0
  {
2799
0
    s->flags |= SEC_EXCLUDE;
2800
0
    continue;
2801
0
  }
2802
2803
0
      if ((s->flags & SEC_HAS_CONTENTS) == 0)
2804
0
  continue;
2805
2806
      /* Allocate memory for the section contents.  */
2807
0
      s->contents = bfd_zalloc (dynobj, s->size);
2808
0
      if (s->contents == NULL)
2809
0
  return false;
2810
0
      s->alloced = 1;
2811
0
    }
2812
2813
0
  return _bfd_elf_add_dynamic_tags (info, relocs_exist);
2814
0
}
2815
2816
2817
/* Classify dynamic relocs such that -z combreloc can reorder and combine
2818
   them.  */
2819
static enum elf_reloc_type_class
2820
elf32_arc_reloc_type_class (const struct bfd_link_info *info ATTRIBUTE_UNUSED,
2821
          const asection *rel_sec ATTRIBUTE_UNUSED,
2822
          const Elf_Internal_Rela *rela)
2823
0
{
2824
0
  switch ((int) ELF32_R_TYPE (rela->r_info))
2825
0
    {
2826
0
    case R_ARC_RELATIVE:
2827
0
      return reloc_class_relative;
2828
0
    case R_ARC_JMP_SLOT:
2829
0
      return reloc_class_plt;
2830
0
    case R_ARC_COPY:
2831
0
      return reloc_class_copy;
2832
    /* TODO: Needed in future to support ifunc.  */
2833
    /*
2834
    case R_ARC_IRELATIVE:
2835
      return reloc_class_ifunc;
2836
    */
2837
0
    default:
2838
0
      return reloc_class_normal;
2839
0
    }
2840
0
}
2841
2842
static const struct elf_size_info arc_elf32_size_info =
2843
{
2844
  sizeof (Elf32_External_Ehdr),
2845
  sizeof (Elf32_External_Phdr),
2846
  sizeof (Elf32_External_Shdr),
2847
  sizeof (Elf32_External_Rel),
2848
  sizeof (Elf32_External_Rela),
2849
  sizeof (Elf32_External_Sym),
2850
  sizeof (Elf32_External_Dyn),
2851
  sizeof (Elf_External_Note),
2852
  4,
2853
  1,
2854
  32, 2,
2855
  ELFCLASS32, EV_CURRENT,
2856
  bfd_elf32_write_out_phdrs,
2857
  bfd_elf32_write_shdrs_and_ehdr,
2858
  bfd_elf32_checksum_contents,
2859
  bfd_elf32_write_relocs,
2860
  bfd_elf32_swap_symbol_in,
2861
  bfd_elf32_swap_symbol_out,
2862
  bfd_elf32_slurp_reloc_table,
2863
  bfd_elf32_slurp_symbol_table,
2864
  bfd_elf32_swap_dyn_in,
2865
  bfd_elf32_swap_dyn_out,
2866
  bfd_elf32_swap_reloc_in,
2867
  bfd_elf32_swap_reloc_out,
2868
  bfd_elf32_swap_reloca_in,
2869
  bfd_elf32_swap_reloca_out
2870
};
2871
2872
#define elf_backend_size_info   arc_elf32_size_info
2873
2874
/* GDB expects general purpose registers to be in section .reg.  However Linux
2875
   kernel doesn't create this section and instead writes registers to NOTE
2876
   section.  It is up to the binutils to create a pseudo-section .reg from the
2877
   contents of NOTE.  Also BFD will read pid and signal number from NOTE.  This
2878
   function relies on offsets inside elf_prstatus structure in Linux to be
2879
   stable.  */
2880
2881
static bool
2882
elf32_arc_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
2883
1
{
2884
1
  int offset;
2885
1
  size_t size;
2886
2887
1
  switch (note->descsz)
2888
1
    {
2889
1
    default:
2890
1
      return false;
2891
2892
0
    case 236: /* sizeof (struct elf_prstatus) on Linux/arc.  */
2893
      /* pr_cursig */
2894
0
      elf_tdata (abfd)->core->signal = bfd_get_16 (abfd, note->descdata + 12);
2895
      /* pr_pid */
2896
0
      elf_tdata (abfd)->core->lwpid = bfd_get_32 (abfd, note->descdata + 24);
2897
      /* pr_regs */
2898
0
      offset = 72;
2899
0
      size = (40 * 4); /* There are 40 registers in user_regs_struct.  */
2900
0
      break;
2901
1
    }
2902
  /* Make a ".reg/999" section.  */
2903
0
  return _bfd_elfcore_make_pseudosection (abfd, ".reg", size,
2904
0
            note->descpos + offset);
2905
1
}
2906
2907
/* Determine whether an object attribute tag takes an integer, a
2908
   string or both.  */
2909
2910
static int
2911
elf32_arc_obj_attrs_arg_type (obj_attr_tag_t tag)
2912
0
{
2913
0
  if (tag == Tag_ARC_CPU_name
2914
0
     || tag == Tag_ARC_ISA_config
2915
0
     || tag == Tag_ARC_ISA_apex)
2916
0
    return ATTR_TYPE_FLAG_STR_VAL;
2917
0
  else if (tag < (Tag_ARC_ISA_mpy_option + 1))
2918
0
    return ATTR_TYPE_FLAG_INT_VAL;
2919
0
  else
2920
0
    return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
2921
0
}
2922
2923
/* Attribute numbers >=14 can be safely ignored.  */
2924
2925
static bool
2926
elf32_arc_obj_attrs_handle_unknown (bfd *abfd, int tag)
2927
0
{
2928
0
  if ((tag & 127) < (Tag_ARC_ISA_mpy_option + 1))
2929
0
    {
2930
0
      _bfd_error_handler
2931
0
  (_("%pB: unknown mandatory ARC object attribute %d"),
2932
0
   abfd, tag);
2933
0
      bfd_set_error (bfd_error_bad_value);
2934
0
      return false;
2935
0
    }
2936
0
  else
2937
0
    {
2938
0
      _bfd_error_handler
2939
0
  (_("warning: %pB: unknown ARC object attribute %d"),
2940
0
   abfd, tag);
2941
0
      return true;
2942
0
    }
2943
0
}
2944
2945
/* Handle an ARC specific section when reading an object file.  This is
2946
   called when bfd_section_from_shdr finds a section with an unknown
2947
   type.  */
2948
2949
static bool
2950
elf32_arc_section_from_shdr (bfd *abfd,
2951
           Elf_Internal_Shdr * hdr,
2952
           const char *name,
2953
           int shindex)
2954
133
{
2955
133
  switch (hdr->sh_type)
2956
133
    {
2957
9
    case 0x0c: /* MWDT specific section, don't complain about it.  */
2958
9
    case SHT_ARC_ATTRIBUTES:
2959
9
      break;
2960
2961
124
    default:
2962
124
      return false;
2963
133
    }
2964
2965
9
  if (!_bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex))
2966
0
    return false;
2967
2968
9
  return true;
2969
9
}
2970
2971
/* Relaxation hook.
2972
2973
   These are the current relaxing opportunities available:
2974
2975
   * R_ARC_GOTPC32 => R_ARC_PCREL.
2976
2977
*/
2978
2979
static bool
2980
arc_elf_relax_section (bfd *abfd, asection *sec,
2981
           struct bfd_link_info *link_info, bool *again)
2982
0
{
2983
0
  Elf_Internal_Shdr *symtab_hdr;
2984
0
  Elf_Internal_Rela *internal_relocs;
2985
0
  Elf_Internal_Rela *irel, *irelend;
2986
0
  bfd_byte *contents = NULL;
2987
0
  Elf_Internal_Sym *isymbuf = NULL;
2988
2989
  /* Assume nothing changes.  */
2990
0
  *again = false;
2991
2992
  /* We don't have to do anything for a relocatable link, if this
2993
     section does not have relocs, or if this is not a code
2994
     section.  */
2995
0
  if (bfd_link_relocatable (link_info)
2996
0
      || sec->reloc_count == 0
2997
0
      || (sec->flags & SEC_RELOC) == 0
2998
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
2999
0
      || (sec->flags & SEC_CODE) == 0)
3000
0
    return true;
3001
3002
0
  symtab_hdr = &elf_symtab_hdr (abfd);
3003
3004
  /* Get a copy of the native relocations.  */
3005
0
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
3006
0
                                               link_info->keep_memory);
3007
0
  if (internal_relocs == NULL)
3008
0
    goto error_return;
3009
3010
  /* Walk through them looking for relaxing opportunities.  */
3011
0
  irelend = internal_relocs + sec->reloc_count;
3012
0
  for (irel = internal_relocs; irel < irelend; irel++)
3013
0
    {
3014
      /* If this isn't something that can be relaxed, then ignore
3015
         this reloc.  */
3016
0
      if (ELF32_R_TYPE (irel->r_info) != (int) R_ARC_GOTPC32)
3017
0
        continue;
3018
3019
      /* Get the section contents if we haven't done so already.  */
3020
0
      if (contents == NULL)
3021
0
        {
3022
          /* Get cached copy if it exists.  */
3023
0
          if (elf_section_data (sec)->this_hdr.contents != NULL)
3024
0
            contents = elf_section_data (sec)->this_hdr.contents;
3025
          /* Go get them off disk.  */
3026
0
          else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
3027
0
            goto error_return;
3028
0
        }
3029
3030
      /* Read this BFD's local symbols if we haven't done so already.  */
3031
0
      if (isymbuf == NULL && symtab_hdr->sh_info != 0)
3032
0
        {
3033
0
          isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
3034
0
          if (isymbuf == NULL)
3035
0
            isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
3036
0
                                            symtab_hdr->sh_info, 0,
3037
0
                                            NULL, NULL, NULL);
3038
0
          if (isymbuf == NULL)
3039
0
            goto error_return;
3040
0
        }
3041
3042
0
      struct elf_link_hash_entry *htop = NULL;
3043
3044
0
      if (ELF32_R_SYM (irel->r_info) >= symtab_hdr->sh_info)
3045
0
  {
3046
    /* An external symbol.  */
3047
0
    unsigned int indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
3048
0
    htop = elf_sym_hashes (abfd)[indx];
3049
0
  }
3050
3051
0
      if (ELF32_R_TYPE (irel->r_info) == (int) R_ARC_GOTPC32
3052
0
    && SYMBOL_REFERENCES_LOCAL (link_info, htop))
3053
0
  {
3054
0
    unsigned int code;
3055
3056
    /* Get the opcode.  */
3057
0
    code = bfd_get_32_me (abfd, contents + irel->r_offset - 4);
3058
3059
    /* Note that we've changed the relocs, section contents, etc.  */
3060
0
    elf_section_data (sec)->relocs = internal_relocs;
3061
0
    elf_section_data (sec)->this_hdr.contents = contents;
3062
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
3063
3064
    /* Fix the relocation's type.  */
3065
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_ARC_PC32);
3066
3067
    /* ld rA,[pcl,symbol@tgot] -> add rA,pcl,symbol@pcl.  */
3068
    /* 0010 0bbb aa11 0ZZX DBBB 1111 10AA AAAA.
3069
             111 00    000 0111        xx xxxx*/
3070
0
    code &= ~0x27307F80;
3071
0
    BFD_ASSERT (code <= 62UL);
3072
0
    code |= 0x27007F80;
3073
3074
    /* Write back the new instruction.  */
3075
0
    bfd_put_32_me (abfd, code, contents + irel->r_offset - 4);
3076
3077
    /* The size isn't changed, don't redo.  */
3078
0
    *again = false;
3079
0
  }
3080
0
    }
3081
3082
0
  if (isymbuf != NULL
3083
0
      && symtab_hdr->contents != (unsigned char *) isymbuf)
3084
0
    {
3085
0
      if (!link_info->keep_memory)
3086
0
        free (isymbuf);
3087
0
      else
3088
       /* Cache the symbols for elf_link_input_bfd.  */
3089
0
       symtab_hdr->contents = (unsigned char *) isymbuf;
3090
0
    }
3091
3092
0
  if (contents != NULL
3093
0
      && elf_section_data (sec)->this_hdr.contents != contents)
3094
0
    {
3095
0
      if (!link_info->keep_memory)
3096
0
        free (contents);
3097
0
      else
3098
       /* Cache the section contents for elf_link_input_bfd.  */
3099
0
       elf_section_data (sec)->this_hdr.contents = contents;
3100
0
    }
3101
3102
0
  if (elf_section_data (sec)->relocs != internal_relocs)
3103
0
    free (internal_relocs);
3104
3105
0
  return true;
3106
3107
0
 error_return:
3108
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
3109
0
    free (isymbuf);
3110
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
3111
0
    free (contents);
3112
0
  if (elf_section_data (sec)->relocs != internal_relocs)
3113
0
    free (internal_relocs);
3114
3115
  return false;
3116
0
}
3117
3118
#define TARGET_LITTLE_SYM   arc_elf32_le_vec
3119
#define TARGET_LITTLE_NAME  "elf32-littlearc"
3120
#define TARGET_BIG_SYM      arc_elf32_be_vec
3121
#define TARGET_BIG_NAME     "elf32-bigarc"
3122
#define ELF_ARCH      bfd_arch_arc
3123
#define ELF_TARGET_ID     ARC_ELF_DATA
3124
#define ELF_MACHINE_CODE    EM_ARC_COMPACT
3125
#define ELF_MACHINE_ALT1    EM_ARC_COMPACT2
3126
#define ELF_MAXPAGESIZE     0x2000
3127
3128
#define bfd_elf32_bfd_link_hash_table_create  arc_elf_link_hash_table_create
3129
3130
#define bfd_elf32_bfd_merge_private_bfd_data    arc_elf_merge_private_bfd_data
3131
#define bfd_elf32_bfd_reloc_type_lookup   arc_elf32_bfd_reloc_type_lookup
3132
#define bfd_elf32_bfd_set_private_flags   arc_elf_set_private_flags
3133
#define bfd_elf32_bfd_print_private_bfd_data    arc_elf_print_private_bfd_data
3134
#define bfd_elf32_bfd_copy_private_bfd_data     arc_elf_copy_private_bfd_data
3135
#define bfd_elf32_bfd_relax_section   arc_elf_relax_section
3136
3137
#define elf_info_to_howto_rel        arc_info_to_howto_rel
3138
#define elf_backend_object_p         arc_elf_object_p
3139
#define elf_backend_final_write_processing   arc_elf_final_write_processing
3140
3141
#define elf_backend_relocate_section       elf_arc_relocate_section
3142
#define elf_backend_check_relocs       elf_arc_check_relocs
3143
#define elf_backend_create_dynamic_sections  _bfd_elf_create_dynamic_sections
3144
3145
#define elf_backend_reloc_type_class    elf32_arc_reloc_type_class
3146
3147
#define elf_backend_adjust_dynamic_symbol    elf_arc_adjust_dynamic_symbol
3148
#define elf_backend_finish_dynamic_symbol    elf_arc_finish_dynamic_symbol
3149
3150
#define elf_backend_finish_dynamic_sections  elf_arc_finish_dynamic_sections
3151
#define elf_backend_late_size_sections       elf_arc_late_size_sections
3152
3153
#define elf_backend_can_gc_sections 1
3154
#define elf_backend_want_got_plt  1
3155
#define elf_backend_plt_readonly  1
3156
#define elf_backend_rela_plts_and_copies_p 1
3157
#define elf_backend_want_plt_sym  0
3158
#define elf_backend_got_header_size 12
3159
#define elf_backend_dtrel_excludes_plt  1
3160
3161
#define elf_backend_may_use_rel_p 0
3162
#define elf_backend_may_use_rela_p  1
3163
#define elf_backend_default_use_rela_p  1
3164
3165
#define elf_backend_grok_prstatus elf32_arc_grok_prstatus
3166
3167
#define elf_backend_default_execstack 0
3168
3169
#undef  elf_backend_obj_attrs_vendor
3170
#define elf_backend_obj_attrs_vendor    "ARC"
3171
#undef  elf_backend_obj_attrs_section
3172
#define elf_backend_obj_attrs_section   ".ARC.attributes"
3173
#undef  elf_backend_obj_attrs_arg_type
3174
#define elf_backend_obj_attrs_arg_type    elf32_arc_obj_attrs_arg_type
3175
#undef  elf_backend_obj_attrs_section_type
3176
#define elf_backend_obj_attrs_section_type  SHT_ARC_ATTRIBUTES
3177
#define elf_backend_obj_attrs_handle_unknown  elf32_arc_obj_attrs_handle_unknown
3178
3179
#define elf_backend_section_from_shdr   elf32_arc_section_from_shdr
3180
3181
#include "elf32-target.h"