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-m68hc1x.c
Line
Count
Source
1
/* Motorola 68HC11/HC12-specific support for 32-bit ELF
2
   Copyright (C) 1999-2026 Free Software Foundation, Inc.
3
   Contributed by Stephane Carrez (stcarrez@nerim.fr)
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., 51 Franklin Street - Fifth Floor, Boston,
20
   MA 02110-1301, USA.  */
21
22
#include "sysdep.h"
23
#include "bfd.h"
24
#include "bfdlink.h"
25
#include "libbfd.h"
26
#include "elf-bfd.h"
27
#include "elf32-m68hc1x.h"
28
#include "elf/m68hc11.h"
29
#include "opcode/m68hc11.h"
30
#include "libiberty.h"
31
32
#define m68hc12_stub_hash_lookup(table, string, create, copy) \
33
0
  ((struct elf32_m68hc11_stub_hash_entry *) \
34
0
   bfd_hash_lookup ((table), (string), (create), (copy)))
35
36
static struct elf32_m68hc11_stub_hash_entry* m68hc12_add_stub
37
  (const char *stub_name,
38
   asection *section,
39
   struct m68hc11_elf_link_hash_table *htab);
40
41
static struct bfd_hash_entry *stub_hash_newfunc
42
  (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
43
44
static void m68hc11_elf_set_symbol (bfd* abfd, struct bfd_link_info *info,
45
            const char* name, bfd_vma value,
46
            asection* sec);
47
48
static bool m68hc11_elf_export_one_stub
49
  (struct bfd_hash_entry *gen_entry, void *in_arg);
50
51
static void scan_sections_for_abi (bfd*, asection*, void *);
52
53
struct m68hc11_scan_param
54
{
55
   struct m68hc11_page_info* pinfo;
56
   bool use_memory_banks;
57
};
58
59
60
/* Destroy a 68HC11/68HC12 ELF linker hash table.  */
61
62
static void
63
m68hc11_elf_bfd_link_hash_table_free (bfd *obfd)
64
0
{
65
0
  struct m68hc11_elf_link_hash_table *ret
66
0
    = (struct m68hc11_elf_link_hash_table *) obfd->link.hash;
67
68
0
  bfd_hash_table_free (ret->stub_hash_table);
69
0
  free (ret->stub_hash_table);
70
0
  _bfd_elf_link_hash_table_free (obfd);
71
0
}
72
73
/* Create a 68HC11/68HC12 ELF linker hash table.  */
74
75
struct m68hc11_elf_link_hash_table*
76
m68hc11_elf_hash_table_create (bfd *abfd)
77
0
{
78
0
  struct m68hc11_elf_link_hash_table *ret;
79
0
  size_t amt = sizeof (struct m68hc11_elf_link_hash_table);
80
81
0
  ret = (struct m68hc11_elf_link_hash_table *) bfd_zmalloc (amt);
82
0
  if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
83
0
    return NULL;
84
85
0
  if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
86
0
              _bfd_elf_link_hash_newfunc,
87
0
              sizeof (struct elf_link_hash_entry)))
88
0
    {
89
0
      free (ret);
90
0
      return NULL;
91
0
    }
92
93
  /* Init the stub hash table too.  */
94
0
  amt = sizeof (struct bfd_hash_table);
95
0
  ret->stub_hash_table = (struct bfd_hash_table*) bfd_malloc (amt);
96
0
  if (ret->stub_hash_table == NULL)
97
0
    {
98
0
      _bfd_elf_link_hash_table_free (abfd);
99
0
      return NULL;
100
0
    }
101
0
  if (!bfd_hash_table_init (ret->stub_hash_table, stub_hash_newfunc,
102
0
          sizeof (struct elf32_m68hc11_stub_hash_entry)))
103
0
    {
104
0
      free (ret->stub_hash_table);
105
0
      _bfd_elf_link_hash_table_free (abfd);
106
0
      return NULL;
107
0
    }
108
0
  ret->root.root.hash_table_free = m68hc11_elf_bfd_link_hash_table_free;
109
110
0
  return ret;
111
0
}
112
113
/* Assorted hash table functions.  */
114
115
/* Initialize an entry in the stub hash table.  */
116
117
static struct bfd_hash_entry *
118
stub_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
119
       const char *string)
120
0
{
121
  /* Allocate the structure if it has not already been allocated by a
122
     subclass.  */
123
0
  if (entry == NULL)
124
0
    {
125
0
      entry = bfd_hash_allocate (table,
126
0
         sizeof (struct elf32_m68hc11_stub_hash_entry));
127
0
      if (entry == NULL)
128
0
  return entry;
129
0
    }
130
131
  /* Call the allocation method of the superclass.  */
132
0
  entry = bfd_hash_newfunc (entry, table, string);
133
0
  if (entry != NULL)
134
0
    {
135
0
      struct elf32_m68hc11_stub_hash_entry *eh;
136
137
      /* Initialize the local fields.  */
138
0
      eh = (struct elf32_m68hc11_stub_hash_entry *) entry;
139
0
      eh->stub_sec = NULL;
140
0
      eh->stub_offset = 0;
141
0
      eh->target_value = 0;
142
0
      eh->target_section = NULL;
143
0
    }
144
145
0
  return entry;
146
0
}
147
148
/* Add a new stub entry to the stub hash.  Not all fields of the new
149
   stub entry are initialised.  */
150
151
static struct elf32_m68hc11_stub_hash_entry *
152
m68hc12_add_stub (const char *stub_name, asection *section,
153
      struct m68hc11_elf_link_hash_table *htab)
154
0
{
155
0
  struct elf32_m68hc11_stub_hash_entry *stub_entry;
156
157
  /* Enter this entry into the linker stub hash table.  */
158
0
  stub_entry = m68hc12_stub_hash_lookup (htab->stub_hash_table, stub_name,
159
0
           true, false);
160
0
  if (stub_entry == NULL)
161
0
    {
162
      /* xgettext:c-format */
163
0
      _bfd_error_handler (_("%pB: cannot create stub entry %s"),
164
0
        section->owner, stub_name);
165
0
      return NULL;
166
0
    }
167
168
0
  if (htab->stub_section == 0)
169
0
    {
170
0
      htab->stub_section = (*htab->add_stub_section) (".tramp",
171
0
                  htab->tramp_section);
172
0
    }
173
174
0
  stub_entry->stub_sec = htab->stub_section;
175
0
  stub_entry->stub_offset = 0;
176
0
  return stub_entry;
177
0
}
178
179
/* Hook called by the linker routine which adds symbols from an object
180
   file.  We use it for identify far symbols and force a loading of
181
   the trampoline handler.  */
182
183
bool
184
elf32_m68hc11_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
185
             Elf_Internal_Sym *sym,
186
             const char **namep ATTRIBUTE_UNUSED,
187
             flagword *flagsp ATTRIBUTE_UNUSED,
188
             asection **secp ATTRIBUTE_UNUSED,
189
             bfd_vma *valp ATTRIBUTE_UNUSED)
