Coverage Report

Created: 2023-08-28 06:25

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