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*, void *);
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;
921 unsigned long e_flags;
923 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
924 sym_hashes = elf_sym_hashes (input_bfd);
925 e_flags = elf_elfheader (input_bfd)->e_flags;
927 htab = m68hc11_elf_hash_table (info);
931 /* Get memory bank parameters. */
932 m68hc11_elf_get_bank_parameters (info);
934 pinfo = & htab->pinfo;
936 relend = relocs + input_section->reloc_count;
938 for (; rel < relend; rel++)
942 reloc_howto_type *howto;
943 unsigned long r_symndx;
944 Elf_Internal_Sym *sym;
946 bfd_vma relocation = 0;
947 bfd_reloc_status_type r = bfd_reloc_undefined;
952 bfd_boolean is_far = FALSE;
953 bfd_boolean is_xgate_symbol = FALSE;
954 bfd_boolean is_section_symbol = FALSE;
955 struct elf_link_hash_entry *h;
958 r_symndx = ELF32_R_SYM (rel->r_info);
959 r_type = ELF32_R_TYPE (rel->r_info);
961 if (r_type == R_M68HC11_GNU_VTENTRY
962 || r_type == R_M68HC11_GNU_VTINHERIT)
965 (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel);
971 if (r_symndx < symtab_hdr->sh_info)
973 sym = local_syms + r_symndx;
974 sec = local_sections[r_symndx];
975 relocation = (sec->output_section->vma
978 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
979 is_xgate_symbol = (sym && (sym->st_target_internal));
980 is_section_symbol = ELF_ST_TYPE (sym->st_info) & STT_SECTION;
984 bfd_boolean unresolved_reloc, warned;
986 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
987 r_symndx, symtab_hdr, sym_hashes,
988 h, sec, relocation, unresolved_reloc,
991 is_far = (h && (h->other & STO_M68HC12_FAR));
992 is_xgate_symbol = (h && (h->target_internal));
995 if (sec != NULL && discarded_section (sec))
996 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
997 rel, 1, relend, howto, 0, contents);
999 if (info->relocatable)
1001 /* This is a relocatable link. We don't have to change
1002 anything, unless the reloc is against a section symbol,
1003 in which case we have to adjust according to where the
1004 section symbol winds up in the output section. */
1005 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1006 rel->r_addend += sec->output_offset;
1011 name = h->root.root.string;
1014 name = (bfd_elf_string_from_elf_section
1015 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1016 if (name == NULL || *name == '\0')
1017 name = bfd_section_name (input_bfd, sec);
1020 if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
1022 struct elf32_m68hc11_stub_hash_entry* stub;
1024 stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
1025 name, FALSE, FALSE);
1028 relocation = stub->stub_offset
1029 + stub->stub_sec->output_section->vma
1030 + stub->stub_sec->output_offset;
1035 /* Do the memory bank mapping. */
1036 phys_addr = m68hc11_phys_addr (pinfo, relocation + rel->r_addend);
1037 phys_page = m68hc11_phys_page (pinfo, relocation + rel->r_addend);
1040 case R_M68HC12_LO8XG:
1041 /* This relocation is specific to XGATE IMM16 calls and will precede
1042 a HI8. tc-m68hc11 only generates them in pairs.
1043 Leave the relocation to the HI8XG step. */
1045 r_type = R_M68HC11_NONE;
1048 case R_M68HC12_HI8XG:
1049 /* This relocation is specific to XGATE IMM16 calls and must follow
1050 a LO8XG. Does not actually check that it was a LO8XG.
1051 Adjusts high and low bytes. */
1052 relocation = phys_addr;
1053 if ((e_flags & E_M68HC11_XGATE_RAMOFFSET)
1054 && (relocation >= 0x2000))
1055 relocation += 0xc000; /* HARDCODED RAM offset for XGATE. */
1057 /* Fetch 16 bit value including low byte in previous insn. */
1058 val = (bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset) << 8)
1059 | bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset - 2);
1061 /* Add on value to preserve carry, then write zero to high byte. */
1064 /* Write out top byte. */
1065 bfd_put_8 (input_bfd, (relocation >> 8) & 0xff,
1066 (bfd_byte*) contents + rel->r_offset);
1068 /* Write out low byte to previous instruction. */
1069 bfd_put_8 (input_bfd, relocation & 0xff,
1070 (bfd_byte*) contents + rel->r_offset - 2);
1072 /* Mark as relocation completed. */
1074 r_type = R_M68HC11_NONE;
1077 /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1078 assembler directives. %hi does not support carry. */
1081 relocation = phys_addr;
1085 /* Reloc used by 68HC12 call instruction. */
1086 bfd_put_16 (input_bfd, phys_addr,
1087 (bfd_byte*) contents + rel->r_offset);
1088 bfd_put_8 (input_bfd, phys_page,
1089 (bfd_byte*) contents + rel->r_offset + 2);
1091 r_type = R_M68HC11_NONE;
1094 case R_M68HC11_NONE:
1098 case R_M68HC11_LO16:
1099 /* Reloc generated by %addr(expr) gas to obtain the
1100 address as mapped in the memory bank window. */
1101 relocation = phys_addr;
1104 case R_M68HC11_PAGE:
1105 /* Reloc generated by %page(expr) gas to obtain the
1106 page number associated with the address. */
1107 relocation = phys_page;
1111 /* Get virtual address of instruction having the relocation. */
1116 msg = _("Reference to the far symbol `%s' using a wrong "
1117 "relocation may result in incorrect execution");
1118 buf = alloca (strlen (msg) + strlen (name) + 10);
1119 sprintf (buf, msg, name);
1121 (* info->callbacks->warning)
1122 (info, buf, name, input_bfd, NULL, rel->r_offset);
1125 /* Get virtual address of instruction having the relocation. */
1126 insn_addr = input_section->output_section->vma
1127 + input_section->output_offset
1130 insn_page = m68hc11_phys_page (pinfo, insn_addr);
1132 /* If we are linking an S12 instruction against an XGATE symbol, we
1133 need to change the offset of the symbol value so that it's correct
1134 from the S12's perspective. */
1135 if (is_xgate_symbol)
1137 /* The ram in the global space is mapped to 0x2000 in the 16-bit
1138 address space for S12 and 0xE000 in the 16-bit address space
1140 if (relocation >= 0xE000)
1142 /* We offset the address by the difference
1143 between these two mappings. */
1144 relocation -= 0xC000;
1152 msg = _("XGATE address (%lx) is not within shared RAM"
1153 "(0xE000-0xFFFF), therefore you must manually offset "
1154 "the address, and possibly manage the page, in your "
1156 buf = alloca (strlen (msg) + 128);
1157 sprintf (buf, msg, phys_addr);
1158 if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
1159 input_section, insn_addr)))
1165 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
1166 && m68hc11_addr_is_banked (pinfo, insn_addr)
1167 && phys_page != insn_page && !(e_flags & E_M68HC11_NO_BANK_WARNING))
1172 msg = _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1173 "as current banked address [%lx:%04lx] (%lx)");
1175 buf = alloca (strlen (msg) + 128);
1176 sprintf (buf, msg, phys_page, phys_addr,
1177 (long) (relocation + rel->r_addend),
1178 insn_page, m68hc11_phys_addr (pinfo, insn_addr),
1179 (long) (insn_addr));
1180 if (!((*info->callbacks->warning)
1181 (info, buf, name, input_bfd, input_section,
1187 if (phys_page != 0 && insn_page == 0)
1192 msg = _("reference to a banked address [%lx:%04lx] in the "
1193 "normal address space at %04lx");
1195 buf = alloca (strlen (msg) + 128);
1196 sprintf (buf, msg, phys_page, phys_addr, insn_addr);
1197 if (!((*info->callbacks->warning)
1198 (info, buf, name, input_bfd, input_section,
1202 relocation = phys_addr;
1206 /* If this is a banked address use the phys_addr so that
1207 we stay in the banked window. */
1208 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend))
1209 relocation = phys_addr;
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 if (!strcmp (howto->name, "R_XGATE_IMM8_LO")
1217 || !strcmp (howto->name, "R_XGATE_IMM8_HI"))
1219 /* We can only offset S12 addresses that lie within the non-paged
1221 if (!is_xgate_symbol && !is_section_symbol)
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 if (relocation >= 0x2000 && relocation < 0x4000)
1227 /* We offset the address by the difference
1228 between these two mappings. */
1229 relocation += 0xC000;
1235 /* Get virtual address of instruction having the relocation. */
1236 insn_addr = input_section->output_section->vma
1237 + input_section->output_offset + rel->r_offset;
1239 msg = _("S12 address (%lx) is not within shared RAM"
1240 "(0x2000-0x4000), therefore you must manually "
1241 "offset the address in your code");
1242 buf = alloca (strlen (msg) + 128);
1243 sprintf (buf, msg, phys_addr);
1244 if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
1245 input_section, insn_addr)))
1252 if (r_type != R_M68HC11_NONE)
1254 if ((r_type == R_M68HC12_PCREL_9) || (r_type == R_M68HC12_PCREL_10))
1255 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1256 contents, rel->r_offset,
1257 relocation - 2, rel->r_addend);
1259 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1260 contents, rel->r_offset,
1261 relocation, rel->r_addend);
1264 if (r != bfd_reloc_ok)
1266 const char * msg = (const char *) 0;
1270 case bfd_reloc_overflow:
1271 if (!((*info->callbacks->reloc_overflow)
1272 (info, NULL, name, howto->name, (bfd_vma) 0,
1273 input_bfd, input_section, rel->r_offset)))
1277 case bfd_reloc_undefined:
1278 if (!((*info->callbacks->undefined_symbol)
1279 (info, name, input_bfd, input_section,
1280 rel->r_offset, TRUE)))
1284 case bfd_reloc_outofrange:
1285 msg = _ ("internal error: out of range error");
1288 case bfd_reloc_notsupported:
1289 msg = _ ("internal error: unsupported relocation error");
1292 case bfd_reloc_dangerous:
1293 msg = _ ("internal error: dangerous error");
1297 msg = _ ("internal error: unknown error");
1301 if (!((*info->callbacks->warning)
1302 (info, msg, name, input_bfd, input_section,
1315 /* Set and control ELF flags in ELF header. */
1318 _bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
1320 BFD_ASSERT (!elf_flags_init (abfd)
1321 || elf_elfheader (abfd)->e_flags == flags);
1323 elf_elfheader (abfd)->e_flags = flags;
1324 elf_flags_init (abfd) = TRUE;
1328 /* Merge backend specific data from an object file to the output
1329 object file when linking. */
1332 _bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
1336 bfd_boolean ok = TRUE;
1338 /* Check if we have the same endianness */
1339 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
1342 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1343 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1346 new_flags = elf_elfheader (ibfd)->e_flags;
1347 elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
1348 old_flags = elf_elfheader (obfd)->e_flags;
1350 if (! elf_flags_init (obfd))
1352 elf_flags_init (obfd) = TRUE;
1353 elf_elfheader (obfd)->e_flags = new_flags;
1354 elf_elfheader (obfd)->e_ident[EI_CLASS]
1355 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
1357 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1358 && bfd_get_arch_info (obfd)->the_default)
1360 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1361 bfd_get_mach (ibfd)))
1368 /* Check ABI compatibility. */
1369 if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
1371 (*_bfd_error_handler)
1372 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1373 "and others for 32-bit integers"), ibfd);
1376 if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
1378 (*_bfd_error_handler)
1379 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1380 "and others for 64-bit double"), ibfd);
1384 /* Processor compatibility. */
1385 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
1387 (*_bfd_error_handler)
1388 (_("%B: linking files compiled for HCS12 with "
1389 "others compiled for HC12"), ibfd);
1392 new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
1393 | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
1395 elf_elfheader (obfd)->e_flags = new_flags;
1397 new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1398 old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1400 /* Warn about any other mismatches */
1401 if (new_flags != old_flags)
1403 (*_bfd_error_handler)
1404 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1405 ibfd, (unsigned long) new_flags, (unsigned long) old_flags);
1411 bfd_set_error (bfd_error_bad_value);
1419 _bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
1421 FILE *file = (FILE *) ptr;
1423 BFD_ASSERT (abfd != NULL && ptr != NULL);
1425 /* Print normal ELF private data. */
1426 _bfd_elf_print_private_bfd_data (abfd, ptr);
1428 /* xgettext:c-format */
1429 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1431 if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
1432 fprintf (file, _("[abi=32-bit int, "));
1434 fprintf (file, _("[abi=16-bit int, "));
1436 if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
1437 fprintf (file, _("64-bit double, "));
1439 fprintf (file, _("32-bit double, "));
1441 if (strcmp (bfd_get_target (abfd), "elf32-m68hc11") == 0)
1442 fprintf (file, _("cpu=HC11]"));
1443 else if (elf_elfheader (abfd)->e_flags & EF_M68HCS12_MACH)
1444 fprintf (file, _("cpu=HCS12]"));
1446 fprintf (file, _("cpu=HC12]"));
1448 if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
1449 fprintf (file, _(" [memory=bank-model]"));
1451 fprintf (file, _(" [memory=flat]"));
1453 if (elf_elfheader (abfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
1454 fprintf (file, _(" [XGATE RAM offsetting]"));
1461 static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
1462 asection *asect, void *arg)
1464 struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
1466 if (asect->vma >= p->pinfo->bank_virtual)
1467 p->use_memory_banks = TRUE;
1470 /* Tweak the OSABI field of the elf header. */
1473 elf32_m68hc11_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
1475 struct m68hc11_scan_param param;
1476 struct m68hc11_elf_link_hash_table *htab;
1478 if (link_info == NULL)
1481 htab = m68hc11_elf_hash_table (link_info);
1485 m68hc11_elf_get_bank_parameters (link_info);
1487 param.use_memory_banks = FALSE;
1488 param.pinfo = & htab->pinfo;
1490 bfd_map_over_sections (abfd, scan_sections_for_abi, ¶m);
1492 if (param.use_memory_banks)
1494 Elf_Internal_Ehdr * i_ehdrp;
1496 i_ehdrp = elf_elfheader (abfd);
1497 i_ehdrp->e_flags |= E_M68HC12_BANKS;