Coverage Report

Created: 2026-07-25 10:20

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-m32c.c
Line
Count
Source
1
/* M16C/M32C specific support for 32-bit ELF.
2
   Copyright (C) 2005-2026 Free Software Foundation, Inc.
3
4
   This file is part of BFD, the Binary File Descriptor library.
5
6
   This program is free software; you can redistribute it and/or modify
7
   it under the terms of the GNU General Public License as published by
8
   the Free Software Foundation; either version 3 of the License, or
9
   (at your option) any later version.
10
11
   This program is distributed in the hope that it will be useful,
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
   GNU General Public License for more details.
15
16
   You should have received a copy of the GNU General Public License
17
   along with this program; if not, write to the Free Software
18
   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
19
   MA 02110-1301, USA.  */
20
21
#include "sysdep.h"
22
#include "bfd.h"
23
#include "libbfd.h"
24
#include "elf-bfd.h"
25
#include "elf/m32c.h"
26
#include "libiberty.h"
27
28
/* Forward declarations.  */
29
static reloc_howto_type * m32c_reloc_type_lookup
30
  (bfd *, bfd_reloc_code_real_type);
31
static bool m32c_info_to_howto_rela
32
  (bfd *, arelent *, Elf_Internal_Rela *);
33
static int m32c_elf_relocate_section
34
  (struct bfd_link_info *, bfd *, asection *, bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *, asection **);
35
static bool m32c_elf_check_relocs
36
  (bfd *, struct bfd_link_info *, asection *, const Elf_Internal_Rela *);
37
static bool m32c_elf_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
38
#ifdef DEBUG
39
char * m32c_get_reloc (long reloc);
40
void dump_symtab (bfd *, void *, void *);
41
#endif
42
static bool m32c_elf_relax_section
43
(bfd *abfd, asection *sec, struct bfd_link_info *link_info, bool *again);
44
static bfd_reloc_status_type m32c_apply_reloc_24
45
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
46
47
48
static reloc_howto_type m32c_elf_howto_table [] =
49
{
50
  /* This reloc does nothing.  */
51
  HOWTO (R_M32C_NONE,   /* type */
52
   0,     /* rightshift */
53
   0,     /* size */
54
   0,     /* bitsize */
55
   false,     /* pc_relative */
56
   0,     /* bitpos */
57
   complain_overflow_dont, /* complain_on_overflow */
58
   bfd_elf_generic_reloc, /* special_function */
59
   "R_M32C_NONE",   /* name */
60
   false,     /* partial_inplace */
61
   0,     /* src_mask */
62
   0,     /* dst_mask */
63
   false),    /* pcrel_offset */
64
65
  /* GCC intentionally overflows these next two in order to work
66
     around limitations in the addressing modes, so don't complain
67
     about overflow.  */
68
  HOWTO (R_M32C_16,   /* type */
69
   0,     /* rightshift */
70
   2,     /* size */
71
   16,      /* bitsize */
72
   false,     /* pc_relative */
73
   0,     /* bitpos */
74
   complain_overflow_dont, /* complain_on_overflow */
75
   bfd_elf_generic_reloc, /* special_function */
76
   "R_M32C_16",   /* name */
77
   false,     /* partial_inplace */
78
   0,     /* src_mask */
79
   0xffff,    /* dst_mask */
80
   false),    /* pcrel_offset */
81
82
  HOWTO (R_M32C_24,   /* type */
83
   0,     /* rightshift */
84
   4,     /* size */
85
   24,      /* bitsize */
86
   false,     /* pc_relative */
87
   0,     /* bitpos */
88
   complain_overflow_dont, /* complain_on_overflow */
89
   m32c_apply_reloc_24, /* special_function */
90
   "R_M32C_24",   /* name */
91
   false,     /* partial_inplace */
92
   0,     /* src_mask */
93
   0xffffff,    /* dst_mask */
94
   false),    /* pcrel_offset */
95
96
  HOWTO (R_M32C_32,   /* type */
97
   0,     /* rightshift */
98
   4,     /* size */
99
   32,      /* bitsize */
100
   false,     /* pc_relative */
101
   0,     /* bitpos */
102
   complain_overflow_bitfield, /* complain_on_overflow */
103
   bfd_elf_generic_reloc, /* special_function */
104
   "R_M32C_32",   /* name */
105
   false,     /* partial_inplace */
106
   0,     /* src_mask */
107
   0xffffffff,    /* dst_mask */
108
   false),    /* pcrel_offset */
109
110
  HOWTO (R_M32C_8_PCREL,  /* type */
111
   0,     /* rightshift */
112
   1,     /* size */
113
   8,     /* bitsize */
114
   true,      /* pc_relative */
115
   0,     /* bitpos */
116
   complain_overflow_signed, /* complain_on_overflow */
117
   bfd_elf_generic_reloc, /* special_function */
118
   "R_M32C_8_PCREL",  /* name */
119
   false,     /* partial_inplace */
120
   0,     /* src_mask */
121
   0xff,      /* dst_mask */
122
   true),     /* pcrel_offset */
123
124
  HOWTO (R_M32C_16_PCREL, /* type */
125
   0,     /* rightshift */
126
   2,     /* size */
127
   16,      /* bitsize */
128
   true,      /* pc_relative */
129
   0,     /* bitpos */
130
   complain_overflow_signed, /* complain_on_overflow */
131
   bfd_elf_generic_reloc, /* special_function */
132
   "R_M32C_16_PCREL", /* name */
133
   false,     /* partial_inplace */
134
   0,     /* src_mask */
135
   0xffff,    /* dst_mask */
136
   true),     /* pcrel_offset */
137
138
  HOWTO (R_M32C_8,    /* type */
139
   0,     /* rightshift */
140
   1,     /* size */
141
   8,     /* bitsize */
142
   false,     /* pc_relative */
143
   0,     /* bitpos */
144
   complain_overflow_unsigned, /* complain_on_overflow */
145
   bfd_elf_generic_reloc, /* special_function */
146
   "R_M32C_8",    /* name */
147
   false,     /* partial_inplace */
148
   0,     /* src_mask */
149
   0xff,      /* dst_mask */
150
   false),    /* pcrel_offset */
151
152
  HOWTO (R_M32C_LO16,   /* type */
153
   0,     /* rightshift */
154
   2,     /* size */
155
   16,      /* bitsize */
156
   false,     /* pc_relative */
157
   0,     /* bitpos */
158
   complain_overflow_dont, /* complain_on_overflow */
159
   bfd_elf_generic_reloc, /* special_function */
160
   "R_M32C_LO16",   /* name */
161
   false,     /* partial_inplace */
162
   0,     /* src_mask */
163
   0xffff,    /* dst_mask */
164
   false),    /* pcrel_offset */
165
166
  HOWTO (R_M32C_HI8,    /* type */
167
   0,     /* rightshift */
168
   1,     /* size */
169
   8,     /* bitsize */
170
   false,     /* pc_relative */
171
   0,     /* bitpos */
172
   complain_overflow_dont, /* complain_on_overflow */
173
   bfd_elf_generic_reloc, /* special_function */
174
   "R_M32C_HI8",    /* name */
175
   false,     /* partial_inplace */
176
   0,     /* src_mask */
177
   0xff,      /* dst_mask */
178
   false),    /* pcrel_offset */
179
180
  HOWTO (R_M32C_HI16,   /* type */
181
   0,     /* rightshift */
182
   2,     /* size */
183
   16,      /* bitsize */
184
   false,     /* pc_relative */
185
   0,     /* bitpos */
186
   complain_overflow_dont, /* complain_on_overflow */
187
   bfd_elf_generic_reloc, /* special_function */
188
   "R_M32C_HI16",   /* name */
189
   false,     /* partial_inplace */
190
   0,     /* src_mask */
191
   0xffff,    /* dst_mask */
192
   false),    /* pcrel_offset */
193
194
  HOWTO (R_M32C_RL_JUMP,  /* type */
195
   0,     /* rightshift */
196
   0,     /* size */
197
   0,     /* bitsize */
198
   false,     /* pc_relative */
199
   0,     /* bitpos */
200
   complain_overflow_signed, /* complain_on_overflow */
201
   bfd_elf_generic_reloc, /* special_function */
202
   "R_M32C_RL_JUMP",  /* name */
203
   false,     /* partial_inplace */
204
   0,     /* src_mask */
205
   0,     /* dst_mask */
206
   false),    /* pcrel_offset */
207
208
  HOWTO (R_M32C_RL_1ADDR, /* type */
209
   0,     /* rightshift */
210
   0,     /* size */
211
   0,     /* bitsize */
212
   false,     /* pc_relative */
213
   0,     /* bitpos */
214
   complain_overflow_signed, /* complain_on_overflow */
215
   bfd_elf_generic_reloc, /* special_function */
216
   "R_M32C_RL_1ADDR", /* name */
217
   false,     /* partial_inplace */
218
   0,     /* src_mask */
219
   0,     /* dst_mask */
220
   false),    /* pcrel_offset */
221
222
  HOWTO (R_M32C_RL_2ADDR, /* type */
223
   0,     /* rightshift */
224
   0,     /* size */
225
   0,     /* bitsize */
226
   false,     /* pc_relative */
227
   0,     /* bitpos */
228
   complain_overflow_signed, /* complain_on_overflow */
229
   bfd_elf_generic_reloc, /* special_function */
230
   "R_M32C_RL_2ADDR", /* name */
231
   false,     /* partial_inplace */
232
   0,     /* src_mask */
233
   0,     /* dst_mask */
234
   false),    /* pcrel_offset */
235
236
};
237

238
/* Map BFD reloc types to M32C ELF reloc types.  */
239
240
struct m32c_reloc_map
241
{
242
  bfd_reloc_code_real_type bfd_reloc_val;
243
  unsigned int m32c_reloc_val;
244
};
245
246
static const struct m32c_reloc_map m32c_reloc_map [] =
247
{
248
  { BFD_RELOC_NONE,   R_M32C_NONE },
249
  { BFD_RELOC_16,   R_M32C_16 },
250
  { BFD_RELOC_24,   R_M32C_24 },
251
  { BFD_RELOC_32,   R_M32C_32 },
252
  { BFD_RELOC_8_PCREL,    R_M32C_8_PCREL },
253
  { BFD_RELOC_16_PCREL,   R_M32C_16_PCREL },
254
  { BFD_RELOC_8,    R_M32C_8 },
255
  { BFD_RELOC_LO16,   R_M32C_LO16 },
256
  { BFD_RELOC_HI16,   R_M32C_HI16 },
257
  { BFD_RELOC_M32C_HI8,   R_M32C_HI8 },
258
  { BFD_RELOC_M32C_RL_JUMP, R_M32C_RL_JUMP },
259
  { BFD_RELOC_M32C_RL_1ADDR,  R_M32C_RL_1ADDR },
260
  { BFD_RELOC_M32C_RL_2ADDR,  R_M32C_RL_2ADDR }
261
};
262
263
static reloc_howto_type *
264
m32c_reloc_type_lookup
265
    (bfd *          abfd ATTRIBUTE_UNUSED,
266
     bfd_reloc_code_real_type code)
