Coverage Report

Created: 2026-11-21 09:00

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/binutils-gdb/bfd/elf32-h8300.c
Line
Count
Source
1
/* BFD back-end for Renesas H8/300 ELF binaries.
2
   Copyright (C) 1993-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/h8.h"
26
#include "cpu-h8300.h"
27
28
static reloc_howto_type *elf32_h8_reloc_type_lookup
29
  (bfd *abfd, bfd_reloc_code_real_type code);
30
static bool elf32_h8_info_to_howto
31
  (bfd *, arelent *, Elf_Internal_Rela *);
32
static bool elf32_h8_info_to_howto_rel
33
  (bfd *, arelent *, Elf_Internal_Rela *);
34
static unsigned long elf32_h8_mach (flagword);
35
static bool elf32_h8_object_p (bfd *);
36
static bool elf32_h8_merge_private_bfd_data
37
  (bfd *, struct bfd_link_info *);
38
static bool elf32_h8_relax_section
39
  (bfd *, asection *, struct bfd_link_info *, bool *);
40
static bool elf32_h8_relax_delete_bytes
41
  (bfd *, asection *, bfd_vma, int);
42
static bool elf32_h8_symbol_address_p (bfd *, asection *, bfd_vma);
43
static bfd_byte *elf32_h8_get_relocated_section_contents
44
  (bfd *, struct bfd_link_info *, const struct bfd_link_order *,
45
   bfd_byte *, bool, asymbol **);
46
static bfd_reloc_status_type elf32_h8_final_link_relocate
47
  (unsigned long, bfd *, bfd *, asection *,
48
   bfd_byte *, bfd_vma, bfd_vma, bfd_vma,
49
   struct bfd_link_info *, asection *, int);
50
static bfd_reloc_status_type special
51
  (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
52
53
/* This does not include any relocation information, but should be
54
   good enough for GDB or objdump to read the file.  */
55
56
static reloc_howto_type h8_elf_howto_table[] =
57
{
58
#define R_H8_NONE_X 0
59
  HOWTO (R_H8_NONE,   /* type */
60
   0,     /* rightshift */
61
   0,     /* size */
62
   0,     /* bitsize */
63
   false,     /* pc_relative */
64
   0,     /* bitpos */
65
   complain_overflow_dont,/* complain_on_overflow */
66
   special,   /* special_function */
67
   "R_H8_NONE",   /* name */
68
   false,     /* partial_inplace */
69
   0,     /* src_mask */
70
   0,     /* dst_mask */
71
   false),    /* pcrel_offset */
72
#define R_H8_DIR32_X (R_H8_NONE_X + 1)
73
  HOWTO (R_H8_DIR32,    /* type */
74
   0,     /* rightshift */
75
   4,     /* size */
76
   32,      /* bitsize */
77
   false,     /* pc_relative */
78
   0,     /* bitpos */
79
   complain_overflow_dont,/* complain_on_overflow */
80
   special,   /* special_function */
81
   "R_H8_DIR32",    /* name */
82
   false,     /* partial_inplace */
83
   0,     /* src_mask */
84
   0xffffffff,    /* dst_mask */
85
   false),    /* pcrel_offset */
86
#define R_H8_DIR16_X (R_H8_DIR32_X + 1)
87
  HOWTO (R_H8_DIR16,    /* type */
88
   0,     /* rightshift */
89
   2,     /* size */
90
   16,      /* bitsize */
91
   false,     /* pc_relative */
92
   0,     /* bitpos */
93
   complain_overflow_dont,/* complain_on_overflow */
94
   special,   /* special_function */
95
   "R_H8_DIR16",    /* name */
96
   false,     /* partial_inplace */
97
   0,     /* src_mask */
98
   0x0000ffff,    /* dst_mask */
99
   false),    /* pcrel_offset */
100
#define R_H8_DIR8_X (R_H8_DIR16_X + 1)
101
  HOWTO (R_H8_DIR8,   /* type */
102
   0,     /* rightshift */
103
   1,     /* size */
104
   8,     /* bitsize */
105
   false,     /* pc_relative */
106
   0,     /* bitpos */
107
   complain_overflow_dont,/* complain_on_overflow */
108
   special,   /* special_function */
109
   "R_H8_DIR8",   /* name */
110
   false,     /* partial_inplace */
111
   0,     /* src_mask */
112
   0x000000ff,    /* dst_mask */
113
   false),    /* pcrel_offset */
114
#define R_H8_DIR16A8_X (R_H8_DIR8_X + 1)
115
  HOWTO (R_H8_DIR16A8,    /* type */
116
   0,     /* rightshift */
117
   2,     /* size */
118
   16,      /* bitsize */
119
   false,     /* pc_relative */
120
   0,     /* bitpos */
121
   complain_overflow_bitfield, /* complain_on_overflow */
122
   special,   /* special_function */
123
   "R_H8_DIR16A8",  /* name */
124
   false,     /* partial_inplace */
125
   0,     /* src_mask */
126
   0x0000ffff,    /* dst_mask */
127
   false),    /* pcrel_offset */
128
#define R_H8_DIR16R8_X (R_H8_DIR16A8_X + 1)
129
  HOWTO (R_H8_DIR16R8,    /* type */
130
   0,     /* rightshift */
131
   2,     /* size */
132
   16,      /* bitsize */
133
   false,     /* pc_relative */
134
   0,     /* bitpos */
135
   complain_overflow_bitfield, /* complain_on_overflow */
136
   special,   /* special_function */
137
   "R_H8_DIR16R8",  /* name */
138
   false,     /* partial_inplace */
139
   0,     /* src_mask */
140
   0x0000ffff,    /* dst_mask */
141
   false),    /* pcrel_offset */
142
#define R_H8_DIR24A8_X (R_H8_DIR16R8_X + 1)
143
  HOWTO (R_H8_DIR24A8,    /* type */
144
   0,     /* rightshift */
145
   4,     /* size */
146
   24,      /* bitsize */
147
   false,     /* pc_relative */
148
   0,     /* bitpos */
149
   complain_overflow_bitfield, /* complain_on_overflow */
150
   special,   /* special_function */
151
   "R_H8_DIR24A8",  /* name */
152
   true,      /* partial_inplace */
153
   0xff000000,    /* src_mask */
154
   0x00ffffff,    /* dst_mask */
155
   false),    /* pcrel_offset */
156
#define R_H8_DIR24R8_X (R_H8_DIR24A8_X + 1)
157
  HOWTO (R_H8_DIR24R8,    /* type */
158
   0,     /* rightshift */
159
   4,     /* size */
160
   24,      /* bitsize */
161
   false,     /* pc_relative */
162
   0,     /* bitpos */
163
   complain_overflow_bitfield, /* complain_on_overflow */
164
   special,   /* special_function */
165
   "R_H8_DIR24R8",  /* name */
166
   true,      /* partial_inplace */
167
   0xff000000,    /* src_mask */
168
   0x00ffffff,    /* dst_mask */
169
   false),    /* pcrel_offset */
170
#define R_H8_DIR32A16_X (R_H8_DIR24R8_X + 1)
171
  HOWTO (R_H8_DIR32A16,   /* type */
172
   0,     /* rightshift */
173
   4,     /* size */
174
   32,      /* bitsize */
175
   false,     /* pc_relative */
176
   0,     /* bitpos */
177
   complain_overflow_dont,/* complain_on_overflow */
178
   special,   /* special_function */
179
   "R_H8_DIR32A16", /* name */
180
   false,     /* partial_inplace */
181
   0,     /* src_mask */
182
   0xffffffff,    /* dst_mask */
183
   false),    /* pcrel_offset */
184
#define R_H8_DISP32A16_X (R_H8_DIR32A16_X + 1)
185
  HOWTO (R_H8_DISP32A16,  /* type */
186
   0,     /* rightshift */
187
   4,     /* size */
188
   32,      /* bitsize */
189
   false,     /* pc_relative */
190
   0,     /* bitpos */
191
   complain_overflow_dont,/* complain_on_overflow */
192
   special,   /* special_function */
193
   "R_H8_DISP32A16",  /* name */
194
   false,     /* partial_inplace */
195
   0,     /* src_mask */
196
   0xffffffff,    /* dst_mask */
197
   false),    /* pcrel_offset */
198
#define R_H8_PCREL16_X (R_H8_DISP32A16_X + 1)
199
  HOWTO (R_H8_PCREL16,    /* type */
200
   0,     /* rightshift */
201
   2,     /* size */
202
   16,      /* bitsize */
203
   true,      /* pc_relative */
204
   0,     /* bitpos */
205
   complain_overflow_signed,/* complain_on_overflow */
206
   special,   /* special_function */
207
   "R_H8_PCREL16",  /* name */
208
   false,     /* partial_inplace */
209
   0xffff,    /* src_mask */
210
   0xffff,    /* dst_mask */
211
   true),     /* pcrel_offset */
212
#define R_H8_PCREL8_X (R_H8_PCREL16_X + 1)
213
  HOWTO (R_H8_PCREL8,   /* type */
214
   0,     /* rightshift */
215
   1,     /* size */
216
   8,     /* bitsize */
217
   true,      /* pc_relative */
218
   0,     /* bitpos */
219
   complain_overflow_signed,/* complain_on_overflow */
220
   special,   /* special_function */
221
   "R_H8_PCREL8",   /* name */
222
   false,     /* partial_inplace */
223
   0xff,      /* src_mask */
224
   0xff,      /* dst_mask */
225
   true),     /* pcrel_offset */
226
};
227
228
/* This structure is used to map BFD reloc codes to H8 ELF relocs.  */
229
230
struct elf_reloc_map {
231
  bfd_reloc_code_real_type bfd_reloc_val;
232
  unsigned char howto_index;
233
};
234
235
/* An array mapping BFD reloc codes to H8 ELF relocs.  */
236
237
static const struct elf_reloc_map h8_reloc_map[] = {
238
  { BFD_RELOC_NONE, R_H8_NONE_X },
239
  { BFD_RELOC_32, R_H8_DIR32_X },
240
  { BFD_RELOC_16, R_H8_DIR16_X },
241
  { BFD_RELOC_8, R_H8_DIR8_X },
242
  { BFD_RELOC_H8_DIR16A8, R_H8_DIR16A8_X },
243
  { BFD_RELOC_H8_DIR16R8, R_H8_DIR16R8_X },
244
  { BFD_RELOC_H8_DIR24A8, R_H8_DIR24A8_X },
245
  { BFD_RELOC_H8_DIR24R8, R_H8_DIR24R8_X },
246
  { BFD_RELOC_H8_DIR32A16, R_H8_DIR32A16_X },
247
  { BFD_RELOC_H8_DISP32A16, R_H8_DISP32A16_X },
248
  { BFD_RELOC_16_PCREL, R_H8_PCREL16_X },
249
  { BFD_RELOC_8_PCREL, R_H8_PCREL8_X },
250
};
251
252
253
static reloc_howto_type *
254
elf32_h8_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
255
          bfd_reloc_code_real_type code)
