Coverage Report

Created: 2026-03-10 08:46

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