267
0
{
268
0
  unsigned int i;
269
270
0
  for (i = ARRAY_SIZE (m32c_reloc_map); i--;)
271
0
    if (m32c_reloc_map [i].bfd_reloc_val == code)
272
0
      return & m32c_elf_howto_table [m32c_reloc_map[i].m32c_reloc_val];
273
274
0
  return NULL;
275
0
}
276
277
static reloc_howto_type *
278
m32c_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
279
0
{
280
0
  unsigned int i;
281
282
0
  for (i = 0;
283
0
       i < sizeof (m32c_elf_howto_table) / sizeof (m32c_elf_howto_table[0]);
284
0
       i++)
285
0
    if (m32c_elf_howto_table[i].name != NULL
286
0
  && strcasecmp (m32c_elf_howto_table[i].name, r_name) == 0)
287
0
      return &m32c_elf_howto_table[i];
288
289
0
  return NULL;
290
0
}
291
292
/* Set the howto pointer for an M32C ELF reloc.  */
293
294
static bool
295
m32c_info_to_howto_rela (bfd *               abfd,
296
       arelent *           cache_ptr,
297
       Elf_Internal_Rela * dst)
298
56
{
299
56
  unsigned int r_type;
300
301
56
  r_type = ELF32_R_TYPE (dst->r_info);
302
56
  if (r_type >= (unsigned int) R_M32C_max)
303
12
    {
304
      /* xgettext:c-format */
305
12
      _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
306
12
        abfd, r_type);
307
12
      bfd_set_error (bfd_error_bad_value);
308
12
      return false;
309
12
    }
310
44
  cache_ptr->howto = & m32c_elf_howto_table [r_type];
311
44
  return true;
312
56
}
313
314

315
316
/* Apply R_M32C_24 relocations.  We have to do this because it's not a
317
   power-of-two size, and the generic code may think it overruns the
318
   section if it's right at the end.
319
320
   Must return something other than bfd_reloc_continue to avoid the
321
   above problem.  Typical return values include bfd_reloc_ok or
322
   bfd_reloc_overflow.
323
*/
324
325
static bfd_reloc_status_type m32c_apply_reloc_24 (bfd *abfd ATTRIBUTE_UNUSED,
326
              arelent *reloc_entry,
327
              asymbol *symbol,
328
              void *vdata_start ATTRIBUTE_UNUSED,
329
              asection *input_section,
330
              bfd *ibfd ATTRIBUTE_UNUSED,
331
              char **error_msg ATTRIBUTE_UNUSED)
332
4
{
333
4
  bfd_vma relocation;
334
4
  bfd_reloc_status_type s;
335
336
4
  s = bfd_elf_generic_reloc (abfd, reloc_entry, symbol,
337
4
           vdata_start,
338
4
           input_section, ibfd, error_msg);
339
4
  if (s != bfd_reloc_continue)
340
0
    return s;
341
342
  /* Get symbol value.  (Common symbols are special.)  */
343
4
  if (bfd_is_com_section (symbol->section))
344
0
    relocation = 0;
345
4
  else
346
4
    relocation = symbol->value;
347
348
4
  relocation += symbol->section->output_offset;
349
350
  /* Add in supplied addend.  */
351
4
  relocation += reloc_entry->addend;
352
353
4
  reloc_entry->addend = relocation;
354
4
  reloc_entry->address += input_section->output_offset;
355
4
  return bfd_reloc_ok;
356
4
}
357
358
/* Relocate an M32C ELF section.
359
   There is some attempt to make this function usable for many architectures,
360
   both USE_REL and USE_RELA ['twould be nice if such a critter existed],
361
   if only to serve as a learning tool.
362
363
   The RELOCATE_SECTION function is called by the new ELF backend linker
364
   to handle the relocations for a section.
365
366
   The relocs are always passed as Rela structures; if the section
367
   actually uses Rel structures, the r_addend field will always be
368
   zero.
369
370
   This function is responsible for adjusting the section contents as
371
   necessary, and (if using Rela relocs and generating a relocatable
372
   output file) adjusting the reloc addend as necessary.
373
374
   This function does not have to worry about setting the reloc
375
   address or the reloc symbol index.
376
377
   LOCAL_SYMS is a pointer to the swapped in local symbols.
378
379
   LOCAL_SECTIONS is an array giving the section in the input file
380
   corresponding to the st_shndx field of each local symbol.
381
382
   The global hash table entry for the global symbols can be found
383
   via elf_sym_hashes (input_bfd).
384
385
   When generating relocatable output, this function must handle
386
   STB_LOCAL/STT_SECTION symbols specially.  The output symbol is
387
   going to be the section symbol corresponding to the output
388
   section, which means that the addend must be adjusted
389
   accordingly.  */
390
391
static int
392
m32c_elf_relocate_section
393
    (struct bfd_link_info *  info,
394
     bfd *         input_bfd,
395
     asection *        input_section,
396
     bfd_byte *        contents,
397
     Elf_Internal_Rela *     relocs,
398
     Elf_Internal_Sym *      local_syms,
399
     asection **       local_sections)
400
0
{
401
0
  Elf_Internal_Shdr *   symtab_hdr;
402
0
  struct elf_link_hash_entry ** sym_hashes;
403
0
  Elf_Internal_Rela *   rel;
404
0
  Elf_Internal_Rela *   relend;
405
0
  asection *splt;
406
407
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
408
0
  sym_hashes = elf_sym_hashes (input_bfd);
409
0
  relend     = relocs + input_section->reloc_count;
410
411
0
  splt = elf_hash_table (info)->splt;
412
413
0
  for (rel = relocs; rel < relend; rel ++)
414
0
    {
415
0
      reloc_howto_type *     howto;
416
0
      unsigned long      r_symndx;
417
0
      Elf_Internal_Sym *     sym;
418
0
      asection *       sec;
419
0
      struct elf_link_hash_entry * h;
420
0
      bfd_vma        relocation;
421
0
      bfd_reloc_status_type    r;
422
0
      const char *       name = NULL;
423
0
      int        r_type;
424
425
0
      r_type = ELF32_R_TYPE (rel->r_info);
426
427
      /* These are only used for relaxing; we don't actually relocate
428
   anything with them, so skip them.  */
429
0
      if (r_type == R_M32C_RL_JUMP
430
0
    || r_type == R_M32C_RL_1ADDR
431
0
    || r_type == R_M32C_RL_2ADDR)
432
0
  continue;
433
434
0
      r_symndx = ELF32_R_SYM (rel->r_info);
435
436
0
      howto  = m32c_elf_howto_table + ELF32_R_TYPE (rel->r_info);
437
0
      h      = NULL;
438
0
      sym    = NULL;
439
0
      sec    = NULL;
440
0
      relocation = 0;
441
442
0
      if (r_symndx < symtab_hdr->sh_info)
443
0
  {
444
0
    sym = local_syms + r_symndx;
445
0
    sec = local_sections [r_symndx];
446
0
    relocation = (sec->output_section->vma
447
0
      + sec->output_offset
448
0
      + sym->st_value);
449
450
0
    name = bfd_elf_string_from_elf_section
451
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name);
452
0
    name = sym->st_name == 0 ? bfd_section_name (sec) : name;
453
0
  }
454
0
      else
455
0
  {
456
0
    h = sym_hashes [r_symndx - symtab_hdr->sh_info];
457
458
0
    if (info->wrap_hash != NULL
459
0
        && (input_section->flags & SEC_DEBUGGING) != 0)
460
0
      h = ((struct elf_link_hash_entry *)
461
0
     unwrap_hash_lookup (info, input_bfd, &h->root));
462
463
0
    while (h->root.type == bfd_link_hash_indirect
464
0
     || h->root.type == bfd_link_hash_warning)
465
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
466
467
0
    name = h->root.root.string;
468
469
0
    if (h->root.type == bfd_link_hash_defined
470
0
        || h->root.type == bfd_link_hash_defweak)
471
0
      {
472
0
        sec = h->root.u.def.section;
473
0
        relocation = (h->root.u.def.value
474
0
          + sec->output_section->vma
475
0
          + sec->output_offset);
476
0
      }
477
0
    else if (h->root.type == bfd_link_hash_undefweak)
478
0
      ;
479
0
    else if (!bfd_link_relocatable (info))
480
0
      (*info->callbacks->undefined_symbol) (info, h->root.root.string,
481
0
              input_bfd, input_section,
482
0
              rel->r_offset, true);
483
0
  }
484
485
0
      if (sec != NULL && discarded_section (sec))
486
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
487
0
           rel, 1, relend, R_M32C_NONE,
488
0
           howto, 0, contents);
489
490
0
      if (bfd_link_relocatable (info))
491
0
  {
492
    /* This is a relocatable link.  We don't have to change
493
       anything, unless the reloc is against a section symbol,
494
       in which case we have to adjust according to where the
495
       section symbol winds up in the output section.  */
496
0
    if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
497
0
      rel->r_addend += sec->output_offset;
498
0
    continue;
499
0
  }
500
501
0
      switch (ELF32_R_TYPE (rel->r_info))