190
0
{
191
0
  if (sym->st_other & STO_M68HC12_FAR)
192
0
    {
193
0
      struct elf_link_hash_entry *h;
194
195
0
      h = (struct elf_link_hash_entry *)
196
0
  bfd_link_hash_lookup (info->hash, "__far_trampoline",
197
0
            false, false, false);
198
0
      if (h == NULL)
199
0
  {
200
0
    struct bfd_link_hash_entry* entry = NULL;
201
202
0
    _bfd_generic_link_add_one_symbol (info, abfd,
203
0
              "__far_trampoline",
204
0
              BSF_GLOBAL,
205
0
              bfd_und_section_ptr,
206
0
              (bfd_vma) 0, (const char*) NULL,
207
0
              false, false, &entry);
208
0
  }
209
210
0
    }
211
0
  return true;
212
0
}
213
214
/* Merge non-visibility st_other attributes, STO_M68HC12_FAR and
215
   STO_M68HC12_INTERRUPT.  */
216
217
void
218
elf32_m68hc11_merge_symbol_attribute (struct elf_link_hash_entry *h,
219
              unsigned int st_other,
220
              bool definition,
221
              bool dynamic ATTRIBUTE_UNUSED)
222
0
{
223
0
  if (definition)
224
0
    h->other = ((st_other & ~ELF_ST_VISIBILITY (-1))
225
0
    | ELF_ST_VISIBILITY (h->other));
226
0
}
227
228
/* External entry points for sizing and building linker stubs.  */
229
230
/* Set up various things so that we can make a list of input sections
231
   for each output section included in the link.  Returns -1 on error,
232
   0 when no stubs will be needed, and 1 on success.  */
233
234
int
235
elf32_m68hc11_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
236
0
{
237
0
  bfd *input_bfd;
238
0
  unsigned int bfd_count;
239
0
  unsigned int top_id, top_index;
240
0
  asection *section;
241
0
  asection **input_list, **list;
242
0
  size_t amt;
243
0
  asection *text_section;
244
0
  struct m68hc11_elf_link_hash_table *htab;
245
246
0
  htab = m68hc11_elf_hash_table (info);
247
0
  if (htab == NULL)
248
0
    return -1;
249
250
0
  if (bfd_get_flavour (info->output_bfd) != bfd_target_elf_flavour)
251
0
    return 0;
252
253
  /* Count the number of input BFDs and find the top input section id.
254
     Also search for an existing ".tramp" section so that we know
255
     where generated trampolines must go.  Default to ".text" if we
256
     can't find it.  */
257
0
  htab->tramp_section = 0;
258
0
  text_section = 0;
259
0
  for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
260
0
       input_bfd != NULL;
261
0
       input_bfd = input_bfd->link.next)
262
0
    {
263
0
      bfd_count += 1;
264
0
      for (section = input_bfd->sections;
265
0
     section != NULL;
266
0
     section = section->next)
267
0
  {
268
0
    const char *name = bfd_section_name (section);
269
270
0
    if (!strcmp (name, ".tramp"))
271
0
      htab->tramp_section = section;
272
273
0
    if (!strcmp (name, ".text"))
274
0
      text_section = section;
275
276
0
    if (top_id < section->id)
277
0
      top_id = section->id;
278
0
  }
279
0
    }
280
0
  htab->bfd_count = bfd_count;
281
0
  if (htab->tramp_section == 0)
282
0
    htab->tramp_section = text_section;
283
284
  /* We can't use output_bfd->section_count here to find the top output
285
     section index as some sections may have been removed, and
286
     strip_excluded_output_sections doesn't renumber the indices.  */
287
0
  for (section = output_bfd->sections, top_index = 0;
288
0
       section != NULL;
289
0
       section = section->next)
290
0
    {
291
0
      if (top_index < section->index)
292
0
  top_index = section->index;
293
0
    }
294
295
0
  htab->top_index = top_index;
296
0
  amt = sizeof (asection *) * (top_index + 1);
297
0
  input_list = (asection **) bfd_malloc (amt);
298
0
  htab->input_list = input_list;
299
0
  if (input_list == NULL)
300
0
    return -1;
301
302
  /* For sections we aren't interested in, mark their entries with a
303
     value we can check later.  */
304
0
  list = input_list + top_index;
305
0
  do
306
0
    *list = bfd_abs_section_ptr;
307
0
  while (list-- != input_list);
308
309
0
  for (section = output_bfd->sections;
310
0
       section != NULL;
311
0
       section = section->next)
312
0
    {
313
0
      if ((section->flags & SEC_CODE) != 0)
314
0
  input_list[section->index] = NULL;
315
0
    }
316
317
0
  return 1;
318
0
}
319
320
/* Determine and set the size of the stub section for a final link.
321
322
   The basic idea here is to examine all the relocations looking for
323
   PC-relative calls to a target that is unreachable with a "bl"
324
   instruction.  */
325
326
bool
327
elf32_m68hc11_size_stubs (bfd *output_bfd, bfd *stub_bfd,
328
        struct bfd_link_info *info,
329
        asection * (*add_stub_section) (const char*, asection*))
330
0
{
331
0
  bfd *input_bfd;
332
0
  asection *section;
333
0
  Elf_Internal_Sym *local_syms, **all_local_syms;
334
0
  unsigned int bfd_indx, bfd_count;
335
0
  size_t amt;
336
0
  asection *stub_sec;
337
0
  struct m68hc11_elf_link_hash_table *htab = m68hc11_elf_hash_table (info);
338
339
0
  if (htab == NULL)
340
0
    return false;
341
342
  /* Stash our params away.  */
343
0
  htab->stub_bfd = stub_bfd;
344
0
  htab->add_stub_section = add_stub_section;
345
346
  /* Count the number of input BFDs and find the top input section id.  */
347
0
  for (input_bfd = info->input_bfds, bfd_count = 0;
348
0
       input_bfd != NULL;
349
0
       input_bfd = input_bfd->link.next)
350
0
    bfd_count += 1;
351
352
  /* We want to read in symbol extension records only once.  To do this
353
     we need to read in the local symbols in parallel and save them for
354
     later use; so hold pointers to the local symbols in an array.  */
355
0
  amt = sizeof (Elf_Internal_Sym *) * bfd_count;
356
0
  all_local_syms = (Elf_Internal_Sym **) bfd_zmalloc (amt);
357
0
  if (all_local_syms == NULL)
358
0
    return false;
359
360
  /* Walk over all the input BFDs, swapping in local symbols.  */
361
0
  for (input_bfd = info->input_bfds, bfd_indx = 0;
362
0
       input_bfd != NULL;
363
0
       input_bfd = input_bfd->link.next, bfd_indx++)
364
0
    {
365
0
      Elf_Internal_Shdr *symtab_hdr;
366
367
      /* We'll need the symbol table in a second.  */
368
0
      symtab_hdr = &elf_symtab_hdr (input_bfd);
369
0
      if (symtab_hdr->sh_info == 0)
370
0
  continue;
371
372
      /* We need an array of the local symbols attached to the input bfd.  */
373
0
      local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
374
0
      if (local_syms == NULL)
375
0
  {
376
0
    local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
377
0
               symtab_hdr->sh_info, 0,
378
0
               NULL, NULL, NULL);
379
    /* Cache them for elf_link_input_bfd.  */
380
0
    symtab_hdr->contents = (unsigned char *) local_syms;
381
0
  }
382
0
      if (local_syms == NULL)
383
0
  {
384
0
    free (all_local_syms);
385
0
    return false;
386
0
  }
387
388
0
      all_local_syms[bfd_indx] = local_syms;
389
0
    }
