1 /* Motorola 68HC11/HC12-specific support for 32-bit ELF
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
4 Contributed by Stephane Carrez (stcarrez@nerim.fr)
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
24 #include "alloca-conf.h"
29 #include "elf32-m68hc1x.h"
30 #include "elf/m68hc11.h"
31 #include "opcode/m68hc11.h"
34 #define m68hc12_stub_hash_lookup(table, string, create, copy) \
35 ((struct elf32_m68hc11_stub_hash_entry *) \
36 bfd_hash_lookup ((table), (string), (create), (copy)))
38 static struct elf32_m68hc11_stub_hash_entry* m68hc12_add_stub
39 (const char *stub_name,
41 struct m68hc11_elf_link_hash_table *htab);
43 static struct bfd_hash_entry *stub_hash_newfunc
44 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
46 static void m68hc11_elf_set_symbol (bfd* abfd, struct bfd_link_info *info,
47 const char* name, bfd_vma value,
50 static bfd_boolean m68hc11_elf_export_one_stub
51 (struct bfd_hash_entry *gen_entry, void *in_arg);
53 static void scan_sections_for_abi (bfd*, asection*, PTR);
55 struct m68hc11_scan_param
57 struct m68hc11_page_info* pinfo;
58 bfd_boolean use_memory_banks;
62 /* Create a 68HC11/68HC12 ELF linker hash table. */
64 struct m68hc11_elf_link_hash_table*
65 m68hc11_elf_hash_table_create (bfd *abfd)
67 struct m68hc11_elf_link_hash_table *ret;
68 bfd_size_type amt = sizeof (struct m68hc11_elf_link_hash_table);
70 ret = (struct m68hc11_elf_link_hash_table *) bfd_malloc (amt);
71 if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
75 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
76 _bfd_elf_link_hash_newfunc,
77 sizeof (struct elf_link_hash_entry),
84 /* Init the stub hash table too. */
85 amt = sizeof (struct bfd_hash_table);
86 ret->stub_hash_table = (struct bfd_hash_table*) bfd_malloc (amt);
87 if (ret->stub_hash_table == NULL)
92 if (!bfd_hash_table_init (ret->stub_hash_table, stub_hash_newfunc,
93 sizeof (struct elf32_m68hc11_stub_hash_entry)))
97 ret->stub_section = 0;
98 ret->add_stub_section = NULL;
99 ret->sym_cache.abfd = NULL;
104 /* Free the derived linker hash table. */
107 m68hc11_elf_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
109 struct m68hc11_elf_link_hash_table *ret
110 = (struct m68hc11_elf_link_hash_table *) hash;
112 bfd_hash_table_free (ret->stub_hash_table);
113 free (ret->stub_hash_table);
114 _bfd_generic_link_hash_table_free (hash);
117 /* Assorted hash table functions. */
119 /* Initialize an entry in the stub hash table. */
121 static struct bfd_hash_entry *
122 stub_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
125 /* Allocate the structure if it has not already been allocated by a
129 entry = bfd_hash_allocate (table,
130 sizeof (struct elf32_m68hc11_stub_hash_entry));
135 /* Call the allocation method of the superclass. */
136 entry = bfd_hash_newfunc (entry, table, string);
139 struct elf32_m68hc11_stub_hash_entry *eh;
141 /* Initialize the local fields. */
142 eh = (struct elf32_m68hc11_stub_hash_entry *) entry;
145 eh->target_value = 0;
146 eh->target_section = NULL;
152 /* Add a new stub entry to the stub hash. Not all fields of the new
153 stub entry are initialised. */
155 static struct elf32_m68hc11_stub_hash_entry *
156 m68hc12_add_stub (const char *stub_name, asection *section,
157 struct m68hc11_elf_link_hash_table *htab)
159 struct elf32_m68hc11_stub_hash_entry *stub_entry;
161 /* Enter this entry into the linker stub hash table. */
162 stub_entry = m68hc12_stub_hash_lookup (htab->stub_hash_table, stub_name,
164 if (stub_entry == NULL)
166 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
167 section->owner, stub_name);
171 if (htab->stub_section == 0)
173 htab->stub_section = (*htab->add_stub_section) (".tramp",
174 htab->tramp_section);
177 stub_entry->stub_sec = htab->stub_section;
178 stub_entry->stub_offset = 0;
182 /* Hook called by the linker routine which adds symbols from an object
183 file. We use it for identify far symbols and force a loading of
184 the trampoline handler. */
187 elf32_m68hc11_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
188 Elf_Internal_Sym *sym,
189 const char **namep ATTRIBUTE_UNUSED,
190 flagword *flagsp ATTRIBUTE_UNUSED,
191 asection **secp ATTRIBUTE_UNUSED,
192 bfd_vma *valp ATTRIBUTE_UNUSED)
194 if (sym->st_other & STO_M68HC12_FAR)
196 struct elf_link_hash_entry *h;
198 h = (struct elf_link_hash_entry *)
199 bfd_link_hash_lookup (info->hash, "__far_trampoline",
200 FALSE, FALSE, FALSE);
203 struct bfd_link_hash_entry* entry = NULL;
205 _bfd_generic_link_add_one_symbol (info, abfd,
209 (bfd_vma) 0, (const char*) NULL,
210 FALSE, FALSE, &entry);
217 /* Merge non-visibility st_other attributes, STO_M68HC12_FAR and
218 STO_M68HC12_INTERRUPT. */
221 elf32_m68hc11_merge_symbol_attribute (struct elf_link_hash_entry *h,
222 const Elf_Internal_Sym *isym,
223 bfd_boolean definition,
224 bfd_boolean dynamic ATTRIBUTE_UNUSED)
227 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
228 | ELF_ST_VISIBILITY (h->other));
231 /* External entry points for sizing and building linker stubs. */
233 /* Set up various things so that we can make a list of input sections
234 for each output section included in the link. Returns -1 on error,
235 0 when no stubs will be needed, and 1 on success. */
238 elf32_m68hc11_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
241 unsigned int bfd_count;
242 int top_id, top_index;
244 asection **input_list, **list;
246 asection *text_section;
247 struct m68hc11_elf_link_hash_table *htab;
249 htab = m68hc11_elf_hash_table (info);
253 if (bfd_get_flavour (info->output_bfd) != bfd_target_elf_flavour)
256 /* Count the number of input BFDs and find the top input section id.
257 Also search for an existing ".tramp" section so that we know
258 where generated trampolines must go. Default to ".text" if we
260 htab->tramp_section = 0;
262 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
264 input_bfd = input_bfd->link_next)
267 for (section = input_bfd->sections;
269 section = section->next)
271 const char* name = bfd_get_section_name (input_bfd, section);
273 if (!strcmp (name, ".tramp"))
274 htab->tramp_section = section;
276 if (!strcmp (name, ".text"))
277 text_section = section;
279 if (top_id < section->id)
280 top_id = section->id;
283 htab->bfd_count = bfd_count;
284 if (htab->tramp_section == 0)
285 htab->tramp_section = text_section;
287 /* We can't use output_bfd->section_count here to find the top output
288 section index as some sections may have been removed, and
289 strip_excluded_output_sections doesn't renumber the indices. */
290 for (section = output_bfd->sections, top_index = 0;
292 section = section->next)
294 if (top_index < section->index)
295 top_index = section->index;
298 htab->top_index = top_index;
299 amt = sizeof (asection *) * (top_index + 1);
300 input_list = (asection **) bfd_malloc (amt);
301 htab->input_list = input_list;
302 if (input_list == NULL)
305 /* For sections we aren't interested in, mark their entries with a
306 value we can check later. */
307 list = input_list + top_index;
309 *list = bfd_abs_section_ptr;
310 while (list-- != input_list);
312 for (section = output_bfd->sections;
314 section = section->next)
316 if ((section->flags & SEC_CODE) != 0)
317 input_list[section->index] = NULL;
323 /* Determine and set the size of the stub section for a final link.
325 The basic idea here is to examine all the relocations looking for
326 PC-relative calls to a target that is unreachable with a "bl"
330 elf32_m68hc11_size_stubs (bfd *output_bfd, bfd *stub_bfd,
331 struct bfd_link_info *info,
332 asection * (*add_stub_section) (const char*, asection*))
336 Elf_Internal_Sym *local_syms, **all_local_syms;
337 unsigned int bfd_indx, bfd_count;
340 struct m68hc11_elf_link_hash_table *htab = m68hc11_elf_hash_table (info);
345 /* Stash our params away. */
346 htab->stub_bfd = stub_bfd;
347 htab->add_stub_section = add_stub_section;
349 /* Count the number of input BFDs and find the top input section id. */
350 for (input_bfd = info->input_bfds, bfd_count = 0;
352 input_bfd = input_bfd->link_next)
355 /* We want to read in symbol extension records only once. To do this
356 we need to read in the local symbols in parallel and save them for
357 later use; so hold pointers to the local symbols in an array. */
358 amt = sizeof (Elf_Internal_Sym *) * bfd_count;
359 all_local_syms = (Elf_Internal_Sym **) bfd_zmalloc (amt);
360 if (all_local_syms == NULL)
363 /* Walk over all the input BFDs, swapping in local symbols. */
364 for (input_bfd = info->input_bfds, bfd_indx = 0;
366 input_bfd = input_bfd->link_next, bfd_indx++)
368 Elf_Internal_Shdr *symtab_hdr;
370 /* We'll need the symbol table in a second. */
371 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
372 if (symtab_hdr->sh_info == 0)
375 /* We need an array of the local symbols attached to the input bfd. */
376 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
377 if (local_syms == NULL)
379 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
380 symtab_hdr->sh_info, 0,
382 /* Cache them for elf_link_input_bfd. */
383 symtab_hdr->contents = (unsigned char *) local_syms;
385 if (local_syms == NULL)
387 free (all_local_syms);
391 all_local_syms[bfd_indx] = local_syms;
394 for (input_bfd = info->input_bfds, bfd_indx = 0;
396 input_bfd = input_bfd->link_next, bfd_indx++)
398 Elf_Internal_Shdr *symtab_hdr;
399 struct elf_link_hash_entry ** sym_hashes;
401 sym_hashes = elf_sym_hashes (input_bfd);
403 /* We'll need the symbol table in a second. */
404 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
405 if (symtab_hdr->sh_info == 0)
408 local_syms = all_local_syms[bfd_indx];
410 /* Walk over each section attached to the input bfd. */
411 for (section = input_bfd->sections;
413 section = section->next)
415 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
417 /* If there aren't any relocs, then there's nothing more
419 if ((section->flags & SEC_RELOC) == 0
420 || section->reloc_count == 0)
423 /* If this section is a link-once section that will be
424 discarded, then don't create any stubs. */
425 if (section->output_section == NULL
426 || section->output_section->owner != output_bfd)
429 /* Get the relocs. */
431 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
432 (Elf_Internal_Rela *) NULL,
434 if (internal_relocs == NULL)
435 goto error_ret_free_local;
437 /* Now examine each relocation. */
438 irela = internal_relocs;
439 irelaend = irela + section->reloc_count;
440 for (; irela < irelaend; irela++)
442 unsigned int r_type, r_indx;
443 struct elf32_m68hc11_stub_hash_entry *stub_entry;
446 struct elf_link_hash_entry *hash;
447 const char *stub_name;
448 Elf_Internal_Sym *sym;
450 r_type = ELF32_R_TYPE (irela->r_info);
452 /* Only look at 16-bit relocs. */
453 if (r_type != (unsigned int) R_M68HC11_16)
456 /* Now determine the call target, its name, value,
458 r_indx = ELF32_R_SYM (irela->r_info);
459 if (r_indx < symtab_hdr->sh_info)
461 /* It's a local symbol. */
462 Elf_Internal_Shdr *hdr;
465 sym = local_syms + r_indx;
466 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
470 if (sym->st_shndx >= elf_numsections (input_bfd))
474 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
475 sym_sec = hdr->bfd_section;
477 stub_name = (bfd_elf_string_from_elf_section
478 (input_bfd, symtab_hdr->sh_link,
480 sym_value = sym->st_value;
485 /* It's an external symbol. */
488 e_indx = r_indx - symtab_hdr->sh_info;
489 hash = (struct elf_link_hash_entry *)
490 (sym_hashes[e_indx]);
492 while (hash->root.type == bfd_link_hash_indirect
493 || hash->root.type == bfd_link_hash_warning)
494 hash = ((struct elf_link_hash_entry *)
495 hash->root.u.i.link);
497 if (hash->root.type == bfd_link_hash_defined
498 || hash->root.type == bfd_link_hash_defweak
499 || hash->root.type == bfd_link_hash_new)
501 if (!(hash->other & STO_M68HC12_FAR))
504 else if (hash->root.type == bfd_link_hash_undefweak)
508 else if (hash->root.type == bfd_link_hash_undefined)
514 bfd_set_error (bfd_error_bad_value);
515 goto error_ret_free_internal;
517 sym_sec = hash->root.u.def.section;
518 sym_value = hash->root.u.def.value;
519 stub_name = hash->root.root.string;
523 goto error_ret_free_internal;
525 stub_entry = m68hc12_stub_hash_lookup
526 (htab->stub_hash_table,
529 if (stub_entry == NULL)
531 if (add_stub_section == 0)
534 stub_entry = m68hc12_add_stub (stub_name, section, htab);
535 if (stub_entry == NULL)
537 error_ret_free_internal:
538 if (elf_section_data (section)->relocs == NULL)
539 free (internal_relocs);
540 goto error_ret_free_local;
544 stub_entry->target_value = sym_value;
545 stub_entry->target_section = sym_sec;
548 /* We're done with the internal relocs, free them. */
549 if (elf_section_data (section)->relocs == NULL)
550 free (internal_relocs);
554 if (add_stub_section)
556 /* OK, we've added some stubs. Find out the new size of the
558 for (stub_sec = htab->stub_bfd->sections;
560 stub_sec = stub_sec->next)
565 bfd_hash_traverse (htab->stub_hash_table, htab->size_one_stub, htab);
567 free (all_local_syms);
570 error_ret_free_local:
571 free (all_local_syms);
575 /* Export the trampoline addresses in the symbol table. */
577 m68hc11_elf_export_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
579 struct bfd_link_info *info;
580 struct m68hc11_elf_link_hash_table *htab;
581 struct elf32_m68hc11_stub_hash_entry *stub_entry;
585 info = (struct bfd_link_info *) in_arg;
586 htab = m68hc11_elf_hash_table (info);
590 /* Massage our args to the form they really have. */
591 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
593 /* Generate the trampoline according to HC11 or HC12. */
594 result = (* htab->build_one_stub) (gen_entry, in_arg);
596 /* Make a printable name that does not conflict with the real function. */
597 name = alloca (strlen (stub_entry->root.string) + 16);
598 sprintf (name, "tramp.%s", stub_entry->root.string);
600 /* Export the symbol for debugging/disassembling. */
601 m68hc11_elf_set_symbol (htab->stub_bfd, info, name,
602 stub_entry->stub_offset,
603 stub_entry->stub_sec);
607 /* Export a symbol or set its value and section. */
609 m68hc11_elf_set_symbol (bfd *abfd, struct bfd_link_info *info,
610 const char *name, bfd_vma value, asection *sec)
612 struct elf_link_hash_entry *h;
614 h = (struct elf_link_hash_entry *)
615 bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
618 _bfd_generic_link_add_one_symbol (info, abfd,
628 h->root.type = bfd_link_hash_defined;
629 h->root.u.def.value = value;
630 h->root.u.def.section = sec;
635 /* Build all the stubs associated with the current output file. The
636 stubs are kept in a hash table attached to the main linker hash
637 table. This function is called via m68hc12elf_finish in the
641 elf32_m68hc11_build_stubs (bfd *abfd, struct bfd_link_info *info)
644 struct bfd_hash_table *table;
645 struct m68hc11_elf_link_hash_table *htab;
646 struct m68hc11_scan_param param;
648 m68hc11_elf_get_bank_parameters (info);
649 htab = m68hc11_elf_hash_table (info);
653 for (stub_sec = htab->stub_bfd->sections;
655 stub_sec = stub_sec->next)
659 /* Allocate memory to hold the linker stubs. */
660 size = stub_sec->size;
661 stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
662 if (stub_sec->contents == NULL && size != 0)
667 /* Build the stubs as directed by the stub hash table. */
668 table = htab->stub_hash_table;
669 bfd_hash_traverse (table, m68hc11_elf_export_one_stub, info);
671 /* Scan the output sections to see if we use the memory banks.
672 If so, export the symbols that define how the memory banks
673 are mapped. This is used by gdb and the simulator to obtain
674 the information. It can be used by programs to burn the eprom
675 at the good addresses. */
676 param.use_memory_banks = FALSE;
677 param.pinfo = &htab->pinfo;
678 bfd_map_over_sections (abfd, scan_sections_for_abi, ¶m);
679 if (param.use_memory_banks)
681 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_START_NAME,
682 htab->pinfo.bank_physical,
683 bfd_abs_section_ptr);
684 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_VIRTUAL_NAME,
685 htab->pinfo.bank_virtual,
686 bfd_abs_section_ptr);
687 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_SIZE_NAME,
688 htab->pinfo.bank_size,
689 bfd_abs_section_ptr);
696 m68hc11_elf_get_bank_parameters (struct bfd_link_info *info)
699 struct m68hc11_page_info *pinfo;
700 struct bfd_link_hash_entry *h;
701 struct m68hc11_elf_link_hash_table *htab;
703 htab = m68hc11_elf_hash_table (info);
707 pinfo = & htab->pinfo;
708 if (pinfo->bank_param_initialized)
711 pinfo->bank_virtual = M68HC12_BANK_VIRT;
712 pinfo->bank_mask = M68HC12_BANK_MASK;
713 pinfo->bank_physical = M68HC12_BANK_BASE;
714 pinfo->bank_shift = M68HC12_BANK_SHIFT;
715 pinfo->bank_size = 1 << M68HC12_BANK_SHIFT;
717 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_START_NAME,
719 if (h != (struct bfd_link_hash_entry*) NULL
720 && h->type == bfd_link_hash_defined)
721 pinfo->bank_physical = (h->u.def.value
722 + h->u.def.section->output_section->vma
723 + h->u.def.section->output_offset);
725 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_VIRTUAL_NAME,
727 if (h != (struct bfd_link_hash_entry*) NULL
728 && h->type == bfd_link_hash_defined)
729 pinfo->bank_virtual = (h->u.def.value
730 + h->u.def.section->output_section->vma
731 + h->u.def.section->output_offset);
733 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_SIZE_NAME,
735 if (h != (struct bfd_link_hash_entry*) NULL
736 && h->type == bfd_link_hash_defined)
737 pinfo->bank_size = (h->u.def.value
738 + h->u.def.section->output_section->vma
739 + h->u.def.section->output_offset);
741 pinfo->bank_shift = 0;
742 for (i = pinfo->bank_size; i != 0; i >>= 1)
745 pinfo->bank_mask = (1 << pinfo->bank_shift) - 1;
746 pinfo->bank_physical_end = pinfo->bank_physical + pinfo->bank_size;
747 pinfo->bank_param_initialized = 1;
749 h = bfd_link_hash_lookup (info->hash, "__far_trampoline", FALSE,
751 if (h != (struct bfd_link_hash_entry*) NULL
752 && h->type == bfd_link_hash_defined)
753 pinfo->trampoline_addr = (h->u.def.value
754 + h->u.def.section->output_section->vma
755 + h->u.def.section->output_offset);
758 /* Return 1 if the address is in banked memory.
759 This can be applied to a virtual address and to a physical address. */
761 m68hc11_addr_is_banked (struct m68hc11_page_info *pinfo, bfd_vma addr)
763 if (addr >= pinfo->bank_virtual)
766 if (addr >= pinfo->bank_physical && addr <= pinfo->bank_physical_end)
772 /* Return the physical address seen by the processor, taking
773 into account banked memory. */
775 m68hc11_phys_addr (struct m68hc11_page_info *pinfo, bfd_vma addr)
777 if (addr < pinfo->bank_virtual)
780 /* Map the address to the memory bank. */
781 addr -= pinfo->bank_virtual;
782 addr &= pinfo->bank_mask;
783 addr += pinfo->bank_physical;
787 /* Return the page number corresponding to an address in banked memory. */
789 m68hc11_phys_page (struct m68hc11_page_info *pinfo, bfd_vma addr)
791 if (addr < pinfo->bank_virtual)
794 /* Map the address to the memory bank. */
795 addr -= pinfo->bank_virtual;
796 addr >>= pinfo->bank_shift;
801 /* This function is used for relocs which are only used for relaxing,
802 which the linker should otherwise ignore. */
804 bfd_reloc_status_type
805 m68hc11_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED,
806 arelent *reloc_entry,
807 asymbol *symbol ATTRIBUTE_UNUSED,
808 void *data ATTRIBUTE_UNUSED,
809 asection *input_section,
811 char **error_message ATTRIBUTE_UNUSED)
813 if (output_bfd != NULL)
814 reloc_entry->address += input_section->output_offset;
818 bfd_reloc_status_type
819 m68hc11_elf_special_reloc (bfd *abfd ATTRIBUTE_UNUSED,
820 arelent *reloc_entry,
822 void *data ATTRIBUTE_UNUSED,
823 asection *input_section,
825 char **error_message ATTRIBUTE_UNUSED)
827 if (output_bfd != (bfd *) NULL
828 && (symbol->flags & BSF_SECTION_SYM) == 0
829 && (! reloc_entry->howto->partial_inplace
830 || reloc_entry->addend == 0))
832 reloc_entry->address += input_section->output_offset;
836 if (output_bfd != NULL)
837 return bfd_reloc_continue;
839 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
840 return bfd_reloc_outofrange;
845 /* Look through the relocs for a section during the first phase.
846 Since we don't do .gots or .plts, we just need to consider the
847 virtual table relocs for gc. */
850 elf32_m68hc11_check_relocs (bfd *abfd, struct bfd_link_info *info,
851 asection *sec, const Elf_Internal_Rela *relocs)
853 Elf_Internal_Shdr * symtab_hdr;
854 struct elf_link_hash_entry ** sym_hashes;
855 const Elf_Internal_Rela * rel;
856 const Elf_Internal_Rela * rel_end;
858 if (info->relocatable)
861 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
862 sym_hashes = elf_sym_hashes (abfd);
863 rel_end = relocs + sec->reloc_count;
865 for (rel = relocs; rel < rel_end; rel++)
867 struct elf_link_hash_entry * h;
868 unsigned long r_symndx;
870 r_symndx = ELF32_R_SYM (rel->r_info);
872 if (r_symndx < symtab_hdr->sh_info)
876 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
877 while (h->root.type == bfd_link_hash_indirect
878 || h->root.type == bfd_link_hash_warning)
879 h = (struct elf_link_hash_entry *) h->root.u.i.link;
882 switch (ELF32_R_TYPE (rel->r_info))
884 /* This relocation describes the C++ object vtable hierarchy.
885 Reconstruct it for later use during GC. */
886 case R_M68HC11_GNU_VTINHERIT:
887 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
891 /* This relocation describes which C++ vtable entries are actually
892 used. Record for later use during GC. */
893 case R_M68HC11_GNU_VTENTRY:
894 BFD_ASSERT (h != NULL);
896 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
905 /* Relocate a 68hc11/68hc12 ELF section. */
907 elf32_m68hc11_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
908 struct bfd_link_info *info,
909 bfd *input_bfd, asection *input_section,
910 bfd_byte *contents, Elf_Internal_Rela *relocs,
911 Elf_Internal_Sym *local_syms,
912 asection **local_sections)
914 Elf_Internal_Shdr *symtab_hdr;
915 struct elf_link_hash_entry **sym_hashes;
916 Elf_Internal_Rela *rel, *relend;
917 const char *name = NULL;
918 struct m68hc11_page_info *pinfo;
919 const struct elf_backend_data * const ebd = get_elf_backend_data (input_bfd);
920 struct m68hc11_elf_link_hash_table *htab;
922 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
923 sym_hashes = elf_sym_hashes (input_bfd);
925 htab = m68hc11_elf_hash_table (info);
929 /* Get memory bank parameters. */
930 m68hc11_elf_get_bank_parameters (info);
932 pinfo = & htab->pinfo;
934 relend = relocs + input_section->reloc_count;
936 for (; rel < relend; rel++)
940 reloc_howto_type *howto;
941 unsigned long r_symndx;
942 Elf_Internal_Sym *sym;
944 bfd_vma relocation = 0;
945 bfd_reloc_status_type r = bfd_reloc_undefined;
950 bfd_boolean is_far = FALSE;
951 struct elf_link_hash_entry *h;
954 r_symndx = ELF32_R_SYM (rel->r_info);
955 r_type = ELF32_R_TYPE (rel->r_info);
957 if (r_type == R_M68HC11_GNU_VTENTRY
958 || r_type == R_M68HC11_GNU_VTINHERIT )
961 (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel);
967 if (r_symndx < symtab_hdr->sh_info)
969 sym = local_syms + r_symndx;
970 sec = local_sections[r_symndx];
971 relocation = (sec->output_section->vma
974 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
978 bfd_boolean unresolved_reloc, warned;
980 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
981 r_symndx, symtab_hdr, sym_hashes,
982 h, sec, relocation, unresolved_reloc,
985 is_far = (h && (h->other & STO_M68HC12_FAR));
988 if (sec != NULL && discarded_section (sec))
989 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
990 rel, 1, relend, howto, 0, contents);
992 if (info->relocatable)
994 /* This is a relocatable link. We don't have to change
995 anything, unless the reloc is against a section symbol,
996 in which case we have to adjust according to where the
997 section symbol winds up in the output section. */
998 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
999 rel->r_addend += sec->output_offset;
1004 name = h->root.root.string;
1007 name = (bfd_elf_string_from_elf_section
1008 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1009 if (name == NULL || *name == '\0')
1010 name = bfd_section_name (input_bfd, sec);
1013 if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
1015 struct elf32_m68hc11_stub_hash_entry* stub;
1017 stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
1018 name, FALSE, FALSE);
1021 relocation = stub->stub_offset
1022 + stub->stub_sec->output_section->vma
1023 + stub->stub_sec->output_offset;
1028 /* Do the memory bank mapping. */
1029 phys_addr = m68hc11_phys_addr (pinfo, relocation + rel->r_addend);
1030 phys_page = m68hc11_phys_page (pinfo, relocation + rel->r_addend);
1033 case R_M68HC12_LO8XG:
1034 /* This relocation is specific to XGATE IMM16 calls and will precede
1035 a HI8. tc-m68hc11 only generates them in pairs.
1036 Leave the relocation to the HI8XG step. */
1038 r_type = R_M68HC11_NONE;
1041 case R_M68HC12_HI8XG:
1042 /* This relocation is specific to XGATE IMM16 calls and must follow
1043 a LO8XG. Does not actually check that it was a LO8XG.
1044 Adjusts high and low bytes. */
1045 relocation = phys_addr;
1046 if ((elf_elfheader (input_bfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
1047 && (relocation >= 0x2000))
1048 relocation += 0xc000; /* HARDCODED RAM offset for XGATE. */
1050 /* Fetch 16 bit value including low byte in previous insn. */
1051 val = (bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset) << 8)
1052 | bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset - 2);
1054 /* Add on value to preserve carry, then write zero to high byte. */
1057 /* Write out top byte. */
1058 bfd_put_8 (input_bfd, (relocation >> 8) & 0xff,
1059 (bfd_byte*) contents + rel->r_offset);
1061 /* Write out low byte to previous instruction. */
1062 bfd_put_8 (input_bfd, relocation & 0xff,
1063 (bfd_byte*) contents + rel->r_offset - 2);
1065 /* Mark as relocation completed. */
1067 r_type = R_M68HC11_NONE;
1070 /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1071 assembler directives. %hi does not support carry. */
1074 relocation = phys_addr;
1078 /* Reloc used by 68HC12 call instruction. */
1079 bfd_put_16 (input_bfd, phys_addr,
1080 (bfd_byte*) contents + rel->r_offset);
1081 bfd_put_8 (input_bfd, phys_page,
1082 (bfd_byte*) contents + rel->r_offset + 2);
1084 r_type = R_M68HC11_NONE;
1087 case R_M68HC11_NONE:
1091 case R_M68HC11_LO16:
1092 /* Reloc generated by %addr(expr) gas to obtain the
1093 address as mapped in the memory bank window. */
1094 relocation = phys_addr;
1097 case R_M68HC11_PAGE:
1098 /* Reloc generated by %page(expr) gas to obtain the
1099 page number associated with the address. */
1100 relocation = phys_page;
1104 /* Get virtual address of instruction having the relocation. */
1109 msg = _("Reference to the far symbol `%s' using a wrong "
1110 "relocation may result in incorrect execution");
1111 buf = alloca (strlen (msg) + strlen (name) + 10);
1112 sprintf (buf, msg, name);
1114 (* info->callbacks->warning)
1115 (info, buf, name, input_bfd, NULL, rel->r_offset);
1118 /* Get virtual address of instruction having the relocation. */
1119 insn_addr = input_section->output_section->vma
1120 + input_section->output_offset
1123 insn_page = m68hc11_phys_page (pinfo, insn_addr);
1125 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
1126 && m68hc11_addr_is_banked (pinfo, insn_addr)
1127 && phys_page != insn_page)
1132 msg = _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1133 "as current banked address [%lx:%04lx] (%lx)");
1135 buf = alloca (strlen (msg) + 128);
1136 sprintf (buf, msg, phys_page, phys_addr,
1137 (long) (relocation + rel->r_addend),
1138 insn_page, m68hc11_phys_addr (pinfo, insn_addr),
1139 (long) (insn_addr));
1140 if (!((*info->callbacks->warning)
1141 (info, buf, name, input_bfd, input_section,
1146 if (phys_page != 0 && insn_page == 0)
1151 msg = _("reference to a banked address [%lx:%04lx] in the "
1152 "normal address space at %04lx");
1154 buf = alloca (strlen (msg) + 128);
1155 sprintf (buf, msg, phys_page, phys_addr, insn_addr);
1156 if (!((*info->callbacks->warning)
1157 (info, buf, name, input_bfd, input_section,
1161 relocation = phys_addr;
1165 /* If this is a banked address use the phys_addr so that
1166 we stay in the banked window. */
1167 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend))
1168 relocation = phys_addr;
1172 if (r_type != R_M68HC11_NONE)
1174 if ((r_type == R_M68HC12_PCREL_9) || (r_type == R_M68HC12_PCREL_10))
1175 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1176 contents, rel->r_offset,
1177 relocation - 2, rel->r_addend);
1179 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1180 contents, rel->r_offset,
1181 relocation, rel->r_addend);
1184 if (r != bfd_reloc_ok)
1186 const char * msg = (const char *) 0;
1190 case bfd_reloc_overflow:
1191 if (!((*info->callbacks->reloc_overflow)
1192 (info, NULL, name, howto->name, (bfd_vma) 0,
1193 input_bfd, input_section, rel->r_offset)))
1197 case bfd_reloc_undefined:
1198 if (!((*info->callbacks->undefined_symbol)
1199 (info, name, input_bfd, input_section,
1200 rel->r_offset, TRUE)))
1204 case bfd_reloc_outofrange:
1205 msg = _ ("internal error: out of range error");
1208 case bfd_reloc_notsupported:
1209 msg = _ ("internal error: unsupported relocation error");
1212 case bfd_reloc_dangerous:
1213 msg = _ ("internal error: dangerous error");
1217 msg = _ ("internal error: unknown error");
1221 if (!((*info->callbacks->warning)
1222 (info, msg, name, input_bfd, input_section,
1235 /* Set and control ELF flags in ELF header. */
1238 _bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
1240 BFD_ASSERT (!elf_flags_init (abfd)
1241 || elf_elfheader (abfd)->e_flags == flags);
1243 elf_elfheader (abfd)->e_flags = flags;
1244 elf_flags_init (abfd) = TRUE;
1248 /* Merge backend specific data from an object file to the output
1249 object file when linking. */
1252 _bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
1256 bfd_boolean ok = TRUE;
1258 /* Check if we have the same endianness */
1259 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
1262 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1263 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1266 new_flags = elf_elfheader (ibfd)->e_flags;
1267 elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
1268 old_flags = elf_elfheader (obfd)->e_flags;
1270 if (! elf_flags_init (obfd))
1272 elf_flags_init (obfd) = TRUE;
1273 elf_elfheader (obfd)->e_flags = new_flags;
1274 elf_elfheader (obfd)->e_ident[EI_CLASS]
1275 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
1277 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1278 && bfd_get_arch_info (obfd)->the_default)
1280 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1281 bfd_get_mach (ibfd)))
1288 /* Check ABI compatibility. */
1289 if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
1291 (*_bfd_error_handler)
1292 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1293 "and others for 32-bit integers"), ibfd);
1296 if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
1298 (*_bfd_error_handler)
1299 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1300 "and others for 64-bit double"), ibfd);
1304 /* Processor compatibility. */
1305 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
1307 (*_bfd_error_handler)
1308 (_("%B: linking files compiled for HCS12 with "
1309 "others compiled for HC12"), ibfd);
1312 new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
1313 | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
1315 elf_elfheader (obfd)->e_flags = new_flags;
1317 new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1318 old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1320 /* Warn about any other mismatches */
1321 if (new_flags != old_flags)
1323 (*_bfd_error_handler)
1324 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1325 ibfd, (unsigned long) new_flags, (unsigned long) old_flags);
1331 bfd_set_error (bfd_error_bad_value);
1339 _bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
1341 FILE *file = (FILE *) ptr;
1343 BFD_ASSERT (abfd != NULL && ptr != NULL);
1345 /* Print normal ELF private data. */
1346 _bfd_elf_print_private_bfd_data (abfd, ptr);
1348 /* xgettext:c-format */
1349 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1351 if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
1352 fprintf (file, _("[abi=32-bit int, "));
1354 fprintf (file, _("[abi=16-bit int, "));
1356 if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
1357 fprintf (file, _("64-bit double, "));
1359 fprintf (file, _("32-bit double, "));
1361 if (strcmp (bfd_get_target (abfd), "elf32-m68hc11") == 0)
1362 fprintf (file, _("cpu=HC11]"));
1363 else if (elf_elfheader (abfd)->e_flags & EF_M68HCS12_MACH)
1364 fprintf (file, _("cpu=HCS12]"));
1366 fprintf (file, _("cpu=HC12]"));
1368 if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
1369 fprintf (file, _(" [memory=bank-model]"));
1371 fprintf (file, _(" [memory=flat]"));
1373 if (elf_elfheader (abfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
1374 fprintf (file, _(" [XGATE RAM offsetting]"));
1381 static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
1382 asection *asect, void *arg)
1384 struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
1386 if (asect->vma >= p->pinfo->bank_virtual)
1387 p->use_memory_banks = TRUE;
1390 /* Tweak the OSABI field of the elf header. */
1393 elf32_m68hc11_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
1395 struct m68hc11_scan_param param;
1396 struct m68hc11_elf_link_hash_table *htab;
1398 if (link_info == NULL)
1401 htab = m68hc11_elf_hash_table (link_info);
1405 m68hc11_elf_get_bank_parameters (link_info);
1407 param.use_memory_banks = FALSE;
1408 param.pinfo = & htab->pinfo;
1410 bfd_map_over_sections (abfd, scan_sections_for_abi, ¶m);
1412 if (param.use_memory_banks)
1414 Elf_Internal_Ehdr * i_ehdrp;
1416 i_ehdrp = elf_elfheader (abfd);
1417 i_ehdrp->e_flags |= E_M68HC12_BANKS;