502
0
  {
503
0
  case R_M32C_16:
504
0
    {
505
0
      bfd_vma *plt_offset;
506
507
0
      if (h != NULL)
508
0
        plt_offset = &h->plt.offset;
509
0
      else
510
0
        plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
511
512
      /*      printf("%s: rel %x plt %d\n", h ? h->root.root.string : "(none)",
513
        relocation, *plt_offset);*/
514
0
      if (relocation <= 0xffff)
515
0
        {
516
    /* If the symbol is in range for a 16-bit address, we should
517
       have deallocated the plt entry in relax_section.  */
518
0
    BFD_ASSERT (*plt_offset == (bfd_vma) -1);
519
0
        }
520
0
      else
521
0
        {
522
    /* If the symbol is out of range for a 16-bit address,
523
       we must have allocated a plt entry.  */
524
0
    BFD_ASSERT (*plt_offset != (bfd_vma) -1);
525
526
    /* If this is the first time we've processed this symbol,
527
       fill in the plt entry with the correct symbol address.  */
528
0
    if ((*plt_offset & 1) == 0)
529
0
      {
530
0
        unsigned int x;
531
532
0
        x = 0x000000fc;  /* jmpf */
533
0
        x |= (relocation << 8) & 0xffffff00;
534
0
        bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
535
0
        *plt_offset |= 1;
536
0
      }
537
538
0
    relocation = (splt->output_section->vma
539
0
            + splt->output_offset
540
0
            + (*plt_offset & -2));
541
0
    if (name)
542
0
    {
543
0
      char *newname = bfd_malloc (strlen(name)+5);
544
0
      strcpy (newname, name);
545
0
      strcat(newname, ".plt");
546
0
      _bfd_generic_link_add_one_symbol (info,
547
0
                input_bfd,
548
0
                newname,
549
0
                BSF_FUNCTION | BSF_WEAK,
550
0
                splt,
551
0
                (*plt_offset & -2),
552
0
                0,
553
0
                1,
554
0
                0,
555
0
                0);
556
0
    }
557
0
        }
558
0
    }
559
0
    break;
560
561
0
  case R_M32C_HI8:
562
0
  case R_M32C_HI16:
563
0
    relocation >>= 16;
564
0
    break;
565
0
  }
566
567
#if 0
568
      printf ("relocate %s at %06lx relocation %06lx addend %ld  ",
569
        m32c_elf_howto_table[ELF32_R_TYPE(rel->r_info)].name,
570
        rel->r_offset + input_section->output_section->vma + input_section->output_offset,
571
        relocation, rel->r_addend);
572
      {
573
  int i;
574
  for (i=0; i<4; i++)
575
    printf (" %02x", contents[rel->r_offset+i]);
576
  printf ("\n");
577
      }
578
#endif
579
0
      switch (ELF32_R_TYPE(rel->r_info))
580
0
  {
581
0
  case R_M32C_24:
582
    /* Like m32c_apply_reloc_24, we must handle this one separately.  */
583
0
    relocation += rel->r_addend;
584
585
    /* Sanity check the address.  */
586
0
    if (rel->r_offset + 3
587
0
        > bfd_get_section_limit_octets (input_bfd, input_section))
588
0
      r = bfd_reloc_outofrange;
589
0
    else
590
0
      {
591
0
        bfd_put_8 (input_bfd, relocation & 0xff, contents + rel->r_offset);
592
0
        bfd_put_8 (input_bfd, (relocation >> 8) & 0xff, contents + rel->r_offset + 1);
593
0
        bfd_put_8 (input_bfd, (relocation >> 16) & 0xff, contents + rel->r_offset + 2);
594
0
        r = bfd_reloc_ok;
595
0
      }
596
597
0
    break;
598
599
0
  default:
600
0
    r = _bfd_final_link_relocate (howto, input_bfd, input_section,
601
0
          contents, rel->r_offset, relocation,
602
0
          rel->r_addend);
603
0
    break;
604
0
  }
605
606
0
      if (r != bfd_reloc_ok)
607
0
  {
608
0
    const char * msg = (const char *) NULL;
609
610
0
    switch (r)
611
0
      {
612
0
      case bfd_reloc_overflow:
613
0
        (*info->callbacks->reloc_overflow)
614
0
    (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
615
0
     input_bfd, input_section, rel->r_offset);
616
0
        break;
617
618
0
      case bfd_reloc_undefined:
619
0
        (*info->callbacks->undefined_symbol)
620
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
621
0
        break;
622
623
0
      case bfd_reloc_outofrange:
624
0
        msg = _("internal error: out of range error");
625
0
        break;
626
627
0
      case bfd_reloc_notsupported:
628
0
        msg = _("internal error: unsupported relocation error");
629
0
        break;
630
631
0
      case bfd_reloc_dangerous:
632
0
        msg = _("internal error: dangerous relocation");
633
0
        break;
634
635
0
      default:
636
0
        msg = _("internal error: unknown error");
637
0
        break;
638
0
      }
639
640
0
    if (msg)
641
0
      (*info->callbacks->warning) (info, msg, name, input_bfd,
642
0
           input_section, rel->r_offset);
643
0
  }
644
0
    }
645
646
0
  return true;
647
0
}
648

649
/* We support 16-bit pointers to code above 64k by generating a thunk
650
   below 64k containing a JMP instruction to the final address.  */
651
652
static bool
653
m32c_elf_check_relocs
654
    (bfd *           abfd,
655
     struct bfd_link_info *    info,
656
     asection *          sec,
657
     const Elf_Internal_Rela * relocs)
658
0
{
659
0
  Elf_Internal_Shdr *   symtab_hdr;
660
0
  struct elf_link_hash_entry ** sym_hashes;
661
0
  const Elf_Internal_Rela * rel;
662
0
  const Elf_Internal_Rela * rel_end;
663
0
  bfd_vma *local_plt_offsets;
664
0
  asection *splt;
665
0
  bfd *dynobj;
666
667
0
  if (bfd_link_relocatable (info))
668
0
    return true;
669
670
0
  symtab_hdr = &elf_symtab_hdr (abfd);
671
0
  sym_hashes = elf_sym_hashes (abfd);
672
0
  local_plt_offsets = elf_local_got_offsets (abfd);
673
0
  splt = NULL;
674
0
  dynobj = elf_hash_table(info)->dynobj;
675
676
0
  rel_end = relocs + sec->reloc_count;
677
0
  for (rel = relocs; rel < rel_end; rel++)
678
0
    {
679
0
      struct elf_link_hash_entry *h;
680
0
      unsigned long r_symndx;
681
0
      bfd_vma *offset;
682
683
0
      r_symndx = ELF32_R_SYM (rel->r_info);
684
0
      if (r_symndx < symtab_hdr->sh_info)
685
0
  h = NULL;
686
0
      else
687
0
  {
688
0
    h = sym_hashes[r_symndx - symtab_hdr->sh_info];
689
0
    while (h->root.type == bfd_link_hash_indirect
690
0
     || h->root.type == bfd_link_hash_warning)
691
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
692
0
  }
693
694
0
      switch (ELF32_R_TYPE (rel->r_info))
695
0
  {
696
    /* This relocation describes a 16-bit pointer to a function.
697
       We may need to allocate a thunk in low memory; reserve memory
698
       for it now.  */
699
0
  case R_M32C_16:
700
0
    if (dynobj == NULL)
701
0
      elf_hash_table (info)->dynobj = dynobj = abfd;
702
0
    splt = elf_hash_table (info)->splt;
703
0
    if (splt == NULL)
704
0
      {
705
0
        flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
706
0
        | SEC_IN_MEMORY | SEC_LINKER_CREATED
707
0
        | SEC_READONLY | SEC_CODE);
708
0
        splt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
709
0
               flags);
710
0
        elf_hash_table (info)->splt = splt;
711
0
        if (splt == NULL
712
0
      || !bfd_set_section_alignment (splt, 1))
713
0
    return false;
714
0
      }
715
716
0
    if (h != NULL)
717
0
      offset = &h->plt.offset;
718
0
    else
719
0
      {
720
0
        if (local_plt_offsets == NULL)
721
0
    {
722
0
      size_t size;
723
0
      unsigned int i;
724
725
0
      size = symtab_hdr->sh_info * sizeof (bfd_vma);
726
0
      local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
727
0
      if (local_plt_offsets == NULL)
728
0
        return false;
729
0
      elf_local_got_offsets (abfd) = local_plt_offsets;
730
731
0
      for (i = 0; i < symtab_hdr->sh_info; i++)
732
0
        local_plt_offsets[i] = (bfd_vma) -1;
733
0
    }
734
0
        offset = &local_plt_offsets[r_symndx];
735
0
      }
736
737
0
    if (*offset == (bfd_vma) -1)
738
0
      {
739
0
        *offset = splt->size;
740
0
        splt->size += 4;
741
0
      }
742
0
    break;
743
0
  }
744
0
    }
745
746
0
  return true;
747
0
}
748
749
/* This must exist if dynobj is ever set.  */
750
751
static bool
752
m32c_elf_finish_dynamic_sections (struct bfd_link_info *info,
753
          bfd_byte *buf ATTRIBUTE_UNUSED)
754
0
{
755
0
  bfd *dynobj = elf_hash_table (info)->dynobj;
756
0
  asection *splt = elf_hash_table (info)->splt;
757
758
  /* As an extra sanity check, verify that all plt entries have
759
     been filled in.  */
760
761
0
  if (dynobj != NULL && splt != NULL)
762
0
    {
763
0
      bfd_byte *contents = splt->contents;
764
0
      unsigned int i, size = splt->size;
765
0
      for (i = 0; i < size; i += 4)
766
0
  {
767
0
    unsigned int x = bfd_get_32 (dynobj, contents + i);
768
0
    BFD_ASSERT (x != 0);
769
0
  }
770
0
    }
771
772
0
  return true;
773
0
}
774
775
static bool
776
m32c_elf_early_size_sections (struct bfd_link_info *info)
777
0
{
778
0
  bfd *dynobj;
779
0
  asection *splt;
780
781
0
  if (bfd_link_relocatable (info))
782
0
    return true;
783
784
0
  dynobj = elf_hash_table (info)->dynobj;
785
0
  if (dynobj == NULL)
786
0
    return true;
787
788
0
  splt = elf_hash_table (info)->splt;
789
0
  BFD_ASSERT (splt != NULL);
790
791
0
  splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
792
0
  if (splt->contents == NULL)
793
0
    return false;
794
0
  splt->alloced = 1;
795
796
0
  return true;
797
0
}
798

799
/* Function to set the ELF flag bits.  */
800
801
static bool
802
m32c_elf_set_private_flags (bfd *abfd, flagword flags)
803
0
{
804
0
  elf_elfheader (abfd)->e_flags = flags;
805
0
  elf_flags_init (abfd) = true;
806
0
  return true;
807
0
}
808
809
/* Merge backend specific data from an object file to the output
810
   object file when linking.  */