256
0
{
257
0
  unsigned int i;
258
259
0
  for (i = 0; i < sizeof (h8_reloc_map) / sizeof (struct elf_reloc_map); i++)
260
0
    {
261
0
      if (h8_reloc_map[i].bfd_reloc_val == code)
262
0
  return &h8_elf_howto_table[(int) h8_reloc_map[i].howto_index];
263
0
    }
264
0
  return NULL;
265
0
}
266
267
static reloc_howto_type *
268
elf32_h8_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
269
          const char *r_name)
270
0
{
271
0
  unsigned int i;
272
273
0
  for (i = 0;
274
0
       i < sizeof (h8_elf_howto_table) / sizeof (h8_elf_howto_table[0]);
275
0
       i++)
276
0
    if (h8_elf_howto_table[i].name != NULL
277
0
  && strcasecmp (h8_elf_howto_table[i].name, r_name) == 0)
278
0
      return &h8_elf_howto_table[i];
279
280
0
  return NULL;
281
0
}
282
283
static bool
284
elf32_h8_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
285
      Elf_Internal_Rela *elf_reloc)
286
85
{
287
85
  unsigned int r;
288
85
  unsigned int i;
289
290
85
  r = ELF32_R_TYPE (elf_reloc->r_info);
291
282
  for (i = 0; i < sizeof (h8_elf_howto_table) / sizeof (reloc_howto_type); i++)
292
269
    if (h8_elf_howto_table[i].type == r)
293
72
      {
294
72
  bfd_reloc->howto = &h8_elf_howto_table[i];
295
72
  return true;
296
72
      }
297
  /* xgettext:c-format */
298
13
  _bfd_error_handler (_("%pB: unsupported relocation type %#x"), abfd, r);
299
13
  bfd_set_error (bfd_error_bad_value);
300
13
  return false;
301
85
}
302
303
static bool
304
elf32_h8_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
305
          arelent *bfd_reloc ATTRIBUTE_UNUSED,
306
          Elf_Internal_Rela *elf_reloc ATTRIBUTE_UNUSED)
307
0
{
308
0
  return false;
309
0
}
310
311
/* Special handling for H8/300 relocs.
312
   We only come here for pcrel stuff and return normally if not an -r link.
313
   When doing -r, we can't do any arithmetic for the pcrel stuff, because
314
   we support relaxing on the H8/300 series chips.  */
315
static bfd_reloc_status_type
316
special (bfd *abfd ATTRIBUTE_UNUSED,
317
   arelent *reloc_entry ATTRIBUTE_UNUSED,
318
   asymbol *symbol ATTRIBUTE_UNUSED,
319
   void * data ATTRIBUTE_UNUSED,
320
   asection *input_section ATTRIBUTE_UNUSED,
321
   bfd *output_bfd,
322
   char **error_message ATTRIBUTE_UNUSED)
323
0
{
324
0
  if (output_bfd == (bfd *) NULL)
325
0
    return bfd_reloc_continue;
326
327
  /* Adjust the reloc address to that in the output section.  */
328
0
  reloc_entry->address += input_section->output_offset;
329
0
  return bfd_reloc_ok;
330
0
}
331
332
/* Perform a relocation as part of a final link.  */
333
static bfd_reloc_status_type
334
elf32_h8_final_link_relocate (unsigned long r_type, bfd *input_bfd,
335
            bfd *output_bfd ATTRIBUTE_UNUSED,
336
            asection *input_section ATTRIBUTE_UNUSED,
337
            bfd_byte *contents, bfd_vma offset,
338
            bfd_vma value, bfd_vma addend,
339
            struct bfd_link_info *info ATTRIBUTE_UNUSED,
340
            asection *sym_sec ATTRIBUTE_UNUSED,
341
            int is_local ATTRIBUTE_UNUSED)
342
0
{
343
0
  bfd_byte *hit_data = contents + offset;
344
345
0
  switch (r_type)
346
0
    {
347
0
    case R_H8_NONE:
348
0
      return bfd_reloc_ok;
349
350
0
    case R_H8_DIR32:
351
0
    case R_H8_DIR32A16:
352
0
    case R_H8_DISP32A16:
353
0
    case R_H8_DIR24A8:
354
0
      value += addend;
355
0
      bfd_put_32 (input_bfd, value, hit_data);
356
0
      return bfd_reloc_ok;
357
358
0
    case R_H8_DIR16:
359
0
    case R_H8_DIR16A8:
360
0
    case R_H8_DIR16R8:
361
0
      value += addend;
362
0
      bfd_put_16 (input_bfd, value, hit_data);
363
0
      return bfd_reloc_ok;
364
365
    /* AKA R_RELBYTE */
366
0
    case R_H8_DIR8:
367
0
      value += addend;
368
369
0
      bfd_put_8 (input_bfd, value, hit_data);
370
0
      return bfd_reloc_ok;
371
372
0
    case R_H8_DIR24R8:
373
0
      value += addend;
374
375
      /* HIT_DATA is the address for the first byte for the relocated
376
   value.  Subtract 1 so that we can manipulate the data in 32-bit
377
   hunks.  */
378
0
      hit_data--;
379
380
      /* Clear out the top byte in value.  */
381
0
      value &= 0xffffff;
382
383
      /* Retrieve the type byte for value from the section contents.  */
384
0
      value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
385
386
      /* Now scribble it out in one 32-bit hunk.  */
387
0
      bfd_put_32 (input_bfd, value, hit_data);
388
0
      return bfd_reloc_ok;
389
390
0
    case R_H8_PCREL16:
391
0
      value -= (input_section->output_section->vma
392
0
    + input_section->output_offset);
393
0
      value -= offset;
394
0
      value += addend;
395
396
      /* The value is relative to the start of the instruction,
397
   not the relocation offset.  Subtract 2 to account for
398
   this minor issue.  */
399
0
      value -= 2;
400
401
0
      bfd_put_16 (input_bfd, value, hit_data);
402
0
      return bfd_reloc_ok;
403
404
0
    case R_H8_PCREL8:
405
0
      value -= (input_section->output_section->vma
406
0
    + input_section->output_offset);
407
0
      value -= offset;
408
0
      value += addend;
409
410
      /* The value is relative to the start of the instruction,
411
   not the relocation offset.  Subtract 1 to account for
412
   this minor issue.  */
413
0
      value -= 1;
414
415
0
      bfd_put_8 (input_bfd, value, hit_data);
416
0
      return bfd_reloc_ok;
417
418
0
    default:
419
0
      return bfd_reloc_notsupported;
420
0
    }
421
0
}
422