390
391
0
  for (input_bfd = info->input_bfds, bfd_indx = 0;
392
0
       input_bfd != NULL;
393
0
       input_bfd = input_bfd->link.next, bfd_indx++)
394
0
    {
395
0
      Elf_Internal_Shdr *symtab_hdr;
396
0
      struct elf_link_hash_entry ** sym_hashes;
397
398
0
      sym_hashes = elf_sym_hashes (input_bfd);
399
400
      /* We'll need the symbol table in a second.  */
401
0
      symtab_hdr = &elf_symtab_hdr (input_bfd);
402
0
      if (symtab_hdr->sh_info == 0)
403
0
  continue;
404
405
0
      local_syms = all_local_syms[bfd_indx];
406
407
      /* Walk over each section attached to the input bfd.  */
408
0
      for (section = input_bfd->sections;
409
0
     section != NULL;
410
0
     section = section->next)
411
0
  {
412
0
    Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
413
414
    /* If there aren't any relocs, then there's nothing more
415
       to do.  */
416
0
    if ((section->flags & SEC_RELOC) == 0
417
0
        || section->reloc_count == 0)
418
0
      continue;
419
420
    /* If this section is a link-once section that will be
421
       discarded, then don't create any stubs.  */
422
0
    if (section->output_section == NULL
423
0
        || section->output_section->owner != output_bfd)
424
0
      continue;
425
426
    /* Get the relocs.  */
427
0
    internal_relocs
428
0
      = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
429
0
           info->keep_memory);
430
0
    if (internal_relocs == NULL)
431
0
      goto error_ret_free_local;
432
433
    /* Now examine each relocation.  */
434
0
    irela = internal_relocs;
435
0
    irelaend = irela + section->reloc_count;
436
0
    for (; irela < irelaend; irela++)
437
0
      {
438
0
        unsigned int r_type, r_indx;
439
0
        struct elf32_m68hc11_stub_hash_entry *stub_entry;
440
0
        asection *sym_sec;
441
0
        bfd_vma sym_value;
442
0
        struct elf_link_hash_entry *hash;
443
0
        const char *stub_name;
444
0
        Elf_Internal_Sym *sym;
445
446
0
        r_type = ELF32_R_TYPE (irela->r_info);
447
448
        /* Only look at 16-bit relocs.  */
449
0
        if (r_type != (unsigned int) R_M68HC11_16)
450
0
    continue;
451
452
        /* Now determine the call target, its name, value,
453
     section.  */
454
0
        r_indx = ELF32_R_SYM (irela->r_info);
455
0
        if (r_indx < symtab_hdr->sh_info)
456
0
    {
457
      /* It's a local symbol.  */
458
0
      Elf_Internal_Shdr *hdr;
459
0
      bool is_far;
460
461
0
      sym = local_syms + r_indx;
462
0
      is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
463
0
      if (!is_far)
464
0
        continue;
465
466
0
      if (sym->st_shndx >= elf_numsections (input_bfd))
467
0
        sym_sec = NULL;
468
0
      else
469
0
        {
470
0
          hdr = elf_elfsections (input_bfd)[sym->st_shndx];
471
0
          sym_sec = hdr->bfd_section;
472
0
        }
473
0
      stub_name = (bfd_elf_string_from_elf_section
474
0
             (input_bfd, symtab_hdr->sh_link,
475
0
        sym->st_name));
476
0
      sym_value = sym->st_value;
477
0
      hash = NULL;
478
0
    }
479
0
        else
480
0
    {
481
      /* It's an external symbol.  */
482
0
      int e_indx;
483
484
0
      e_indx = r_indx - symtab_hdr->sh_info;
485
0
      hash = (struct elf_link_hash_entry *)
486
0
        (sym_hashes[e_indx]);
487
488
0
      while (hash->root.type == bfd_link_hash_indirect
489
0
       || hash->root.type == bfd_link_hash_warning)
490
0
        hash = ((struct elf_link_hash_entry *)
491
0
          hash->root.u.i.link);
492
493
0
      if (hash->root.type == bfd_link_hash_defined
494
0
          || hash->root.type == bfd_link_hash_defweak
495
0
          || hash->root.type == bfd_link_hash_new)
496
0
        {
497
0
          if (!(hash->other & STO_M68HC12_FAR))
498
0
      continue;
499
0
        }
500
0
      else if (hash->root.type == bfd_link_hash_undefweak)
501
0
        {
502
0
          continue;
503
0
        }
504
0
      else if (hash->root.type == bfd_link_hash_undefined)
505
0
        {
506
0
          continue;
507
0
        }
508
0
      else
509
0
        {
510
0
          bfd_set_error (bfd_error_bad_value);
511
0
          goto error_ret_free_internal;
512
0
        }
513
0
      sym_sec = hash->root.u.def.section;
514
0
      sym_value = hash->root.u.def.value;
515
0
      stub_name = hash->root.root.string;
516
0
    }
517
518
0
        if (!stub_name)
519
0
    goto error_ret_free_internal;
520
521
0
        stub_entry = m68hc12_stub_hash_lookup
522
0
    (htab->stub_hash_table,
523
0
     stub_name,
524
0
     false, false);
525
0
        if (stub_entry == NULL)
526
0
    {
527
0
      if (add_stub_section == 0)
528
0
        continue;
529
530
0
      stub_entry = m68hc12_add_stub (stub_name, section, htab);
531
0
      if (stub_entry == NULL)
532
0
        {
533
0
        error_ret_free_internal:
534
0
          if (elf_section_data (section)->relocs == NULL)
535
0
      free (internal_relocs);
536
0
          goto error_ret_free_local;
537
0
        }
538
0
    }
539
540
0
        stub_entry->target_value = sym_value;
541
0
        stub_entry->target_section = sym_sec;
542
0
      }
543
544
    /* We're done with the internal relocs, free them.  */
545
0
    if (elf_section_data (section)->relocs == NULL)
546
0
      free (internal_relocs);
547
0
  }
548
0
    }
549
550
0
  if (add_stub_section)
551
0
    {
552
      /* OK, we've added some stubs.  Find out the new size of the
553
   stub sections.  */
554
0
      for (stub_sec = htab->stub_bfd->sections;
555
0
     stub_sec != NULL;
556
0
     stub_sec = stub_sec->next)
557
0
  {
558
0
    stub_sec->size = 0;
559
0
  }
560
561
0
      bfd_hash_traverse (htab->stub_hash_table, htab->size_one_stub, htab);
562
0
    }
563
0
  free (all_local_syms);
564
0
  return true;
565
566
0
 error_ret_free_local:
567
0
  free (all_local_syms);
568
0
  return false;