811
812
static bool
813
m32c_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
814
0
{
815
0
  bfd *obfd = info->output_bfd;
816
0
  flagword old_flags, old_partial;
817
0
  flagword new_flags, new_partial;
818
0
  bool error = false;
819
0
  char new_opt[80];
820
0
  char old_opt[80];
821
822
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
823
0
    return true;
824
825
0
  new_opt[0] = old_opt[0] = '\0';
826
0
  new_flags = elf_elfheader (ibfd)->e_flags;
827
0
  old_flags = elf_elfheader (obfd)->e_flags;
828
829
#ifdef DEBUG
830
  _bfd_error_handler
831
    ("old_flags = 0x%.8x, new_flags = 0x%.8x, init = %s, filename = %s",
832
     old_flags, new_flags, elf_flags_init (obfd) ? "yes" : "no",
833
     bfd_get_filename (ibfd));
834
#endif
835
836
0
  if (!elf_flags_init (obfd))
837
0
    {
838
      /* First call, no flags set.  */
839
0
      elf_flags_init (obfd) = true;
840
0
      elf_elfheader (obfd)->e_flags = new_flags;
841
0
    }
842
843
0
  else if (new_flags == old_flags)
844
    /* Compatible flags are ok.  */
845
0
    ;
846
847
0
  else    /* Possibly incompatible flags.  */
848
0
    {
849
      /* Warn if different cpu is used (allow a specific cpu to override
850
   the generic cpu).  */
851
0
      new_partial = (new_flags & EF_M32C_CPU_MASK);
852
0
      old_partial = (old_flags & EF_M32C_CPU_MASK);
853
0
      if (new_partial == old_partial)
854
0
  ;
855
856
0
      else
857
0
  {
858
0
    switch (new_partial)
859
0
      {
860
0
      default:      strcat (new_opt, " -m16c"); break;
861
0
      case EF_M32C_CPU_M16C: strcat (new_opt, " -m16c");  break;
862
0
      case EF_M32C_CPU_M32C:  strcat (new_opt, " -m32c");  break;
863
0
      }
864
865
0
    switch (old_partial)
866
0
      {
867
0
      default:      strcat (old_opt, " -m16c"); break;
868
0
      case EF_M32C_CPU_M16C: strcat (old_opt, " -m16c");  break;
869
0
      case EF_M32C_CPU_M32C:  strcat (old_opt, " -m32c");  break;
870
0
      }
871
0
  }
872
873
      /* Print out any mismatches from above.  */
874
0
      if (new_opt[0])
875
0
  {
876
0
    error = true;
877
0
    _bfd_error_handler
878
      /* xgettext:c-format */
879
0
      (_("%pB: compiled with %s and linked with modules compiled with %s"),
880
0
       ibfd, new_opt, old_opt);
881
0
  }
882
883
0
      new_flags &= ~ EF_M32C_ALL_FLAGS;
884
0
      old_flags &= ~ EF_M32C_ALL_FLAGS;
885
886
      /* Warn about any other mismatches.  */
887
0
      if (new_flags != old_flags)
888
0
  {
889
0
    error = true;
890
0
    _bfd_error_handler
891
      /* xgettext:c-format */
892
0
      (_("%pB: uses different e_flags (%#x) fields"
893
0
         " than previous modules (%#x)"),
894
0
       ibfd, new_flags, old_flags);
895
0
  }
896
0
    }
897
898
0
  if (error)
899
0
    bfd_set_error (bfd_error_bad_value);
900
901
0
  return !error;
902
0
}
903
904

905
static bool
906
m32c_elf_print_private_bfd_data (bfd *abfd, void *ptr)
907
102
{
908
102
  FILE *file = (FILE *) ptr;
909
102
  flagword flags;
910
911
102
  BFD_ASSERT (abfd != NULL && ptr != NULL);
912
913
  /* Print normal ELF private data.  */
914
102
  _bfd_elf_print_private_bfd_data (abfd, ptr);
915
916
102
  flags = elf_elfheader (abfd)->e_flags;
917
102
  fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
918
919
102
  switch (flags & EF_M32C_CPU_MASK)
920
102
    {
921
102
    default:              break;
922
102
    case EF_M32C_CPU_M16C: fprintf (file, " -m16c"); break;
923
0
    case EF_M32C_CPU_M32C:  fprintf (file, " -m32c");  break;
924
102
    }
925
926
102
  fputc ('\n', file);
927
102
  return true;
928
102
}
929
930
/* Return the MACH for an e_flags value.  */
931
932
static int
933
elf32_m32c_machine (bfd *abfd)
934
276
{
935
276
  switch (elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK)
936
276
    {
937
0
    case EF_M32C_CPU_M16C: return bfd_mach_m16c;
938
0
    case EF_M32C_CPU_M32C: return bfd_mach_m32c;
939
276
    }
940
941
276
  return bfd_mach_m16c;
942
276
}
943
944
static bool
945
m32c_elf_object_p (bfd *abfd)
946
276
{
947
276
  bfd_default_set_arch_mach (abfd, bfd_arch_m32c,
948
276
           elf32_m32c_machine (abfd));
949
276
  return true;
950
276
}
951
 
952
953
#ifdef DEBUG
954
void
955
dump_symtab (bfd * abfd, void *internal_syms, void *external_syms)
956
{
957
  size_t locsymcount;
958
  Elf_Internal_Sym *isymbuf;
959
  Elf_Internal_Sym *isymend;
960
  Elf_Internal_Sym *isym;
961
  Elf_Internal_Shdr *symtab_hdr;
962
  bool free_internal = 0, free_external = 0;
963
  char * st_info_str;
964
  char * st_info_stb_str;
965
  char * st_other_str;
966
  char * st_shndx_str;
967
968
  if (! internal_syms)
969
    {
970
      internal_syms = bfd_malloc (1000);
971
      free_internal = 1;
972
    }
973
  if (! external_syms)
974
    {
975
      external_syms = bfd_malloc (1000);
976
      free_external = 1;
977
    }
978
979
  symtab_hdr = &elf_symtab_hdr (abfd);
980
  locsymcount = symtab_hdr->sh_size / get_elf_backend_data(abfd)->s->sizeof_sym;
981
  if (free_internal)
982
    isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
983
            symtab_hdr->sh_info, 0,
984
            internal_syms, external_syms, NULL);
985
  else
986
    isymbuf = internal_syms;
987
  isymend = isymbuf + locsymcount;
988
989
  for (isym = isymbuf ; isym < isymend ; isym++)
990
    {
991
      switch (ELF_ST_TYPE (isym->st_info))
992
  {
993
  case STT_FUNC:
994
    st_info_str = "STT_FUNC";
995
    break;
996
997
  case STT_SECTION:
998
    st_info_str = "STT_SECTION";
999
    break;
1000
1001
  case STT_FILE:
1002
    st_info_str = "STT_FILE";
1003
    break;
1004
1005
  case STT_OBJECT:
1006
    st_info_str = "STT_OBJECT";
1007
    break;
1008
1009
  case STT_TLS:
1010
    st_info_str = "STT_TLS";
1011
    break;
1012
1013
  default:
1014
    st_info_str = "";
1015
  }
1016
1017
      switch (ELF_ST_BIND (isym->st_info))
1018
  {
1019
  case STB_LOCAL:
1020
    st_info_stb_str = "STB_LOCAL";
1021
    break;
1022
1023
  case STB_GLOBAL:
1024
    st_info_stb_str = "STB_GLOBAL";
1025
    break;
1026
1027
  default:
1028
    st_info_stb_str = "";
1029
  }
1030
1031
      switch (ELF_ST_VISIBILITY (isym->st_other))
1032
  {
1033
  case STV_DEFAULT:
1034
    st_other_str = "STV_DEFAULT";
1035
    break;
1036
1037
  case STV_INTERNAL:
1038
    st_other_str = "STV_INTERNAL";
1039
    break;
1040
1041
  case STV_PROTECTED:
1042
    st_other_str = "STV_PROTECTED";
1043
    break;
1044
1045
  default:
1046
    st_other_str = "";
1047
  }
1048
1049
      switch (isym->st_shndx)
1050
  {
1051
  case SHN_ABS:
1052
    st_shndx_str = "SHN_ABS";
1053
    break;
1054
1055
  case SHN_COMMON:
1056
    st_shndx_str = "SHN_COMMON";
1057
    break;
1058
1059
  case SHN_UNDEF:
1060
    st_shndx_str = "SHN_UNDEF";
1061
    break;
1062
1063
  default:
1064
    st_shndx_str = "";
1065
  }
1066
1067
      printf ("isym = %p st_value = %lx st_size = %lx st_name = (%lu) %s "
1068
        "st_info = (%d) %s %s st_other = (%d) %s st_shndx = (%d) %s\n",
1069
        isym,
1070
        (unsigned long) isym->st_value,
1071
        (unsigned long) isym->st_size,
1072
        isym->st_name,
1073
        bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
1074
                 isym->st_name),
1075
        isym->st_info, st_info_str, st_info_stb_str,
1076
        isym->st_other, st_other_str,
1077
        isym->st_shndx, st_shndx_str);
1078
    }
1079
  if (free_internal)
1080
    free (internal_syms);
1081
  if (free_external)
1082
    free (external_syms);
1083
}
1084
1085
char *
1086
m32c_get_reloc (long reloc)
1087
{
1088
  if (0 <= reloc && reloc < R_M32C_max)
1089
    return m32c_elf_howto_table[reloc].name;
1090
  else
1091
    return "";
1092
}
1093
#endif /* DEBUG */
1094
1095
/* Handle relaxing.  */
1096
1097
/* A subroutine of m32c_elf_relax_section.  If the global symbol H
1098
   is within the low 64k, remove any entry for it in the plt.  */
1099
1100
struct relax_plt_data
1101
{
1102
  asection *splt;
1103
  bool *again;
1104
};
1105
1106
static bool
1107
m32c_relax_plt_check (struct elf_link_hash_entry *h, void * xdata)
1108
0
{
1109
0
  struct relax_plt_data *data = (struct relax_plt_data *) xdata;
1110
1111
0
  if (h->plt.offset != (bfd_vma) -1)
1112
0
    {
1113
0
      bfd_vma address;
1114
1115
0
      if (h->root.type == bfd_link_hash_undefined
1116
0
    || h->root.type == bfd_link_hash_undefweak)
1117
0
  address = 0;
1118
0
      else
1119
0
  address = (h->root.u.def.section->output_section->vma
1120
0
       + h->root.u.def.section->output_offset
1121
0
       + h->root.u.def.value);
1122
1123
0
      if (address <= 0xffff)
1124
0
  {
1125
0
    h->plt.offset = -1;
1126
0
    data->splt->size -= 4;
1127
0
    *data->again = true;
1128
0
  }
1129
0
    }
1130
1131
0
  return true;
1132
0
}
1133
1134
/* A subroutine of m32c_elf_relax_section.  If the global symbol H
1135
   previously had a plt entry, give it a new entry offset.  */
