1 /* Motorola 68HC11/HC12-specific support for 32-bit ELF
2 Copyright (C) 1999-2015 Free Software Foundation, Inc.
3 Contributed by Stephane Carrez (stcarrez@nerim.fr)
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
23 #include "alloca-conf.h"
28 #include "elf32-m68hc1x.h"
29 #include "elf/m68hc11.h"
30 #include "opcode/m68hc11.h"
33 #define m68hc12_stub_hash_lookup(table, string, create, copy) \
34 ((struct elf32_m68hc11_stub_hash_entry *) \
35 bfd_hash_lookup ((table), (string), (create), (copy)))
37 static struct elf32_m68hc11_stub_hash_entry* m68hc12_add_stub
38 (const char *stub_name,
40 struct m68hc11_elf_link_hash_table *htab);
42 static struct bfd_hash_entry *stub_hash_newfunc
43 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
45 static void m68hc11_elf_set_symbol (bfd* abfd, struct bfd_link_info *info,
46 const char* name, bfd_vma value,
49 static bfd_boolean m68hc11_elf_export_one_stub
50 (struct bfd_hash_entry *gen_entry, void *in_arg);
52 static void scan_sections_for_abi (bfd*, asection*, void *);
54 struct m68hc11_scan_param
56 struct m68hc11_page_info* pinfo;
57 bfd_boolean use_memory_banks;
61 /* Destroy a 68HC11/68HC12 ELF linker hash table. */
64 m68hc11_elf_bfd_link_hash_table_free (bfd *obfd)
66 struct m68hc11_elf_link_hash_table *ret
67 = (struct m68hc11_elf_link_hash_table *) obfd->link.hash;
69 bfd_hash_table_free (ret->stub_hash_table);
70 free (ret->stub_hash_table);
71 _bfd_elf_link_hash_table_free (obfd);
74 /* Create a 68HC11/68HC12 ELF linker hash table. */
76 struct m68hc11_elf_link_hash_table*
77 m68hc11_elf_hash_table_create (bfd *abfd)
79 struct m68hc11_elf_link_hash_table *ret;
80 bfd_size_type amt = sizeof (struct m68hc11_elf_link_hash_table);
82 ret = (struct m68hc11_elf_link_hash_table *) bfd_zmalloc (amt);
83 if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
86 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
87 _bfd_elf_link_hash_newfunc,
88 sizeof (struct elf_link_hash_entry),
95 /* Init the stub hash table too. */
96 amt = sizeof (struct bfd_hash_table);
97 ret->stub_hash_table = (struct bfd_hash_table*) bfd_malloc (amt);
98 if (ret->stub_hash_table == NULL)
100 _bfd_elf_link_hash_table_free (abfd);
103 if (!bfd_hash_table_init (ret->stub_hash_table, stub_hash_newfunc,
104 sizeof (struct elf32_m68hc11_stub_hash_entry)))
106 free (ret->stub_hash_table);
107 _bfd_elf_link_hash_table_free (abfd);
110 ret->root.root.hash_table_free = m68hc11_elf_bfd_link_hash_table_free;
115 /* Assorted hash table functions. */
117 /* Initialize an entry in the stub hash table. */
119 static struct bfd_hash_entry *
120 stub_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
123 /* Allocate the structure if it has not already been allocated by a
127 entry = bfd_hash_allocate (table,
128 sizeof (struct elf32_m68hc11_stub_hash_entry));
133 /* Call the allocation method of the superclass. */
134 entry = bfd_hash_newfunc (entry, table, string);
137 struct elf32_m68hc11_stub_hash_entry *eh;
139 /* Initialize the local fields. */
140 eh = (struct elf32_m68hc11_stub_hash_entry *) entry;
143 eh->target_value = 0;
144 eh->target_section = NULL;
150 /* Add a new stub entry to the stub hash. Not all fields of the new
151 stub entry are initialised. */
153 static struct elf32_m68hc11_stub_hash_entry *
154 m68hc12_add_stub (const char *stub_name, asection *section,
155 struct m68hc11_elf_link_hash_table *htab)
157 struct elf32_m68hc11_stub_hash_entry *stub_entry;
159 /* Enter this entry into the linker stub hash table. */
160 stub_entry = m68hc12_stub_hash_lookup (htab->stub_hash_table, stub_name,
162 if (stub_entry == NULL)
164 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
165 section->owner, stub_name);
169 if (htab->stub_section == 0)
171 htab->stub_section = (*htab->add_stub_section) (".tramp",
172 htab->tramp_section);
175 stub_entry->stub_sec = htab->stub_section;
176 stub_entry->stub_offset = 0;
180 /* Hook called by the linker routine which adds symbols from an object
181 file. We use it for identify far symbols and force a loading of
182 the trampoline handler. */
185 elf32_m68hc11_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
186 Elf_Internal_Sym *sym,
187 const char **namep ATTRIBUTE_UNUSED,
188 flagword *flagsp ATTRIBUTE_UNUSED,
189 asection **secp ATTRIBUTE_UNUSED,
190 bfd_vma *valp ATTRIBUTE_UNUSED)
192 if (sym->st_other & STO_M68HC12_FAR)
194 struct elf_link_hash_entry *h;
196 h = (struct elf_link_hash_entry *)
197 bfd_link_hash_lookup (info->hash, "__far_trampoline",
198 FALSE, FALSE, FALSE);
201 struct bfd_link_hash_entry* entry = NULL;
203 _bfd_generic_link_add_one_symbol (info, abfd,
207 (bfd_vma) 0, (const char*) NULL,
208 FALSE, FALSE, &entry);
215 /* Merge non-visibility st_other attributes, STO_M68HC12_FAR and
216 STO_M68HC12_INTERRUPT. */
219 elf32_m68hc11_merge_symbol_attribute (struct elf_link_hash_entry *h,
220 const Elf_Internal_Sym *isym,
221 bfd_boolean definition,
222 bfd_boolean dynamic ATTRIBUTE_UNUSED)
225 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
226 | ELF_ST_VISIBILITY (h->other));
229 /* External entry points for sizing and building linker stubs. */
231 /* Set up various things so that we can make a list of input sections
232 for each output section included in the link. Returns -1 on error,
233 0 when no stubs will be needed, and 1 on success. */
236 elf32_m68hc11_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
239 unsigned int bfd_count;
240 int top_id, top_index;
242 asection **input_list, **list;
244 asection *text_section;
245 struct m68hc11_elf_link_hash_table *htab;
247 htab = m68hc11_elf_hash_table (info);
251 if (bfd_get_flavour (info->output_bfd) != bfd_target_elf_flavour)
254 /* Count the number of input BFDs and find the top input section id.
255 Also search for an existing ".tramp" section so that we know
256 where generated trampolines must go. Default to ".text" if we
258 htab->tramp_section = 0;
260 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
262 input_bfd = input_bfd->link.next)
265 for (section = input_bfd->sections;
267 section = section->next)
269 const char* name = bfd_get_section_name (input_bfd, section);
271 if (!strcmp (name, ".tramp"))
272 htab->tramp_section = section;
274 if (!strcmp (name, ".text"))
275 text_section = section;
277 if (top_id < section->id)
278 top_id = section->id;
281 htab->bfd_count = bfd_count;
282 if (htab->tramp_section == 0)
283 htab->tramp_section = text_section;
285 /* We can't use output_bfd->section_count here to find the top output
286 section index as some sections may have been removed, and
287 strip_excluded_output_sections doesn't renumber the indices. */
288 for (section = output_bfd->sections, top_index = 0;
290 section = section->next)
292 if (top_index < section->index)
293 top_index = section->index;
296 htab->top_index = top_index;
297 amt = sizeof (asection *) * (top_index + 1);
298 input_list = (asection **) bfd_malloc (amt);
299 htab->input_list = input_list;
300 if (input_list == NULL)
303 /* For sections we aren't interested in, mark their entries with a
304 value we can check later. */
305 list = input_list + top_index;
307 *list = bfd_abs_section_ptr;
308 while (list-- != input_list);
310 for (section = output_bfd->sections;
312 section = section->next)
314 if ((section->flags & SEC_CODE) != 0)
315 input_list[section->index] = NULL;
321 /* Determine and set the size of the stub section for a final link.
323 The basic idea here is to examine all the relocations looking for
324 PC-relative calls to a target that is unreachable with a "bl"
328 elf32_m68hc11_size_stubs (bfd *output_bfd, bfd *stub_bfd,
329 struct bfd_link_info *info,
330 asection * (*add_stub_section) (const char*, asection*))
334 Elf_Internal_Sym *local_syms, **all_local_syms;
335 unsigned int bfd_indx, bfd_count;
338 struct m68hc11_elf_link_hash_table *htab = m68hc11_elf_hash_table (info);
343 /* Stash our params away. */
344 htab->stub_bfd = stub_bfd;
345 htab->add_stub_section = add_stub_section;
347 /* Count the number of input BFDs and find the top input section id. */
348 for (input_bfd = info->input_bfds, bfd_count = 0;
350 input_bfd = input_bfd->link.next)
353 /* We want to read in symbol extension records only once. To do this
354 we need to read in the local symbols in parallel and save them for
355 later use; so hold pointers to the local symbols in an array. */
356 amt = sizeof (Elf_Internal_Sym *) * bfd_count;
357 all_local_syms = (Elf_Internal_Sym **) bfd_zmalloc (amt);
358 if (all_local_syms == NULL)
361 /* Walk over all the input BFDs, swapping in local symbols. */
362 for (input_bfd = info->input_bfds, bfd_indx = 0;
364 input_bfd = input_bfd->link.next, bfd_indx++)
366 Elf_Internal_Shdr *symtab_hdr;
368 /* We'll need the symbol table in a second. */
369 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
370 if (symtab_hdr->sh_info == 0)
373 /* We need an array of the local symbols attached to the input bfd. */
374 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
375 if (local_syms == NULL)
377 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
378 symtab_hdr->sh_info, 0,
380 /* Cache them for elf_link_input_bfd. */
381 symtab_hdr->contents = (unsigned char *) local_syms;
383 if (local_syms == NULL)
385 free (all_local_syms);
389 all_local_syms[bfd_indx] = local_syms;
392 for (input_bfd = info->input_bfds, bfd_indx = 0;
394 input_bfd = input_bfd->link.next, bfd_indx++)
396 Elf_Internal_Shdr *symtab_hdr;
397 struct elf_link_hash_entry ** sym_hashes;
399 sym_hashes = elf_sym_hashes (input_bfd);
401 /* We'll need the symbol table in a second. */
402 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
403 if (symtab_hdr->sh_info == 0)
406 local_syms = all_local_syms[bfd_indx];
408 /* Walk over each section attached to the input bfd. */
409 for (section = input_bfd->sections;
411 section = section->next)
413 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
415 /* If there aren't any relocs, then there's nothing more
417 if ((section->flags & SEC_RELOC) == 0
418 || section->reloc_count == 0)
421 /* If this section is a link-once section that will be
422 discarded, then don't create any stubs. */
423 if (section->output_section == NULL
424 || section->output_section->owner != output_bfd)
427 /* Get the relocs. */
429 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
430 (Elf_Internal_Rela *) NULL,
432 if (internal_relocs == NULL)
433 goto error_ret_free_local;
435 /* Now examine each relocation. */
436 irela = internal_relocs;
437 irelaend = irela + section->reloc_count;
438 for (; irela < irelaend; irela++)
440 unsigned int r_type, r_indx;
441 struct elf32_m68hc11_stub_hash_entry *stub_entry;
444 struct elf_link_hash_entry *hash;
445 const char *stub_name;
446 Elf_Internal_Sym *sym;
448 r_type = ELF32_R_TYPE (irela->r_info);
450 /* Only look at 16-bit relocs. */
451 if (r_type != (unsigned int) R_M68HC11_16)
454 /* Now determine the call target, its name, value,
456 r_indx = ELF32_R_SYM (irela->r_info);
457 if (r_indx < symtab_hdr->sh_info)
459 /* It's a local symbol. */
460 Elf_Internal_Shdr *hdr;
463 sym = local_syms + r_indx;
464 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
468 if (sym->st_shndx >= elf_numsections (input_bfd))
472 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
473 sym_sec = hdr->bfd_section;
475 stub_name = (bfd_elf_string_from_elf_section
476 (input_bfd, symtab_hdr->sh_link,
478 sym_value = sym->st_value;
483 /* It's an external symbol. */
486 e_indx = r_indx - symtab_hdr->sh_info;
487 hash = (struct elf_link_hash_entry *)
488 (sym_hashes[e_indx]);
490 while (hash->root.type == bfd_link_hash_indirect
491 || hash->root.type == bfd_link_hash_warning)
492 hash = ((struct elf_link_hash_entry *)
493 hash->root.u.i.link);
495 if (hash->root.type == bfd_link_hash_defined
496 || hash->root.type == bfd_link_hash_defweak
497 || hash->root.type == bfd_link_hash_new)
499 if (!(hash->other & STO_M68HC12_FAR))
502 else if (hash->root.type == bfd_link_hash_undefweak)
506 else if (hash->root.type == bfd_link_hash_undefined)
512 bfd_set_error (bfd_error_bad_value);
513 goto error_ret_free_internal;
515 sym_sec = hash->root.u.def.section;
516 sym_value = hash->root.u.def.value;
517 stub_name = hash->root.root.string;
521 goto error_ret_free_internal;
523 stub_entry = m68hc12_stub_hash_lookup
524 (htab->stub_hash_table,
527 if (stub_entry == NULL)
529 if (add_stub_section == 0)
532 stub_entry = m68hc12_add_stub (stub_name, section, htab);
533 if (stub_entry == NULL)
535 error_ret_free_internal:
536 if (elf_section_data (section)->relocs == NULL)
537 free (internal_relocs);
538 goto error_ret_free_local;
542 stub_entry->target_value = sym_value;
543 stub_entry->target_section = sym_sec;
546 /* We're done with the internal relocs, free them. */
547 if (elf_section_data (section)->relocs == NULL)
548 free (internal_relocs);
552 if (add_stub_section)
554 /* OK, we've added some stubs. Find out the new size of the
556 for (stub_sec = htab->stub_bfd->sections;
558 stub_sec = stub_sec->next)
563 bfd_hash_traverse (htab->stub_hash_table, htab->size_one_stub, htab);
565 free (all_local_syms);
568 error_ret_free_local:
569 free (all_local_syms);
573 /* Export the trampoline addresses in the symbol table. */
575 m68hc11_elf_export_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
577 struct bfd_link_info *info;
578 struct m68hc11_elf_link_hash_table *htab;
579 struct elf32_m68hc11_stub_hash_entry *stub_entry;
583 info = (struct bfd_link_info *) in_arg;
584 htab = m68hc11_elf_hash_table (info);
588 /* Massage our args to the form they really have. */
589 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
591 /* Generate the trampoline according to HC11 or HC12. */
592 result = (* htab->build_one_stub) (gen_entry, in_arg);
594 /* Make a printable name that does not conflict with the real function. */
595 name = alloca (strlen (stub_entry->root.string) + 16);
596 sprintf (name, "tramp.%s", stub_entry->root.string);
598 /* Export the symbol for debugging/disassembling. */
599 m68hc11_elf_set_symbol (htab->stub_bfd, info, name,
600 stub_entry->stub_offset,
601 stub_entry->stub_sec);
605 /* Export a symbol or set its value and section. */
607 m68hc11_elf_set_symbol (bfd *abfd, struct bfd_link_info *info,
608 const char *name, bfd_vma value, asection *sec)
610 struct elf_link_hash_entry *h;
612 h = (struct elf_link_hash_entry *)
613 bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
616 _bfd_generic_link_add_one_symbol (info, abfd,
626 h->root.type = bfd_link_hash_defined;
627 h->root.u.def.value = value;
628 h->root.u.def.section = sec;
633 /* Build all the stubs associated with the current output file. The
634 stubs are kept in a hash table attached to the main linker hash
635 table. This function is called via m68hc12elf_finish in the
639 elf32_m68hc11_build_stubs (bfd *abfd, struct bfd_link_info *info)
642 struct bfd_hash_table *table;
643 struct m68hc11_elf_link_hash_table *htab;
644 struct m68hc11_scan_param param;
646 m68hc11_elf_get_bank_parameters (info);
647 htab = m68hc11_elf_hash_table (info);
651 for (stub_sec = htab->stub_bfd->sections;
653 stub_sec = stub_sec->next)
657 /* Allocate memory to hold the linker stubs. */
658 size = stub_sec->size;
659 stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
660 if (stub_sec->contents == NULL && size != 0)
665 /* Build the stubs as directed by the stub hash table. */
666 table = htab->stub_hash_table;
667 bfd_hash_traverse (table, m68hc11_elf_export_one_stub, info);
669 /* Scan the output sections to see if we use the memory banks.
670 If so, export the symbols that define how the memory banks
671 are mapped. This is used by gdb and the simulator to obtain
672 the information. It can be used by programs to burn the eprom
673 at the good addresses. */
674 param.use_memory_banks = FALSE;
675 param.pinfo = &htab->pinfo;
676 bfd_map_over_sections (abfd, scan_sections_for_abi, ¶m);
677 if (param.use_memory_banks)
679 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_START_NAME,
680 htab->pinfo.bank_physical,
681 bfd_abs_section_ptr);
682 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_VIRTUAL_NAME,
683 htab->pinfo.bank_virtual,
684 bfd_abs_section_ptr);
685 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_SIZE_NAME,
686 htab->pinfo.bank_size,
687 bfd_abs_section_ptr);
694 m68hc11_elf_get_bank_parameters (struct bfd_link_info *info)
697 struct m68hc11_page_info *pinfo;
698 struct bfd_link_hash_entry *h;
699 struct m68hc11_elf_link_hash_table *htab;
701 htab = m68hc11_elf_hash_table (info);
705 pinfo = & htab->pinfo;
706 if (pinfo->bank_param_initialized)
709 pinfo->bank_virtual = M68HC12_BANK_VIRT;
710 pinfo->bank_mask = M68HC12_BANK_MASK;
711 pinfo->bank_physical = M68HC12_BANK_BASE;
712 pinfo->bank_shift = M68HC12_BANK_SHIFT;
713 pinfo->bank_size = 1 << M68HC12_BANK_SHIFT;
715 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_START_NAME,
717 if (h != (struct bfd_link_hash_entry*) NULL
718 && h->type == bfd_link_hash_defined)
719 pinfo->bank_physical = (h->u.def.value
720 + h->u.def.section->output_section->vma
721 + h->u.def.section->output_offset);
723 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_VIRTUAL_NAME,
725 if (h != (struct bfd_link_hash_entry*) NULL
726 && h->type == bfd_link_hash_defined)
727 pinfo->bank_virtual = (h->u.def.value
728 + h->u.def.section->output_section->vma
729 + h->u.def.section->output_offset);
731 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_SIZE_NAME,
733 if (h != (struct bfd_link_hash_entry*) NULL
734 && h->type == bfd_link_hash_defined)
735 pinfo->bank_size = (h->u.def.value
736 + h->u.def.section->output_section->vma
737 + h->u.def.section->output_offset);
739 pinfo->bank_shift = 0;
740 for (i = pinfo->bank_size; i != 0; i >>= 1)
743 pinfo->bank_mask = (1 << pinfo->bank_shift) - 1;
744 pinfo->bank_physical_end = pinfo->bank_physical + pinfo->bank_size;
745 pinfo->bank_param_initialized = 1;
747 h = bfd_link_hash_lookup (info->hash, "__far_trampoline", FALSE,
749 if (h != (struct bfd_link_hash_entry*) NULL
750 && h->type == bfd_link_hash_defined)
751 pinfo->trampoline_addr = (h->u.def.value
752 + h->u.def.section->output_section->vma
753 + h->u.def.section->output_offset);
756 /* Return 1 if the address is in banked memory.
757 This can be applied to a virtual address and to a physical address. */
759 m68hc11_addr_is_banked (struct m68hc11_page_info *pinfo, bfd_vma addr)
761 if (addr >= pinfo->bank_virtual)
764 if (addr >= pinfo->bank_physical && addr <= pinfo->bank_physical_end)
770 /* Return the physical address seen by the processor, taking
771 into account banked memory. */
773 m68hc11_phys_addr (struct m68hc11_page_info *pinfo, bfd_vma addr)
775 if (addr < pinfo->bank_virtual)
778 /* Map the address to the memory bank. */
779 addr -= pinfo->bank_virtual;
780 addr &= pinfo->bank_mask;
781 addr += pinfo->bank_physical;
785 /* Return the page number corresponding to an address in banked memory. */
787 m68hc11_phys_page (struct m68hc11_page_info *pinfo, bfd_vma addr)
789 if (addr < pinfo->bank_virtual)
792 /* Map the address to the memory bank. */
793 addr -= pinfo->bank_virtual;
794 addr >>= pinfo->bank_shift;
799 /* This function is used for relocs which are only used for relaxing,
800 which the linker should otherwise ignore. */
802 bfd_reloc_status_type
803 m68hc11_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED,
804 arelent *reloc_entry,
805 asymbol *symbol ATTRIBUTE_UNUSED,
806 void *data ATTRIBUTE_UNUSED,
807 asection *input_section,
809 char **error_message ATTRIBUTE_UNUSED)
811 if (output_bfd != NULL)
812 reloc_entry->address += input_section->output_offset;
816 bfd_reloc_status_type
817 m68hc11_elf_special_reloc (bfd *abfd ATTRIBUTE_UNUSED,
818 arelent *reloc_entry,
820 void *data ATTRIBUTE_UNUSED,
821 asection *input_section,
823 char **error_message ATTRIBUTE_UNUSED)
825 if (output_bfd != (bfd *) NULL
826 && (symbol->flags & BSF_SECTION_SYM) == 0
827 && (! reloc_entry->howto->partial_inplace
828 || reloc_entry->addend == 0))
830 reloc_entry->address += input_section->output_offset;
834 if (output_bfd != NULL)
835 return bfd_reloc_continue;
837 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
838 return bfd_reloc_outofrange;
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. */
848 elf32_m68hc11_check_relocs (bfd *abfd, struct bfd_link_info *info,
849 asection *sec, const Elf_Internal_Rela *relocs)
851 Elf_Internal_Shdr * symtab_hdr;
852 struct elf_link_hash_entry ** sym_hashes;
853 const Elf_Internal_Rela * rel;
854 const Elf_Internal_Rela * rel_end;
856 if (info->relocatable)
859 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
860 sym_hashes = elf_sym_hashes (abfd);
861 rel_end = relocs + sec->reloc_count;
863 for (rel = relocs; rel < rel_end; rel++)
865 struct elf_link_hash_entry * h;
866 unsigned long r_symndx;
868 r_symndx = ELF32_R_SYM (rel->r_info);
870 if (r_symndx < symtab_hdr->sh_info)
874 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
875 while (h->root.type == bfd_link_hash_indirect
876 || h->root.type == bfd_link_hash_warning)
877 h = (struct elf_link_hash_entry *) h->root.u.i.link;
879 /* PR15323, ref flags aren't set for references in the same
881 h->root.non_ir_ref = 1;
884 switch (ELF32_R_TYPE (rel->r_info))
886 /* This relocation describes the C++ object vtable hierarchy.
887 Reconstruct it for later use during GC. */
888 case R_M68HC11_GNU_VTINHERIT:
889 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
893 /* This relocation describes which C++ vtable entries are actually
894 used. Record for later use during GC. */
895 case R_M68HC11_GNU_VTENTRY:
896 BFD_ASSERT (h != NULL);
898 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
907 /* Relocate a 68hc11/68hc12 ELF section. */
909 elf32_m68hc11_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
910 struct bfd_link_info *info,
911 bfd *input_bfd, asection *input_section,
912 bfd_byte *contents, Elf_Internal_Rela *relocs,
913 Elf_Internal_Sym *local_syms,
914 asection **local_sections)
916 Elf_Internal_Shdr *symtab_hdr;
917 struct elf_link_hash_entry **sym_hashes;
918 Elf_Internal_Rela *rel, *relend;
919 const char *name = NULL;
920 struct m68hc11_page_info *pinfo;
921 const struct elf_backend_data * const ebd = get_elf_backend_data (input_bfd);
922 struct m68hc11_elf_link_hash_table *htab;
923 unsigned long e_flags;
925 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
926 sym_hashes = elf_sym_hashes (input_bfd);
927 e_flags = elf_elfheader (input_bfd)->e_flags;
929 htab = m68hc11_elf_hash_table (info);
933 /* Get memory bank parameters. */
934 m68hc11_elf_get_bank_parameters (info);
936 pinfo = & htab->pinfo;
938 relend = relocs + input_section->reloc_count;
940 for (; rel < relend; rel++)
944 reloc_howto_type *howto;
945 unsigned long r_symndx;
946 Elf_Internal_Sym *sym;
948 bfd_vma relocation = 0;
949 bfd_reloc_status_type r = bfd_reloc_undefined;
954 bfd_boolean is_far = FALSE;
955 bfd_boolean is_xgate_symbol = FALSE;
956 bfd_boolean is_section_symbol = FALSE;
957 struct elf_link_hash_entry *h;
960 r_symndx = ELF32_R_SYM (rel->r_info);
961 r_type = ELF32_R_TYPE (rel->r_info);
963 if (r_type == R_M68HC11_GNU_VTENTRY
964 || r_type == R_M68HC11_GNU_VTINHERIT)
967 (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel);
973 if (r_symndx < symtab_hdr->sh_info)
975 sym = local_syms + r_symndx;
976 sec = local_sections[r_symndx];
977 relocation = (sec->output_section->vma
980 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
981 is_xgate_symbol = (sym && (sym->st_target_internal));
982 is_section_symbol = ELF_ST_TYPE (sym->st_info) & STT_SECTION;
986 bfd_boolean unresolved_reloc, warned, ignored;
988 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
989 r_symndx, symtab_hdr, sym_hashes,
990 h, sec, relocation, unresolved_reloc,
993 is_far = (h && (h->other & STO_M68HC12_FAR));
994 is_xgate_symbol = (h && (h->target_internal));
997 if (sec != NULL && discarded_section (sec))
998 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
999 rel, 1, relend, howto, 0, contents);
1001 if (info->relocatable)
1003 /* This is a relocatable link. We don't have to change
1004 anything, unless the reloc is against a section symbol,
1005 in which case we have to adjust according to where the
1006 section symbol winds up in the output section. */
1007 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1008 rel->r_addend += sec->output_offset;
1013 name = h->root.root.string;
1016 name = (bfd_elf_string_from_elf_section
1017 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1018 if (name == NULL || *name == '\0')
1019 name = bfd_section_name (input_bfd, sec);
1022 if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
1024 struct elf32_m68hc11_stub_hash_entry* stub;
1026 stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
1027 name, FALSE, FALSE);
1030 relocation = stub->stub_offset
1031 + stub->stub_sec->output_section->vma
1032 + stub->stub_sec->output_offset;
1037 /* Do the memory bank mapping. */
1038 phys_addr = m68hc11_phys_addr (pinfo, relocation + rel->r_addend);
1039 phys_page = m68hc11_phys_page (pinfo, relocation + rel->r_addend);
1042 case R_M68HC12_LO8XG:
1043 /* This relocation is specific to XGATE IMM16 calls and will precede
1044 a HI8. tc-m68hc11 only generates them in pairs.
1045 Leave the relocation to the HI8XG step. */
1047 r_type = R_M68HC11_NONE;
1050 case R_M68HC12_HI8XG:
1051 /* This relocation is specific to XGATE IMM16 calls and must follow
1052 a LO8XG. Does not actually check that it was a LO8XG.
1053 Adjusts high and low bytes. */
1054 relocation = phys_addr;
1055 if ((e_flags & E_M68HC11_XGATE_RAMOFFSET)
1056 && (relocation >= 0x2000))
1057 relocation += 0xc000; /* HARDCODED RAM offset for XGATE. */
1059 /* Fetch 16 bit value including low byte in previous insn. */
1060 val = (bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset) << 8)
1061 | bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset - 2);
1063 /* Add on value to preserve carry, then write zero to high byte. */
1066 /* Write out top byte. */
1067 bfd_put_8 (input_bfd, (relocation >> 8) & 0xff,
1068 (bfd_byte*) contents + rel->r_offset);
1070 /* Write out low byte to previous instruction. */
1071 bfd_put_8 (input_bfd, relocation & 0xff,
1072 (bfd_byte*) contents + rel->r_offset - 2);
1074 /* Mark as relocation completed. */
1076 r_type = R_M68HC11_NONE;
1079 /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1080 assembler directives. %hi does not support carry. */
1083 relocation = phys_addr;
1087 /* Reloc used by 68HC12 call instruction. */
1088 bfd_put_16 (input_bfd, phys_addr,
1089 (bfd_byte*) contents + rel->r_offset);
1090 bfd_put_8 (input_bfd, phys_page,
1091 (bfd_byte*) contents + rel->r_offset + 2);
1093 r_type = R_M68HC11_NONE;
1096 case R_M68HC11_NONE:
1100 case R_M68HC11_LO16:
1101 /* Reloc generated by %addr(expr) gas to obtain the
1102 address as mapped in the memory bank window. */
1103 relocation = phys_addr;
1106 case R_M68HC11_PAGE:
1107 /* Reloc generated by %page(expr) gas to obtain the
1108 page number associated with the address. */
1109 relocation = phys_page;
1113 /* Get virtual address of instruction having the relocation. */
1118 msg = _("Reference to the far symbol `%s' using a wrong "
1119 "relocation may result in incorrect execution");
1120 buf = alloca (strlen (msg) + strlen (name) + 10);
1121 sprintf (buf, msg, name);
1123 (* info->callbacks->warning)
1124 (info, buf, name, input_bfd, NULL, rel->r_offset);
1127 /* Get virtual address of instruction having the relocation. */
1128 insn_addr = input_section->output_section->vma
1129 + input_section->output_offset
1132 insn_page = m68hc11_phys_page (pinfo, insn_addr);
1134 /* If we are linking an S12 instruction against an XGATE symbol, we
1135 need to change the offset of the symbol value so that it's correct
1136 from the S12's perspective. */
1137 if (is_xgate_symbol)
1139 /* The ram in the global space is mapped to 0x2000 in the 16-bit
1140 address space for S12 and 0xE000 in the 16-bit address space
1142 if (relocation >= 0xE000)
1144 /* We offset the address by the difference
1145 between these two mappings. */
1146 relocation -= 0xC000;
1154 msg = _("XGATE address (%lx) is not within shared RAM"
1155 "(0xE000-0xFFFF), therefore you must manually offset "
1156 "the address, and possibly manage the page, in your "
1158 buf = alloca (strlen (msg) + 128);
1159 sprintf (buf, msg, phys_addr);
1160 if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
1161 input_section, insn_addr)))
1167 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
1168 && m68hc11_addr_is_banked (pinfo, insn_addr)
1169 && phys_page != insn_page && !(e_flags & E_M68HC11_NO_BANK_WARNING))
1174 msg = _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1175 "as current banked address [%lx:%04lx] (%lx)");
1177 buf = alloca (strlen (msg) + 128);
1178 sprintf (buf, msg, phys_page, phys_addr,
1179 (long) (relocation + rel->r_addend),
1180 insn_page, m68hc11_phys_addr (pinfo, insn_addr),
1181 (long) (insn_addr));
1182 if (!((*info->callbacks->warning)
1183 (info, buf, name, input_bfd, input_section,
1189 if (phys_page != 0 && insn_page == 0)
1194 msg = _("reference to a banked address [%lx:%04lx] in the "
1195 "normal address space at %04lx");
1197 buf = alloca (strlen (msg) + 128);
1198 sprintf (buf, msg, phys_page, phys_addr, insn_addr);
1199 if (!((*info->callbacks->warning)
1200 (info, buf, name, input_bfd, input_section,
1204 relocation = phys_addr;
1208 /* If this is a banked address use the phys_addr so that
1209 we stay in the banked window. */
1210 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend))
1211 relocation = phys_addr;
1215 /* If we are linking an XGATE instruction against an S12 symbol, we
1216 need to change the offset of the symbol value so that it's correct
1217 from the XGATE's perspective. */
1218 if (!strcmp (howto->name, "R_XGATE_IMM8_LO")
1219 || !strcmp (howto->name, "R_XGATE_IMM8_HI"))
1221 /* We can only offset S12 addresses that lie within the non-paged
1223 if (!is_xgate_symbol && !is_section_symbol)
1225 /* The ram in the global space is mapped to 0x2000 and stops at
1226 0x4000 in the 16-bit address space for S12 and 0xE000 in the
1227 16-bit address space for XGATE. */
1228 if (relocation >= 0x2000 && relocation < 0x4000)
1229 /* We offset the address by the difference
1230 between these two mappings. */
1231 relocation += 0xC000;
1237 /* Get virtual address of instruction having the relocation. */
1238 insn_addr = input_section->output_section->vma
1239 + input_section->output_offset + rel->r_offset;
1241 msg = _("S12 address (%lx) is not within shared RAM"
1242 "(0x2000-0x4000), therefore you must manually "
1243 "offset the address in your code");
1244 buf = alloca (strlen (msg) + 128);
1245 sprintf (buf, msg, phys_addr);
1246 if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
1247 input_section, insn_addr)))
1254 if (r_type != R_M68HC11_NONE)
1256 if ((r_type == R_M68HC12_PCREL_9) || (r_type == R_M68HC12_PCREL_10))
1257 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1258 contents, rel->r_offset,
1259 relocation - 2, rel->r_addend);
1261 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1262 contents, rel->r_offset,
1263 relocation, rel->r_addend);
1266 if (r != bfd_reloc_ok)
1268 const char * msg = (const char *) 0;
1272 case bfd_reloc_overflow:
1273 if (!((*info->callbacks->reloc_overflow)
1274 (info, NULL, name, howto->name, (bfd_vma) 0,
1275 input_bfd, input_section, rel->r_offset)))
1279 case bfd_reloc_undefined:
1280 if (!((*info->callbacks->undefined_symbol)
1281 (info, name, input_bfd, input_section,
1282 rel->r_offset, TRUE)))
1286 case bfd_reloc_outofrange:
1287 msg = _ ("internal error: out of range error");
1290 case bfd_reloc_notsupported:
1291 msg = _ ("internal error: unsupported relocation error");
1294 case bfd_reloc_dangerous:
1295 msg = _ ("internal error: dangerous error");
1299 msg = _ ("internal error: unknown error");
1303 if (!((*info->callbacks->warning)
1304 (info, msg, name, input_bfd, input_section,
1317 /* Set and control ELF flags in ELF header. */
1320 _bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
1322 BFD_ASSERT (!elf_flags_init (abfd)
1323 || elf_elfheader (abfd)->e_flags == flags);
1325 elf_elfheader (abfd)->e_flags = flags;
1326 elf_flags_init (abfd) = TRUE;
1330 /* Merge backend specific data from an object file to the output
1331 object file when linking. */
1334 _bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
1338 bfd_boolean ok = TRUE;
1340 /* Check if we have the same endianness */
1341 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
1344 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1345 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1348 new_flags = elf_elfheader (ibfd)->e_flags;
1349 elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
1350 old_flags = elf_elfheader (obfd)->e_flags;
1352 if (! elf_flags_init (obfd))
1354 elf_flags_init (obfd) = TRUE;
1355 elf_elfheader (obfd)->e_flags = new_flags;
1356 elf_elfheader (obfd)->e_ident[EI_CLASS]
1357 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
1359 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1360 && bfd_get_arch_info (obfd)->the_default)
1362 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1363 bfd_get_mach (ibfd)))
1370 /* Check ABI compatibility. */
1371 if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
1373 (*_bfd_error_handler)
1374 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1375 "and others for 32-bit integers"), ibfd);
1378 if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
1380 (*_bfd_error_handler)
1381 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1382 "and others for 64-bit double"), ibfd);
1386 /* Processor compatibility. */
1387 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
1389 (*_bfd_error_handler)
1390 (_("%B: linking files compiled for HCS12 with "
1391 "others compiled for HC12"), ibfd);
1394 new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
1395 | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
1397 elf_elfheader (obfd)->e_flags = new_flags;
1399 new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1400 old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1402 /* Warn about any other mismatches */
1403 if (new_flags != old_flags)
1405 (*_bfd_error_handler)
1406 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1407 ibfd, (unsigned long) new_flags, (unsigned long) old_flags);
1413 bfd_set_error (bfd_error_bad_value);
1421 _bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
1423 FILE *file = (FILE *) ptr;
1425 BFD_ASSERT (abfd != NULL && ptr != NULL);
1427 /* Print normal ELF private data. */
1428 _bfd_elf_print_private_bfd_data (abfd, ptr);
1430 /* xgettext:c-format */
1431 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1433 if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
1434 fprintf (file, _("[abi=32-bit int, "));
1436 fprintf (file, _("[abi=16-bit int, "));
1438 if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
1439 fprintf (file, _("64-bit double, "));
1441 fprintf (file, _("32-bit double, "));
1443 if (strcmp (bfd_get_target (abfd), "elf32-m68hc11") == 0)
1444 fprintf (file, _("cpu=HC11]"));
1445 else if (elf_elfheader (abfd)->e_flags & EF_M68HCS12_MACH)
1446 fprintf (file, _("cpu=HCS12]"));
1448 fprintf (file, _("cpu=HC12]"));
1450 if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
1451 fprintf (file, _(" [memory=bank-model]"));
1453 fprintf (file, _(" [memory=flat]"));
1455 if (elf_elfheader (abfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
1456 fprintf (file, _(" [XGATE RAM offsetting]"));
1463 static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
1464 asection *asect, void *arg)
1466 struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
1468 if (asect->vma >= p->pinfo->bank_virtual)
1469 p->use_memory_banks = TRUE;
1472 /* Tweak the OSABI field of the elf header. */
1475 elf32_m68hc11_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
1477 struct m68hc11_scan_param param;
1478 struct m68hc11_elf_link_hash_table *htab;
1480 if (link_info == NULL)
1483 htab = m68hc11_elf_hash_table (link_info);
1487 m68hc11_elf_get_bank_parameters (link_info);
1489 param.use_memory_banks = FALSE;
1490 param.pinfo = & htab->pinfo;
1492 bfd_map_over_sections (abfd, scan_sections_for_abi, ¶m);
1494 if (param.use_memory_banks)
1496 Elf_Internal_Ehdr * i_ehdrp;
1498 i_ehdrp = elf_elfheader (abfd);
1499 i_ehdrp->e_flags |= E_M68HC12_BANKS;