423
/* Relocate an H8 ELF section.  */
424
static int
425
elf32_h8_relocate_section (struct bfd_link_info *info,
426
         bfd *input_bfd, asection *input_section,
427
         bfd_byte *contents, Elf_Internal_Rela *relocs,
428
         Elf_Internal_Sym *local_syms,
429
         asection **local_sections)
430
0
{
431
0
  Elf_Internal_Shdr *symtab_hdr;
432
0
  struct elf_link_hash_entry **sym_hashes;
433
0
  Elf_Internal_Rela *rel, *relend;
434
435
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
436
0
  sym_hashes = elf_sym_hashes (input_bfd);
437
438
0
  rel = relocs;
439
0
  relend = relocs + input_section->reloc_count;
440
0
  for (; rel < relend; rel++)
441
0
    {
442
0
      unsigned int r_type;
443
0
      unsigned long r_symndx;
444
0
      const Elf_Internal_Sym *sym;
445
0
      asection *sec;
446
0
      struct elf_link_hash_entry *h;
447
0
      bfd_vma relocation;
448
0
      bfd_reloc_status_type r;
449
0
      arelent bfd_reloc;
450
0
      reloc_howto_type *howto;
451
452
0
      if (! elf32_h8_info_to_howto (input_bfd, &bfd_reloc, rel))
453
0
  continue;
454
0
      howto = bfd_reloc.howto;
455
456
0
      r_symndx = ELF32_R_SYM (rel->r_info);
457
0
      r_type = ELF32_R_TYPE (rel->r_info);
458
0
      h = NULL;
459
0
      sym = NULL;
460
0
      sec = NULL;
461
0
      if (r_symndx < symtab_hdr->sh_info)
462
0
  {
463
0
    sym = local_syms + r_symndx;
464
0
    sec = local_sections[r_symndx];
465
0
    relocation = _bfd_elf_rela_local_sym (info->output_bfd,
466
0
            sym, &sec, rel);
467
0
  }
468
0
      else
469
0
  {
470
0
    bool unresolved_reloc, warned, ignored;
471
472
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
473
0
           r_symndx, symtab_hdr, sym_hashes,
474
0
           h, sec, relocation,
475
0
           unresolved_reloc, warned, ignored);
476
0
  }
477
478
0
      if (sec != NULL && discarded_section (sec))
479
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
480
0
           rel, 1, relend, R_H8_NONE,
481
0
           howto, 0, contents);
482
483
0
      if (bfd_link_relocatable (info))
484
0
  continue;
485
486
0
      r = elf32_h8_final_link_relocate (r_type, input_bfd, info->output_bfd,
487
0
          input_section,
488
0
          contents, rel->r_offset,
489
0
          relocation, rel->r_addend,
490
0
          info, sec, h == NULL);
491
492
0
      if (r != bfd_reloc_ok)
493
0
  {
494
0
    const char *name;
495
0
    const char *msg = (const char *) 0;
496
497
0
    if (h != NULL)
498
0
      name = h->root.root.string;
499
0
    else
500
0
      {
501
0
        name = (bfd_elf_string_from_elf_section
502
0
          (input_bfd, symtab_hdr->sh_link, sym->st_name));
503
0
        if (name == NULL || *name == '\0')
504
0
    name = bfd_section_name (sec);
505
0
      }
506
507
0
    switch (r)
508
0
      {
509
0
      case bfd_reloc_overflow:
510
0
        (*info->callbacks->reloc_overflow)
511
0
    (info, (h ? &h->root : NULL), name, howto->name,
512
0
     (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
513
0
        break;
514
515
0
      case bfd_reloc_undefined:
516
0
        (*info->callbacks->undefined_symbol)
517
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
518
0
        break;
519
520
0
      case bfd_reloc_outofrange:
521
0
        msg = _("internal error: out of range error");
522
0
        goto common_error;
523
524
0
      case bfd_reloc_notsupported:
525
0
        msg = _("internal error: unsupported relocation error");
526
0
        goto common_error;
527
528
0
      case bfd_reloc_dangerous:
529
0
        msg = _("internal error: dangerous error");
530
0
        goto common_error;
531
532
0
      default:
533
0
        msg = _("internal error: unknown error");
534
        /* fall through */
535
536
0
      common_error:
537
0
        (*info->callbacks->warning) (info, msg, name, input_bfd,
538
0
             input_section, rel->r_offset);
539
0
        break;
540
0
      }
541
0
  }
542
0
    }
543
544
0
  return true;
545
0
}
546
547
/* Object files encode the specific H8 model they were compiled
548
   for in the ELF flags field.
549
550
   Examine that field and return the proper BFD machine type for
551
   the object file.  */
552
static unsigned long
553
elf32_h8_mach (flagword flags)
554
546
{
555
546
  switch (flags & EF_H8_MACH)
556
546
    {
557
0
    case E_H8_MACH_H8300:
558
523
    default:
559
523
      return bfd_mach_h8300;
560
561
6
    case E_H8_MACH_H8300H:
562
6
      return bfd_mach_h8300h;
563
564
0
    case E_H8_MACH_H8300S:
565
0
      return bfd_mach_h8300s;
566
567
17
    case E_H8_MACH_H8300HN:
568
17
      return bfd_mach_h8300hn;
569
570
0
    case E_H8_MACH_H8300SN:
571
0
      return bfd_mach_h8300sn;
572
573
0
    case E_H8_MACH_H8300SX:
574
0
      return bfd_mach_h8300sx;
575
576
0
    case E_H8_MACH_H8300SXN:
577
0
      return bfd_mach_h8300sxn;
578
546
    }
579
546
}
580
581
/* The final processing done just before writing out a H8 ELF object
582
   file.  We use this opportunity to encode the BFD machine type
583
   into the flags field in the object file.  */
584
585
static bool
586
elf32_h8_final_write_processing (bfd *abfd)
587
2
{
588
2
  unsigned long val;
589
590
2
  switch (bfd_get_mach (abfd))
591
2
    {
592
0
    default:
593
2
    case bfd_mach_h8300:
594
2
      val = E_H8_MACH_H8300;
595
2
      break;
596
597
0
    case bfd_mach_h8300h:
598
0
      val = E_H8_MACH_H8300H;
599
0
      break;
600
601
0
    case bfd_mach_h8300s:
602
0
      val = E_H8_MACH_H8300S;
603
0
      break;
604
605
0
    case bfd_mach_h8300hn:
606
0
      val = E_H8_MACH_H8300HN;
607
0
      break;
608
609
0
    case bfd_mach_h8300sn:
610
0
      val = E_H8_MACH_H8300SN;
611
0
      break;
612
613
0
    case bfd_mach_h8300sx:
614
0
      val = E_H8_MACH_H8300SX;
615
0
      break;
616
617
0
    case bfd_mach_h8300sxn:
618
0
      val = E_H8_MACH_H8300SXN;
619
0
      break;
620
2
    }
621
622
2
  elf_elfheader (abfd)->e_flags &= ~ (EF_H8_MACH);
623
2
  elf_elfheader (abfd)->e_flags |= val;
624
2
  return _bfd_elf_final_write_processing (abfd);
625
2
}
626
627
/* Return nonzero if ABFD represents a valid H8 ELF object file; also
628
   record the encoded machine type found in the ELF flags.  */
629
630
static bool
631
elf32_h8_object_p (bfd *abfd)
632
546
{
633
546
  bfd_default_set_arch_mach (abfd, bfd_arch_h8300,
634
546
           elf32_h8_mach (elf_elfheader (abfd)->e_flags));
635
546
  return true;
636
546
}
637
638
/* Merge backend specific data from an object file to the output
639
   object file when linking.  The only data we need to copy at this
640
   time is the architecture/machine information.  */
641
642
static bool
643
elf32_h8_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
644
0
{
645
0
  bfd *obfd = info->output_bfd;
646
647
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
648
0
    return true;
649
650
0
  if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
651
0
      && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
652
0
    {
653
0
      if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
654
0
             bfd_get_mach (ibfd)))
655
0
  return false;
656
0
    }
657
658
0
  return true;