1136
1137
static bool
1138
m32c_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata)
1139
0
{
1140
0
  bfd_vma *entry = (bfd_vma *) xdata;
1141
1142
0
  if (h->plt.offset != (bfd_vma) -1)
1143
0
    {
1144
0
      h->plt.offset = *entry;
1145
0
      *entry += 4;
1146
0
    }
1147
1148
0
  return true;
1149
0
}
1150
1151
static bool
1152
m32c_elf_relax_plt_section (asection *splt,
1153
          struct bfd_link_info *info,
1154
          bool *again)
1155
0
{
1156
0
  struct relax_plt_data relax_plt_data;
1157
0
  bfd *ibfd;
1158
1159
  /* Assume nothing changes.  */
1160
0
  *again = false;
1161
1162
0
  if (bfd_link_relocatable (info))
1163
0
    return true;
1164
1165
  /* Quick check for an empty plt.  */
1166
0
  if (splt->size == 0)
1167
0
    return true;
1168
1169
  /* Map across all global symbols; see which ones happen to
1170
     fall in the low 64k.  */
1171
0
  relax_plt_data.splt = splt;
1172
0
  relax_plt_data.again = again;
1173
0
  elf_link_hash_traverse (elf_hash_table (info), m32c_relax_plt_check,
1174
0
        &relax_plt_data);
1175
1176
  /* Likewise for local symbols, though that's somewhat less convenient
1177
     as we have to walk the list of input bfds and swap in symbol data.  */
1178
0
  for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1179
0
    {
1180
0
      bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1181
0
      Elf_Internal_Shdr *symtab_hdr;
1182
0
      Elf_Internal_Sym *isymbuf = NULL;
1183
0
      unsigned int idx;
1184
1185
0
      if (! local_plt_offsets)
1186
0
  continue;
1187
1188
0
      symtab_hdr = &elf_symtab_hdr (ibfd);
1189
0
      if (symtab_hdr->sh_info != 0)
1190
0
  {
1191
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1192
0
    if (isymbuf == NULL)
1193
0
      isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
1194
0
              symtab_hdr->sh_info, 0,
1195
0
              NULL, NULL, NULL);
1196
0
    if (isymbuf == NULL)
1197
0
      return false;
1198
0
  }
1199
1200
0
      for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
1201
0
  {
1202
0
    Elf_Internal_Sym *isym;
1203
0
    asection *tsec;
1204
0
    bfd_vma address;
1205
1206
0
    if (local_plt_offsets[idx] == (bfd_vma) -1)
1207
0
      continue;
1208
1209
0
    isym = &isymbuf[idx];
1210
0
    if (isym->st_shndx == SHN_UNDEF)
1211
0
      continue;
1212
0
    else if (isym->st_shndx == SHN_ABS)
1213
0
      tsec = bfd_abs_section_ptr;
1214
0
    else if (isym->st_shndx == SHN_COMMON)
1215
0
      tsec = bfd_com_section_ptr;
1216
0
    else
1217
0
      tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
1218
1219
0
    address = (tsec->output_section->vma
1220
0
         + tsec->output_offset
1221
0
         + isym->st_value);
1222
0
    if (address <= 0xffff)
1223
0
      {
1224
0
        local_plt_offsets[idx] = -1;
1225
0
        splt->size -= 4;
1226
0
        *again = true;
1227
0
      }
1228
0
  }
1229
1230
0
      if (isymbuf != NULL
1231
0
    && symtab_hdr->contents != (unsigned char *) isymbuf)
1232
0
  {
1233
0
    if (! info->keep_memory)
1234
0
      free (isymbuf);
1235
0
    else
1236
0
      {
1237
        /* Cache the symbols for elf_link_input_bfd.  */
1238
0
        symtab_hdr->contents = (unsigned char *) isymbuf;
1239
0
      }
1240
0
  }
1241
0
    }
1242
1243
  /* If we changed anything, walk the symbols again to reallocate
1244
     .plt entry addresses.  */
1245
0
  if (*again && splt->size > 0)
1246
0
    {
1247
0
      bfd_vma entry = 0;
1248
1249
0
      elf_link_hash_traverse (elf_hash_table (info),
1250
0
            m32c_relax_plt_realloc, &entry);
1251
1252
0
      for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
1253
0
  {
1254
0
    bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
1255
0
    unsigned int nlocals = elf_symtab_hdr (ibfd).sh_info;
1256
0
    unsigned int idx;
1257
1258
0
    if (! local_plt_offsets)
1259
0
      continue;
1260
1261
0
    for (idx = 0; idx < nlocals; ++idx)
1262
0
      if (local_plt_offsets[idx] != (bfd_vma) -1)
1263
0
        {
1264
0
    local_plt_offsets[idx] = entry;
1265
0
    entry += 4;
1266
0
        }
1267
0
  }
1268
0
    }
1269
1270
0
  return true;
1271
0
}
1272
1273
static int
1274
compare_reloc (const void *e1, const void *e2)
1275
0
{
1276
0
  const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
1277
0
  const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
1278
1279
0
  if (i1->r_offset == i2->r_offset)
1280
0
    return 0;
1281
0
  else
1282
0
    return i1->r_offset < i2->r_offset ? -1 : 1;
1283
0
}
1284
1285
0
#define OFFSET_FOR_RELOC(rel) m32c_offset_for_reloc (abfd, rel, symtab_hdr, shndx_buf, intsyms)
1286
static bfd_vma
1287
m32c_offset_for_reloc (bfd *abfd,
1288
           Elf_Internal_Rela *rel,
1289
           Elf_Internal_Shdr *symtab_hdr,
1290
           bfd_byte *shndx_buf ATTRIBUTE_UNUSED,
1291
           Elf_Internal_Sym *intsyms)
1292
0
{
1293
0
  bfd_vma symval;
1294
1295
  /* Get the value of the symbol referred to by the reloc.  */
1296
0
  if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
1297
0
    {
1298
      /* A local symbol.  */
1299
0
      Elf_Internal_Sym *isym;
1300
0
      asection *ssec;
1301
1302
0
      isym = intsyms + ELF32_R_SYM (rel->r_info);
1303
0
      ssec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1304
0
      symval = isym->st_value;
1305
0
      if (ssec)
1306
0
  symval += ssec->output_section->vma
1307
0
    + ssec->output_offset;
1308
0
    }
1309
0
  else
1310
0
    {
1311
0
      unsigned long indx;
1312
0
      struct elf_link_hash_entry *h;
1313
1314
      /* An external symbol.  */
1315
0
      indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
1316
0
      h = elf_sym_hashes (abfd)[indx];
1317
0
      BFD_ASSERT (h != NULL);
1318
1319
0
      if (h->root.type != bfd_link_hash_defined
1320
0
    && h->root.type != bfd_link_hash_defweak)
1321
  /* This appears to be a reference to an undefined
1322
     symbol.  Just ignore it--it will be caught by the
1323
     regular reloc processing.  */
1324
0
  return 0;
1325
1326
0
      symval = (h->root.u.def.value
1327
0
    + h->root.u.def.section->output_section->vma
1328
0
    + h->root.u.def.section->output_offset);
1329
0
    }
1330
0
  return symval;
1331
0
}
1332
1333
static int bytes_saved = 0;
1334
1335
static int bytes_to_reloc[] = {
1336
  R_M32C_NONE,
1337
  R_M32C_8,
1338
  R_M32C_16,
1339
  R_M32C_24,
1340
  R_M32C_32
1341
};
1342
1343
/* What we use the bits in a relax reloc addend (R_M32C_RL_*) for.  */
1344
1345
/* Mask for the number of relocs associated with this insn.  */
1346
#define RLA_RELOCS    0x0000000f
1347
/* Number of bytes gas emitted (before gas's relaxing) */
1348
#define RLA_NBYTES    0x00000ff0
1349
1350
/* If the displacement is within the given range and the new encoding
1351
   differs from the old encoding (the index), then the insn can be
1352
   relaxed to the new encoding.  */
1353
typedef const struct {
1354
  int bytes;
1355
  unsigned int max_disp;
1356
  unsigned char new_encoding;
1357
} EncodingTable;
1358
1359
static EncodingTable m16c_addr_encodings[] = {
1360
  { 0,   0,  0 }, /* R0 */
1361
  { 0,   0,  1 }, /* R1 */
1362
  { 0,   0,  2 }, /* R2 */
1363
  { 0,   0,  3 }, /* R3 */
1364
  { 0,   0,  4 }, /* A0 */
1365
  { 0,   0,  5 }, /* A1 */
1366
  { 0,   0,  6 }, /* [A0] */
1367
  { 0,   0,  7 }, /* [A1] */
1368
  { 1,   0,  6 }, /* udsp:8[A0] */
1369
  { 1,   0,  7 }, /* udsp:8[A1] */
1370
  { 1,   0, 10 }, /* udsp:8[SB] */
1371
  { 1,   0, 11 }, /* sdsp:8[FB] */
1372
  { 2, 255,  8 }, /* udsp:16[A0] */
1373
  { 2, 255,  9 }, /* udsp:16[A1] */
1374
  { 2, 255, 10 }, /* udsp:16[SB] */
1375
  { 2,   0, 15 }, /* abs:16 */
1376
};
1377
1378
static EncodingTable m16c_jmpaddr_encodings[] = {
1379
  { 0,   0,  0 }, /* R0 */
1380
  { 0,   0,  1 }, /* R1 */
1381
  { 0,   0,  2 }, /* R2 */
1382
  { 0,   0,  3 }, /* R3 */
1383
  { 0,   0,  4 }, /* A0 */
1384
  { 0,   0,  5 }, /* A1 */
1385
  { 0,   0,  6 }, /* [A0] */
1386
  { 0,   0,  7 }, /* [A1] */
1387
  { 1,   0,  6 }, /* udsp:8[A0] */
1388
  { 1,   0,  7 }, /* udsp:8[A1] */
1389
  { 1,   0, 10 }, /* udsp:8[SB] */
1390
  { 1,   0, 11 }, /* sdsp:8[FB] */
1391
  { 3, 255,  8 }, /* udsp:20[A0] */
1392
  { 3, 255,  9 }, /* udsp:20[A1] */
1393
  { 2, 255, 10 }, /* udsp:16[SB] */
1394
  { 2,   0, 15 }, /* abs:16 */
1395
};
1396
1397
static EncodingTable m32c_addr_encodings[] = {
1398
  { 0,     0,  0 }, /* [A0] */
1399
  { 0,     0,  1 }, /* [A1] */
1400
  { 0,     0,  2 }, /* A0 */
1401
  { 0,     0,  3 }, /* A1 */
1402
  { 1,     0,  0 }, /* udsp:8[A0] */
1403
  { 1,     0,  1 }, /* udsp:8[A1] */
1404
  { 1,     0,  6 }, /* udsp:8[SB] */
1405
  { 1,     0,  7 }, /* sdsp:8[FB] */
1406
  { 2,   255,  4 }, /* udsp:16[A0] */
1407
  { 2,   255,  5 }, /* udsp:16[A1] */
1408
  { 2,   255,  6 }, /* udsp:16[SB] */
1409
  { 2,   127,  7 }, /* sdsp:16[FB] */
1410
  { 3, 65535, 8 }, /* udsp:24[A0] */
1411
  { 3, 65535, 9 }, /* udsp:24[A1] */
1412
  { 3, 65535, 15 }, /* abs24 */
1413
  { 2,     0, 15 }, /* abs16 */
1414
  { 0,     0, 16 }, /* R2 */
1415
  { 0,     0, 17 }, /* R3 */
1416
  { 0,     0, 18 }, /* R0 */
1417
  { 0,     0, 19 }, /* R1 */
1418
  { 0,     0, 20 }, /*  */
1419
  { 0,     0, 21 }, /*  */
1420
  { 0,     0, 22 }, /*  */
1421
  { 0,     0, 23 }, /*  */
1422
  { 0,     0, 24 }, /*  */
1423
  { 0,     0, 25 }, /*  */
1424
  { 0,     0, 26 }, /*  */
1425
  { 0,     0, 27 }, /*  */
1426
  { 0,     0, 28 }, /*  */
1427
  { 0,     0, 29 }, /*  */
1428
  { 0,     0, 30 }, /*  */
1429
  { 0,     0, 31 }, /*  */
1430
};
1431
1432
static bool
1433
m32c_elf_relax_section (bfd *abfd,
1434
      asection *sec,
1435
      struct bfd_link_info *link_info,
1436
      bool *again)