569
0
}
570
571
/* Export the trampoline addresses in the symbol table.  */
572
static bool
573
m68hc11_elf_export_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
574
0
{
575
0
  struct bfd_link_info *info;
576
0
  struct m68hc11_elf_link_hash_table *htab;
577
0
  struct elf32_m68hc11_stub_hash_entry *stub_entry;
578
0
  char* name;
579
0
  bool result;
580
581
0
  info = (struct bfd_link_info *) in_arg;
582
0
  htab = m68hc11_elf_hash_table (info);
583
0
  if (htab == NULL)
584
0
    return false;
585
586
  /* Massage our args to the form they really have.  */
587
0
  stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
588
589
  /* Generate the trampoline according to HC11 or HC12.  */
590
0
  result = (* htab->build_one_stub) (gen_entry, in_arg);
591
592
  /* Make a printable name that does not conflict with the real function.  */
593
0
  name = concat ("tramp.", stub_entry->root.string, NULL);
594
595
  /* Export the symbol for debugging/disassembling.  */
596
0
  m68hc11_elf_set_symbol (htab->stub_bfd, info, name,
597
0
        stub_entry->stub_offset,
598
0
        stub_entry->stub_sec);
599
0
  free (name);
600
0
  return result;
601
0
}
602
603
/* Export a symbol or set its value and section.  */
604
static void
605
m68hc11_elf_set_symbol (bfd *abfd, struct bfd_link_info *info,
606
      const char *name, bfd_vma value, asection *sec)
607
0
{
608
0
  struct elf_link_hash_entry *h;
609
610
0
  h = (struct elf_link_hash_entry *)
611
0
    bfd_link_hash_lookup (info->hash, name, false, false, false);
612
0
  if (h == NULL)
613
0
    {
614
0
      _bfd_generic_link_add_one_symbol (info, abfd,
615
0
          name,
616
0
          BSF_GLOBAL,
617
0
          sec,
618
0
          value,
619
0
          (const char*) NULL,
620
0
          true, false, NULL);
621
0
    }
622
0
  else
623
0
    {
624
0
      h->root.type = bfd_link_hash_defined;
625
0
      h->root.u.def.value = value;
626
0
      h->root.u.def.section = sec;
627
0
    }
628
0
}
629
630
631
/* Build all the stubs associated with the current output file.  The
632
   stubs are kept in a hash table attached to the main linker hash
633
   table.  This function is called via m68hc12elf_finish in the
634
   linker.  */
635
636
bool
637
elf32_m68hc11_build_stubs (bfd *abfd, struct bfd_link_info *info)
638
0
{
639
0
  asection *stub_sec;
640
0
  struct bfd_hash_table *table;
641
0
  struct m68hc11_elf_link_hash_table *htab;
642
0
  struct m68hc11_scan_param param;
643
644
0
  m68hc11_elf_get_bank_parameters (info);
645
0
  htab = m68hc11_elf_hash_table (info);
646
0
  if (htab == NULL)
647
0
    return false;
648
649
0
  for (stub_sec = htab->stub_bfd->sections;
650
0
       stub_sec != NULL;
651
0
       stub_sec = stub_sec->next)
652
0
    {
653
0
      bfd_size_type size;
654
655
      /* Allocate memory to hold the linker stubs.  */
656
0
      size = stub_sec->size;
657
0
      stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
658
0
      if (stub_sec->contents == NULL && size != 0)
659
0
  return false;
660
0
      stub_sec->alloced = 1;
661
0
      stub_sec->size = 0;
662
0
    }
663
664
  /* Build the stubs as directed by the stub hash table.  */
665
0
  table = htab->stub_hash_table;
666
0
  bfd_hash_traverse (table, m68hc11_elf_export_one_stub, info);
667
668
  /* Scan the output sections to see if we use the memory banks.
669
     If so, export the symbols that define how the memory banks
670
     are mapped.  This is used by gdb and the simulator to obtain
671
     the information.  It can be used by programs to burn the eprom
672
     at the good addresses.  */
673
0
  param.use_memory_banks = false;
674
0
  param.pinfo = &htab->pinfo;
675
0
  bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
676
0
  if (param.use_memory_banks)
677
0
    {
678
0
      m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_START_NAME,
679
0
            htab->pinfo.bank_physical,
680
0
            bfd_abs_section_ptr);
681
0
      m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_VIRTUAL_NAME,
682
0
            htab->pinfo.bank_virtual,
683
0
            bfd_abs_section_ptr);
684
0
      m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_SIZE_NAME,
685
0
            htab->pinfo.bank_size,
686
0
            bfd_abs_section_ptr);
687
0
    }
688
689
0
  return true;
690
0
}
691
692
void
693
m68hc11_elf_get_bank_parameters (struct bfd_link_info *info)
694
0
{
695
0
  unsigned i;
696
0
  struct m68hc11_page_info *pinfo;
697
0
  struct bfd_link_hash_entry *h;
698
0
  struct m68hc11_elf_link_hash_table *htab;
699
700
0
  htab = m68hc11_elf_hash_table (info);
701
0
  if (htab == NULL)
702
0
    return;
703
704
0
  pinfo = & htab->pinfo;
705
0
  if (pinfo->bank_param_initialized)
706
0
    return;
707
708
0
  pinfo->bank_virtual = M68HC12_BANK_VIRT;
709
0
  pinfo->bank_mask = M68HC12_BANK_MASK;
710
0
  pinfo->bank_physical = M68HC12_BANK_BASE;
711
0
  pinfo->bank_shift = M68HC12_BANK_SHIFT;
712
0
  pinfo->bank_size = 1 << M68HC12_BANK_SHIFT;
713
714
0
  h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_START_NAME,
715
0
          false, false, true);
716
0
  if (h != (struct bfd_link_hash_entry*) NULL
717
0
      && h->type == bfd_link_hash_defined)
718
0
    pinfo->bank_physical = (h->u.def.value
719
0
          + h->u.def.section->output_section->vma
720
0
          + h->u.def.section->output_offset);
721
722
0
  h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_VIRTUAL_NAME,
723
0
          false, false, true);
724
0
  if (h != (struct bfd_link_hash_entry*) NULL
725
0
      && h->type == bfd_link_hash_defined)
726
0
    pinfo->bank_virtual = (h->u.def.value
727
0
         + h->u.def.section->output_section->vma
728
0
         + h->u.def.section->output_offset);
729
730
0
  h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_SIZE_NAME,
731
0
          false, false, true);
732
0
  if (h != (struct bfd_link_hash_entry*) NULL
733
0
      && h->type == bfd_link_hash_defined)
734
0
    pinfo->bank_size = (h->u.def.value
735
0
      + h->u.def.section->output_section->vma
736
0
      + h->u.def.section->output_offset);
737
738
0
  pinfo->bank_shift = 0;
739
0
  for (i = pinfo->bank_size; i != 0; i >>= 1)
740
0
    pinfo->bank_shift++;
741
0
  pinfo->bank_shift--;
742
0
  pinfo->bank_mask = (1 << pinfo->bank_shift) - 1;
743
0
  pinfo->bank_physical_end = pinfo->bank_physical + pinfo->bank_size;
744
0
  pinfo->bank_param_initialized = 1;
745
746
0
  h = bfd_link_hash_lookup (info->hash, "__far_trampoline", false,
747
0
          false, true);
748
0
  if (h != (struct bfd_link_hash_entry*) NULL
749
0
      && h->type == bfd_link_hash_defined)
750
0
    pinfo->trampoline_addr = (h->u.def.value
751
0
            + h->u.def.section->output_section->vma
752
0
            + h->u.def.section->output_offset);
