OpenCores
URL https://opencores.org/ocsvn/open8_urisc/open8_urisc/trunk

Subversion Repositories open8_urisc

[/] [open8_urisc/] [trunk/] [gnu/] [binutils/] [bfd/] [elf32-m32c.c] - Blame information for rev 53

Go to most recent revision | Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.