1437
0
{
1438
0
  Elf_Internal_Shdr *symtab_hdr;
1439
0
  Elf_Internal_Shdr *shndx_hdr;
1440
0
  Elf_Internal_Rela *internal_relocs;
1441
0
  Elf_Internal_Rela *free_relocs = NULL;
1442
0
  Elf_Internal_Rela *irel, *irelend, *srel;
1443
0
  bfd_byte * contents = NULL;
1444
0
  bfd_byte * free_contents = NULL;
1445
0
  Elf_Internal_Sym *intsyms = NULL;
1446
0
  Elf_Internal_Sym *free_intsyms = NULL;
1447
0
  bfd_byte *shndx_buf = NULL;
1448
0
  int machine;
1449
1450
0
  if (is_elf_hash_table (link_info->hash)
1451
0
      && abfd == elf_hash_table (link_info)->dynobj
1452
0
      && (sec->flags & SEC_LINKER_CREATED) != 0
1453
0
      && strcmp (sec->name, ".plt") == 0)
1454
0
    return m32c_elf_relax_plt_section (sec, link_info, again);
1455
1456
  /* Assume nothing changes.  */
1457
0
  *again = false;
1458
1459
0
  machine = elf32_m32c_machine (abfd);
1460
1461
  /* We don't have to do anything for a relocatable link, if
1462
     this section does not have relocs, or if this is not a
1463
     code section.  */
1464
0
  if (bfd_link_relocatable (link_info)
1465
0
      || sec->reloc_count == 0
1466
0
      || (sec->flags & SEC_RELOC) == 0
1467
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
1468
0
      || (sec->flags & SEC_CODE) == 0)
1469
0
    return true;
1470
1471
0
  symtab_hdr = & elf_symtab_hdr (abfd);
1472
0
  if (elf_symtab_shndx_list (abfd))
1473
0
    shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
1474
0
  else
1475
0
    shndx_hdr = NULL;
1476
1477
  /* Get the section contents.  */
1478
0
  if (elf_section_data (sec)->this_hdr.contents != NULL)
1479
0
    contents = elf_section_data (sec)->this_hdr.contents;
1480
  /* Go get them off disk.  */
1481
0
  else if (!bfd_malloc_and_get_section (abfd, sec, &contents))
1482
0
    goto error_return;
1483
1484
  /* Read this BFD's symbols.  */
1485
  /* Get cached copy if it exists.  */
1486
0
  if (symtab_hdr->contents != NULL)
1487
0
    {
1488
0
      intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
1489
0
    }
1490
0
  else
1491
0
    {
1492
0
      intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
1493
0
      symtab_hdr->contents = (bfd_byte *) intsyms;
1494
0
    }
1495
1496
0
  if (shndx_hdr && shndx_hdr->sh_size != 0)
1497
0
    {
1498
0
      size_t amt;
1499
1500
0
      if (_bfd_mul_overflow (symtab_hdr->sh_info,
1501
0
           sizeof (Elf_External_Sym_Shndx), &amt))
1502
0
  {
1503
0
    bfd_set_error (bfd_error_file_too_big);
1504
0
    goto error_return;
1505
0
  }
1506
0
      if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0)
1507
0
  goto error_return;
1508
0
      shndx_buf = _bfd_malloc_and_read (abfd, amt, amt);
1509
0
      if (shndx_buf == NULL)
1510
0
  goto error_return;
1511
0
      shndx_hdr->contents = shndx_buf;
1512
0
    }
1513
1514
  /* Get a copy of the native relocations.  */
1515
0
  internal_relocs = (_bfd_elf_link_read_relocs
1516
0
         (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
1517
0
          link_info->keep_memory));
1518
0
  if (internal_relocs == NULL)
1519
0
    goto error_return;
1520
0
  if (! link_info->keep_memory)
1521
0
    free_relocs = internal_relocs;
1522
1523
  /* The RL_ relocs must be just before the operand relocs they go
1524
     with, so we must sort them to guarantee this.  */
1525
0
  qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
1526
0
   compare_reloc);
1527
1528
  /* Walk through them looking for relaxing opportunities.  */
1529
0
  irelend = internal_relocs + sec->reloc_count;
1530
1531
0
  for (irel = internal_relocs; irel < irelend; irel++)
