elf32-arc.c 77.1 KB
Newer Older
Richard Henderson's avatar
Richard Henderson committed
1
/* ARC-specific support for 32-bit ELF
Alan Modra's avatar
Alan Modra committed
2
   Copyright (C) 1994-2016 Free Software Foundation, Inc.
Nick Clifton's avatar
Nick Clifton committed
3
   Contributed by Cupertino Miranda (cmiranda@synopsys.com).
Richard Henderson's avatar
Richard Henderson committed
4

5
   This file is part of BFD, the Binary File Descriptor library.
Richard Henderson's avatar
Richard Henderson committed
6

7
8
   This program is free software; you can redistribute it and/or modify
   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.
Richard Henderson's avatar
Richard Henderson committed
11

12
13
14
15
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
Richard Henderson's avatar
Richard Henderson committed
16

17
18
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
19
20
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
   MA 02110-1301, USA.  */
Richard Henderson's avatar
Richard Henderson committed
21
22

#include "sysdep.h"
Alan Modra's avatar
   
Alan Modra committed
23
#include "bfd.h"
Richard Henderson's avatar
Richard Henderson committed
24
25
26
#include "libbfd.h"
#include "elf-bfd.h"
#include "elf/arc.h"
27
#include "libiberty.h"
Nick Clifton's avatar
Nick Clifton committed
28
#include "opcode/arc-func.h"
29
#include "opcode/arc.h"
30
#include "arc-plt.h"
Nick Clifton's avatar
Nick Clifton committed
31

32
33
34
35
36
37
38
39
#ifdef DEBUG
# define PR_DEBUG(fmt, args...) fprintf (stderr, fmt, ##args)
#else
# define PR_DEBUG(fmt, args...)
#endif

/* #define ARC_ENABLE_DEBUG 1 */
#ifndef ARC_ENABLE_DEBUG
Nick Clifton's avatar
Nick Clifton committed
40
#define ARC_DEBUG(...)
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#else
static char *
name_for_global_symbol (struct elf_link_hash_entry *h)
{
  static char *local_str = "(local)";
  if (h == NULL)
    return local_str;
  else
    return h->root.root.string;
}
#define ARC_DEBUG(args...) fprintf (stderr, ##args)
#endif