753
0
}
754
755
/* Return 1 if the address is in banked memory.
756
   This can be applied to a virtual address and to a physical address.  */
757
int
758
m68hc11_addr_is_banked (struct m68hc11_page_info *pinfo, bfd_vma addr)
759
0
{
760
0
  if (addr >= pinfo->bank_virtual)
761
0
    return 1;
762
763
0
  if (addr >= pinfo->bank_physical && addr <= pinfo->bank_physical_end)
764
0
    return 1;
765
766
0
  return 0;
767
0
}
768
769
/* Return the physical address seen by the processor, taking
770
   into account banked memory.  */
771
bfd_vma
772
m68hc11_phys_addr (struct m68hc11_page_info *pinfo, bfd_vma addr)
773
0
{
774
0
  if (addr < pinfo->bank_virtual)
775
0
    return addr;
776
777
  /* Map the address to the memory bank.  */
778
0
  addr -= pinfo->bank_virtual;
779
0
  addr &= pinfo->bank_mask;
780
0
  addr += pinfo->bank_physical;
781
0
  return addr;
782
0
}
783
784
/* Return the page number corresponding to an address in banked memory.  */
785
bfd_vma
786
m68hc11_phys_page (struct m68hc11_page_info *pinfo, bfd_vma addr)
787
0
{
788
0
  if (addr < pinfo->bank_virtual)
789
0
    return 0;
790
791
  /* Map the address to the memory bank.  */
792
0
  addr -= pinfo->bank_virtual;
793
0
  addr >>= pinfo->bank_shift;
794
0
  addr &= 0x0ff;
795
0
  return addr;
796
0
}
797
798
/* This function is used for relocs which are only used for relaxing,
799
   which the linker should otherwise ignore.  */
800
801
bfd_reloc_status_type
802
m68hc11_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED,
803
        arelent *reloc_entry,
804
        asymbol *symbol ATTRIBUTE_UNUSED,
805
        void *data ATTRIBUTE_UNUSED,
806
        asection *input_section,
807
        bfd *output_bfd,
808
        char **error_message ATTRIBUTE_UNUSED)
809
0
{
810
0
  if (output_bfd != NULL)
811
0
    reloc_entry->address += input_section->output_offset;
812
0
  return bfd_reloc_ok;
813
0
}
814
815
bfd_reloc_status_type
816
m68hc11_elf_special_reloc (bfd *abfd,
817
         arelent *reloc_entry,
818
         asymbol *symbol,
819
         void *data ATTRIBUTE_UNUSED,
820
         asection *input_section,
821
         bfd *output_bfd,
822
         char **error_message ATTRIBUTE_UNUSED)
823
0
{
824
0
  if (output_bfd != NULL
825
0
      && (symbol->flags & BSF_SECTION_SYM) == 0
826
0
      && (! reloc_entry->howto->partial_inplace
827
0
    || reloc_entry->addend == 0))
828
0
    {
829
0
      reloc_entry->address += input_section->output_offset;
830
0
      return bfd_reloc_ok;
831
0
    }
832
833
0
  if (output_bfd != NULL)
834
0
    return bfd_reloc_continue;
835
836
0
  if (!bfd_reloc_offset_in_range (reloc_entry->howto, abfd,
837
0
          input_section, reloc_entry->address))
838
0
    return bfd_reloc_outofrange;
839
840
0
  return bfd_reloc_notsupported;
841
0
}
842
843
/* Look through the relocs for a section during the first phase.
844
   Since we don't do .gots or .plts, we just need to consider the
845
   virtual table relocs for gc.  */
846
847
bool
848
elf32_m68hc11_check_relocs (bfd *abfd, struct bfd_link_info *info,
849
          asection *sec, const Elf_Internal_Rela *relocs)
850
0
{
851
0
  Elf_Internal_Shdr *   symtab_hdr;
852
0
  struct elf_link_hash_entry ** sym_hashes;
853
0
  const Elf_Internal_Rela * rel;
854
0
  const Elf_Internal_Rela * rel_end;
855
856
0
  if (bfd_link_relocatable (info))
857
0
    return true;
858
859
0
  symtab_hdr = &elf_symtab_hdr (abfd);
860
0
  sym_hashes = elf_sym_hashes (abfd);
861
0
  rel_end = relocs + sec->reloc_count;
862
863
0
  for (rel = relocs; rel < rel_end; rel++)
864
0
    {
865
0
      struct elf_link_hash_entry * h;
866
0
      unsigned long r_symndx;
867
868
0
      r_symndx = ELF32_R_SYM (rel->r_info);
869
870
0
      if (r_symndx < symtab_hdr->sh_info)
871
0
  h = NULL;
872
0
      else
873
0
  {
874
0
    h = sym_hashes [r_symndx - symtab_hdr->sh_info];
875
0
    while (h->root.type == bfd_link_hash_indirect
876
0
     || h->root.type == bfd_link_hash_warning)
877
0
      h = (struct elf_link_hash_entry *) h->root.u.i.link;
878
0
  }
879
880
0
      switch (ELF32_R_TYPE (rel->r_info))
881
0
  {
882
  /* This relocation describes the C++ object vtable hierarchy.
883
     Reconstruct it for later use during GC.  */
884
0
  case R_M68HC11_GNU_VTINHERIT:
885
0
    if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
886
0
      return false;
887
0
    break;
888
889
  /* This relocation describes which C++ vtable entries are actually
890
     used.  Record for later use during GC.  */
891
0
  case R_M68HC11_GNU_VTENTRY:
892
0
    if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
893
0
      return false;
894
0
    break;
895
0
  }
896
0
    }
897
898
0
  return true;
899
0
}
900
901
static bool ATTRIBUTE_PRINTF (6, 7)
902
reloc_warning (struct bfd_link_info *info, const char *name, bfd *input_bfd,
903
         asection *input_section, const Elf_Internal_Rela *rel,
904
         const char *fmt, ...)
905
0
{
906
0
  va_list ap;
907
0
  char *buf;
908
0
  int ret;
909
910
0
  va_start (ap, fmt);
911
0
  ret = vasprintf (&buf, fmt, ap);
912
0
  va_end (ap);
913
0
  if (ret < 0)
914
0
    {
915
0
      bfd_set_error (bfd_error_no_memory);
916
0
      return false;
917
0
    }
918
0
  info->callbacks->warning (info, buf, name, input_bfd, input_section,
919
0
          rel->r_offset);
920
0
  free (buf);
921
0
  return true;
922
0
}
923
924
/* Relocate a 68hc11/68hc12 ELF section.  */
925
int
926
elf32_m68hc11_relocate_section (struct bfd_link_info *info,
927
        bfd *input_bfd, asection *input_section,
928
        bfd_byte *contents, Elf_Internal_Rela *relocs,
929
        Elf_Internal_Sym *local_syms,
930
        asection **local_sections)