659
0
}
660
661
/* This function handles relaxing for the H8..
662
663
   There are a few relaxing opportunities available on the H8:
664
665
     jmp/jsr:24    ->  bra/bsr:8    2 bytes
666
     The jmp may be completely eliminated if the previous insn is a
667
     conditional branch to the insn after the jump.  In that case
668
     we invert the branch and delete the jump and save 4 bytes.
669
670
     bCC:16      ->    bCC:8      2 bytes
671
     bsr:16      ->    bsr:8      2 bytes
672
673
     bset:16       ->    bset:8     2 bytes
674
     bset:24/32      ->    bset:8     4 bytes
675
     (also applicable to other bit manipulation instructions)
676
677
     mov.b:16      ->    mov.b:8      2 bytes
678
     mov.b:24/32     ->    mov.b:8      4 bytes
679
680
     bset:24/32      ->    bset:16      2 bytes
681
     (also applicable to other bit manipulation instructions)
682
683
     mov.[bwl]:24/32 ->    mov.[bwl]:16     2 bytes
684
685
     mov.[bwl] @(displ:24/32+ERx) -> mov.[bwl] @(displ:16+ERx)  4 bytes.  */
686
687
static bool
688
elf32_h8_relax_section (bfd *abfd, asection *sec,
689
      struct bfd_link_info *link_info, bool *again)
690
0
{
691
0
  Elf_Internal_Shdr *symtab_hdr;
692
0
  Elf_Internal_Rela *internal_relocs;
693
0
  Elf_Internal_Rela *irel, *irelend;
694
0
  bfd_byte *contents = NULL;
695
0
  Elf_Internal_Sym *isymbuf = NULL;
696
0
  static asection *last_input_section = NULL;
697
0
  static Elf_Internal_Rela *last_reloc = NULL;
698
699
  /* Assume nothing changes.  */
700
0
  *again = false;
701
702
  /* We don't have to do anything for a relocatable link, if
703
     this section does not have relocs, or if this is not a
704
     code section.  */
705
0
  if (bfd_link_relocatable (link_info)
706
0
      || sec->reloc_count == 0
707
0
      || (sec->flags & SEC_RELOC) == 0
708
0
      || (sec->flags & SEC_HAS_CONTENTS) == 0
709
0
      || (sec->flags & SEC_CODE) == 0)
710
0
    return true;
711
712
0
  symtab_hdr = &elf_symtab_hdr (abfd);
713
714
  /* Get a copy of the native relocations.  */
715
0
  internal_relocs = _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL,
716
0
                 link_info->keep_memory);
717
0
  if (internal_relocs == NULL)
718
0
    goto error_return;
719
720
0
  if (sec != last_input_section)
721
0
    last_reloc = NULL;
722
723
0
  last_input_section = sec;
724
725
  /* Walk through the relocs looking for relaxing opportunities.  */
726
0
  irelend = internal_relocs + sec->reloc_count;
727
0
  for (irel = internal_relocs; irel < irelend; irel++)
728
0
    {
729
0
      bfd_vma symval;
730
731
0
      {
732
0
  arelent bfd_reloc;
733
734
0
  if (! elf32_h8_info_to_howto (abfd, &bfd_reloc, irel))
735
0
    continue;
736
0
      }
737
      /* Keep track of the previous reloc so that we can delete
738
   some long jumps created by the compiler.  */
739
0
      if (irel != internal_relocs)
740
0
  last_reloc = irel - 1;
741
742
0
      switch(ELF32_R_TYPE (irel->r_info))
743
0
  {
744
0
  case R_H8_DIR24R8:
745
0
  case R_H8_PCREL16:
746
0
  case R_H8_DIR16A8:
747
0
  case R_H8_DIR24A8:
748
0
  case R_H8_DIR32A16:
749
0
  case R_H8_DISP32A16:
750
0
    break;
751
0
  default:
752
0
    continue;
753
0
  }
754
755
      /* Get the section contents if we haven't done so already.  */
756
0
      if (contents == NULL)
757
0
  {
758
    /* Get cached copy if it exists.  */
759
0
    if (elf_section_data (sec)->this_hdr.contents != NULL)
760
0
      contents = elf_section_data (sec)->this_hdr.contents;
761
0
    else
762
0
      {
763
        /* Go get them off disk.  */
764
0
        if (!bfd_malloc_and_get_section (abfd, sec, &contents))
765
0
    goto error_return;
766
0
      }
767
0
  }
768
769
      /* Read this BFD's local symbols if we haven't done so already.  */
770
0
      if (isymbuf == NULL && symtab_hdr->sh_info != 0)
771
0
  {
772
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
773
0
    if (isymbuf == NULL)
774
0
      isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
775
0
              symtab_hdr->sh_info, 0,
776
0
              NULL, NULL, NULL);
777
0
    if (isymbuf == NULL)
778
0
      goto error_return;
779
0
  }
780
781
      /* Get the value of the symbol referred to by the reloc.  */
782
0
      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
783
0
  {
784
    /* A local symbol.  */
785
0
    Elf_Internal_Sym *isym;
786
0
    asection *sym_sec;
787
788
0
    isym = isymbuf + ELF32_R_SYM (irel->r_info);
789
0
    sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
790
0
    symval = isym->st_value;
791
    /* If the reloc is absolute, it will not have
792
       a symbol or section associated with it.  */
793
0
    if (sym_sec)
794
0
      symval += sym_sec->output_section->vma
795
0
        + sym_sec->output_offset;
796
0
  }
797
0
      else
798
0
  {
799
0
    unsigned long indx;
800
0
    struct elf_link_hash_entry *h;
801
802
    /* An external symbol.  */
803
0
    indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
804
0
    h = elf_sym_hashes (abfd)[indx];
805
0
    BFD_ASSERT (h != NULL);
806
0
    if (h->root.type != bfd_link_hash_defined
807
0
        && h->root.type != bfd_link_hash_defweak)
808
0
      {
809
        /* This appears to be a reference to an undefined
810
     symbol.  Just ignore it--it will be caught by the
811
     regular reloc processing.  */
812
0
        continue;
813
0
      }
814
815
0
    symval = (h->root.u.def.value
816
0
        + h->root.u.def.section->output_section->vma
817
0
        + h->root.u.def.section->output_offset);
818
0
  }
819
820
      /* For simplicity of coding, we are going to modify the section
821
   contents, the section relocs, and the BFD symbol table.  We
822
   must tell the rest of the code not to free up this
823
   information.  It would be possible to instead create a table
824
   of changes which have to be made, as is done in coff-mips.c;
825
   that would be more work, but would require less memory when
826
   the linker is run.  */
827
0
      switch (ELF32_R_TYPE (irel->r_info))
828
0
  {
829
    /* Try to turn a 24-bit absolute branch/call into an 8-bit
830
       pc-relative branch/call.  */
831
0
  case R_H8_DIR24R8:
832
0
    {
833
0
      bfd_vma value = symval + irel->r_addend;
834
0
      bfd_vma dot, gap;
835
836
      /* Get the address of this instruction.  */
837
0
      dot = (sec->output_section->vma
838
0
       + sec->output_offset + irel->r_offset - 1);
839
840
      /* Compute the distance from this insn to the branch target.  */
841
0
      gap = value - dot;
842
843
      /* If the distance is within -126..+130 inclusive, then we can
844
         relax this jump.  +130 is valid since the target will move
845
         two bytes closer if we do relax this branch.  */
846
0
      if ((int) gap >= -126 && (int) gap <= 130)
847
0
        {
848
0
    unsigned char code;
849
850
    /* Note that we've changed the relocs, section contents,
851
       etc.  */
852
0
    elf_section_data (sec)->relocs = internal_relocs;
853
0
    elf_section_data (sec)->this_hdr.contents = contents;
854
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
855
856
    /* Get the instruction code being relaxed.  */
857
0
    code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
858
859
    /* If the previous instruction conditionally jumped around
860
       this instruction, we may be able to reverse the condition
861
       and redirect the previous instruction to the target of
862
       this instruction.
863
864
       Such sequences are used by the compiler to deal with
865
       long conditional branches.
866
867
       Only perform this optimisation for jumps (code 0x5a) not
868
       subroutine calls, as otherwise it could transform:
869
870
       mov.w   r0,r0
871
       beq     .L1
872
       jsr     @_bar
873
       .L1:   rts
874
       _bar:  rts
875
       into:
876
       mov.w   r0,r0
877
       bne     _bar
878
       rts
879
       _bar:  rts
880
881
       which changes the call (jsr) into a branch (bne).  */
882
0
    if (code == 0x5a  /* jmp24.  */
883
0
        && (int) gap <= 130
884
0
        && (int) gap >= -128
885
0
        && last_reloc
886
0
        && ELF32_R_TYPE (last_reloc->r_info) == R_H8_PCREL8
887
0
        && ELF32_R_SYM (last_reloc->r_info) < symtab_hdr->sh_info)
888
0
      {
889
0
        bfd_vma last_value;
890
0
        asection *last_sym_sec;
891
0
        Elf_Internal_Sym *last_sym;
892
893
        /* We will need to examine the symbol used by the
894
           previous relocation.  */
895
896
0
        last_sym = isymbuf + ELF32_R_SYM (last_reloc->r_info);
897
0
        last_sym_sec
898
0
          = bfd_section_from_elf_index (abfd, last_sym->st_shndx);
899
0
        last_value = (last_sym->st_value
900
0
          + last_sym_sec->output_section->vma
901
0
          + last_sym_sec->output_offset);
902
903
        /* Verify that the previous relocation was for a
904
           branch around this instruction and that no symbol
905
           exists at the current location.  */
906
0
        if (last_value == dot + 4
907
0
      && last_reloc->r_offset + 2 == irel->r_offset
908
0
      && ! elf32_h8_symbol_address_p (abfd, sec, dot))
909
0
          {
910
      /* We can eliminate this jump.  Twiddle the
911
         previous relocation as necessary.  */
912
0
      irel->r_info
913
0
        = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
914
0
            ELF32_R_TYPE (R_H8_NONE));
915
916
0
      last_reloc->r_info
917
0
        = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
918
0
            ELF32_R_TYPE (R_H8_PCREL8));
919
0
      last_reloc->r_addend = irel->r_addend;
920
921
0
      code = bfd_get_8 (abfd,
922
0
            contents + last_reloc->r_offset - 1);
923
0
      code ^= 1;
924
0
      bfd_put_8 (abfd,
925
0
           code,
926
0
           contents + last_reloc->r_offset - 1);
927
928
      /* Delete four bytes of data.  */
929
0
      if (!elf32_h8_relax_delete_bytes (abfd, sec,
930
0
                irel->r_offset - 1,
931
0
                4))
932
0
        goto error_return;
933
934
0
      *again = true;
935
0
      break;
936
0
          }
937
0
      }
938
939
0
    if (code == 0x5e)
940
      /* This is jsr24  */
941
0
      bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 1); /* bsr8. */