#define ADD_RELA(BFD, SECTION, OFFSET, SYM_IDX, TYPE, ADDEND)		\
  {									\
    struct elf_link_hash_table *_htab = elf_hash_table (info);		\
    Elf_Internal_Rela _rel;						\
59
    bfd_byte * _loc;							\
60
									\
61
    _loc = _htab->srel##SECTION->contents				\
62
63
64
65
66
67
      + ((_htab->srel##SECTION->reloc_count)				\
	 * sizeof (Elf32_External_Rela));				\
    _htab->srel##SECTION->reloc_count++;				\
    _rel.r_addend = ADDEND;						\
    _rel.r_offset = (_htab->s##SECTION)->output_section->vma		\
      + (_htab->s##SECTION)->output_offset + OFFSET;			\
68
    BFD_ASSERT ((long) SYM_IDX != -1);					\
69
    _rel.r_info = ELF32_R_INFO (SYM_IDX, TYPE);				\
70
    bfd_elf32_swap_reloca_out (BFD, &_rel, _loc);			\
71
  }
Nick Clifton's avatar
Nick Clifton committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92

struct arc_local_data
{
  bfd_vma	  sdata_begin_symbol_vma;
  asection *      sdata_output_section;
  bfd_vma	  got_symbol_vma;
};

struct arc_local_data global_arc_data =
{
  .sdata_begin_symbol_vma = 0,
  .sdata_output_section = NULL,
  .got_symbol_vma = 0,
};

struct dynamic_sections
{
  bfd_boolean	  initialized;
  asection *      sgot;
  asection *      srelgot;
  asection *      sgotplt;
93
  asection *      srelgotplt;
Nick Clifton's avatar
Nick Clifton committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
  asection *      sdyn;
  asection *      splt;
  asection *      srelplt;
};

enum dyn_section_types
{
  got = 0,
  relgot,
  gotplt,
  dyn,
  plt,
  relplt,
  DYN_SECTION_TYPES_END
};

const char * dyn_section_names[DYN_SECTION_TYPES_END] =
{
  ".got",
  ".rela.got",
  ".got.plt",
  ".dynamic",
  ".plt",
  ".rela.plt"
};

120
121
122
123
124
125
126
127
128
129
130
enum tls_type_e
{
  GOT_UNKNOWN = 0,
  GOT_NORMAL,
  GOT_TLS_GD,
  GOT_TLS_IE,
  GOT_TLS_LE
};

enum tls_got_entries
{
131
132
133
134
  TLS_GOT_NONE = 0,
  TLS_GOT_MOD,
  TLS_GOT_OFF,
  TLS_GOT_MOD_AND_OFF
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
};

struct got_entry
{
  struct got_entry *next;
  enum tls_type_e type;
  bfd_vma offset;
  bfd_boolean processed;
  bfd_boolean created_dyn_relocation;
  enum tls_got_entries existing_entries;
};

static void
new_got_entry_to_list (struct got_entry **list,
		       enum tls_type_e type,
		       bfd_vma offset,
		       enum tls_got_entries existing_entries)
{
  /* Find list end.  Avoid having multiple entries of the same
     type.  */
  struct got_entry **p = list;
  while (*p != NULL)
    {
      if ((*p)->type == type)
	return;
      p = &((*p)->next);
    }

  struct got_entry *entry =
		      (struct got_entry *) malloc (sizeof(struct got_entry));

  entry->type = type;
  entry->offset = offset;
  entry->next = NULL;
  entry->processed = FALSE;
  entry->created_dyn_relocation = FALSE;
  entry->existing_entries = existing_entries;

  /* Add the entry to the end of the list.  */
  *p = entry;
}

static bfd_boolean
symbol_has_entry_of_type (struct got_entry *list, enum tls_type_e type)
{
  while (list != NULL)
    {
      if (list->type == type)
	return TRUE;
      list = list->next;
    }

  return FALSE;
}

Nick Clifton's avatar
Nick Clifton committed
190
191
192
193
194
195
196
197
198
199
200
/* The default symbols representing the init and fini dyn values.
   TODO: Check what is the relation of those strings with arclinux.em
   and DT_INIT.  */
#define INIT_SYM_STRING "_init"
#define FINI_SYM_STRING "_fini"

char * init_str = INIT_SYM_STRING;
char * fini_str = FINI_SYM_STRING;

#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
      case VALUE: \
201
	return "R_" #TYPE; \
Nick Clifton's avatar
Nick Clifton committed
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
	break;

static ATTRIBUTE_UNUSED const char *
reloc_type_to_name (unsigned int type)
{
  switch (type)
    {
      #include "elf/arc-reloc.def"

      default:
	return "UNKNOWN";
	break;
    }
}
#undef ARC_RELOC_HOWTO
Richard Henderson's avatar
Richard Henderson committed
217
218
219

/* Try to minimize the amount of space occupied by relocation tables
   on the ROM (not that the ROM won't be swamped by other ELF overhead).  */
220

Nick Clifton's avatar
Nick Clifton committed
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#define USE_REL 1

static ATTRIBUTE_UNUSED bfd_boolean
is_reloc_PC_relative (reloc_howto_type *howto)
{
  return (strstr (howto->name, "PC") != NULL) ? TRUE : FALSE;
}

static bfd_boolean
is_reloc_SDA_relative (reloc_howto_type *howto)
{
  return (strstr (howto->name, "SDA") != NULL) ? TRUE : FALSE;
}

static bfd_boolean
is_reloc_for_GOT (reloc_howto_type * howto)
{
238
239
  if (strstr (howto->name, "TLS") != NULL)
    return FALSE;
Nick Clifton's avatar
Nick Clifton committed
240
241
242
243
244
245
246
247
248
  return (strstr (howto->name, "GOT") != NULL) ? TRUE : FALSE;
}

static bfd_boolean
is_reloc_for_PLT (reloc_howto_type * howto)
{
  return (strstr (howto->name, "PLT") != NULL) ? TRUE : FALSE;
}

249
250
251
252
253
254
static bfd_boolean
is_reloc_for_TLS (reloc_howto_type *howto)
{
  return (strstr (howto->name, "TLS") != NULL) ? TRUE : FALSE;
}

Nick Clifton's avatar
Nick Clifton committed
255
256
#define arc_bfd_get_8(A,B,C) bfd_get_8(A,B)
#define arc_bfd_get_16(A,B,C) bfd_get_16(A,B)
257
#define arc_bfd_get_32(A,B,C) bfd_get_32(A,B)
Nick Clifton's avatar
Nick Clifton committed
258
259
#define arc_bfd_put_8(A,B,C,D) bfd_put_8(A,B,C)
#define arc_bfd_put_16(A,B,C,D) bfd_put_16(A,B,C)
260
#define arc_bfd_put_32(A,B,C,D) bfd_put_32(A,B,C)
Nick Clifton's avatar
Nick Clifton committed
261

Richard Henderson's avatar
Richard Henderson committed
262

263
static bfd_reloc_status_type
Nick Clifton's avatar
Nick Clifton committed
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
arc_elf_reloc (bfd *abfd ATTRIBUTE_UNUSED,
	       arelent *reloc_entry,
	       asymbol *symbol_in,
	       void *data ATTRIBUTE_UNUSED,
	       asection *input_section,
	       bfd *output_bfd,
	       char ** error_message ATTRIBUTE_UNUSED)
{
  if (output_bfd != NULL)
    {
      reloc_entry->address += input_section->output_offset;

      /* In case of relocateable link and if the reloc is against a
	 section symbol, the addend needs to be adjusted according to
	 where the section symbol winds up in the output section.  */
      if ((symbol_in->flags & BSF_SECTION_SYM) && symbol_in->section)
	reloc_entry->addend += symbol_in->section->output_offset;

      return bfd_reloc_ok;
    }

  return bfd_reloc_continue;
}


#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  TYPE = VALUE,
enum howto_list
{
#include "elf/arc-reloc.def"
  HOWTO_LIST_LAST
};
#undef ARC_RELOC_HOWTO

#define ARC_RELOC_HOWTO(TYPE, VALUE, RSIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
299
  [TYPE] = HOWTO (R_##TYPE, 0, RSIZE, BITSIZE, FALSE, 0, complain_overflow_##OVERFLOW, arc_elf_reloc, "R_" #TYPE, FALSE, 0, 0, FALSE),
Nick Clifton's avatar
Nick Clifton committed
300
301
302
303

static struct reloc_howto_struct elf_arc_howto_table[] =
{
#include "elf/arc-reloc.def"
304
305
/* Example of what is generated by the preprocessor.  Currently kept as an
   example.
Nick Clifton's avatar
Nick Clifton committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
 HOWTO (R_ARC_NONE, // Type.
    0, // Rightshift.
    2, // Size (0 = byte, 1 = short, 2 = long).
    32, // Bitsize.
    FALSE, // PC_relative.
    0, // Bitpos.
    complain_overflow_bitfield, // Complain_on_overflow.
    bfd_elf_generic_reloc, // Special_function.
    "R_ARC_NONE", // Name.
    TRUE, // Partial_inplace.
    0, // Src_mask.
    0, // Dst_mask.
    FALSE), // PCrel_offset.
*/
};
#undef ARC_RELOC_HOWTO

static void arc_elf_howto_init (void)
{
#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
326
  elf_arc_howto_table[TYPE].pc_relative = \
Alan Modra's avatar
Alan Modra committed
327
    (strstr (#FORMULA, " P ") != NULL || strstr (#FORMULA, " PDATA ") != NULL); \
328
329
330
331
332
333
  elf_arc_howto_table[TYPE].dst_mask = RELOC_FUNCTION(0, ~0); \
  /* Only 32 bit data relocations should be marked as ME.  */ \
  if (strstr (#FORMULA, " ME ") != NULL) \
    { \
      BFD_ASSERT (SIZE == 2); \
    }
Nick Clifton's avatar
Nick Clifton committed
334

335
#include "elf/arc-reloc.def"
336

337
}
Nick Clifton's avatar
Nick Clifton committed
338
#undef ARC_RELOC_HOWTO
339

Nick Clifton's avatar
Nick Clifton committed
340
341
342
343
344

#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  [TYPE] = VALUE,
const int howto_table_lookup[] =
{
345
#include "elf/arc-reloc.def"
Richard Henderson's avatar
Richard Henderson committed
346
};
Nick Clifton's avatar
Nick Clifton committed
347
348
#undef ARC_RELOC_HOWTO

349
350
351
352
353
354
355
static reloc_howto_type *
arc_elf_howto (unsigned int r_type)
{
  if (elf_arc_howto_table[R_ARC_32].dst_mask == 0)
    arc_elf_howto_init ();
  return &elf_arc_howto_table[r_type];
}
Richard Henderson's avatar
Richard Henderson committed
356
357
358
359
360

/* Map BFD reloc types to ARC ELF reloc types.  */

struct arc_reloc_map
{
361
362
  bfd_reloc_code_real_type  bfd_reloc_val;
  unsigned char             elf_reloc_val;
Richard Henderson's avatar
Richard Henderson committed
363
364
};

Nick Clifton's avatar
Nick Clifton committed
365
366
#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  { BFD_RELOC_##TYPE, R_##TYPE },
Richard Henderson's avatar
Richard Henderson committed
367
368
static const struct arc_reloc_map arc_reloc_map[] =
{
369
370
#include "elf/arc-reloc.def"

Nick Clifton's avatar
Nick Clifton committed
371
372
373
374
375
  {BFD_RELOC_NONE,  R_ARC_NONE},
  {BFD_RELOC_8,  R_ARC_8},
  {BFD_RELOC_16, R_ARC_16},
  {BFD_RELOC_24, R_ARC_24},
  {BFD_RELOC_32, R_ARC_32},
Richard Henderson's avatar
Richard Henderson committed
376
};
Nick Clifton's avatar
Nick Clifton committed
377
#undef ARC_RELOC_HOWTO
Richard Henderson's avatar
Richard Henderson committed
378

379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
typedef ATTRIBUTE_UNUSED bfd_vma (*replace_func) (unsigned, int ATTRIBUTE_UNUSED);

#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  case TYPE: \
    func = (void *) RELOC_FUNCTION; \
    break;
static replace_func
get_replace_function (bfd *abfd, unsigned int r_type)
{
  void *func = NULL;

  switch (r_type)
    {
      #include "elf/arc-reloc.def"
    }

  if (func == replace_bits24 && bfd_big_endian (abfd))
    return (replace_func) replace_bits24_be;

  return (replace_func) func;
}
#undef ARC_RELOC_HOWTO

Richard Henderson's avatar
Richard Henderson committed
402
static reloc_howto_type *
403
arc_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
404
				 bfd_reloc_code_real_type code)
Richard Henderson's avatar
Richard Henderson committed
405
406
407
{
  unsigned int i;

408
  for (i = ARRAY_SIZE (arc_reloc_map); i--;)
Nick Clifton's avatar
Nick Clifton committed
409
410
    {
      if (arc_reloc_map[i].bfd_reloc_val == code)
411
	return arc_elf_howto (arc_reloc_map[i].elf_reloc_val);
Nick Clifton's avatar
Nick Clifton committed
412
    }
413

Richard Henderson's avatar
Richard Henderson committed
414
415
416
  return NULL;
}

417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
/* Function to set the ELF flag bits.  */
static bfd_boolean
arc_elf_set_private_flags (bfd *abfd, flagword flags)
{
  elf_elfheader (abfd)->e_flags = flags;
  elf_flags_init (abfd) = TRUE;
  return TRUE;
}

/* Print private flags.  */
static bfd_boolean
arc_elf_print_private_bfd_data (bfd *abfd, void * ptr)
{
  FILE *file = (FILE *) ptr;
  flagword flags;

  BFD_ASSERT (abfd != NULL && ptr != NULL);

  /* Print normal ELF private data.  */
  _bfd_elf_print_private_bfd_data (abfd, ptr);

  flags = elf_elfheader (abfd)->e_flags;
  fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);

  switch (flags & EF_ARC_MACH_MSK)
    {
    case EF_ARC_CPU_ARCV2HS : fprintf (file, " -mcpu=ARCv2HS");    break;
    case EF_ARC_CPU_ARCV2EM : fprintf (file, " -mcpu=ARCv2EM");    break;
    case E_ARC_MACH_ARC600  : fprintf (file, " -mcpu=ARC600");     break;
    case E_ARC_MACH_ARC601  : fprintf (file, " -mcpu=ARC601");     break;
    case E_ARC_MACH_ARC700  : fprintf (file, " -mcpu=ARC700");     break;
    default:
      fprintf (file, "-mcpu=unknown");
      break;
    }

  switch (flags & EF_ARC_OSABI_MSK)
    {
    case E_ARC_OSABI_ORIG : fprintf (file, " (ABI:legacy)"); break;
    case E_ARC_OSABI_V2   : fprintf (file, " (ABI:v2)");     break;
    case E_ARC_OSABI_V3   : fprintf (file, " (ABI:v3)");     break;
    default:
      fprintf (file, "(ABI:unknown)");
      break;
    }

  fputc ('\n', file);
  return TRUE;
}

/* Copy backend specific data from one object module to another.  */

static bfd_boolean
arc_elf_copy_private_bfd_data (bfd *ibfd, bfd *obfd)
{
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
    return TRUE;

  BFD_ASSERT (!elf_flags_init (obfd)
	      || elf_elfheader (obfd)->e_flags == elf_elfheader (ibfd)->e_flags);

  elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
  elf_flags_init (obfd) = TRUE;

  /* Copy object attributes.  */
  _bfd_elf_copy_obj_attributes (ibfd, obfd);

  return _bfd_elf_copy_private_bfd_data (ibfd, obfd);
}

488
static reloc_howto_type *
489
490
bfd_elf32_bfd_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED,
				 const char *r_name)
491
492
493
{
  unsigned int i;

Nick Clifton's avatar
Nick Clifton committed
494
  for (i = 0; i < ARRAY_SIZE (elf_arc_howto_table); i++)
495
496
    if (elf_arc_howto_table[i].name != NULL
	&& strcasecmp (elf_arc_howto_table[i].name, r_name) == 0)
497
      return arc_elf_howto (i);
498
499
500
501

  return NULL;
}

Nick Clifton's avatar
Nick Clifton committed
502
/* Set the howto pointer for an ARC ELF reloc.  */
503

Richard Henderson's avatar
Richard Henderson committed
504
static void
Nick Clifton's avatar
Nick Clifton committed
505
506
507
arc_info_to_howto_rel (bfd * abfd ATTRIBUTE_UNUSED,
		       arelent * cache_ptr,
		       Elf_Internal_Rela * dst)
Richard Henderson's avatar
Richard Henderson committed
508
509
510
511
{
  unsigned int r_type;

  r_type = ELF32_R_TYPE (dst->r_info);
Nick Clifton's avatar
Nick Clifton committed
512
  BFD_ASSERT (r_type < (unsigned int) R_ARC_max);
513
  cache_ptr->howto = arc_elf_howto (r_type);
Richard Henderson's avatar
Richard Henderson committed
514
515
}

516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/* Merge backend specific data from an object file to the output
   object file when linking.  */

static bfd_boolean
arc_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
{
  unsigned short mach_ibfd;
  static unsigned short mach_obfd = EM_NONE;
  flagword out_flags;
  flagword in_flags;
  asection *sec;

   /* Check if we have the same endianess.  */
  if (! _bfd_generic_verify_endian_match (ibfd, obfd))
    {
      _bfd_error_handler (_("ERROR: Endian Match failed. Attempting to link "
			    "%B with binary %s of opposite endian-ness"),
			  ibfd, bfd_get_filename (obfd));
      return FALSE;
    }

  /* Collect ELF flags.  */
  in_flags = elf_elfheader (ibfd)->e_flags & EF_ARC_MACH_MSK;
  out_flags = elf_elfheader (obfd)->e_flags & EF_ARC_MACH_MSK;

  if (!elf_flags_init (obfd)) /* First call, no flags set.  */
    {
      elf_flags_init (obfd) = TRUE;
      out_flags = in_flags;
    }

  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
      || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
    return TRUE;

  /* Check to see if the input BFD actually contains any sections.  Do
     not short-circuit dynamic objects; their section list may be
     emptied by elf_link_add_object_symbols.  */
  if (!(ibfd->flags & DYNAMIC))
    {
      bfd_boolean null_input_bfd = TRUE;
      bfd_boolean only_data_sections = TRUE;

      for (sec = ibfd->sections; sec != NULL; sec = sec->next)
	{
	  if ((bfd_get_section_flags (ibfd, sec)
	       & (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
	      == (SEC_LOAD | SEC_CODE | SEC_HAS_CONTENTS))
	    only_data_sections = FALSE;

	  null_input_bfd = FALSE;
	}

      if (null_input_bfd || only_data_sections)
	return TRUE;
    }

  /* Complain about various flag/architecture mismatches.  */
  mach_ibfd = elf_elfheader (ibfd)->e_machine;
  if (mach_obfd == EM_NONE)
    {
      mach_obfd = mach_ibfd;
    }
  else
    {
      if (mach_ibfd != mach_obfd)
	{
	  _bfd_error_handler (_("ERROR: Attempting to link %B "
				"with a binary %s of different architecture"),
			      ibfd, bfd_get_filename (obfd));
	  return FALSE;
	}
      else if (in_flags != out_flags)
	{
	  /* Warn if different flags.  */
	  (*_bfd_error_handler)
	    (_("%s: uses different e_flags (0x%lx) fields than "
	       "previous modules (0x%lx)"),
	     bfd_get_filename (ibfd), (long)in_flags, (long)out_flags);
	  if (in_flags && out_flags)
	    return FALSE;
	  /* MWDT doesnt set the eflags hence make sure we choose the
	     eflags set by gcc.  */
	  in_flags = in_flags > out_flags ? in_flags : out_flags;
	}
    }

  /* Update the flags.  */
  elf_elfheader (obfd)->e_flags = in_flags;

  if (bfd_get_mach (obfd) < bfd_get_mach (ibfd))
    {
      return bfd_set_arch_mach (obfd, bfd_arch_arc, bfd_get_mach (ibfd));
    }

  return TRUE;
}

Richard Henderson's avatar
Richard Henderson committed
614
/* Set the right machine number for an ARC ELF file.  */
615
static bfd_boolean
Nick Clifton's avatar
Nick Clifton committed
616
arc_elf_object_p (bfd * abfd)
Richard Henderson's avatar
Richard Henderson committed
617
{
Nick Clifton's avatar
Nick Clifton committed
618
619
620
621
622
623
  /* Make sure this is initialised, or you'll have the potential of passing
     garbage---or misleading values---into the call to
     bfd_default_set_arch_mach ().  */
  int		  mach = bfd_mach_arc_arc700;
  unsigned long   arch = elf_elfheader (abfd)->e_flags & EF_ARC_MACH_MSK;
  unsigned	  e_machine = elf_elfheader (abfd)->e_machine;
Richard Henderson's avatar
Richard Henderson committed
624

Nick Clifton's avatar
Nick Clifton committed
625
  if (e_machine == EM_ARC_COMPACT || e_machine == EM_ARC_COMPACT2)
Richard Henderson's avatar
Richard Henderson committed
626
    {
627
628
      switch (arch)
	{
Nick Clifton's avatar
Nick Clifton committed
629
630
631
632
633
634
635
636
637
	  case E_ARC_MACH_ARC600:
	    mach = bfd_mach_arc_arc600;
	    break;
	  case E_ARC_MACH_ARC601:
	    mach = bfd_mach_arc_arc601;
	    break;
	  case E_ARC_MACH_ARC700:
	    mach = bfd_mach_arc_arc700;
	    break;
638
639
640
	  case E_ARC_MACH_NPS400:
	    mach = bfd_mach_arc_nps400;
	    break;
Nick Clifton's avatar
Nick Clifton committed
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
	  case EF_ARC_CPU_ARCV2HS:
	  case EF_ARC_CPU_ARCV2EM:
	    mach = bfd_mach_arc_arcv2;
	    break;
	  default:
	    mach = (e_machine == EM_ARC_COMPACT) ?
	      bfd_mach_arc_arc700 : bfd_mach_arc_arcv2;
	    break;
	}
    }
  else
    {
      if (e_machine == EM_ARC)
	{
	  (*_bfd_error_handler)
	    (_("Error: The ARC4 architecture is no longer supported.\n"));
	  return FALSE;
	}
      else
	{
	  (*_bfd_error_handler)
	    (_("Warning: unset or old architecture flags. \n"
	       "	       Use default machine.\n"));
664
	}
Richard Henderson's avatar
Richard Henderson committed
665
    }
Nick Clifton's avatar
Nick Clifton committed
666

667
  return bfd_default_set_arch_mach (abfd, bfd_arch_arc, mach);
Richard Henderson's avatar
Richard Henderson committed
668
669
670
671
672
673
}

/* The final processing done just before writing out an ARC ELF object file.
   This gets the ARC architecture right based on the machine number.  */

static void
674
675
arc_elf_final_write_processing (bfd * abfd,
				bfd_boolean linker ATTRIBUTE_UNUSED)
Richard Henderson's avatar
Richard Henderson committed
676
{
Nick Clifton's avatar
Nick Clifton committed
677
  unsigned long emf;
Richard Henderson's avatar
Richard Henderson committed
678

679
  switch (bfd_get_mach (abfd))
Richard Henderson's avatar
Richard Henderson committed
680
    {
Nick Clifton's avatar
Nick Clifton committed
681
682
    case bfd_mach_arc_arc600:
      emf = EM_ARC_COMPACT;
683
      break;
Nick Clifton's avatar
Nick Clifton committed
684
685
    case bfd_mach_arc_arc601:
      emf = EM_ARC_COMPACT;
686
      break;
Nick Clifton's avatar
Nick Clifton committed
687
688
    case bfd_mach_arc_arc700:
      emf = EM_ARC_COMPACT;
689
      break;
690
691
692
    case bfd_mach_arc_nps400:
      emf = EM_ARC_COMPACT;
      break;
Nick Clifton's avatar
Nick Clifton committed
693
694
    case bfd_mach_arc_arcv2:
      emf = EM_ARC_COMPACT2;
695
      break;
Nick Clifton's avatar
Nick Clifton committed
696
697
    default:
      abort ();
Richard Henderson's avatar
Richard Henderson committed
698
    }
699

Nick Clifton's avatar
Nick Clifton committed
700
  elf_elfheader (abfd)->e_machine = emf;
Claudiu Zissulescu's avatar
Claudiu Zissulescu committed
701

Nick Clifton's avatar
Nick Clifton committed
702
703
  /* Record whatever is the current syscall ABI version.  */
  elf_elfheader (abfd)->e_flags |= E_ARC_OSABI_CURRENT;
Richard Henderson's avatar
Richard Henderson committed
704
705
}

Nick Clifton's avatar
Nick Clifton committed
706
707
708
709
#define BFD_DEBUG_PIC(...)

struct arc_relocation_data
{
710
711
712
  bfd_signed_vma  reloc_offset;
  bfd_signed_vma  reloc_addend;
  bfd_signed_vma  got_offset_value;
Nick Clifton's avatar
Nick Clifton committed
713

714
  bfd_signed_vma  sym_value;
Nick Clifton's avatar
Nick Clifton committed
715
716
717
718
719
720
  asection *	  sym_section;

  reloc_howto_type *howto;

  asection *	  input_section;

721
  bfd_signed_vma  sdata_begin_symbol_vma;
Nick Clifton's avatar
Nick Clifton committed
722
  bfd_boolean	  sdata_begin_symbol_vma_set;
723
  bfd_signed_vma  got_symbol_vma;
Nick Clifton's avatar
Nick Clifton committed
724
725

  bfd_boolean	  should_relocate;
726
727

  const char *    symbol_name;
Nick Clifton's avatar
Nick Clifton committed
728
729
730
731
732
};

static void
debug_arc_reloc (struct arc_relocation_data reloc_data)
{
733
  PR_DEBUG ("Reloc type=%s, should_relocate = %s\n",
Nick Clifton's avatar
Nick Clifton committed
734
735
	   reloc_data.howto->name,
	   reloc_data.should_relocate ? "true" : "false");
736
  PR_DEBUG ("  offset = 0x%x, addend = 0x%x\n",
Nick Clifton's avatar
Nick Clifton committed
737
738
	   (unsigned int) reloc_data.reloc_offset,
	   (unsigned int) reloc_data.reloc_addend);
739
740
  PR_DEBUG (" Symbol:\n");
  PR_DEBUG ("  value = 0x%08x\n",
Nick Clifton's avatar
Nick Clifton committed
741
742
743
	   (unsigned int) reloc_data.sym_value);
  if (reloc_data.sym_section != NULL)
    {
744
745
746
      PR_DEBUG ("IN IF\n");
      PR_DEBUG (
	       "  section name = %s, output_offset 0x%08x",
Nick Clifton's avatar
Nick Clifton committed
747
	       reloc_data.sym_section->name,
748
749
750
751
752
753
754
755
756
	       (unsigned int) reloc_data.sym_section->output_offset);
      if (reloc_data.sym_section->output_section != NULL)
	{
	  PR_DEBUG (
		   ", output_section->vma = 0x%08x",
		   ((unsigned int) reloc_data.sym_section->output_section->vma));
	}

      PR_DEBUG ( "\n");
Nick Clifton's avatar
Nick Clifton committed
757
758
    }
  else
759
760
761
    {
      PR_DEBUG ( "  symbol section is NULL\n");
    }
Nick Clifton's avatar
Nick Clifton committed
762

763
  PR_DEBUG ( " Input_section:\n");
Nick Clifton's avatar
Nick Clifton committed
764
765
  if (reloc_data.input_section != NULL)
    {
766
      PR_DEBUG (
Nick Clifton's avatar
Nick Clifton committed
767
768
769
770
	       "  section name = %s, output_offset 0x%08x, output_section->vma = 0x%08x\n",
	       reloc_data.input_section->name,
	       (unsigned int) reloc_data.input_section->output_offset,
	       (unsigned int) reloc_data.input_section->output_section->vma);
771
      PR_DEBUG ( "  changed_address = 0x%08x\n",
Nick Clifton's avatar
Nick Clifton committed
772
773
774
775
776
	       (unsigned int) (reloc_data.input_section->output_section->vma +
	       reloc_data.input_section->output_offset +
	       reloc_data.reloc_offset));
    }
  else
777
778
779
    {
      PR_DEBUG ( "	input section is NULL\n");
    }
Nick Clifton's avatar
Nick Clifton committed
780
781
}

782
783
static bfd_vma
middle_endian_convert (bfd_vma insn, bfd_boolean do_it)
784
{
785
786
787
788
789
790
791
  if (do_it)
    {
      insn =
	((insn & 0xffff0000) >> 16) |
	((insn & 0xffff) << 16);
    }
  return insn;
792
793
}

794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
/* This function is called for relocations that are otherwise marked as NOT
   requiring overflow checks.  In here we perform non-standard checks of
   the relocation value.  */

static inline bfd_reloc_status_type
arc_special_overflow_checks (const struct arc_relocation_data reloc_data,
                             bfd_signed_vma relocation,
			     struct bfd_link_info *info ATTRIBUTE_UNUSED)
{
  switch (reloc_data.howto->type)
    {
    case R_ARC_NPS_CMEM16:
      if (((relocation >> 16) & 0xffff) != NPS_CMEM_HIGH_VALUE)
        {
          if (reloc_data.reloc_addend == 0)
            (*_bfd_error_handler)
              (_("%B(%A+0x%lx): CMEM relocation to `%s' is invalid, "
                 "16 MSB should be 0x%04x (value is 0x%lx)"),
               reloc_data.input_section->owner,
               reloc_data.input_section,
               reloc_data.reloc_offset,
               reloc_data.symbol_name,
               NPS_CMEM_HIGH_VALUE,
               (relocation));
          else
            (*_bfd_error_handler)
              (_("%B(%A+0x%lx): CMEM relocation to `%s+0x%lx' is invalid, "
                 "16 MSB should be 0x%04x (value is 0x%lx)"),
               reloc_data.input_section->owner,
               reloc_data.input_section,
               reloc_data.reloc_offset,
               reloc_data.symbol_name,
               reloc_data.reloc_addend,
               NPS_CMEM_HIGH_VALUE,
               (relocation));
          return bfd_reloc_overflow;
        }
      break;

    default:
      break;
    }

  return bfd_reloc_ok;
}

840
841
842
843
844
#define ME(reloc) (reloc)

#define IS_ME(FORMULA,BFD) ((strstr (FORMULA, "ME") != NULL) \
			    && (!bfd_big_endian (BFD)))

845
#define S ((bfd_signed_vma) (reloc_data.sym_value			\
846
847
	   + (reloc_data.sym_section->output_section != NULL ?		\
	      (reloc_data.sym_section->output_offset			\
848
849
	       + reloc_data.sym_section->output_section->vma) : 0)))
#define L ((bfd_signed_vma) (reloc_data.sym_value			\
850
851
	   + (reloc_data.sym_section->output_section != NULL ?		\
	      (reloc_data.sym_section->output_offset			\
852
	      + reloc_data.sym_section->output_section->vma) : 0)))
Nick Clifton's avatar
Nick Clifton committed
853
854
855
#define A (reloc_data.reloc_addend)
#define B (0)
#define G (reloc_data.got_offset_value)
856
857
858
#define GOT (reloc_data.got_symbol_vma)
#define GOT_BEGIN (htab->sgot->output_section->vma)

Nick Clifton's avatar
Nick Clifton committed
859
#define MES (0)
860
861
	/* P: relative offset to PCL The offset should be to the
	  current location aligned to 32 bits.  */
862
#define P ((bfd_signed_vma) (						\
863
864
865
866
	   (								\
	    (reloc_data.input_section->output_section != NULL ?		\
	     reloc_data.input_section->output_section->vma : 0)		\
	    + reloc_data.input_section->output_offset			\
867
868
869
	    + (reloc_data.reloc_offset - (bitsize >= 32 ? 4 : 0)))	\
	   & ~0x3))