931
0
{
932
0
  Elf_Internal_Shdr *symtab_hdr;
933
0
  struct elf_link_hash_entry **sym_hashes;
934
0
  Elf_Internal_Rela *rel, *relend;
935
0
  const char *name = NULL;
936
0
  struct m68hc11_page_info *pinfo;
937
0
  elf_backend_data * const ebd = get_elf_backend_data (input_bfd);
938
0
  struct m68hc11_elf_link_hash_table *htab;
939
0
  unsigned long e_flags;
940
941
0
  symtab_hdr = &elf_symtab_hdr (input_bfd);
942
0
  sym_hashes = elf_sym_hashes (input_bfd);
943
0
  e_flags = elf_elfheader (input_bfd)->e_flags;
944
945
0
  htab = m68hc11_elf_hash_table (info);
946
0
  if (htab == NULL)
947
0
    return false;
948
949
  /* Get memory bank parameters.  */
950
0
  m68hc11_elf_get_bank_parameters (info);
951
952
0
  pinfo = & htab->pinfo;
953
0
  rel = relocs;
954
0
  relend = relocs + input_section->reloc_count;
955
956
0
  for (; rel < relend; rel++)
957
0
    {
958
0
      int r_type;
959
0
      arelent arel;
960
0
      reloc_howto_type *howto;
961
0
      unsigned long r_symndx;
962
0
      const Elf_Internal_Sym *sym;
963
0
      asection *sec;
964
0
      bfd_vma relocation = 0;
965
0
      bfd_reloc_status_type r = bfd_reloc_undefined;
966
0
      bfd_vma phys_page;
967
0
      bfd_vma phys_addr;
968
0
      bfd_vma insn_addr;
969
0
      bfd_vma insn_page;
970
0
      bool is_far = false;
971
0
      bool is_xgate_symbol = false;
972
0
      bool is_section_symbol = false;
973
0
      struct elf_link_hash_entry *h;
974
0
      bfd_vma val;
975
0
      const char *msg;
976
977
0
      r_symndx = ELF32_R_SYM (rel->r_info);
978
0
      r_type = ELF32_R_TYPE (rel->r_info);
979
980
0
      if (r_type == R_M68HC11_GNU_VTENTRY
981
0
    || r_type == R_M68HC11_GNU_VTINHERIT)
982
0
  continue;
983
984
0
      if (! (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel))
985
0
  continue;
986
0
      howto = arel.howto;
987
988
0
      h = NULL;
989
0
      sym = NULL;
990
0
      sec = NULL;
991
0
      if (r_symndx < symtab_hdr->sh_info)
992
0
  {
993
0
    sym = local_syms + r_symndx;
994
0
    sec = local_sections[r_symndx];
995
0
    relocation = (sec->output_section->vma
996
0
      + sec->output_offset
997
0
      + sym->st_value);
998
0
    is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
999
0
    is_xgate_symbol = (sym && (sym->st_target_internal));
1000
0
    is_section_symbol = ELF_ST_TYPE (sym->st_info) & STT_SECTION;
1001
0
  }
1002
0
      else
1003
0
  {
1004
0
    bool unresolved_reloc, warned, ignored;
1005
1006
0
    RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
1007
0
           r_symndx, symtab_hdr, sym_hashes,
1008
0
           h, sec, relocation, unresolved_reloc,
1009
0
           warned, ignored);
1010
1011
0
    is_far = (h && (h->other & STO_M68HC12_FAR));
1012
0
    is_xgate_symbol = (h && (h->target_internal));
1013
0
  }
1014
1015
0
      if (sec != NULL && discarded_section (sec))
1016
0
  RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
1017
0
           rel, 1, relend, R_M68HC11_NONE,
1018
0
           howto, 0, contents);
1019
1020
0
      if (bfd_link_relocatable (info))
1021
0
  {
1022
    /* This is a relocatable link.  We don't have to change
1023
       anything, unless the reloc is against a section symbol,
1024
       in which case we have to adjust according to where the
1025
       section symbol winds up in the output section.  */
1026
0
    if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1027
0
      rel->r_addend += sec->output_offset;
1028
0
    continue;
1029
0
  }
1030
1031
0
      if (h != NULL)
1032
0
  name = h->root.root.string;
1033
0
      else
1034
0
  {
1035
0
    name = (bfd_elf_string_from_elf_section
1036
0
      (input_bfd, symtab_hdr->sh_link, sym->st_name));
1037
0
    if (name == NULL || *name == '\0')
1038
0
      name = bfd_section_name (sec);
1039
0
  }
1040
1041
0
      if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
1042
0
  {
1043
0
    struct elf32_m68hc11_stub_hash_entry* stub;
1044
1045
0
    stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
1046
0
             name, false, false);
1047
0
    if (stub)
1048
0
      {
1049
0
        relocation = stub->stub_offset
1050
0
    + stub->stub_sec->output_section->vma
1051
0
    + stub->stub_sec->output_offset;
1052
0
        is_far = false;
1053
0
      }
1054
0
  }
1055
1056
      /* Do the memory bank mapping.  */
1057
0
      phys_addr = m68hc11_phys_addr (pinfo, relocation + rel->r_addend);
1058
0
      phys_page = m68hc11_phys_page (pinfo, relocation + rel->r_addend);
1059
0
      switch (r_type)
1060
0
  {
1061
0
  case R_M68HC12_LO8XG:
1062
    /* This relocation is specific to XGATE IMM16 calls and will precede
1063
       a HI8. tc-m68hc11 only generates them in pairs.
1064
       Leave the relocation to the HI8XG step.  */
1065
0
    r = bfd_reloc_ok;
1066
0
    r_type = R_M68HC11_NONE;
1067
0
    break;
1068
1069
0
  case R_M68HC12_HI8XG:
1070
    /* This relocation is specific to XGATE IMM16 calls and must follow
1071
       a LO8XG. Does not actually check that it was a LO8XG.
1072
       Adjusts high and low bytes.  */
1073
0
    relocation = phys_addr;
1074
0
    if ((e_flags & E_M68HC11_XGATE_RAMOFFSET)
1075
0
        && (relocation >= 0x2000))
1076
0
      relocation += 0xc000; /* HARDCODED RAM offset for XGATE.  */
1077
1078
    /* Fetch 16 bit value including low byte in previous insn.  */
1079
0
    val = (bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset) << 8)
1080
0
      | bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset - 2);
1081
1082
    /* Add on value to preserve carry, then write zero to high byte.  */
1083
0
    relocation += val;
1084
1085
    /* Write out top byte.  */
1086
0
    bfd_put_8 (input_bfd, (relocation >> 8) & 0xff,
1087
0
         (bfd_byte*) contents + rel->r_offset);
1088
1089
    /* Write out low byte to previous instruction.  */
1090
0
    bfd_put_8 (input_bfd, relocation & 0xff,
1091
0
         (bfd_byte*) contents + rel->r_offset - 2);
1092
1093
    /* Mark as relocation completed.  */
1094
0
    r = bfd_reloc_ok;
1095
0
    r_type = R_M68HC11_NONE;
1096
0
    break;
1097
1098
  /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1099
     assembler directives. %hi does not support carry.  */
1100
0
  case R_M68HC11_HI8:
1101
0
  case R_M68HC11_LO8:
1102
0
    relocation = phys_addr;
1103
0
    break;
1104
1105
0
  case R_M68HC11_24:
1106
    /* Reloc used by 68HC12 call instruction.  */
1107
0
    bfd_put_16 (input_bfd, phys_addr,
1108
0
          (bfd_byte*) contents + rel->r_offset);