1532
0
    {
1533
0
      bfd_vma symval;
1534
0
      unsigned char *insn, *gap, *einsn;
1535
0
      bfd_vma pc;
1536
0
      bfd_signed_vma pcrel;
1537
0
      int relax_relocs;
1538
0
      int gap_size;
1539
0
      int new_type;
1540
0
      int posn;
1541
0
      int enc;
1542
0
      EncodingTable *enctbl;
1543
0
      EncodingTable *e;
1544
1545
0
      if (ELF32_R_TYPE(irel->r_info) != R_M32C_RL_JUMP
1546
0
    && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_1ADDR
1547
0
    && ELF32_R_TYPE(irel->r_info) != R_M32C_RL_2ADDR)
1548
0
  continue;
1549
1550
0
      srel = irel;
1551
1552
      /* There will always be room for the relaxed insn, since it is smaller
1553
   than the one it would replace.  */
1554
0
      BFD_ASSERT (irel->r_offset < sec->size);
1555
1556
0
      insn = contents + irel->r_offset;
1557
0
      relax_relocs = irel->r_addend % 16;
1558
1559
      /* Ok, we only have three relocs we care about, and they're all
1560
   fake.  The lower four bits of the addend is always the number
1561
   of following relocs (hence the qsort above) that are assigned
1562
   to this opcode.  The next 8 bits of the addend indicates the
1563
   number of bytes in the insn.  We use the rest of them
1564
   ourselves as flags for the more expensive operations (defines
1565
   above).  The three relocs are:
1566
1567
   RL_JUMP: This marks all direct jump insns.  We check the
1568
    displacement and replace them with shorter jumps if
1569
    they're in range.  We also use this to find JMP.S
1570
    insns and manually shorten them when we delete bytes.
1571
    We have to decode these insns to figure out what to
1572
    do.
1573
1574
   RL_1ADDR: This is a :G or :Q insn, which has a single
1575
    "standard" operand.  We have to extract the type
1576
    field, see if it's a wide displacement, then figure
1577
    out if we can replace it with a narrow displacement.
1578
    We don't have to decode these insns.
1579
1580
   RL_2ADDR: Similarly, but two "standard" operands.  Note that
1581
    r_addend may still be 1, as standard operands don't
1582
    always have displacements.  Gas shouldn't give us one
1583
    with zero operands, but since we don't know which one
1584
    has the displacement, we check them both anyway.
1585
1586
   These all point to the beginning of the insn itself, not the
1587
   operands.
1588
1589
   Note that we only relax one step at a time, relying on the
1590
   linker to call us repeatedly.  Thus, there is no code for
1591
   JMP.A->JMP.B although that will happen in two steps.
1592
   Likewise, for 2ADDR relaxes, we do one operand per cycle.
1593
      */
1594
1595
      /* Get the value of the symbol referred to by the reloc.  Just
1596
   in case this is the last reloc in the list, use the RL's
1597
   addend to choose between this reloc (no addend) or the next
1598
   (yes addend, which means at least one following reloc).  */
1599
0
      srel = irel + (relax_relocs ? 1 : 0);
1600
0
      symval = OFFSET_FOR_RELOC (srel);
1601
1602
      /* Setting gap_size nonzero is the flag which means "something
1603
   shrunk".  */
1604
0
      gap_size = 0;
1605
0
      gap = NULL;
1606
0
      new_type = ELF32_R_TYPE(srel->r_info);
1607
1608
0
      pc = sec->output_section->vma + sec->output_offset
1609
0
  + srel->r_offset;
1610
0
      pcrel = symval - pc + srel->r_addend;
1611
1612
0
      if (machine == bfd_mach_m16c)
1613
0
  {
1614
    /* R8C / M16C */
1615
1616
0
    switch (ELF32_R_TYPE(irel->r_info))
1617
0
      {
1618
1619
0
      case R_M32C_RL_JUMP:
1620
0
        switch (insn[0])
1621
0
    {
1622
0
    case 0xfe: /* jmp.b */
1623
0
      if (pcrel >= 2 && pcrel <= 9)
1624
0
        {
1625
          /* Relax JMP.B -> JMP.S.  We need to get rid of
1626
       the following reloc though. */
1627
0
          insn[0] = 0x60 | (pcrel - 2);
1628
0
          new_type = R_M32C_NONE;
1629
0
          irel->r_addend = 0x10;
1630
0
          gap_size = 1;
1631
0
          gap = insn + 1;
1632
0
        }
1633
0
      break;
1634
1635
0
    case 0xf4: /* jmp.w */
1636
      /* 128 is allowed because it will be one byte closer
1637
         after relaxing.  Likewise for all other pc-rel
1638
         jumps.  */
1639
0
      if (pcrel <= 128 && pcrel >= -128)
1640
0
        {
1641
          /* Relax JMP.W -> JMP.B */
1642
0
          insn[0] = 0xfe;
1643
0
          insn[1] = 0;
1644
0
          new_type = R_M32C_8_PCREL;
1645
0
          gap_size = 1;
1646
0
          gap = insn + 2;
1647
0
        }
1648
0
      break;
1649
1650
0
    case 0xfc: /* jmp.a */
1651
0
      if (pcrel <= 32768 && pcrel >= -32768)
1652
0
        {
1653
          /* Relax JMP.A -> JMP.W */
1654
0
          insn[0] = 0xf4;
1655
0
          insn[1] = 0;
1656
0
          insn[2] = 0;
1657
0
          new_type = R_M32C_16_PCREL;
1658
0
          gap_size = 1;
1659
0
          gap = insn + 3;
1660
0
        }
1661
0
      break;
1662
1663
0
    case 0xfd: /* jsr.a */
1664
0
      if (pcrel <= 32768 && pcrel >= -32768)
1665
0
        {
1666
          /* Relax JSR.A -> JSR.W */
1667
0
          insn[0] = 0xf5;
1668
0
          insn[1] = 0;
1669
0
          insn[2] = 0;
1670
0
          new_type = R_M32C_16_PCREL;
1671
0
          gap_size = 1;
1672
0
          gap = insn + 3;
1673
0
        }
1674
0
      break;
1675
0
    }
1676
0
        break;
1677
1678
0
      case R_M32C_RL_2ADDR:
1679
        /* xxxx xxxx srce dest [src-disp] [dest-disp]*/
1680
1681
0
        enctbl = m16c_addr_encodings;
1682
0
        posn = 2;
1683
0
        enc = (insn[1] >> 4) & 0x0f;
1684
0
        e = & enctbl[enc];
1685
1686
0
        if (srel->r_offset == irel->r_offset + posn
1687
0
      && e->new_encoding != enc
1688
0
      && symval <= e->max_disp)
1689
0
    {
1690
0
      insn[1] &= 0x0f;
1691
0
      insn[1] |= e->new_encoding << 4;
1692
0
      gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1693
0
      gap = insn + posn + enctbl[e->new_encoding].bytes;
1694
0
      new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1695
0
      break;
1696
0
    }
1697
0
        if (relax_relocs == 2)
1698
0
    srel ++;
1699
0
        posn += e->bytes;
1700
1701
0
        goto try_1addr_16;
1702
1703
0
      case R_M32C_RL_1ADDR:
1704
        /* xxxx xxxx xxxx dest [disp] */
1705
1706
0
        enctbl = m16c_addr_encodings;
1707
0
        posn = 2;
1708
1709
        /* Check the opcode for jumps.  We know it's safe to
1710
     do this because all 2ADDR insns are at least two
1711
     bytes long.  */
1712
0
        enc = insn[0] * 256 + insn[1];
1713
0
        enc &= 0xfff0;
1714
0
        if (enc == 0x7d20
1715
0
      || enc == 0x7d00
1716
0
      || enc == 0x7d30
1717
0
      || enc == 0x7d10)
1718
0
    {
1719
0
      enctbl = m16c_jmpaddr_encodings;
1720
0
    }
1721
1722
0
      try_1addr_16:
1723
        /* srel, posn, and enc must be set here.  */
1724
1725
0
        symval = OFFSET_FOR_RELOC (srel);
1726
0
        enc = insn[1] & 0x0f;
1727
0
        e = & enctbl[enc];
1728
1729
0
        if (srel->r_offset == irel->r_offset + posn
1730
0
      && e->new_encoding != enc
1731
0
      && symval <= e->max_disp)
1732
0
    {
1733
0
      insn[1] &= 0xf0;
1734
0
      insn[1] |= e->new_encoding;
1735
0
      gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1736
0
      gap = insn + posn + enctbl[e->new_encoding].bytes;
1737
0
      new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1738
0
      break;
1739
0
    }
1740
1741
0
        break;
1742
1743
0
      } /* Ends switch (reloc type) for m16c.  */
1744
0
  }
1745
0
      else /* machine == bfd_mach_m32c */
1746
0
  {
1747
    /* M32CM / M32C */
1748
1749
0
    switch (ELF32_R_TYPE(irel->r_info))
1750
0
      {
1751
1752
0
      case R_M32C_RL_JUMP:
1753
0
        switch (insn[0])
1754
0
    {
1755
0
    case 0xbb: /* jmp.b */
1756
0
      if (pcrel >= 2 && pcrel <= 9)
1757
0
        {
1758
0
          int p = pcrel - 2;
1759
          /* Relax JMP.B -> JMP.S.  We need to get rid of
1760
       the following reloc though. */
1761
0
          insn[0] = 0x4a | ((p << 3) & 0x30) | (p & 1);
1762
0
          new_type = R_M32C_NONE;
1763
0
          irel->r_addend = 0x10;
1764
0
          gap_size = 1;
1765
0
          gap = insn + 1;
1766
0
        }
1767
0
      break;
1768
1769
0
    case 0xce: /* jmp.w */
1770
0
      if (pcrel <= 128 && pcrel >= -128)
1771
0
        {
1772
          /* Relax JMP.W -> JMP.B */
1773
0
          insn[0] = 0xbb;
1774
0
          insn[1] = 0;
1775
0
          new_type = R_M32C_8_PCREL;
1776
0
          gap_size = 1;
1777
0
          gap = insn + 2;
1778
0
        }
1779
0
      break;
1780
1781
0
    case 0xcc: /* jmp.a */
1782
0
      if (pcrel <= 32768 && pcrel >= -32768)
1783
0
        {
1784
          /* Relax JMP.A -> JMP.W */
1785
0
          insn[0] = 0xce;
1786
0
          insn[1] = 0;
1787
0
          insn[2] = 0;
1788
0
          new_type = R_M32C_16_PCREL;
1789
0
          gap_size = 1;
1790
0
          gap = insn + 3;
1791
0
        }
1792
0
      break;
1793
1794
0
    case 0xcd: /* jsr.a */
1795
0
      if (pcrel <= 32768 && pcrel >= -32768)
1796
0
        {
1797
          /* Relax JSR.A -> JSR.W */
1798
0
          insn[0] = 0xcf;
1799
0
          insn[1] = 0;
1800
0
          insn[2] = 0;
1801
0
          new_type = R_M32C_16_PCREL;
1802
0
          gap_size = 1;
1803
0
          gap = insn + 3;
1804
0
        }
1805
0
      break;
1806
0
    }
1807
0
        break;
1808
1809
0
      case R_M32C_RL_2ADDR:
1810
        /* xSSS DDDx DDSS xxxx [src-disp] [dest-disp]*/
1811
1812
0
        einsn = insn;
1813
0
        posn = 2;
1814
0
        if (einsn[0] == 1)
1815
0
    {
1816
      /* prefix; remove it as far as the RL reloc is concerned.  */
1817
0
      einsn ++;
1818
0
      posn ++;
1819
0
    }
1820
1821
0
        enctbl = m32c_addr_encodings;
1822
0
        enc = ((einsn[0] & 0x70) >> 2) | ((einsn[1] & 0x30) >> 4);
1823
0
        e = & enctbl[enc];
1824
1825
0
        if (srel->r_offset == irel->r_offset + posn
1826
0
      && e->new_encoding != enc
1827
0
      && symval <= e->max_disp)
1828
0
    {
1829
0
      einsn[0] &= 0x8f;
1830
0
      einsn[0] |= (e->new_encoding & 0x1c) << 2;
1831
0
      einsn[1] &= 0xcf;
1832
0
      einsn[1] |= (e->new_encoding & 0x03) << 4;
1833
0
      gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1834
0
      gap = insn + posn + enctbl[e->new_encoding].bytes;
1835
0
      new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1836
0
      break;
1837
0
    }
1838
0
        if (relax_relocs == 2)
1839
0
      srel ++;
1840
0
        posn += e->bytes;
1841
1842
0
        goto try_1addr_32;
1843
1844
0
      case R_M32C_RL_1ADDR:
1845
        /* xxxx DDDx DDxx xxxx [disp] */
1846
1847
0
        einsn = insn;
1848
0
        posn = 2;
1849
0
        if (einsn[0] == 1)
1850
0
    {
1851
      /* prefix; remove it as far as the RL reloc is concerned.  */
1852
0
      einsn ++;
1853
0
      posn ++;
1854
0
    }
1855
1856
0
        enctbl = m32c_addr_encodings;
1857
1858
0
      try_1addr_32:
1859
        /* srel, posn, and enc must be set here.  */
1860
1861
0
        symval = OFFSET_FOR_RELOC (srel);
1862
0
        enc = ((einsn[0] & 0x0e) << 1) |  ((einsn[1] & 0xc0) >> 6);
1863
0
        e = & enctbl[enc];
1864
1865
0
        if (srel->r_offset == irel->r_offset + posn
1866
0
      && e->new_encoding != enc
1867
0
      && symval <= e->max_disp)
1868
0
    {
1869
0
      einsn[0] &= 0xf1;
1870
0
      einsn[0] |= (e->new_encoding & 0x1c) >> 1;
1871
0
      einsn[1] &= 0x3f;
1872
0
      einsn[1] |= (e->new_encoding & 0x03) << 6;
1873
0
      gap_size = e->bytes - enctbl[e->new_encoding].bytes;
1874
0
      gap = insn + posn + enctbl[e->new_encoding].bytes;
1875
0
      new_type = bytes_to_reloc[enctbl[e->new_encoding].bytes];
1876
0
      break;
1877
0
    }
1878
1879
0
        break;
1880
1881
0
      } /* Ends switch (reloc type) for m32c.  */
1882
0
  }
1883
1884
0
      if (gap_size == 0)
1885
0
  continue;
1886
1887
0
      *again = true;
1888
1889
0
      srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), new_type);
1890
1891
      /* Note that we've changed the relocs, section contents, etc.  */
1892
0
      elf_section_data (sec)->relocs = internal_relocs;
1893
0
      free_relocs = NULL;
1894
1895
0
      elf_section_data (sec)->this_hdr.contents = contents;
1896
0
      free_contents = NULL;
1897
1898
0
      symtab_hdr->contents = (bfd_byte *) intsyms;
1899
0
      free_intsyms = NULL;
1900
1901
0
      bytes_saved += gap_size;
1902
1903
0
      if (! m32c_elf_relax_delete_bytes(abfd, sec, gap - contents, gap_size))
1904
0
  goto error_return;
1905
1906
0
    } /* next relocation */
1907
1908
0
  free (free_relocs);