942
0
    else if (code == 0x5a)
943
      /* This is jmp24  */
944
0
      bfd_put_8 (abfd, 0x40, contents + irel->r_offset - 1); /* bra8. */
945
0
    else
946
0
      abort ();
947
948
    /* Fix the relocation's type.  */
949
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
950
0
               R_H8_PCREL8);
951
952
    /* Delete two bytes of data.  */
953
0
    if (!elf32_h8_relax_delete_bytes (abfd, sec,
954
0
              irel->r_offset + 1, 2))
955
0
      goto error_return;
956
957
    /* That will change things, so, we should relax again.
958
       Note that this is not required, and it may be slow.  */
959
0
    *again = true;
960
0
        }
961
0
      break;
962
0
    }
963
964
    /* Try to turn a 16-bit pc-relative branch into a 8-bit pc-relative
965
       branch.  */
966
0
  case R_H8_PCREL16:
967
0
    {
968
0
      bfd_vma value = symval + irel->r_addend;
969
0
      bfd_vma dot;
970
0
      bfd_vma gap;
971
972
      /* Get the address of this instruction.  */
973
0
      dot = (sec->output_section->vma
974
0
       + sec->output_offset
975
0
       + irel->r_offset - 2);
976
977
0
      gap = value - dot;
978
979
      /* If the distance is within -126..+130 inclusive, then we can
980
         relax this jump.  +130 is valid since the target will move
981
         two bytes closer if we do relax this branch.  */
982
0
      if ((int) gap >= -126 && (int) gap <= 130)
983
0
        {
984
0
    unsigned char code;
985
986
    /* Note that we've changed the relocs, section contents,
987
       etc.  */
988
0
    elf_section_data (sec)->relocs = internal_relocs;
989
0
    elf_section_data (sec)->this_hdr.contents = contents;
990
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
991
992
    /* Get the opcode.  */
993
0
    code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
994
995
0
    if (code == 0x58)
996
0
      {
997
        /* bCC:16 -> bCC:8 */
998
        /* Get the second byte of the original insn, which
999
           contains the condition code.  */
1000
0
        code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1001
1002
        /* Compute the first byte of the relaxed
1003
           instruction.  The original sequence 0x58 0xX0
1004
           is relaxed to 0x4X, where X represents the
1005
           condition code.  */
1006
0
        code &= 0xf0;
1007
0
        code >>= 4;
1008
0
        code |= 0x40;
1009
0
        bfd_put_8 (abfd, code, contents + irel->r_offset - 2); /* bCC:8.  */
1010
0
      }
1011
0
    else if (code == 0x5c) /* bsr16.  */
1012
      /* This is bsr.  */
1013
0
      bfd_put_8 (abfd, 0x55, contents + irel->r_offset - 2);  /* bsr8.  */
1014
0
    else
1015
      /* Might be MOVSD.  */
1016
0
      break;
1017
1018
    /* Fix the relocation's type.  */
1019
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1020
0
               R_H8_PCREL8);
1021
0
    irel->r_offset--;
1022
1023
    /* Delete two bytes of data.  */
1024
0
    if (!elf32_h8_relax_delete_bytes (abfd, sec,
1025
0
              irel->r_offset + 1, 2))
1026
0
      goto error_return;
1027
1028
    /* That will change things, so, we should relax again.
1029
       Note that this is not required, and it may be slow.  */
1030
0
    *again = true;
1031
0
        }
1032
0
      break;
1033
0
    }
1034
1035
    /* This is a 16-bit absolute address in one of the following
1036
       instructions:
1037
1038
       "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
1039
       "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
1040
       "mov.b"
1041
1042
       We may relax this into an 8-bit absolute address if it's in
1043
       the right range.  */
1044
0
  case R_H8_DIR16A8:
1045
0
    {
1046
0
      bfd_vma value;
1047
1048
0
      value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
1049
0
      if (value >= 0xffffff00u)
1050
0
        {
1051
0
    unsigned char code;
1052
0
    unsigned char temp_code;
1053
1054
    /* Note that we've changed the relocs, section contents,
1055
       etc.  */
1056
0
    elf_section_data (sec)->relocs = internal_relocs;
1057
0
    elf_section_data (sec)->this_hdr.contents = contents;
1058
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
1059
1060
    /* Get the opcode.  */
1061
0
    code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1062
1063
    /* All instructions with R_H8_DIR16A8 start with
1064
       0x6a.  */
1065
0
    if (code != 0x6a)
1066
0
      abort ();
1067
1068
0
    temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1069
    /* If this is a mov.b instruction, clear the lower
1070
       nibble, which contains the source/destination
1071
       register number.  */
1072
0
    if ((temp_code & 0x10) != 0x10)
1073
0
      temp_code &= 0xf0;
1074
1075
0
    switch (temp_code)
1076
0
      {
1077
0
      case 0x00:
1078
        /* This is mov.b @aa:16,Rd.  */
1079
0
        bfd_put_8 (abfd, (code & 0xf) | 0x20,
1080
0
             contents + irel->r_offset - 2);
1081
0
        break;
1082
0
      case 0x80:
1083
        /* This is mov.b Rs,@aa:16.  */
1084
0
        bfd_put_8 (abfd, (code & 0xf) | 0x30,
1085
0
             contents + irel->r_offset - 2);
1086
0
        break;
1087
0
      case 0x18:
1088
        /* This is a bit-maniputation instruction that
1089
           stores one bit into memory, one of "bclr",
1090
           "bist", "bnot", "bset", and "bst".  */
1091
0
        bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2);
1092
0
        break;
1093
0
      case 0x10:
1094
        /* This is a bit-maniputation instruction that
1095
           loads one bit from memory, one of "band",
1096
           "biand", "bild", "bior", "bixor", "bld", "bor",
1097
           "btst", and "bxor".  */
1098
0
        bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2);
1099
0
        break;
1100
0
      default:
1101
0
        abort ();
1102
0
      }
1103
1104
    /* Fix the relocation's type.  */
1105
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1106
0
               R_H8_DIR8);
1107
1108
    /* Move the relocation.  */
1109
0
    irel->r_offset--;
1110
1111
    /* Delete two bytes of data.  */
1112
0
    if (!elf32_h8_relax_delete_bytes (abfd, sec,
1113
0
              irel->r_offset + 1, 2))
1114
0
      goto error_return;
1115
1116
    /* That will change things, so, we should relax again.
1117
       Note that this is not required, and it may be slow.  */
1118
0
    *again = true;
1119
0
        }
1120
0
      break;
1121
0
    }
1122
1123
    /* This is a 24-bit absolute address in one of the following
1124
       instructions:
1125
1126
       "band", "bclr", "biand", "bild", "bior", "bist", "bixor",
1127
       "bld", "bnot", "bor", "bset", "bst", "btst", "bxor", and
1128
       "mov.b"
1129
1130
       We may relax this into an 8-bit absolute address if it's in
1131
       the right range.  */
1132
0
  case R_H8_DIR24A8:
1133
0
    {
1134
0
      bfd_vma value;
1135
1136
0
      value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
1137
0
      if (value >= 0xffffff00u)
1138
0
        {
1139
0
    unsigned char code;
1140
0
    unsigned char temp_code;
1141
1142
    /* Note that we've changed the relocs, section contents,
1143
       etc.  */
1144
0
    elf_section_data (sec)->relocs = internal_relocs;
1145
0
    elf_section_data (sec)->this_hdr.contents = contents;
1146
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
1147
1148
    /* Get the opcode.  */
1149
0
    code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1150
1151
    /* All instructions with R_H8_DIR24A8 start with
1152
       0x6a.  */
1153
0
    if (code != 0x6a)
1154
0
      abort ();
1155
1156
0
    temp_code = code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1157
1158
    /* If this is a mov.b instruction, clear the lower
1159
       nibble, which contains the source/destination
1160
       register number.  */
1161
0
    if ((temp_code & 0x30) != 0x30)
1162
0
      temp_code &= 0xf0;
1163
1164
0
    switch (temp_code)
1165
0
      {
1166
0
      case 0x20:
1167
        /* This is mov.b @aa:24/32,Rd.  */
1168
0
        bfd_put_8 (abfd, (code & 0xf) | 0x20,
1169
0
             contents + irel->r_offset - 2);
1170
0
        break;
1171
0
      case 0xa0:
1172
        /* This is mov.b Rs,@aa:24/32.  */
1173
0
        bfd_put_8 (abfd, (code & 0xf) | 0x30,
1174
0
             contents + irel->r_offset - 2);
1175
0
        break;
1176
0
      case 0x38:
1177
        /* This is a bit-maniputation instruction that
1178
           stores one bit into memory, one of "bclr",
1179
           "bist", "bnot", "bset", and "bst".  */
1180
0
        bfd_put_8 (abfd, 0x7f, contents + irel->r_offset - 2);
1181
0
        break;
1182
0
      case 0x30:
1183
        /* This is a bit-maniputation instruction that
1184
           loads one bit from memory, one of "band",
1185
           "biand", "bild", "bior", "bixor", "bld", "bor",
1186
           "btst", and "bxor".  */
1187
0
        bfd_put_8 (abfd, 0x7e, contents + irel->r_offset - 2);
1188
0
        break;
1189
0
      default:
1190
0
        abort();
1191
0
      }
1192
1193
    /* Fix the relocation's type.  */
1194
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1195
0
               R_H8_DIR8);
1196
0
    irel->r_offset--;
1197
1198
    /* Delete four bytes of data.  */
1199
0
    if (!elf32_h8_relax_delete_bytes (abfd, sec,
1200
0
              irel->r_offset + 1, 4))
1201
0
      goto error_return;
1202
1203
    /* That will change things, so, we should relax again.
1204
       Note that this is not required, and it may be slow.  */
1205
0
    *again = true;
1206
0
    break;
1207
0
        }
1208
0
    }
1209
1210
    /* Fall through.  */
1211
1212
    /* This is a 24-/32-bit absolute address in one of the
1213
       following instructions:
1214
1215
       "band", "bclr", "biand", "bild", "bior", "bist",
1216
       "bixor", "bld", "bnot", "bor", "bset", "bst", "btst",
1217
       "bxor", "ldc.w", "stc.w" and "mov.[bwl]"
1218
1219
       We may relax this into an 16-bit absolute address if it's
1220
       in the right range.  */
1221
0
  case R_H8_DIR32A16:
1222
0
    {
1223
0
      bfd_vma value;
1224
1225
0
      value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
1226
0
      if (value <= 0x7fff || value >= 0xffff8000u)
1227
0
        {
1228
0
    unsigned char code;
1229
0
    unsigned char op0, op1, op2, op3;
1230
0
    unsigned char *op_ptr;
1231
1232
    /* Note that we've changed the relocs, section contents,
1233
       etc.  */
1234
0
    elf_section_data (sec)->relocs = internal_relocs;
1235
0
    elf_section_data (sec)->this_hdr.contents = contents;
1236
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
1237
1238
0
    if (irel->r_offset >= 4)
1239
0
      {
1240
        /* Check for 4-byte MOVA relaxation (SH-specific).  */
1241
0
        int second_reloc = 0;
1242
1243
0
        op_ptr = contents + irel->r_offset - 4;
1244
1245
0
        if (last_reloc)
1246
0
          {
1247
0
      arelent bfd_reloc;
1248
0
      reloc_howto_type *h;
1249
0
      bfd_vma last_reloc_size;
1250
1251
0
      if (! elf32_h8_info_to_howto (abfd, &bfd_reloc, last_reloc))
1252
0
        break;
1253
0
      h = bfd_reloc.howto;
1254
0
      last_reloc_size = 1 << h->size;
1255
0
      if (last_reloc->r_offset + last_reloc_size
1256
0
          == irel->r_offset)
1257
0
        {
1258
0
          op_ptr -= last_reloc_size;
1259
0
          second_reloc = 1;
1260
0
        }
1261
0
          }
1262
1263
0
        if (irel + 1 < irelend)
1264
0
          {
1265
0
      Elf_Internal_Rela *next_reloc = irel + 1;
1266
0
      arelent bfd_reloc;
1267
0
      reloc_howto_type *h;
1268
0
      bfd_vma next_reloc_size;
1269
1270
0
      if (! elf32_h8_info_to_howto (abfd, &bfd_reloc, next_reloc))
1271
0
        break;
1272
0
      h = bfd_reloc.howto;
1273
0
      next_reloc_size = 1 << h->size;
1274
0
      if (next_reloc->r_offset + next_reloc_size
1275
0
          == irel->r_offset)
1276
0
        {
1277
0
          op_ptr -= next_reloc_size;
1278
0
          second_reloc = 1;
1279
0
        }
1280
0
          }
1281
1282
0
        op0 = bfd_get_8 (abfd, op_ptr + 0);
1283
0
        op1 = bfd_get_8 (abfd, op_ptr + 1);
1284
0
        op2 = bfd_get_8 (abfd, op_ptr + 2);
1285
0
        op3 = bfd_get_8 (abfd, op_ptr + 3);
1286
1287
0
        if (op0 == 0x01
1288
0
      && (op1 & 0xdf) == 0x5f
1289
0
      && (op2 & 0x40) == 0x40
1290
0
      && (op3 & 0x80) == 0x80)
1291
0
          {
1292
0
      if ((op2 & 0x08) == 0)
1293
0
        second_reloc = 1;
1294
1295
0
      if (second_reloc)
1296
0
        {
1297
0
          op3 &= ~0x08;
1298
0
          bfd_put_8 (abfd, op3, op_ptr + 3);
1299
0
        }
1300
0
      else
1301
0
        {
1302
0
          op2 &= ~0x08;
1303
0
          bfd_put_8 (abfd, op2, op_ptr + 2);
1304
0
        }
1305
0
      goto r_h8_dir32a16_common;
1306
0
          }
1307
0
      }
1308
1309
    /* Now check for short version of MOVA.  (SH-specific) */
1310
0
    op_ptr = contents + irel->r_offset - 2;
1311
0
    op0 = bfd_get_8 (abfd, op_ptr + 0);
1312
0
    op1 = bfd_get_8 (abfd, op_ptr + 1);
1313
1314
0
    if (op0 == 0x7a
1315
0
        && (op1 & 0x88) == 0x80)
1316
0
      {
1317
0
        op1 |= 0x08;
1318
0
        bfd_put_8 (abfd, op1, op_ptr + 1);
1319
0
        goto r_h8_dir32a16_common;
1320
0
      }
1321
1322
    /* Get the opcode.  */
1323
0
    code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1324
1325
    /* Fix the opcode.  For all the instructions that
1326
       belong to this relaxation, we simply need to turn
1327
       off bit 0x20 in the previous byte.  */
1328
0
    code &= ~0x20;
1329
1330
0
    bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1331
1332
0
        r_h8_dir32a16_common:
1333
    /* Fix the relocation's type.  */
1334
0
    irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1335
0
               R_H8_DIR16);
1336
1337
    /* Delete two bytes of data.  */
1338
0
    if (!elf32_h8_relax_delete_bytes (abfd, sec,
1339
0
              irel->r_offset + 1, 2))
1340
0
      goto error_return;
1341
1342
    /* That will change things, so, we should relax again.
1343
       Note that this is not required, and it may be slow.  */
1344
0
    *again = true;
1345
0
        }
1346
0
      break; /* case R_H8_DIR32A16 */
1347
0
    }
1348
1349
0
  case R_H8_DISP32A16:
1350
    /* mov.[bwl] @(displ:24/32+ERx) -> mov.[bwl] @(displ:16+ERx)  4 bytes
1351
       It is assured that instruction uses at least 4 bytes opcode before
1352
       reloc entry addressing mode "register indirect with displacement"
1353
       relaxing options (all saving 4 bytes):
1354
       0x78 0sss0000 0x6A 0010dddd disp:32  mov.b @(d:32,ERs),Rd  ->
1355
       0x6E 0sssdddd disp:16  mov.b @(d:16,ERs),Rd
1356
       0x78 0sss0000 0x6B 0010dddd disp:32  mov.w @(d:32,ERs),Rd  ->
1357
       0x6F 0sssdddd disp:16  mov.w @(d:16,ERs),Rd
1358
       0x01 0x00 0x78 0sss0000 0x6B 00100ddd disp:32  mov.l @(d:32,ERs),ERd ->
1359
       0x01 0x00 0x6F 0sss0ddd disp:16  mov.l @(d:16,ERs),ERd
1360
1361
       0x78 0ddd0000 0x6A 1010ssss disp:32  mov.b Rs,@(d:32,ERd)  ->
1362
       0x6E 1dddssss disp:16  mov.b Rs,@(d:16,ERd)
1363
       0x78 0ddd0000 0x6B 1010ssss disp:32  mov.w Rs,@(d:32,ERd)  ->
1364
       0x6F 1dddssss disp:16  mov.w Rs,@(d:16,ERd)
1365
       0x01 0x00 0x78 xddd0000 0x6B 10100sss disp:32  mov.l ERs,@(d:32,ERd) ->
1366
       0x01 0x00 0x6F 1ddd0sss disp:16  mov.l ERs,@(d:16,ERd)
1367
       mov.l prefix 0x01 0x00 can be left as is and mov.l handled same
1368
       as mov.w/  */
1369
0
    {
1370
0
      bfd_vma value;
1371
1372
0
      value = bfd_h8300_pad_address (abfd, symval + irel->r_addend);
1373
0
      if (value <= 0x7fff || value >= 0xffff8000u)
1374
0
        {
1375
0
    unsigned char op0, op1, op2, op3, op0n, op1n;
1376
0
    int relax = 0;
1377
1378
    /* Note that we've changed the relocs, section contents,
1379
       etc.  */
1380
0
    elf_section_data (sec)->relocs = internal_relocs;
1381
0
    elf_section_data (sec)->this_hdr.contents = contents;
1382
0
    symtab_hdr->contents = (unsigned char *) isymbuf;
1383
1384
0
    if (irel->r_offset >= 4)
1385
0
      {
1386
0
        op0 = bfd_get_8 (abfd, contents + irel->r_offset - 4);
1387
0
        op1 = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1388
0
        op2 = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1389
0
        op3 = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1390
1391
0
        if (op0 == 0x78)
1392
0
          {
1393
0
      switch(op2)
1394
0
        {
1395
0
        case 0x6A:
1396
0
          if ((op1 & 0x8F) == 0x00 && (op3 & 0x70) == 0x20)
1397
0
            {
1398
        /* mov.b.  */
1399
0
        op0n = 0x6E;
1400
0
        relax = 1;
1401
0
            }
1402
0
          break;
1403
0
        case 0x6B:
1404
0
          if ((op1 & 0x0F) == 0x00 && (op3 & 0x70) == 0x20)
1405
0
            {
1406
        /* mov.w/l.  */
1407
0
        op0n = 0x6F;
1408
0
        relax = 1;
1409
0
            }
1410
0
          break;
1411
0
        default:
1412
0
          break;
1413
0
        }
1414
0
          }
1415
0
      }
1416
1417
0
    if (relax)
1418
0
      {
1419
0
        op1n = (op3 & 0x8F) | (op1 & 0x70);
1420
0
        bfd_put_8 (abfd, op0n, contents + irel->r_offset - 4);
1421
0
        bfd_put_8 (abfd, op1n, contents + irel->r_offset - 3);
1422
1423
        /* Fix the relocation's type.  */
1424
0
        irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info), R_H8_DIR16);
1425
0
        irel->r_offset -= 2;
1426
1427
        /* Delete four bytes of data.  */
1428
0
        if (!elf32_h8_relax_delete_bytes (abfd, sec, irel->r_offset + 2, 4))
1429
0
          goto error_return;
1430
1431
        /* That will change things, so, we should relax again.
1432
           Note that this is not required, and it may be slow.  */
1433
0
        *again = true;
1434
0
      }
1435
0
        }
1436
0
    }
1437
0
    break;
1438
1439
0
  default:
1440
0
    break;
1441
0
  }
1442
0
    }
1443
1444
0
  if (isymbuf != NULL
1445
0
      && symtab_hdr->contents != (unsigned char *) isymbuf)
1446
0
    {
1447
0
      if (! link_info->keep_memory)
1448
0
  free (isymbuf);
1449
0
      else
1450
0
  symtab_hdr->contents = (unsigned char *) isymbuf;
1451
0
    }
1452
1453
0
  if (contents != NULL
1454
0
      && elf_section_data (sec)->this_hdr.contents != contents)
1455
0
    {
1456
0
      if (! link_info->keep_memory)
1457
0
  free (contents);
1458
0
      else
1459
0
  {
1460
    /* Cache the section contents for elf_link_input_bfd.  */
1461
0
    elf_section_data (sec)->this_hdr.contents = contents;
1462
0
  }
1463
0
    }
1464
1465
0
  if (elf_section_data (sec)->relocs != internal_relocs)
1466
0
    free (internal_relocs);
1467
1468
0
  return true;
1469
1470
0
 error_return:
1471
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
1472
0
    free (isymbuf);
1473
0
  if (elf_section_data (sec)->this_hdr.contents != contents)
1474
0
    free (contents);
1475
0
  if (elf_section_data (sec)->relocs != internal_relocs)
1476
0
    free (internal_relocs);
1477
0
  return false;
1478
0
}
1479
1480
/* Delete some bytes from a section while relaxing.  */
1481
1482
static bool
1483
elf32_h8_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count)
1484
0
{
1485
0
  Elf_Internal_Shdr *symtab_hdr;
1486
0
  unsigned int sec_shndx;
1487
0
  bfd_byte *contents;
1488
0
  Elf_Internal_Rela *irel, *irelend;
1489
0
  Elf_Internal_Sym *isym;
1490
0
  Elf_Internal_Sym *isymend;
1491
0
  bfd_vma toaddr;
1492
0
  struct elf_link_hash_entry **sym_hashes;
1493
0
  struct elf_link_hash_entry **end_hashes;
1494
0
  unsigned int symcount;
1495
1496
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1497
1498
0
  contents = elf_section_data (sec)->this_hdr.contents;
1499
1500
0
  toaddr = sec->size;
1501
1502
0
  irel = elf_section_data (sec)->relocs;
1503
0
  irelend = irel + sec->reloc_count;
1504
1505
  /* Actually delete the bytes.  */
1506
0
  memmove (contents + addr, contents + addr + count,
1507
0
     (size_t) (toaddr - addr - count));
1508
0
  sec->size -= count;
1509
1510
  /* Adjust all the relocs.  */
1511
0
  for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1512
0
    {
1513
      /* Get the new reloc address.  */
1514
0
      if ((irel->r_offset > addr
1515
0
     && irel->r_offset <= toaddr))
1516
0
  irel->r_offset -= count;
1517
0
    }
1518
1519
  /* Adjust the local symbols defined in this section.  */
1520
0
  symtab_hdr = &elf_symtab_hdr (abfd);
1521
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1522
0
  isymend = isym + symtab_hdr->sh_info;
1523
0
  for (; isym < isymend; isym++)
1524
0
    {
1525
0
      if (isym->st_shndx == sec_shndx
1526
0
    && isym->st_value > addr
1527
0
    && isym->st_value <= toaddr)
1528
0
  isym->st_value -= count;
1529
0
    }
1530
1531
  /* Now adjust the global symbols defined in this section.  */
1532
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1533
0
        - symtab_hdr->sh_info);
1534
0
  sym_hashes = elf_sym_hashes (abfd);
1535
0
  end_hashes = sym_hashes + symcount;
1536
0
  for (; sym_hashes < end_hashes; sym_hashes++)
1537
0
    {
1538
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
1539
1540
0
      if ((sym_hash->root.type == bfd_link_hash_defined
1541
0
     || sym_hash->root.type == bfd_link_hash_defweak)
1542
0
    && sym_hash->root.u.def.section == sec
1543
0
    && sym_hash->root.u.def.value > addr
1544
0
    && sym_hash->root.u.def.value <= toaddr)
1545
0
  sym_hash->root.u.def.value -= count;
1546
0
    }
1547
1548
0
  return true;
1549
0
}
1550
1551
/* Return TRUE if a symbol exists at the given address, else return
1552
   FALSE.  */