1109
0
    bfd_put_8 (input_bfd, phys_page,
1110
0
         (bfd_byte*) contents + rel->r_offset + 2);
1111
0
    r = bfd_reloc_ok;
1112
0
    r_type = R_M68HC11_NONE;
1113
0
    break;
1114
1115
0
  case R_M68HC11_NONE:
1116
0
    r = bfd_reloc_ok;
1117
0
    break;
1118
1119
0
  case R_M68HC11_LO16:
1120
    /* Reloc generated by %addr(expr) gas to obtain the
1121
       address as mapped in the memory bank window.  */
1122
0
    relocation = phys_addr;
1123
0
    break;
1124
1125
0
  case R_M68HC11_PAGE:
1126
    /* Reloc generated by %page(expr) gas to obtain the
1127
       page number associated with the address.  */
1128
0
    relocation = phys_page;
1129
0
    break;
1130
1131
0
  case R_M68HC11_16:
1132
0
    if (is_far)
1133
0
      {
1134
0
        if (!reloc_warning (info, name, input_bfd, input_section, rel,
1135
0
          _("reference to the far symbol `%s' using a "
1136
0
            "wrong relocation may result in incorrect "
1137
0
            "execution"), name))
1138
0
    return false;
1139
0
      }
1140
1141
    /* Get virtual address of instruction having the relocation.  */
1142
0
    insn_addr = input_section->output_section->vma
1143
0
      + input_section->output_offset
1144
0
      + rel->r_offset;
1145
1146
0
    insn_page = m68hc11_phys_page (pinfo, insn_addr);
1147
1148
   /* If we are linking an S12 instruction against an XGATE symbol, we
1149
      need to change the offset of the symbol value so that it's correct
1150
      from the S12's perspective.  */
1151
0
    if (is_xgate_symbol)
1152
0
      {
1153
        /* The ram in the global space is mapped to 0x2000 in the 16-bit
1154
     address space for S12 and 0xE000 in the 16-bit address space
1155
     for XGATE.  */
1156
0
        if (relocation >= 0xE000)
1157
0
    {
1158
      /* We offset the address by the difference
1159
         between these two mappings.  */
1160
0
      relocation -= 0xC000;
1161
0
      break;
1162
0
    }
1163
0
        else
1164
0
    {
1165
0
      if (!reloc_warning (info, name, input_bfd, input_section, rel,
1166
0
              _("XGATE address (%lx) is not within "
1167
0
          "shared RAM(0xE000-0xFFFF), therefore "
1168
0
          "you must manually offset the address, "
1169
0
          "and possibly manage the page, in your "
1170
0
          "code."), (long) phys_addr))
1171
0
        return false;
1172
0
      break;
1173
0
    }
1174
0
      }
1175
1176
0
    if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
1177
0
        && m68hc11_addr_is_banked (pinfo, insn_addr)
1178
0
        && phys_page != insn_page
1179
0
        && !(e_flags & E_M68HC11_NO_BANK_WARNING))
1180
0
      {
1181
0
        if (!reloc_warning (info, name, input_bfd, input_section, rel,
1182
0
          _("banked address [%lx:%04lx] (%lx) is not "
1183
0
            "in the same bank as current banked "
1184
0
            "address [%lx:%04lx] (%lx)"),
1185
0
          (long) phys_page, (long) phys_addr,
1186
0
          (long) (relocation + rel->r_addend),
1187
0
          (long) insn_page,
1188
0
          (long) m68hc11_phys_addr (pinfo, insn_addr),
1189
0
          (long) insn_addr))
1190
0
    return false;
1191
0
        break;
1192
0
      }
1193
1194
0
    if (phys_page != 0 && insn_page == 0)
1195
0
      {
1196
0
        if (!reloc_warning (info, name, input_bfd, input_section, rel,
1197
0
          _("reference to a banked address [%lx:%04lx] "
1198
0
            "in the normal address space at %04lx"),
1199
0
          (long) phys_page, (long) phys_addr,
1200
0
          (long) insn_addr))
1201
0
    return false;
1202
0
        relocation = phys_addr;
1203
0
        break;
1204
0
      }
1205
1206
    /* If this is a banked address use the phys_addr so that
1207
       we stay in the banked window.  */
1208
0
    if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend))
1209
0
      relocation = phys_addr;
1210
0
    break;
1211
0
  }
1212
1213
      /* If we are linking an XGATE instruction against an S12 symbol, we
1214
   need to change the offset of the symbol value so that it's correct
1215
   from the XGATE's perspective.  */
1216
0
      if (!strcmp (howto->name, "R_XGATE_IMM8_LO")
1217
0
    || !strcmp (howto->name, "R_XGATE_IMM8_HI"))
1218
0
  {
1219
    /* We can only offset S12 addresses that lie within the non-paged
1220
       area of RAM.  */
1221
0
    if (!is_xgate_symbol && !is_section_symbol)
1222
0
      {
1223
        /* The ram in the global space is mapped to 0x2000 and stops at
1224
     0x4000 in the 16-bit address space for S12 and 0xE000 in the
1225
     16-bit address space for XGATE.  */
1226
0
        if (relocation >= 0x2000 && relocation < 0x4000)
1227
     /* We offset the address by the difference
1228
       between these two mappings.  */
1229
0
    relocation += 0xC000;
1230
0
        else
1231
0
    {
1232
0
      if (!reloc_warning (info, name, input_bfd, input_section, rel,
1233
0
              _("S12 address (%lx) is not within "
1234
0
          "shared RAM(0x2000-0x4000), therefore "
1235
0
          "you must manually offset the address "
1236
0
          "in your code"), (long) phys_addr))
1237
0
        return false;
1238
0
      break;
1239
0
    }
1240
0
      }
1241
0
  }
1242
1243
0
      if (r_type != R_M68HC11_NONE)
1244
0
  {
1245
0
    if ((r_type == R_M68HC12_PCREL_9) || (r_type == R_M68HC12_PCREL_10))
1246
0
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1247
0
              contents, rel->r_offset,
1248
0
              relocation - 2, rel->r_addend);
1249
0
    else
1250
0
      r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1251
0
            contents, rel->r_offset,
1252
0
            relocation, rel->r_addend);
1253
0
  }
1254
1255
0
      if (r != bfd_reloc_ok)
1256
0
  {
1257
0
    switch (r)
1258
0
      {
1259
0
      case bfd_reloc_overflow:
1260
0
        (*info->callbacks->reloc_overflow)
1261
0
    (info, NULL, name, howto->name, (bfd_vma) 0,
1262
0
     input_bfd, input_section, rel->r_offset);
1263
0
        break;
1264
1265
0
      case bfd_reloc_undefined:
1266
0
        (*info->callbacks->undefined_symbol)
1267
0
    (info, name, input_bfd, input_section, rel->r_offset, true);
1268
0
        break;
1269
1270
0
      case bfd_reloc_outofrange:
1271
0
        msg = _ ("internal error: out of range error");
1272
0
        goto common_error;
1273
1274
0
      case bfd_reloc_notsupported:
1275
0
        msg = _ ("internal error: unsupported relocation error");
1276
0
        goto common_error;
1277
1278
0
      case bfd_reloc_dangerous:
1279
0
        msg = _ ("internal error: dangerous error");
1280
0
        goto common_error;
1281
1282
0
      default:
1283
0
        msg = _ ("internal error: unknown error");
1284
        /* fall through */
1285
1286
0
      common_error:
1287
0
        (*info->callbacks->warning) (info, msg, name, input_bfd,
1288
0
             input_section, rel->r_offset);
1289
0
        break;
1290
0
      }
1291
0
  }