#define PDATA ((bfd_signed_vma) ( \
870
871
	    (reloc_data.input_section->output_section->vma \
	     + reloc_data.input_section->output_offset \
872
873
874
875
876
	     + (reloc_data.reloc_offset))))
#define SECTSTART (bfd_signed_vma) (reloc_data.input_section->output_offset)
#define _SDA_BASE_ (bfd_signed_vma) (reloc_data.sdata_begin_symbol_vma)
#define TLS_REL (bfd_signed_vma) \
  ((elf_hash_table (info))->tls_sec->output_section->vma)
877
878
879
#define TLS_TBSS (8)
#define TCB_SIZE (8)

Nick Clifton's avatar
Nick Clifton committed
880
881
#define none (0)

882
#define PRINT_DEBUG_RELOC_INFO_BEFORE(FORMULA, TYPE) \
883
884
885
    {\
      asection *sym_section = reloc_data.sym_section; \
      asection *input_section = reloc_data.input_section; \
886
887
      ARC_DEBUG ("RELOC_TYPE = " TYPE "\n"); \
      ARC_DEBUG ("FORMULA = " FORMULA "\n"); \
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
      ARC_DEBUG ("S = 0x%x\n", S); \
      ARC_DEBUG ("A = 0x%x\n", A); \
      ARC_DEBUG ("L = 0x%x\n", L); \
      if (sym_section->output_section != NULL) \
	{ \
	  ARC_DEBUG ("symbol_section->vma = 0x%x\n", \
	     sym_section->output_section->vma + sym_section->output_offset); \
	} \
      else \
	{ \
	  ARC_DEBUG ("symbol_section->vma = NULL\n"); \
	} \
      if (input_section->output_section != NULL) \
	{ \
	  ARC_DEBUG ("symbol_section->vma = 0x%x\n", \
	     input_section->output_section->vma + input_section->output_offset); \
	} \
      else \
	{ \
	  ARC_DEBUG ("symbol_section->vma = NULL\n"); \
	} \
      ARC_DEBUG ("PCL = 0x%x\n", P); \
      ARC_DEBUG ("P = 0x%x\n", P); \
      ARC_DEBUG ("G = 0x%x\n", G); \
      ARC_DEBUG ("SDA_OFFSET = 0x%x\n", _SDA_BASE_); \
      ARC_DEBUG ("SDA_SET = %d\n", reloc_data.sdata_begin_symbol_vma_set); \
      ARC_DEBUG ("GOT_OFFSET = 0x%x\n", GOT); \
      ARC_DEBUG ("relocation = 0x%08x\n", relocation); \
      ARC_DEBUG ("before = 0x%08x\n", (unsigned int) insn); \
      ARC_DEBUG ("data   = 0x%08x (%u) (%d)\n", (unsigned int) relocation, (unsigned int) relocation, (int) relocation); \
    }