1553
static bool
1554
elf32_h8_symbol_address_p (bfd *abfd, asection *sec, bfd_vma addr)
1555
0
{
1556
0
  Elf_Internal_Shdr *symtab_hdr;
1557
0
  unsigned int sec_shndx;
1558
0
  Elf_Internal_Sym *isym;
1559
0
  Elf_Internal_Sym *isymend;
1560
0
  struct elf_link_hash_entry **sym_hashes;
1561
0
  struct elf_link_hash_entry **end_hashes;
1562
0
  unsigned int symcount;
1563
1564
0
  sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1565
1566
  /* Examine all the symbols.  */
1567
0
  symtab_hdr = &elf_symtab_hdr (abfd);
1568
0
  isym = (Elf_Internal_Sym *) symtab_hdr->contents;
1569
0
  isymend = isym + symtab_hdr->sh_info;
1570
0
  for (; isym < isymend; isym++)
1571
0
    {
1572
0
      if (isym->st_shndx == sec_shndx
1573
0
    && isym->st_value == addr)
1574
0
  return true;
1575
0
    }
1576
1577
0
  symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1578
0
        - symtab_hdr->sh_info);
1579
0
  sym_hashes = elf_sym_hashes (abfd);
1580
0
  end_hashes = sym_hashes + symcount;
1581
0
  for (; sym_hashes < end_hashes; sym_hashes++)
1582
0
    {
1583
0
      struct elf_link_hash_entry *sym_hash = *sym_hashes;
1584
0
      if ((sym_hash->root.type == bfd_link_hash_defined
1585
0
     || sym_hash->root.type == bfd_link_hash_defweak)
1586
0
    && sym_hash->root.u.def.section == sec
1587
0
    && sym_hash->root.u.def.value == addr)
1588
0
  return true;
1589
0
    }
1590
1591
0
  return false;
1592
0
}
1593
1594
/* This is a version of bfd_generic_get_relocated_section_contents
1595
   which uses elf32_h8_relocate_section.  */
1596
1597
static bfd_byte *
1598
elf32_h8_get_relocated_section_contents
1599
  (bfd *output_bfd,
1600
   struct bfd_link_info *link_info,
1601
   const struct bfd_link_order *link_order,
1602
   bfd_byte *data,
1603
   bool relocatable,
1604
   asymbol **symbols)
1605
0
{
1606
0
  Elf_Internal_Shdr *symtab_hdr;
1607
0
  asection *input_section = link_order->u.indirect.section;
1608
0
  bfd *input_bfd = input_section->owner;
1609
0
  asection **sections = NULL;
1610
0
  Elf_Internal_Rela *internal_relocs = NULL;
1611
0
  Elf_Internal_Sym *isymbuf = NULL;
1612
1613
  /* We only need to handle the case of relaxing, or of having a
1614
     particular set of section contents, specially.  */
1615
0
  if (relocatable
1616
0
      || elf_section_data (input_section)->this_hdr.contents == NULL)
1617
0
    return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1618
0
                   link_order, data,
1619
0
                   relocatable,
1620
0
                   symbols);
1621
1622
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
1623
1624
0
  bfd_byte *orig_data = data;
1625
0
  if (data == NULL)
1626
0
    {
1627
0
      data = bfd_malloc (input_section->size);
1628
0
      if (data == NULL)
1629
0
  return NULL;
1630
0
    }
1631
0
  memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1632
0
    (size_t) input_section->size);
1633
1634
0
  if ((input_section->flags & SEC_RELOC) != 0
1635
0
      && input_section->reloc_count > 0)
1636
0
    {
1637
0
      asection **secpp;
1638
0
      Elf_Internal_Sym *isym, *isymend;
1639
0
      bfd_size_type amt;
1640
1641
0
      internal_relocs = _bfd_elf_link_read_relocs (input_bfd, input_section,
1642
0
               NULL, NULL, false);
1643
0
      if (internal_relocs == NULL)
1644
0
  goto error_return;
1645
1646
0
      if (symtab_hdr->sh_info != 0)
1647
0
  {
1648
0
    isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1649
0
    if (isymbuf == NULL)
1650
0
      isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1651
0
              symtab_hdr->sh_info, 0,
1652
0
              NULL, NULL, NULL);
1653
0
    if (isymbuf == NULL)
1654
0
      goto error_return;
1655
0
  }
1656
1657
0
      amt = symtab_hdr->sh_info;
1658
0
      amt *= sizeof (asection *);
1659
0
      sections = (asection **) bfd_malloc (amt);
1660
0
      if (sections == NULL && amt != 0)
1661
0
  goto error_return;
1662
1663
0
      isymend = isymbuf + symtab_hdr->sh_info;
1664
0
      for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
1665
0
  {
1666
0
    asection *isec;
1667
1668
0
    if (isym->st_shndx == SHN_UNDEF)
1669
0
      isec = bfd_und_section_ptr;
1670
0
    else if (isym->st_shndx == SHN_ABS)
1671
0
      isec = bfd_abs_section_ptr;
1672
0
    else if (isym->st_shndx == SHN_COMMON)
1673
0
      isec = bfd_com_section_ptr;
1674
0
    else
1675
0
      isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
1676
1677
0
    *secpp = isec;
1678
0
  }
1679
1680
0
      if (! elf32_h8_relocate_section (link_info, input_bfd,
1681
0
               input_section, data, internal_relocs,
1682
0
               isymbuf, sections))
1683
0
  goto error_return;
1684
1685
0
      free (sections);
1686
0
      if (symtab_hdr->contents != (unsigned char *) isymbuf)
1687
0
  free (isymbuf);
1688
0
      if (elf_section_data (input_section)->relocs != internal_relocs)
1689
0
  free (internal_relocs);
1690
0
    }
1691
1692
0
  return data;
1693
1694
0
 error_return:
1695
0
  free (sections);
1696
0
  if (symtab_hdr->contents != (unsigned char *) isymbuf)
1697
0
    free (isymbuf);
1698
0
  if (elf_section_data (input_section)->relocs != internal_relocs)
1699
0
    free (internal_relocs);
1700
0
  if (orig_data == NULL)
1701
0
    free (data);
1702
  return NULL;
1703
0
}
1704
1705
1706
#define TARGET_BIG_SYM      h8300_elf32_vec
1707
#define TARGET_BIG_NAME     "elf32-h8300"
1708
#define ELF_ARCH      bfd_arch_h8300
1709
#define ELF_MACHINE_CODE    EM_H8_300
1710
#define ELF_MAXPAGESIZE     0x1
1711
#define bfd_elf32_bfd_reloc_type_lookup elf32_h8_reloc_type_lookup
1712
#define bfd_elf32_bfd_reloc_name_lookup elf32_h8_reloc_name_lookup
1713
#define elf_info_to_howto   elf32_h8_info_to_howto
1714
#define elf_info_to_howto_rel   elf32_h8_info_to_howto_rel
1715
1716
/* So we can set/examine bits in e_flags to get the specific
1717
   H8 architecture in use.  */
1718
#define elf_backend_final_write_processing \
1719
  elf32_h8_final_write_processing
1720
#define elf_backend_object_p \
1721
  elf32_h8_object_p
1722
#define bfd_elf32_bfd_merge_private_bfd_data \
1723
  elf32_h8_merge_private_bfd_data
1724
1725
/* ??? when elf_backend_relocate_section is not defined, elf32-target.h
1726
   defaults to using _bfd_generic_link_hash_table_create, but
1727
   bfd_elf_size_dynamic_sections uses
1728
   dynobj = elf_hash_table (info)->dynobj;
1729
   and thus requires an elf hash table.  */
1730
#define bfd_elf32_bfd_link_hash_table_create _bfd_elf_link_hash_table_create
1731
1732
/* Use an H8 specific linker, not the ELF generic linker.  */
1733
#define elf_backend_relocate_section elf32_h8_relocate_section
1734
#define elf_backend_rela_normal   1
1735
#define elf_backend_can_gc_sections 1
1736
1737
/* And relaxing stuff.  */
1738
#define bfd_elf32_bfd_relax_section     elf32_h8_relax_section
1739
#define bfd_elf32_bfd_get_relocated_section_contents \
1740
        elf32_h8_get_relocated_section_contents
1741
1742
#define elf_symbol_leading_char '_'
1743
1744
#include "elf32-target.h"
1745
1746
#undef  TARGET_BIG_SYM
1747
#define TARGET_BIG_SYM      h8300_elf32_linux_vec
1748
#undef  TARGET_BIG_NAME
1749
#define TARGET_BIG_NAME     "elf32-h8300-linux"
1750
#undef  elf_symbol_leading_char
1751
#define elf32_bed     elf32_h8300_linux_bed
1752
1753
#include "elf32-target.h"