1292
0
    }
1293
1294
0
  return true;
1295
0
}
1296
1297
1298

1299
/* Set and control ELF flags in ELF header.  */
1300
1301
bool
1302
_bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
1303
0
{
1304
0
  BFD_ASSERT (!elf_flags_init (abfd)
1305
0
        || elf_elfheader (abfd)->e_flags == flags);
1306
1307
0
  elf_elfheader (abfd)->e_flags = flags;
1308
0
  elf_flags_init (abfd) = true;
1309
0
  return true;
1310
0
}
1311
1312
/* Merge backend specific data from an object file to the output
1313
   object file when linking.  */
1314
1315
bool
1316
_bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
1317
0
{
1318
0
  bfd *obfd = info->output_bfd;
1319
0
  flagword old_flags;
1320
0
  flagword new_flags;
1321
0
  bool ok = true;
1322
1323
  /* Check if we have the same endianness */
1324
0
  if (!_bfd_generic_verify_endian_match (ibfd, info))
1325
0
    return false;
1326
1327
0
  if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
1328
0
    return true;
1329
1330
0
  new_flags = elf_elfheader (ibfd)->e_flags;
1331
0
  elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
1332
0
  old_flags = elf_elfheader (obfd)->e_flags;
1333
1334
0
  if (! elf_flags_init (obfd))
1335
0
    {
1336
0
      elf_flags_init (obfd) = true;
1337
0
      elf_elfheader (obfd)->e_flags = new_flags;
1338
0
      elf_elfheader (obfd)->e_ident[EI_CLASS]
1339
0
  = elf_elfheader (ibfd)->e_ident[EI_CLASS];
1340
1341
0
      if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1342
0
    && bfd_get_arch_info (obfd)->the_default)
1343
0
  {
1344
0
    if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1345
0
           bfd_get_mach (ibfd)))
1346
0
      return false;
1347
0
  }
1348
1349
0
      return true;
1350
0
    }
1351
1352
  /* Check ABI compatibility.  */
1353
0
  if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
1354
0
    {
1355
0
      _bfd_error_handler
1356
0
  (_("%pB: linking files compiled for 16-bit integers (-mshort) "
1357
0
     "and others for 32-bit integers"), ibfd);
1358
0
      ok = false;
1359
0
    }
1360
0
  if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
1361
0
    {
1362
0
      _bfd_error_handler
1363
0
  (_("%pB: linking files compiled for 32-bit double (-fshort-double) "
1364
0
     "and others for 64-bit double"), ibfd);
1365
0
      ok = false;
1366
0
    }
1367
1368
  /* Processor compatibility.  */
1369
0
  if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
1370
0
    {
1371
0
      _bfd_error_handler
1372
0
  (_("%pB: linking files compiled for HCS12 with "
1373
0
     "others compiled for HC12"), ibfd);
1374
0
      ok = false;
1375
0
    }
1376
0
  new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
1377
0
         | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
1378
1379
0
  elf_elfheader (obfd)->e_flags = new_flags;
1380
1381
0
  new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1382
0
  old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1383
1384
  /* Warn about any other mismatches */
1385
0
  if (new_flags != old_flags)
1386
0
    {
1387
0
      _bfd_error_handler
1388
  /* xgettext:c-format */
1389
0
  (_("%pB: uses different e_flags (%#x) fields than previous modules (%#x)"),
1390
0
   ibfd, new_flags, old_flags);
1391
0
      ok = false;
1392
0
    }
1393
1394
0
  if (! ok)
1395
0
    {
1396
0
      bfd_set_error (bfd_error_bad_value);
1397
0
      return false;
1398
0
    }
1399
1400
0
  return true;
1401
0
}
1402
1403
bool
1404
_bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
1405
120
{
1406
120
  FILE *file = (FILE *) ptr;
1407
1408
120
  BFD_ASSERT (abfd != NULL && ptr != NULL);
1409
1410
  /* Print normal ELF private data.  */
1411
120
  _bfd_elf_print_private_bfd_data (abfd, ptr);
1412
1413
  /* xgettext:c-format */
1414
120
  fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1415
1416
120
  if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
1417
41
    fprintf (file, _("[abi=32-bit int, "));
1418
79
  else
1419
79
    fprintf (file, _("[abi=16-bit int, "));
1420
1421
120
  if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
1422
4
    fprintf (file, _("64-bit double, "));
1423
116
  else
1424
116
    fprintf (file, _("32-bit double, "));
1425
1426
120
  if (strcmp (bfd_get_target (abfd), "elf32-m68hc11") == 0)
1427
18
    fprintf (file, _("cpu=HC11]"));
1428
102
  else if (elf_elfheader (abfd)->e_flags & EF_M68HCS12_MACH)
1429
1
    fprintf (file, _("cpu=HCS12]"));
1430
101
  else
1431
101
    fprintf (file, _("cpu=HC12]"));
1432
1433
120
  if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
1434
4
    fprintf (file, _(" [memory=bank-model]"));
1435
116
  else
1436
116
    fprintf (file, _(" [memory=flat]"));
1437
1438
120
  if (elf_elfheader (abfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
1439
72
    fprintf (file, _(" [XGATE RAM offsetting]"));
1440
1441
120
  fputc ('\n', file);
1442
1443
120
  return true;
1444
120
}
1445
1446
static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
1447
           asection *asect, void *arg)
1448
0
{
1449
0
  struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
1450
1451
0
  if (asect->vma >= p->pinfo->bank_virtual)
1452
0
    p->use_memory_banks = true;
1453
0
}
1454
1455
/* Tweak the OSABI field of the elf header.  */
1456
1457
bool
1458
elf32_m68hc11_init_file_header (bfd *abfd, struct bfd_link_info *link_info)
1459
2
{
1460
2
  struct m68hc11_scan_param param;
1461
2
  struct m68hc11_elf_link_hash_table *htab;
1462
1463
2
  if (!_bfd_elf_init_file_header (abfd, link_info))
1464
0
    return false;
1465
1466
2
  if (link_info == NULL)
1467
2
    return true;
1468
1469
0
  htab = m68hc11_elf_hash_table (link_info);
1470
0
  if (htab == NULL)
1471
0
    return true;
1472
1473
0
  m68hc11_elf_get_bank_parameters (link_info);
1474
1475
0
  param.use_memory_banks = false;
1476
0
  param.pinfo = & htab->pinfo;
1477
1478
0
  bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
1479
1480
0
  if (param.use_memory_banks)
1481
0
    {
1482
0
      Elf_Internal_Ehdr * i_ehdrp;
1483
1484
0
      i_ehdrp = elf_elfheader (abfd);
1485
0
      i_ehdrp->e_flags |= E_M68HC12_BANKS;
1486
0
    }
1487
  return true;
1488
0
}