1909
0
  free_relocs = NULL;
1910
1911
0
  if (free_contents != NULL)
1912
0
    {
1913
0
      if (! link_info->keep_memory)
1914
0
  free (free_contents);
1915
      /* Cache the section contents for elf_link_input_bfd.  */
1916
0
      else
1917
0
  elf_section_data (sec)->this_hdr.contents = contents;
1918
1919
0
      free_contents = NULL;
1920
0
    }
1921
1922
0
  if (shndx_buf != NULL)
1923
0
    {
1924
0
      shndx_hdr->contents = NULL;
1925
0
      free (shndx_buf);
1926
0
    }
1927
1928
0
  if (free_intsyms != NULL)
1929
0
    {
1930
0
      if (! link_info->keep_memory)
1931
0
  free (free_intsyms);
1932
      /* Cache the symbols for elf_link_input_bfd.  */
1933
0
      else
1934
0
  {
1935
0
    symtab_hdr->contents = NULL /* (unsigned char *) intsyms*/;
1936
0
  }
1937
1938
0
      free_intsyms = NULL;
1939
0
    }
1940
1941
0
  return true;
1942
1943
0
 error_return:
1944
0
  free (free_relocs);
1945
0
  free (free_contents);
1946
0
  if (shndx_buf != NULL)
1947
0
    {
1948
0
      shndx_hdr->contents = NULL;
1949
0
      free (shndx_buf);
1950
0
    }
1951
0
  free (free_intsyms);
1952
0
  return false;
1953
0
}
1954
1955
/* Delete some bytes from a section while relaxing.  */
1956
1957
static bool
1958
m32c_elf_relax_delete_bytes (bfd *abfd,
1959
           asection *sec,
1960
           bfd_vma addr,
1961
           int count)
1962
0
{
1963
0
  Elf_Internal_Shdr *symtab_hdr;
1964
0
  Elf_Internal_Shdr *shndx_hdr;
1965
0
  int sec_shndx;
1966
0
  bfd_byte *contents;
1967
0
  Elf_Internal_Rela *irel;
1968
0
  Elf_Internal_Rela *irelend;
1969
0
  bfd_vma toaddr;
1970
0
  Elf_Internal_Sym *isym;
1971
0
  Elf_Internal_Sym *isymend;
1972
0
  Elf_Internal_Sym *intsyms;
1973
0
  Elf_External_Sym_Shndx *shndx_buf;
1974
0
  Elf_External_Sym_Shndx *shndx;
1975
0
  struct elf_link_hash_entry ** sym_hashes;
1976
0
  struct elf_link_hash_entry ** end_hashes;
1977
0
  unsigned int      symcount;
1978
1979
0
  contents   = elf_section_data (sec)->this_hdr.contents;
1980
1981
0
  toaddr = sec->size;
1982
1983
0
  irel = elf_section_data (sec)->relocs;
1984
0
  irelend = irel + sec->reloc_count;
1985
1986
  /* Actually delete the bytes.  */
1987
0
  memmove (contents + addr, contents + addr + count, (size_t) (toaddr - addr - count));
1988
0
  sec->size -= count;
1989
1990
  /* Adjust all the relocs.  */
1991
0
  for (irel = elf_section_data (sec)->relocs; irel < irelend; irel ++)
1992
0
    {
1993
      /* Get the new reloc address.  */
1994
0
      if (irel->r_offset > addr && irel->r_offset < toaddr)
1995
0
  irel->r_offset -= count;
1996
1997
0
      if (ELF32_R_TYPE(irel->r_info) == R_M32C_RL_JUMP
1998
0
    && irel->r_addend == 0x10 /* one byte insn, no relocs */
1999
0
    && irel->r_offset + 1 < addr
2000
0
    && irel->r_offset + 7 > addr)
2001
0
  {
2002
0
    bfd_vma disp;
2003
0
    unsigned char *insn = &contents[irel->r_offset];
2004
0
    disp = *insn;
2005
    /* This is a JMP.S, which we have to manually update. */
2006
0
    if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
2007
0
      {
2008
0
        if ((*insn & 0xf8) != 0x60)
2009
0
    continue;
2010
0
        disp = (disp & 7);
2011
0
      }
2012
0
    else
2013
0
      {
2014
0
        if ((*insn & 0xce) != 0x4a)
2015
0
    continue;
2016
0
        disp = ((disp & 0x30) >> 3) | (disp & 1);
2017
0
      }
2018
0
    if (irel->r_offset + disp + 2 >= addr+count)
2019
0
      {
2020
0
        disp -= count;
2021
0
        if (elf32_m32c_machine (abfd) == bfd_mach_m16c)
2022
0
    {
2023
0
      *insn = (*insn & 0xf8) | disp;
2024
0
    }
2025
0
        else
2026
0
    {
2027
0
      *insn = (*insn & 0xce) | ((disp & 6) << 3) | (disp & 1);
2028
0
    }
2029
0
      }
2030
0
  }
2031
0
    }
2032
2033
  /* Adjust the local symbols defined in this section.  */
2034
0
  symtab_hdr = &elf_symtab_hdr (abfd);
2035
0
  intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
2036
0
  isym = intsyms;
2037
0
  isymend = isym + symtab_hdr->sh_info;
2038
2039
0
  sec_shndx  = _bfd_elf_section_from_bfd_section (abfd, sec);
2040
0
  if (elf_symtab_shndx_list (abfd))
2041
0
    {
2042
0
      shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
2043
0
      shndx_buf  = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
2044
0
    }
2045
0
  else
2046
0
    {
2047
0
      shndx_hdr = NULL;
2048
0
      shndx_buf = NULL;
2049
0
    }
2050
0
  shndx = shndx_buf;
2051
2052
0
  for (; isym < isymend; isym++, shndx = (shndx ? shndx + 1 : NULL))
2053
0
    {
2054
      /* If the symbol is in the range of memory we just moved, we
2055
   have to adjust its value.  */
2056
0
      if ((int) isym->st_shndx == sec_shndx
2057
0
    && isym->st_value > addr
2058
0
    && isym->st_value < toaddr)
2059
0
  {
2060
0
    isym->st_value -= count;
2061
0
  }
2062
      /* If the symbol *spans* the bytes we just deleted (i.e. it's
2063
   *end* is in the moved bytes but it's *start* isn't), then we
2064
   must adjust its size.  */
2065
0
      if ((int) isym->st_shndx == sec_shndx
2066
0
      && isym->st_value < addr
2067
0
    && isym->st_value + isym->st_size > addr
2068
0
    && isym->st_value + isym->st_size < toaddr)
2069
0
  {
2070
0
    isym->st_size -= count;
2071
0
  }
2072
0
    }
2073
2074
  /* Now adjust the global symbols defined in this section.  */
2075
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2076
0
        - symtab_hdr->sh_info);
2077
0
  sym_hashes = elf_sym_hashes (abfd);
2078
0
  end_hashes = sym_hashes + symcount;
2079
2080
0
  for (; sym_hashes < end_hashes; sym_hashes ++)
2081
0
    {
2082
0
      struct elf_link_hash_entry * sym_hash = * sym_hashes;
2083
2084
0
      if (sym_hash &&
2085
0
    (sym_hash->root.type == bfd_link_hash_defined
2086
0
     || sym_hash->root.type == bfd_link_hash_defweak)
2087
0
    && sym_hash->root.u.def.section == sec)
2088
0
  {
2089
0
    if (sym_hash->root.u.def.value > addr
2090
0
        && sym_hash->root.u.def.value < toaddr)
2091
0
      {
2092
0
        sym_hash->root.u.def.value -= count;
2093
0
      }
2094
0
    if (sym_hash->root.u.def.value < addr
2095
0
        && sym_hash->root.u.def.value + sym_hash->size > addr
2096
0
        && sym_hash->root.u.def.value + sym_hash->size < toaddr)
2097
0
      {
2098
0
        sym_hash->size -= count;
2099
0
      }
2100
0
  }
2101
0
    }
2102
2103
0
  return true;
2104
0
}
2105

2106
/* This is for versions of gcc prior to 4.3.  */
2107
static unsigned int
2108
_bfd_m32c_elf_eh_frame_address_size (bfd *abfd,
2109
             const asection *sec ATTRIBUTE_UNUSED)
2110
0
{
2111
0
  if ((elf_elfheader (abfd)->e_flags & EF_M32C_CPU_MASK) == EF_M32C_CPU_M16C)
2112
0
    return 2;
2113
0
  return 4;
2114
0
}
2115
2116

2117
2118
#define ELF_ARCH    bfd_arch_m32c
2119
#define ELF_MACHINE_CODE  EM_M32C
2120
#define ELF_MACHINE_ALT1  EM_M32C_OLD
2121
#define ELF_MAXPAGESIZE   0x100
2122
2123
#if 0
2124
#define TARGET_BIG_SYM    m32c_elf32_vec
2125
#define TARGET_BIG_NAME   "elf32-m32c"
2126
#else
2127
#define TARGET_LITTLE_SYM   m32c_elf32_vec
2128
#define TARGET_LITTLE_NAME    "elf32-m32c"
2129
#endif
2130
2131
#define elf_info_to_howto_rel     NULL
2132
#define elf_info_to_howto     m32c_info_to_howto_rela
2133
#define elf_backend_object_p      m32c_elf_object_p
2134
#define elf_backend_relocate_section    m32c_elf_relocate_section
2135
#define elf_backend_check_relocs    m32c_elf_check_relocs
2136
#define elf_backend_object_p      m32c_elf_object_p
2137
#define elf_symbol_leading_char     ('_')
2138
#define elf_backend_early_size_sections \
2139
  m32c_elf_early_size_sections
2140
#define elf_backend_finish_dynamic_sections \
2141
  m32c_elf_finish_dynamic_sections
2142
2143
#define elf_backend_can_gc_sections   1
2144
#define elf_backend_eh_frame_address_size _bfd_m32c_elf_eh_frame_address_size
2145
2146
#define bfd_elf32_bfd_reloc_type_lookup   m32c_reloc_type_lookup
2147
#define bfd_elf32_bfd_reloc_name_lookup m32c_reloc_name_lookup
2148
#define bfd_elf32_bfd_relax_section   m32c_elf_relax_section
2149
#define bfd_elf32_bfd_set_private_flags   m32c_elf_set_private_flags
2150
#define bfd_elf32_bfd_merge_private_bfd_data  m32c_elf_merge_private_bfd_data
2151
#define bfd_elf32_bfd_print_private_bfd_data  m32c_elf_print_private_bfd_data
2152
2153
#include "elf32-target.h"