#define PRINT_DEBUG_RELOC_INFO_AFTER \
    { \
      ARC_DEBUG ("after  = 0x%08x\n", (unsigned int) insn); \
    }

Nick Clifton's avatar
Nick Clifton committed
925
926
927
#define ARC_RELOC_HOWTO(TYPE, VALUE, SIZE, BITSIZE, RELOC_FUNCTION, OVERFLOW, FORMULA) \
  case R_##TYPE: \
    { \
928
      bfd_signed_vma bitsize ATTRIBUTE_UNUSED = BITSIZE; \
Nick Clifton's avatar
Nick Clifton committed
929
      relocation = FORMULA  ; \
930
      PRINT_DEBUG_RELOC_INFO_BEFORE (#FORMULA, #TYPE); \
931
      insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
932
      insn = (* get_replace_function (abfd, TYPE)) (insn, relocation); \
933
      insn = middle_endian_convert (insn, IS_ME (#FORMULA, abfd)); \
934
      PRINT_DEBUG_RELOC_INFO_AFTER \
Nick Clifton's avatar
Nick Clifton committed
935
936
937
938
    } \
    break;

static bfd_reloc_status_type
939
940
941
arc_do_relocation (bfd_byte * contents,
		   struct arc_relocation_data reloc_data,
		   struct bfd_link_info *info)
Nick Clifton's avatar
Nick Clifton committed
942
{
943
  bfd_signed_vma relocation = 0;
Nick Clifton's avatar
Nick Clifton committed
944
945
  bfd_vma insn;
  bfd_vma orig_insn ATTRIBUTE_UNUSED;
946
  bfd * abfd = reloc_data.input_section->owner;
947
  struct elf_link_hash_table *htab ATTRIBUTE_UNUSED = elf_hash_table (info);
948
  bfd_reloc_status_type flag;
Nick Clifton's avatar
Nick Clifton committed
949
950

  if (reloc_data.should_relocate == FALSE)
951
    return bfd_reloc_ok;
Nick Clifton's avatar
Nick Clifton committed
952
953
954
955

  switch (reloc_data.howto->size)
    {
      case 2:
956
	insn = arc_bfd_get_32 (abfd,
Nick Clifton's avatar
Nick Clifton committed
957
958
959
960
			       contents + reloc_data.reloc_offset,
			       reloc_data.input_section);
	break;
      case 1:
961
962
963
964
	insn = arc_bfd_get_16 (abfd,
			       contents + reloc_data.reloc_offset,
			       reloc_data.input_section);
	break;
Nick Clifton's avatar
Nick Clifton committed
965
      case 0:
966
	insn = arc_bfd_get_8 (abfd,
Nick Clifton's avatar
Nick Clifton committed
967
968
969
970
971
972
973
974
975
976
977
978
979
			       contents + reloc_data.reloc_offset,
			       reloc_data.input_section);
	break;
      default:
	insn = 0;
	BFD_ASSERT (0);
	break;
    }

  orig_insn = insn;

  switch (reloc_data.howto->type)
    {
980
#include "elf/arc-reloc.def"
Nick Clifton's avatar
Nick Clifton committed
981
982
983
984
985
986
987
988

      default:
	BFD_ASSERT (0);
	break;
    }

  /* Check for relocation overflow.  */
  if (reloc_data.howto->complain_on_overflow != complain_overflow_dont)
989
990
991
992
993
994
995
    flag = bfd_check_overflow (reloc_data.howto->complain_on_overflow,
                               reloc_data.howto->bitsize,
                               reloc_data.howto->rightshift,
                               bfd_arch_bits_per_address (abfd),
                               relocation);
  else
    flag = arc_special_overflow_checks (reloc_data, relocation, info);
Nick Clifton's avatar
Nick Clifton committed
996

997
#undef  DEBUG_ARC_RELOC
Nick Clifton's avatar
Nick Clifton committed
998
#define DEBUG_ARC_RELOC(A) debug_arc_reloc (A)
999
1000
1001
  if (flag != bfd_reloc_ok)
    {
      PR_DEBUG ( "Relocation overflows !!!!\n");
Nick Clifton's avatar
Nick Clifton committed
1002

1003
      DEBUG_ARC_RELOC (reloc_data);
Nick Clifton's avatar
Nick Clifton committed
1004

1005
1006
1007
1008
1009
1010
1011
      PR_DEBUG (
                "Relocation value = signed -> %d, unsigned -> %u"
                ", hex -> (0x%08x)\n",
                (int) relocation,
                (unsigned int) relocation,
                (unsigned int) relocation);
      return flag;
Nick Clifton's avatar
Nick Clifton committed
1012
    }
1013
#undef  DEBUG_ARC_RELOC
Nick Clifton's avatar
Nick Clifton committed
1014
1015
#define DEBUG_ARC_RELOC(A)

1016
  /* Write updated instruction back to memory.  */
Nick Clifton's avatar
Nick Clifton committed
1017
1018
1019
  switch (reloc_data.howto->size)
    {
      case 2:
1020
	arc_bfd_put_32 (abfd, insn,
Nick Clifton's avatar
Nick Clifton committed
1021
1022
1023
1024
		       contents + reloc_data.reloc_offset,
		       reloc_data.input_section);
	break;
      case 1:
1025
1026
1027
1028
	arc_bfd_put_16 (abfd, insn,
		       contents + reloc_data.reloc_offset,
		       reloc_data.input_section);
	break;
Nick Clifton's avatar
Nick Clifton committed
1029
      case 0:
1030
	arc_bfd_put_8 (abfd, insn,
Nick Clifton's avatar
Nick Clifton committed
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
		       contents + reloc_data.reloc_offset,
		       reloc_data.input_section);
	break;
      default:
	ARC_DEBUG ("size = %d\n", reloc_data.howto->size);
	BFD_ASSERT (0);
	break;
    }

  return bfd_reloc_ok;
}
#undef S
#undef A
#undef B
#undef G
#undef GOT
#undef L
#undef MES
#undef P
#undef SECTSTAR
#undef SECTSTART
#undef _SDA_BASE_
#undef none

#undef ARC_RELOC_HOWTO

1057
1058
static struct got_entry **
arc_get_local_got_ents (bfd * abfd)
Nick Clifton's avatar
Nick Clifton committed
1059
{
1060
  static struct got_entry **local_got_ents = NULL;
Nick Clifton's avatar
Nick Clifton committed
1061

1062
  if (local_got_ents == NULL)
Nick Clifton's avatar
Nick Clifton committed
1063
1064
1065
1066
1067
    {
      size_t	   size;
      Elf_Internal_Shdr *symtab_hdr = &((elf_tdata (abfd))->symtab_hdr);

      size = symtab_hdr->sh_info * sizeof (bfd_vma);
1068
1069
1070
      local_got_ents = (struct got_entry **)
	bfd_alloc (abfd, sizeof(struct got_entry *) * size);
      if (local_got_ents == NULL)
Nick Clifton's avatar
Nick Clifton committed
1071
	return FALSE;
1072
1073
1074

      memset (local_got_ents, 0, sizeof(struct got_entry *) * size);
      elf_local_got_ents (abfd) = local_got_ents;
Nick Clifton's avatar
Nick Clifton committed
1075
1076
    }

1077
  return local_got_ents;
Nick Clifton's avatar
Nick Clifton committed
1078
1079
1080
1081
1082
}

/* Relocate an arc ELF section.
   Function : elf_arc_relocate_section
   Brief    : Relocate an arc section, by handling all the relocations
1083
	     appearing in that section.
Nick Clifton's avatar
Nick Clifton committed
1084
   Args     : output_bfd    : The bfd being written to.
1085
1086
1087
1088
1089
1090
1091
1092
1093
	      info	    : Link information.
	      input_bfd     : The input bfd.
	      input_section : The section being relocated.
	      contents	    : contents of the section being relocated.
	      relocs	    : List of relocations in the section.
	      local_syms    : is a pointer to the swapped in local symbols.
	      local_section : is an array giving the section in the input file
			      corresponding to the st_shndx field of each
			      local symbol.  */
Nick Clifton's avatar
Nick Clifton committed
1094
static bfd_boolean
1095
elf_arc_relocate_section (bfd *		   output_bfd,
Nick Clifton's avatar
Nick Clifton committed
1096
			  struct bfd_link_info *  info,
1097
1098
1099
			  bfd *		   input_bfd,
			  asection *	      input_section,
			  bfd_byte *	      contents,
Nick Clifton's avatar
Nick Clifton committed
1100
1101
			  Elf_Internal_Rela *     relocs,
			  Elf_Internal_Sym *      local_syms,
1102
			  asection **	     local_sections)
Nick Clifton's avatar
Nick Clifton committed
1103
{
1104
  Elf_Internal_Shdr *	   symtab_hdr;
Nick Clifton's avatar
Nick Clifton committed
1105
  struct elf_link_hash_entry ** sym_hashes;
1106
1107
1108
1109
  struct got_entry **	   local_got_ents;
  Elf_Internal_Rela *	   rel;
  Elf_Internal_Rela *	   relend;
  struct elf_link_hash_table *htab = elf_hash_table (info);
Nick Clifton's avatar
Nick Clifton committed
1110
1111
1112
1113
1114
1115
1116
1117
1118

  symtab_hdr = &((elf_tdata (input_bfd))->symtab_hdr);
  sym_hashes = elf_sym_hashes (input_bfd);

  rel = relocs;
  relend = relocs + input_section->reloc_count;
  for (; rel < relend; rel++)
    {
      enum elf_arc_reloc_type       r_type;
1119
1120
      reloc_howto_type *	    howto;
      unsigned long		 r_symndx;
Nick Clifton's avatar
Nick Clifton committed
1121
      struct elf_link_hash_entry *  h;
1122
1123
1124
      Elf_Internal_Sym *	    sym;
      asection *		    sec;
      struct elf_link_hash_entry *h2;
Nick Clifton's avatar
Nick Clifton committed
1125
1126
1127

      struct arc_relocation_data reloc_data =
      {
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
	.reloc_offset = 0,
	.reloc_addend = 0,
	.got_offset_value = 0,
	.sym_value = 0,	
	.sym_section = NULL,
	.howto = NULL,
	.input_section = NULL,
	.sdata_begin_symbol_vma = 0,
	.sdata_begin_symbol_vma_set = FALSE,
	.got_symbol_vma = 0,
	.should_relocate = FALSE
Nick Clifton's avatar
Nick Clifton committed
1139
1140
      };

1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
      r_type = ELF32_R_TYPE (rel->r_info);

      if (r_type >= (int) R_ARC_max)
	{
	  bfd_set_error (bfd_error_bad_value);
	  return FALSE;
	}
      howto = &elf_arc_howto_table[r_type];

      r_symndx = ELF32_R_SYM (rel->r_info);

      /* If we are generating another .o file and the symbol in not
	 local, skip this relocation.  */
      if (bfd_link_relocatable (info))
	{
	  /* This is a relocateable link.  We don't have to change
	     anything, unless the reloc is against a section symbol,
	     in which case we have to adjust according to where the
	     section symbol winds up in the output section.  */

	  /* Checks if this is a local symbol and thus the reloc
	     might (will??) be against a section symbol.  */
	  if (r_symndx < symtab_hdr->sh_info)
	    {
	      sym = local_syms + r_symndx;
	      if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
		{
		  sec = local_sections[r_symndx];

		  /* for RELA relocs.Just adjust the addend
		     value in the relocation entry.  */
		  rel->r_addend += sec->output_offset + sym->st_value;

		  BFD_DEBUG_PIC (
		    PR_DEBUG ("local symbols reloc "
			      "(section=%d %s) seen in %s\n",
			      r_symndx,
			      local_sections[r_symndx]->name,
			      __PRETTY_FUNCTION__)
		  );
		}
	    }

	  continue;
	}
Nick Clifton's avatar
Nick Clifton committed
1186
1187
1188
1189
1190

      h2 = elf_link_hash_lookup (elf_hash_table (info), "__SDATA_BEGIN__",
				 FALSE, FALSE, TRUE);

      if (reloc_data.sdata_begin_symbol_vma_set == FALSE
1191
1192
1193
	    && h2 != NULL && h2->root.type != bfd_link_hash_undefined
	    && h2->root.u.def.section->output_section != NULL)
	/* TODO: Verify this condition.  */
Nick Clifton's avatar
Nick Clifton committed
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
	{
	  reloc_data.sdata_begin_symbol_vma =
	    (h2->root.u.def.value +
	     h2->root.u.def.section->output_section->vma);
	  reloc_data.sdata_begin_symbol_vma_set = TRUE;
	}

      reloc_data.input_section = input_section;
      reloc_data.howto = howto;
      reloc_data.reloc_offset = rel->r_offset;
      reloc_data.reloc_addend = rel->r_addend;

      /* This is a final link.  */
      h = NULL;
      sym = NULL;
      sec = NULL;

      if (r_symndx < symtab_hdr->sh_info) /* A local symbol.  */
	{
1213
1214
1215
	  local_got_ents = arc_get_local_got_ents (output_bfd);
	  struct got_entry *entry = local_got_ents[r_symndx];

Nick Clifton's avatar
Nick Clifton committed
1216
1217
1218
1219
1220
	  sym = local_syms + r_symndx;
	  sec = local_sections[r_symndx];

	  reloc_data.sym_value = sym->st_value;
	  reloc_data.sym_section = sec;
1221
1222
1223
1224
	  reloc_data.symbol_name =
            bfd_elf_string_from_elf_section (input_bfd,
                                             symtab_hdr->sh_link,
                                             sym->st_name);
Nick Clifton's avatar
Nick Clifton committed
1225

1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
	  /* Mergeable section handling.  */
	  if ((sec->flags & SEC_MERGE)
	      && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
	    {
	      asection *msec;
	      msec = sec;
	      rel->r_addend = _bfd_elf_rel_local_sym (output_bfd, sym,
						      &msec, rel->r_addend);
	      rel->r_addend -= (sec->output_section->vma
				+ sec->output_offset
				+ sym->st_value);
	      rel->r_addend += msec->output_section->vma + msec->output_offset;

	      reloc_data.reloc_addend = rel->r_addend;
	    }

1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
	  if ((is_reloc_for_GOT (howto)
	       || is_reloc_for_TLS (howto)) && entry != NULL)
	    {
	      if (is_reloc_for_TLS (howto))
		while (entry->type == GOT_NORMAL && entry->next != NULL)
		  entry = entry->next;

	      if (is_reloc_for_GOT (howto))
		while (entry->type != GOT_NORMAL && entry->next != NULL)
		  entry = entry->next;

	      if (entry->type == GOT_TLS_GD && entry->processed == FALSE)
		{
		  bfd_vma sym_vma = sym->st_value
				    + sec->output_section->vma
				    + sec->output_offset;

		  /* Create dynamic relocation for local sym.  */
		  ADD_RELA (output_bfd, got, entry->offset, 0,
			    R_ARC_TLS_DTPMOD, 0);
		  ADD_RELA (output_bfd, got, entry->offset+4, 0,
			    R_ARC_TLS_DTPOFF, 0);

		  bfd_vma sec_vma = sec->output_section->vma
				    + sec->output_offset;
		  bfd_put_32 (output_bfd, sym_vma - sec_vma,
			      htab->sgot->contents + entry->offset + 4);

		  ARC_DEBUG ("arc_info: FIXED -> GOT_TLS_GD value "
			 "= 0x%x @ 0x%x, for symbol %s\n",
			 sym_vma - sec_vma,
			 htab->sgot->contents + entry->offset + 4,
			 "(local)");

		  entry->processed = TRUE;
		}
	      if (entry->type == GOT_TLS_IE && entry->processed == FALSE)
		{
		  bfd_vma sym_vma = sym->st_value
				    + sec->output_section->vma
				    + sec->output_offset;
		  bfd_vma sec_vma = htab->tls_sec->output_section->vma;
		  bfd_put_32 (output_bfd, sym_vma - sec_vma,
			      htab->sgot->contents + entry->offset);
		  /* TODO: Check if this type of relocs is the cause
		     for all the ARC_NONE dynamic relocs.  */

		  ARC_DEBUG ("arc_info: FIXED -> GOT_TLS_IE value = "
			 "0x%x @ 0x%x, for symbol %s\n",
			 sym_vma - sec_vma,
			 htab->sgot->contents + entry->offset,
			 "(local)");

		  entry->processed = TRUE;
		}
	      if (entry->type == GOT_NORMAL && entry->processed == FALSE)
		{
		  bfd_vma sec_vma = reloc_data.sym_section->output_section->vma
				    + reloc_data.sym_section->output_offset;

		  bfd_put_32 (output_bfd, reloc_data.sym_value + sec_vma,
			      htab->sgot->contents + entry->offset);

		  ARC_DEBUG ("arc_info: PATCHED: 0x%08x @ 0x%08x for "
			 "sym %s in got offset 0x%x\n",
			 reloc_data.sym_value + sec_vma,
			 htab->sgot->output_section->vma
			 + htab->sgot->output_offset + entry->offset,
			 "(local)",
			 entry->offset);
		  entry->processed = TRUE;
		}

	      reloc_data.got_offset_value = entry->offset;
	      ARC_DEBUG ("arc_info: GOT_ENTRY = %d, offset = 0x%x, "
		     "vma = 0x%x for symbol %s\n",
		     entry->type, entry->offset,
		     htab->sgot->output_section->vma
		     + htab->sgot->output_offset + entry->offset,
		     "(local)");
	    }

	  BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
	  if (htab->sgot != NULL)
	    reloc_data.got_symbol_vma = htab->sgot->output_section->vma
					+ htab->sgot->output_offset;
Nick Clifton's avatar
Nick Clifton committed
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340

	  reloc_data.should_relocate = TRUE;
	}
      else /* Global symbol.  */
	{
	  /* Get the symbol's entry in the symtab.  */
	  h = sym_hashes[r_symndx - symtab_hdr->sh_info];

	  while (h->root.type == bfd_link_hash_indirect
		 || h->root.type == bfd_link_hash_warning)
	    h = (struct elf_link_hash_entry *) h->root.u.i.link;

	  BFD_ASSERT ((h->dynindx == -1) >= (h->forced_local != 0));
1341
	  reloc_data.symbol_name = h->root.root.string;
Nick Clifton's avatar
Nick Clifton committed
1342
1343
1344
1345
1346
1347
1348
1349
1350
	  /* If we have encountered a definition for this symbol.  */
	  if (h->root.type == bfd_link_hash_defined
	      || h->root.type == bfd_link_hash_defweak)
	    {
	      reloc_data.sym_value = h->root.u.def.value;
	      reloc_data.sym_section = h->root.u.def.section;

	      reloc_data.should_relocate = TRUE;

1351
	      if (is_reloc_for_GOT (howto) && !bfd_link_pic (info))
Nick Clifton's avatar
Nick Clifton committed
1352
		{
1353
1354
		  /* TODO: Change it to use arc_do_relocation with
		    ARC_32 reloc.  Try to use ADD_RELA macro.  */
Nick Clifton's avatar
Nick Clifton committed
1355
1356
		  bfd_vma relocation =
		    reloc_data.sym_value + reloc_data.reloc_addend
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
		    + (reloc_data.sym_section->output_section != NULL ?
			(reloc_data.sym_section->output_offset
			 + reloc_data.sym_section->output_section->vma)
		      : 0);

		  BFD_ASSERT (h->got.glist);
		  bfd_vma got_offset = h->got.glist->offset;
		  bfd_put_32 (output_bfd, relocation,
			      htab->sgot->contents + got_offset);
		}
	      if (is_reloc_for_PLT (howto) && h->plt.offset != (bfd_vma) -1)
		{
		  /* TODO: This is repeated up here.  */
		  reloc_data.sym_value = h->plt.offset;
		  reloc_data.sym_section = htab->splt;
Nick Clifton's avatar
Nick Clifton committed
1372
1373
1374
1375
1376
		}
	    }
	  else if (h->root.type == bfd_link_hash_undefweak)
	    {
	      /* Is weak symbol and has no definition.  */
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
	      if (is_reloc_for_GOT (howto))
		{
		  reloc_data.sym_value = h->root.u.def.value;
		  reloc_data.sym_section = htab->sgot;
		  reloc_data.should_relocate = TRUE;
		}
	      else if (is_reloc_for_PLT (howto)
		       && h->plt.offset != (bfd_vma) -1)
		{
		  /* TODO: This is repeated up here.  */
		  reloc_data.sym_value = h->plt.offset;
		  reloc_data.sym_section = htab->splt;
		  reloc_data.should_relocate = TRUE;
		}
	      else
		continue;
Nick Clifton's avatar
Nick Clifton committed
1393
1394
1395
1396
1397
1398
	    }
	  else
	    {
	      if (is_reloc_for_GOT (howto))
		{
		  reloc_data.sym_value = h->root.u.def.value;
1399
		  reloc_data.sym_section = htab->sgot;
Nick Clifton's avatar
Nick Clifton committed
1400
1401
1402
1403
1404

		  reloc_data.should_relocate = TRUE;
		}
	      else if (is_reloc_for_PLT (howto))
		{
Claudiu Zissulescu's avatar
Claudiu Zissulescu committed
1405
1406
1407
1408
1409
1410
1411
1412
1413
		  /* Fail if it is linking for PIE and the symbol is
		     undefined.  */
		  if (bfd_link_executable (info)
		      && !(*info->callbacks->undefined_symbol)
		        (info, h->root.root.string, input_bfd, input_section,
		         rel->r_offset, TRUE))
		    {
		      return FALSE;
		    }
Nick Clifton's avatar
Nick Clifton committed
1414
		  reloc_data.sym_value = h->plt.offset;
1415
		  reloc_data.sym_section = htab->splt;
Nick Clifton's avatar
Nick Clifton committed
1416
1417
1418

		  reloc_data.should_relocate = TRUE;
		}
Claudiu Zissulescu's avatar
Claudiu Zissulescu committed
1419
1420
	      else if (!bfd_link_pic (info)
		       && !(*info->callbacks->undefined_symbol)
Nick Clifton's avatar
Nick Clifton committed
1421
		       (info, h->root.root.string, input_bfd, input_section,
Claudiu Zissulescu's avatar
Claudiu Zissulescu committed
1422
			rel->r_offset, TRUE))
Nick Clifton's avatar
Nick Clifton committed
1423
1424
1425
1426
1427
		{
		  return FALSE;
		}
	    }

1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
	  if (h->got.glist != NULL)
	    {
	      struct got_entry *entry = h->got.glist;

	      if (is_reloc_for_GOT (howto) || is_reloc_for_TLS (howto))
		{
		  if (! elf_hash_table (info)->dynamic_sections_created
		      || (bfd_link_pic (info)
			  && SYMBOL_REFERENCES_LOCAL (info, h)))
		    {
		      reloc_data.sym_value = h->root.u.def.value;
		      reloc_data.sym_section = h->root.u.def.section;

		      if (is_reloc_for_TLS (howto))
			while (entry->type == GOT_NORMAL && entry->next != NULL)
			  entry = entry->next;

		      if (entry->processed == FALSE
			  && (entry->type == GOT_TLS_GD
			      || entry->type == GOT_TLS_IE))
			{
			  bfd_vma sym_value = h->root.u.def.value
			    + h->root.u.def.section->output_section->vma
			    + h->root.u.def.section->output_offset;

			  bfd_vma sec_vma =
			    elf_hash_table (info)->tls_sec->output_section->vma;

			  bfd_put_32 (output_bfd,
				      sym_value - sec_vma,
				      htab->sgot->contents + entry->offset
1459
				      + (entry->existing_entries == TLS_GOT_MOD_AND_OFF ? 4 : 0));
1460
1461
1462
1463
1464
1465
1466

			  ARC_DEBUG ("arc_info: FIXED -> %s value = 0x%x "
				     "@ 0x%x, for symbol %s\n",
				     (entry->type == GOT_TLS_GD ? "GOT_TLS_GD" :
				      "GOT_TLS_IE"),
				     sym_value - sec_vma,
				     htab->sgot->contents + entry->offset
1467
				     + (entry->existing_entries == TLS_GOT_MOD_AND_OFF ? 4 : 0),
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
				     h->root.root.string);

			  entry->processed = TRUE;
			}

		      if (entry->type == GOT_TLS_IE && entry->processed == FALSE)
			{
			  bfd_vma sec_vma = htab->tls_sec->output_section->vma;
			  bfd_put_32 (output_bfd,
				      reloc_data.sym_value - sec_vma,
				      htab->sgot->contents + entry->offset);
			}

		      if (entry->type == GOT_NORMAL && entry->processed == FALSE)
			{
			  bfd_vma sec_vma =
			    reloc_data.sym_section->output_section->vma
			    + reloc_data.sym_section->output_offset;

			  if (h->root.type != bfd_link_hash_undefweak)
			    {
			      bfd_put_32 (output_bfd,
					  reloc_data.sym_value + sec_vma,
					  htab->sgot->contents + entry->offset);

			      ARC_DEBUG ("arc_info: PATCHED: 0x%08x "
					 "@ 0x%08x for sym %s in got offset 0x%x\n",
					 reloc_data.sym_value + sec_vma,
					 htab->sgot->output_section->vma
					 + htab->sgot->output_offset + entry->offset,
					 h->root.root.string,
					 entry->offset);
			    }
			  else
			    {
			      ARC_DEBUG ("arc_info: PATCHED: NOT_PATCHED "
					 "@ 0x%08x for sym %s in got offset 0x%x "
					 "(is undefweak)\n",
					 htab->sgot->output_section->vma
					 + htab->sgot->output_offset + entry->offset,
					 h->root.root.string,
					 entry->offset);
			    }

			  entry->processed = TRUE;
			}
		    }
		}

	      reloc_data.got_offset_value = entry->offset;

	      ARC_DEBUG ("arc_info: GOT_ENTRY = %d, offset = 0x%x, "
			 "vma = 0x%x for symbol %s\n",
			 entry->type, entry->offset,
			 htab->sgot->output_section->vma
			 + htab->sgot->output_offset + entry->offset,
			 h->root.root.string);
	    }

	  BFD_ASSERT (htab->sgot != NULL || !is_reloc_for_GOT (howto));
	  if (htab->sgot != NULL)
	    reloc_data.got_symbol_vma = htab->sgot->output_section->vma
					+ htab->sgot->output_offset;
Nick Clifton's avatar
Nick Clifton committed
1531
1532
	}

1533
1534
1535
1536
1537
1538
      switch (r_type)
	{
	  case R_ARC_32:
	  case R_ARC_32_ME:
	  case R_ARC_PC32:
	  case R_ARC_32_PCREL:
Claudiu Zissulescu's avatar
Claudiu Zissulescu committed
1539
	    if (bfd_link_pic (info) && !bfd_link_pie (info)
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
		&& ((r_type != R_ARC_PC32 && r_type != R_ARC_32_PCREL)
		    || (h != NULL
			&& h->dynindx != -1
			&& (!info->symbolic || !h->def_regular))))
	      {
		Elf_Internal_Rela outrel;
		bfd_byte *loc;
		bfd_boolean skip = FALSE;
		bfd_boolean relocate = FALSE;
		asection *sreloc = _bfd_elf_get_dynamic_reloc_section
				 (input_bfd, input_section,
				  /*RELA*/ TRUE);

		BFD_ASSERT (sreloc != NULL);

		outrel.r_offset = _bfd_elf_section_offset (output_bfd,
							   info,
							   input_section,
							   rel->r_offset);
		if (outrel.r_offset == (bfd_vma) -1)
		  skip = TRUE;

		outrel.r_addend = rel->r_addend;
		outrel.r_offset += (input_section->output_section->vma
				    + input_section->output_offset);

		if (skip)
		  {
		    memset (&outrel, 0, sizeof outrel);
		    relocate = FALSE;
		  }
		else if (r_type == R_ARC_PC32
			 || r_type == R_ARC_32_PCREL)
		  {
1574
		    BFD_ASSERT (h != NULL);
1575
1576
1577
1578
		    if ((input_section->flags & SEC_ALLOC) != 0)
		      relocate = FALSE;
		    else
		      relocate = TRUE;