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_zmalloc (amt);
71 if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
74 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
75 _bfd_elf_link_hash_newfunc,
76 sizeof (struct elf_link_hash_entry),
83 /* Init the stub hash table too. */
84 amt = sizeof (struct bfd_hash_table);
85 ret->stub_hash_table = (struct bfd_hash_table*) bfd_malloc (amt);
86 if (ret->stub_hash_table == NULL)
91 if (!bfd_hash_table_init (ret->stub_hash_table, stub_hash_newfunc,
92 sizeof (struct elf32_m68hc11_stub_hash_entry)))
98 /* Free the derived linker hash table. */
101 m68hc11_elf_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
103 struct m68hc11_elf_link_hash_table *ret
104 = (struct m68hc11_elf_link_hash_table *) hash;
106 bfd_hash_table_free (ret->stub_hash_table);
107 free (ret->stub_hash_table);
108 _bfd_elf_link_hash_table_free (hash);
111 /* Assorted hash table functions. */
113 /* Initialize an entry in the stub hash table. */
115 static struct bfd_hash_entry *
116 stub_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
119 /* Allocate the structure if it has not already been allocated by a
123 entry = bfd_hash_allocate (table,
124 sizeof (struct elf32_m68hc11_stub_hash_entry));
129 /* Call the allocation method of the superclass. */
130 entry = bfd_hash_newfunc (entry, table, string);
133 struct elf32_m68hc11_stub_hash_entry *eh;
135 /* Initialize the local fields. */
136 eh = (struct elf32_m68hc11_stub_hash_entry *) entry;
139 eh->target_value = 0;
140 eh->target_section = NULL;
146 /* Add a new stub entry to the stub hash. Not all fields of the new
147 stub entry are initialised. */
149 static struct elf32_m68hc11_stub_hash_entry *
150 m68hc12_add_stub (const char *stub_name, asection *section,
151 struct m68hc11_elf_link_hash_table *htab)
153 struct elf32_m68hc11_stub_hash_entry *stub_entry;
155 /* Enter this entry into the linker stub hash table. */
156 stub_entry = m68hc12_stub_hash_lookup (htab->stub_hash_table, stub_name,
158 if (stub_entry == NULL)
160 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
161 section->owner, stub_name);
165 if (htab->stub_section == 0)
167 htab->stub_section = (*htab->add_stub_section) (".tramp",
168 htab->tramp_section);
171 stub_entry->stub_sec = htab->stub_section;
172 stub_entry->stub_offset = 0;
176 /* Hook called by the linker routine which adds symbols from an object
177 file. We use it for identify far symbols and force a loading of
178 the trampoline handler. */
181 elf32_m68hc11_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
182 Elf_Internal_Sym *sym,
183 const char **namep ATTRIBUTE_UNUSED,
184 flagword *flagsp ATTRIBUTE_UNUSED,
185 asection **secp ATTRIBUTE_UNUSED,
186 bfd_vma *valp ATTRIBUTE_UNUSED)
188 if (sym->st_other & STO_M68HC12_FAR)
190 struct elf_link_hash_entry *h;
192 h = (struct elf_link_hash_entry *)
193 bfd_link_hash_lookup (info->hash, "__far_trampoline",
194 FALSE, FALSE, FALSE);
197 struct bfd_link_hash_entry* entry = NULL;
199 _bfd_generic_link_add_one_symbol (info, abfd,
203 (bfd_vma) 0, (const char*) NULL,
204 FALSE, FALSE, &entry);
211 /* Merge non-visibility st_other attributes, STO_M68HC12_FAR and
212 STO_M68HC12_INTERRUPT. */
215 elf32_m68hc11_merge_symbol_attribute (struct elf_link_hash_entry *h,
216 const Elf_Internal_Sym *isym,
217 bfd_boolean definition,
218 bfd_boolean dynamic ATTRIBUTE_UNUSED)
221 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
222 | ELF_ST_VISIBILITY (h->other));
225 /* External entry points for sizing and building linker stubs. */
227 /* Set up various things so that we can make a list of input sections
228 for each output section included in the link. Returns -1 on error,
229 0 when no stubs will be needed, and 1 on success. */
232 elf32_m68hc11_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
235 unsigned int bfd_count;
236 int top_id, top_index;
238 asection **input_list, **list;
240 asection *text_section;
241 struct m68hc11_elf_link_hash_table *htab;
243 htab = m68hc11_elf_hash_table (info);
247 if (bfd_get_flavour (info->output_bfd) != bfd_target_elf_flavour)
250 /* Count the number of input BFDs and find the top input section id.
251 Also search for an existing ".tramp" section so that we know
252 where generated trampolines must go. Default to ".text" if we
254 htab->tramp_section = 0;
256 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
258 input_bfd = input_bfd->link_next)
261 for (section = input_bfd->sections;
263 section = section->next)
265 const char* name = bfd_get_section_name (input_bfd, section);
267 if (!strcmp (name, ".tramp"))
268 htab->tramp_section = section;
270 if (!strcmp (name, ".text"))
271 text_section = section;
273 if (top_id < section->id)
274 top_id = section->id;
277 htab->bfd_count = bfd_count;
278 if (htab->tramp_section == 0)
279 htab->tramp_section = text_section;
281 /* We can't use output_bfd->section_count here to find the top output
282 section index as some sections may have been removed, and
283 strip_excluded_output_sections doesn't renumber the indices. */
284 for (section = output_bfd->sections, top_index = 0;
286 section = section->next)
288 if (top_index < section->index)
289 top_index = section->index;
292 htab->top_index = top_index;
293 amt = sizeof (asection *) * (top_index + 1);
294 input_list = (asection **) bfd_malloc (amt);
295 htab->input_list = input_list;
296 if (input_list == NULL)
299 /* For sections we aren't interested in, mark their entries with a
300 value we can check later. */
301 list = input_list + top_index;
303 *list = bfd_abs_section_ptr;
304 while (list-- != input_list);
306 for (section = output_bfd->sections;
308 section = section->next)
310 if ((section->flags & SEC_CODE) != 0)
311 input_list[section->index] = NULL;
317 /* Determine and set the size of the stub section for a final link.
319 The basic idea here is to examine all the relocations looking for
320 PC-relative calls to a target that is unreachable with a "bl"
324 elf32_m68hc11_size_stubs (bfd *output_bfd, bfd *stub_bfd,
325 struct bfd_link_info *info,
326 asection * (*add_stub_section) (const char*, asection*))
330 Elf_Internal_Sym *local_syms, **all_local_syms;
331 unsigned int bfd_indx, bfd_count;
334 struct m68hc11_elf_link_hash_table *htab = m68hc11_elf_hash_table (info);
339 /* Stash our params away. */
340 htab->stub_bfd = stub_bfd;
341 htab->add_stub_section = add_stub_section;
343 /* Count the number of input BFDs and find the top input section id. */
344 for (input_bfd = info->input_bfds, bfd_count = 0;
346 input_bfd = input_bfd->link_next)
349 /* We want to read in symbol extension records only once. To do this
350 we need to read in the local symbols in parallel and save them for
351 later use; so hold pointers to the local symbols in an array. */
352 amt = sizeof (Elf_Internal_Sym *) * bfd_count;
353 all_local_syms = (Elf_Internal_Sym **) bfd_zmalloc (amt);
354 if (all_local_syms == NULL)
357 /* Walk over all the input BFDs, swapping in local symbols. */
358 for (input_bfd = info->input_bfds, bfd_indx = 0;
360 input_bfd = input_bfd->link_next, bfd_indx++)
362 Elf_Internal_Shdr *symtab_hdr;
364 /* We'll need the symbol table in a second. */
365 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
366 if (symtab_hdr->sh_info == 0)
369 /* We need an array of the local symbols attached to the input bfd. */
370 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
371 if (local_syms == NULL)
373 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
374 symtab_hdr->sh_info, 0,
376 /* Cache them for elf_link_input_bfd. */
377 symtab_hdr->contents = (unsigned char *) local_syms;
379 if (local_syms == NULL)
381 free (all_local_syms);
385 all_local_syms[bfd_indx] = local_syms;
388 for (input_bfd = info->input_bfds, bfd_indx = 0;
390 input_bfd = input_bfd->link_next, bfd_indx++)
392 Elf_Internal_Shdr *symtab_hdr;
393 struct elf_link_hash_entry ** sym_hashes;
395 sym_hashes = elf_sym_hashes (input_bfd);
397 /* We'll need the symbol table in a second. */
398 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
399 if (symtab_hdr->sh_info == 0)
402 local_syms = all_local_syms[bfd_indx];
404 /* Walk over each section attached to the input bfd. */
405 for (section = input_bfd->sections;
407 section = section->next)
409 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
411 /* If there aren't any relocs, then there's nothing more
413 if ((section->flags & SEC_RELOC) == 0
414 || section->reloc_count == 0)
417 /* If this section is a link-once section that will be
418 discarded, then don't create any stubs. */
419 if (section->output_section == NULL
420 || section->output_section->owner != output_bfd)
423 /* Get the relocs. */
425 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
426 (Elf_Internal_Rela *) NULL,
428 if (internal_relocs == NULL)
429 goto error_ret_free_local;
431 /* Now examine each relocation. */
432 irela = internal_relocs;
433 irelaend = irela + section->reloc_count;
434 for (; irela < irelaend; irela++)
436 unsigned int r_type, r_indx;
437 struct elf32_m68hc11_stub_hash_entry *stub_entry;
440 struct elf_link_hash_entry *hash;
441 const char *stub_name;
442 Elf_Internal_Sym *sym;
444 r_type = ELF32_R_TYPE (irela->r_info);
446 /* Only look at 16-bit relocs. */
447 if (r_type != (unsigned int) R_M68HC11_16)
450 /* Now determine the call target, its name, value,
452 r_indx = ELF32_R_SYM (irela->r_info);
453 if (r_indx < symtab_hdr->sh_info)
455 /* It's a local symbol. */
456 Elf_Internal_Shdr *hdr;
459 sym = local_syms + r_indx;
460 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
464 if (sym->st_shndx >= elf_numsections (input_bfd))
468 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
469 sym_sec = hdr->bfd_section;
471 stub_name = (bfd_elf_string_from_elf_section
472 (input_bfd, symtab_hdr->sh_link,
474 sym_value = sym->st_value;
479 /* It's an external symbol. */
482 e_indx = r_indx - symtab_hdr->sh_info;
483 hash = (struct elf_link_hash_entry *)
484 (sym_hashes[e_indx]);
486 while (hash->root.type == bfd_link_hash_indirect
487 || hash->root.type == bfd_link_hash_warning)
488 hash = ((struct elf_link_hash_entry *)
489 hash->root.u.i.link);
491 if (hash->root.type == bfd_link_hash_defined
492 || hash->root.type == bfd_link_hash_defweak
493 || hash->root.type == bfd_link_hash_new)
495 if (!(hash->other & STO_M68HC12_FAR))
498 else if (hash->root.type == bfd_link_hash_undefweak)
502 else if (hash->root.type == bfd_link_hash_undefined)
508 bfd_set_error (bfd_error_bad_value);
509 goto error_ret_free_internal;
511 sym_sec = hash->root.u.def.section;
512 sym_value = hash->root.u.def.value;
513 stub_name = hash->root.root.string;
517 goto error_ret_free_internal;
519 stub_entry = m68hc12_stub_hash_lookup
520 (htab->stub_hash_table,
523 if (stub_entry == NULL)
525 if (add_stub_section == 0)
528 stub_entry = m68hc12_add_stub (stub_name, section, htab);
529 if (stub_entry == NULL)
531 error_ret_free_internal:
532 if (elf_section_data (section)->relocs == NULL)
533 free (internal_relocs);
534 goto error_ret_free_local;
538 stub_entry->target_value = sym_value;
539 stub_entry->target_section = sym_sec;
542 /* We're done with the internal relocs, free them. */
543 if (elf_section_data (section)->relocs == NULL)
544 free (internal_relocs);
548 if (add_stub_section)
550 /* OK, we've added some stubs. Find out the new size of the
552 for (stub_sec = htab->stub_bfd->sections;
554 stub_sec = stub_sec->next)
559 bfd_hash_traverse (htab->stub_hash_table, htab->size_one_stub, htab);
561 free (all_local_syms);
564 error_ret_free_local:
565 free (all_local_syms);
569 /* Export the trampoline addresses in the symbol table. */
571 m68hc11_elf_export_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
573 struct bfd_link_info *info;
574 struct m68hc11_elf_link_hash_table *htab;
575 struct elf32_m68hc11_stub_hash_entry *stub_entry;
579 info = (struct bfd_link_info *) in_arg;
580 htab = m68hc11_elf_hash_table (info);
584 /* Massage our args to the form they really have. */
585 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
587 /* Generate the trampoline according to HC11 or HC12. */
588 result = (* htab->build_one_stub) (gen_entry, in_arg);
590 /* Make a printable name that does not conflict with the real function. */
591 name = alloca (strlen (stub_entry->root.string) + 16);
592 sprintf (name, "tramp.%s", stub_entry->root.string);
594 /* Export the symbol for debugging/disassembling. */
595 m68hc11_elf_set_symbol (htab->stub_bfd, info, name,
596 stub_entry->stub_offset,
597 stub_entry->stub_sec);
601 /* Export a symbol or set its value and section. */
603 m68hc11_elf_set_symbol (bfd *abfd, struct bfd_link_info *info,
604 const char *name, bfd_vma value, asection *sec)
606 struct elf_link_hash_entry *h;
608 h = (struct elf_link_hash_entry *)
609 bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
612 _bfd_generic_link_add_one_symbol (info, abfd,
622 h->root.type = bfd_link_hash_defined;
623 h->root.u.def.value = value;
624 h->root.u.def.section = sec;
629 /* Build all the stubs associated with the current output file. The
630 stubs are kept in a hash table attached to the main linker hash
631 table. This function is called via m68hc12elf_finish in the
635 elf32_m68hc11_build_stubs (bfd *abfd, struct bfd_link_info *info)
638 struct bfd_hash_table *table;
639 struct m68hc11_elf_link_hash_table *htab;
640 struct m68hc11_scan_param param;
642 m68hc11_elf_get_bank_parameters (info);
643 htab = m68hc11_elf_hash_table (info);
647 for (stub_sec = htab->stub_bfd->sections;
649 stub_sec = stub_sec->next)
653 /* Allocate memory to hold the linker stubs. */
654 size = stub_sec->size;
655 stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
656 if (stub_sec->contents == NULL && size != 0)
661 /* Build the stubs as directed by the stub hash table. */
662 table = htab->stub_hash_table;
663 bfd_hash_traverse (table, m68hc11_elf_export_one_stub, info);
665 /* Scan the output sections to see if we use the memory banks.
666 If so, export the symbols that define how the memory banks
667 are mapped. This is used by gdb and the simulator to obtain
668 the information. It can be used by programs to burn the eprom
669 at the good addresses. */
670 param.use_memory_banks = FALSE;
671 param.pinfo = &htab->pinfo;
672 bfd_map_over_sections (abfd, scan_sections_for_abi, ¶m);
673 if (param.use_memory_banks)
675 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_START_NAME,
676 htab->pinfo.bank_physical,
677 bfd_abs_section_ptr);
678 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_VIRTUAL_NAME,
679 htab->pinfo.bank_virtual,
680 bfd_abs_section_ptr);
681 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_SIZE_NAME,
682 htab->pinfo.bank_size,
683 bfd_abs_section_ptr);
690 m68hc11_elf_get_bank_parameters (struct bfd_link_info *info)
693 struct m68hc11_page_info *pinfo;
694 struct bfd_link_hash_entry *h;
695 struct m68hc11_elf_link_hash_table *htab;
697 htab = m68hc11_elf_hash_table (info);
701 pinfo = & htab->pinfo;
702 if (pinfo->bank_param_initialized)
705 pinfo->bank_virtual = M68HC12_BANK_VIRT;
706 pinfo->bank_mask = M68HC12_BANK_MASK;
707 pinfo->bank_physical = M68HC12_BANK_BASE;
708 pinfo->bank_shift = M68HC12_BANK_SHIFT;
709 pinfo->bank_size = 1 << M68HC12_BANK_SHIFT;
711 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_START_NAME,
713 if (h != (struct bfd_link_hash_entry*) NULL
714 && h->type == bfd_link_hash_defined)
715 pinfo->bank_physical = (h->u.def.value
716 + h->u.def.section->output_section->vma
717 + h->u.def.section->output_offset);
719 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_VIRTUAL_NAME,
721 if (h != (struct bfd_link_hash_entry*) NULL
722 && h->type == bfd_link_hash_defined)
723 pinfo->bank_virtual = (h->u.def.value
724 + h->u.def.section->output_section->vma
725 + h->u.def.section->output_offset);
727 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_SIZE_NAME,
729 if (h != (struct bfd_link_hash_entry*) NULL
730 && h->type == bfd_link_hash_defined)
731 pinfo->bank_size = (h->u.def.value
732 + h->u.def.section->output_section->vma
733 + h->u.def.section->output_offset);
735 pinfo->bank_shift = 0;
736 for (i = pinfo->bank_size; i != 0; i >>= 1)
739 pinfo->bank_mask = (1 << pinfo->bank_shift) - 1;
740 pinfo->bank_physical_end = pinfo->bank_physical + pinfo->bank_size;
741 pinfo->bank_param_initialized = 1;
743 h = bfd_link_hash_lookup (info->hash, "__far_trampoline", FALSE,
745 if (h != (struct bfd_link_hash_entry*) NULL
746 && h->type == bfd_link_hash_defined)
747 pinfo->trampoline_addr = (h->u.def.value
748 + h->u.def.section->output_section->vma
749 + h->u.def.section->output_offset);
752 /* Return 1 if the address is in banked memory.
753 This can be applied to a virtual address and to a physical address. */
755 m68hc11_addr_is_banked (struct m68hc11_page_info *pinfo, bfd_vma addr)
757 if (addr >= pinfo->bank_virtual)
760 if (addr >= pinfo->bank_physical && addr <= pinfo->bank_physical_end)
766 /* Return the physical address seen by the processor, taking
767 into account banked memory. */
769 m68hc11_phys_addr (struct m68hc11_page_info *pinfo, bfd_vma addr)
771 if (addr < pinfo->bank_virtual)
774 /* Map the address to the memory bank. */
775 addr -= pinfo->bank_virtual;
776 addr &= pinfo->bank_mask;
777 addr += pinfo->bank_physical;
781 /* Return the page number corresponding to an address in banked memory. */
783 m68hc11_phys_page (struct m68hc11_page_info *pinfo, bfd_vma addr)
785 if (addr < pinfo->bank_virtual)
788 /* Map the address to the memory bank. */
789 addr -= pinfo->bank_virtual;
790 addr >>= pinfo->bank_shift;
795 /* This function is used for relocs which are only used for relaxing,
796 which the linker should otherwise ignore. */
798 bfd_reloc_status_type
799 m68hc11_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED,
800 arelent *reloc_entry,
801 asymbol *symbol ATTRIBUTE_UNUSED,
802 void *data ATTRIBUTE_UNUSED,
803 asection *input_section,
805 char **error_message ATTRIBUTE_UNUSED)
807 if (output_bfd != NULL)
808 reloc_entry->address += input_section->output_offset;
812 bfd_reloc_status_type
813 m68hc11_elf_special_reloc (bfd *abfd ATTRIBUTE_UNUSED,
814 arelent *reloc_entry,
816 void *data ATTRIBUTE_UNUSED,
817 asection *input_section,
819 char **error_message ATTRIBUTE_UNUSED)
821 if (output_bfd != (bfd *) NULL
822 && (symbol->flags & BSF_SECTION_SYM) == 0
823 && (! reloc_entry->howto->partial_inplace
824 || reloc_entry->addend == 0))
826 reloc_entry->address += input_section->output_offset;
830 if (output_bfd != NULL)
831 return bfd_reloc_continue;
833 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
834 return bfd_reloc_outofrange;
839 /* Look through the relocs for a section during the first phase.
840 Since we don't do .gots or .plts, we just need to consider the
841 virtual table relocs for gc. */
844 elf32_m68hc11_check_relocs (bfd *abfd, struct bfd_link_info *info,
845 asection *sec, const Elf_Internal_Rela *relocs)
847 Elf_Internal_Shdr * symtab_hdr;
848 struct elf_link_hash_entry ** sym_hashes;
849 const Elf_Internal_Rela * rel;
850 const Elf_Internal_Rela * rel_end;
852 if (info->relocatable)
855 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
856 sym_hashes = elf_sym_hashes (abfd);
857 rel_end = relocs + sec->reloc_count;
859 for (rel = relocs; rel < rel_end; rel++)
861 struct elf_link_hash_entry * h;
862 unsigned long r_symndx;
864 r_symndx = ELF32_R_SYM (rel->r_info);
866 if (r_symndx < symtab_hdr->sh_info)
870 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
871 while (h->root.type == bfd_link_hash_indirect
872 || h->root.type == bfd_link_hash_warning)
873 h = (struct elf_link_hash_entry *) h->root.u.i.link;
875 /* PR15323, ref flags aren't set for references in the same
877 h->root.non_ir_ref = 1;
880 switch (ELF32_R_TYPE (rel->r_info))
882 /* This relocation describes the C++ object vtable hierarchy.
883 Reconstruct it for later use during GC. */
884 case R_M68HC11_GNU_VTINHERIT:
885 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
889 /* This relocation describes which C++ vtable entries are actually
890 used. Record for later use during GC. */
891 case R_M68HC11_GNU_VTENTRY:
892 BFD_ASSERT (h != NULL);
894 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
903 /* Relocate a 68hc11/68hc12 ELF section. */
905 elf32_m68hc11_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
906 struct bfd_link_info *info,
907 bfd *input_bfd, asection *input_section,
908 bfd_byte *contents, Elf_Internal_Rela *relocs,
909 Elf_Internal_Sym *local_syms,
910 asection **local_sections)
912 Elf_Internal_Shdr *symtab_hdr;
913 struct elf_link_hash_entry **sym_hashes;
914 Elf_Internal_Rela *rel, *relend;
915 const char *name = NULL;
916 struct m68hc11_page_info *pinfo;
917 const struct elf_backend_data * const ebd = get_elf_backend_data (input_bfd);
918 struct m68hc11_elf_link_hash_table *htab;
919 unsigned long e_flags;
921 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
922 sym_hashes = elf_sym_hashes (input_bfd);
923 e_flags = elf_elfheader (input_bfd)->e_flags;
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 bfd_boolean is_xgate_symbol = FALSE;
952 bfd_boolean is_section_symbol = FALSE;
953 struct elf_link_hash_entry *h;
956 r_symndx = ELF32_R_SYM (rel->r_info);
957 r_type = ELF32_R_TYPE (rel->r_info);
959 if (r_type == R_M68HC11_GNU_VTENTRY
960 || r_type == R_M68HC11_GNU_VTINHERIT)
963 (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel);
969 if (r_symndx < symtab_hdr->sh_info)
971 sym = local_syms + r_symndx;
972 sec = local_sections[r_symndx];
973 relocation = (sec->output_section->vma
976 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
977 is_xgate_symbol = (sym && (sym->st_target_internal));
978 is_section_symbol = ELF_ST_TYPE (sym->st_info) & STT_SECTION;
982 bfd_boolean unresolved_reloc, warned;
984 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
985 r_symndx, symtab_hdr, sym_hashes,
986 h, sec, relocation, unresolved_reloc,
989 is_far = (h && (h->other & STO_M68HC12_FAR));
990 is_xgate_symbol = (h && (h->target_internal));
993 if (sec != NULL && discarded_section (sec))
994 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
995 rel, 1, relend, howto, 0, contents);
997 if (info->relocatable)
999 /* This is a relocatable link. We don't have to change
1000 anything, unless the reloc is against a section symbol,
1001 in which case we have to adjust according to where the
1002 section symbol winds up in the output section. */
1003 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1004 rel->r_addend += sec->output_offset;
1009 name = h->root.root.string;
1012 name = (bfd_elf_string_from_elf_section
1013 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1014 if (name == NULL || *name == '\0')
1015 name = bfd_section_name (input_bfd, sec);
1018 if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
1020 struct elf32_m68hc11_stub_hash_entry* stub;
1022 stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
1023 name, FALSE, FALSE);
1026 relocation = stub->stub_offset
1027 + stub->stub_sec->output_section->vma
1028 + stub->stub_sec->output_offset;
1033 /* Do the memory bank mapping. */
1034 phys_addr = m68hc11_phys_addr (pinfo, relocation + rel->r_addend);
1035 phys_page = m68hc11_phys_page (pinfo, relocation + rel->r_addend);
1038 case R_M68HC12_LO8XG:
1039 /* This relocation is specific to XGATE IMM16 calls and will precede
1040 a HI8. tc-m68hc11 only generates them in pairs.
1041 Leave the relocation to the HI8XG step. */
1043 r_type = R_M68HC11_NONE;
1046 case R_M68HC12_HI8XG:
1047 /* This relocation is specific to XGATE IMM16 calls and must follow
1048 a LO8XG. Does not actually check that it was a LO8XG.
1049 Adjusts high and low bytes. */
1050 relocation = phys_addr;
1051 if ((e_flags & E_M68HC11_XGATE_RAMOFFSET)
1052 && (relocation >= 0x2000))
1053 relocation += 0xc000; /* HARDCODED RAM offset for XGATE. */
1055 /* Fetch 16 bit value including low byte in previous insn. */
1056 val = (bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset) << 8)
1057 | bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset - 2);
1059 /* Add on value to preserve carry, then write zero to high byte. */
1062 /* Write out top byte. */
1063 bfd_put_8 (input_bfd, (relocation >> 8) & 0xff,
1064 (bfd_byte*) contents + rel->r_offset);
1066 /* Write out low byte to previous instruction. */
1067 bfd_put_8 (input_bfd, relocation & 0xff,
1068 (bfd_byte*) contents + rel->r_offset - 2);
1070 /* Mark as relocation completed. */
1072 r_type = R_M68HC11_NONE;
1075 /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1076 assembler directives. %hi does not support carry. */
1079 relocation = phys_addr;
1083 /* Reloc used by 68HC12 call instruction. */
1084 bfd_put_16 (input_bfd, phys_addr,
1085 (bfd_byte*) contents + rel->r_offset);
1086 bfd_put_8 (input_bfd, phys_page,
1087 (bfd_byte*) contents + rel->r_offset + 2);
1089 r_type = R_M68HC11_NONE;
1092 case R_M68HC11_NONE:
1096 case R_M68HC11_LO16:
1097 /* Reloc generated by %addr(expr) gas to obtain the
1098 address as mapped in the memory bank window. */
1099 relocation = phys_addr;
1102 case R_M68HC11_PAGE:
1103 /* Reloc generated by %page(expr) gas to obtain the
1104 page number associated with the address. */
1105 relocation = phys_page;
1109 /* Get virtual address of instruction having the relocation. */
1114 msg = _("Reference to the far symbol `%s' using a wrong "
1115 "relocation may result in incorrect execution");
1116 buf = alloca (strlen (msg) + strlen (name) + 10);
1117 sprintf (buf, msg, name);
1119 (* info->callbacks->warning)
1120 (info, buf, name, input_bfd, NULL, rel->r_offset);
1123 /* Get virtual address of instruction having the relocation. */
1124 insn_addr = input_section->output_section->vma
1125 + input_section->output_offset
1128 insn_page = m68hc11_phys_page (pinfo, insn_addr);
1130 /* If we are linking an S12 instruction against an XGATE symbol, we
1131 need to change the offset of the symbol value so that it's correct
1132 from the S12's perspective. */
1133 if (is_xgate_symbol)
1135 /* The ram in the global space is mapped to 0x2000 in the 16-bit
1136 address space for S12 and 0xE000 in the 16-bit address space
1138 if (relocation >= 0xE000)
1140 /* We offset the address by the difference
1141 between these two mappings. */
1142 relocation -= 0xC000;
1150 msg = _("XGATE address (%lx) is not within shared RAM"
1151 "(0xE000-0xFFFF), therefore you must manually offset "
1152 "the address, and possibly manage the page, in your "
1154 buf = alloca (strlen (msg) + 128);
1155 sprintf (buf, msg, phys_addr);
1156 if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
1157 input_section, insn_addr)))
1163 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
1164 && m68hc11_addr_is_banked (pinfo, insn_addr)
1165 && phys_page != insn_page && !(e_flags & E_M68HC11_NO_BANK_WARNING))
1170 msg = _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1171 "as current banked address [%lx:%04lx] (%lx)");
1173 buf = alloca (strlen (msg) + 128);
1174 sprintf (buf, msg, phys_page, phys_addr,
1175 (long) (relocation + rel->r_addend),
1176 insn_page, m68hc11_phys_addr (pinfo, insn_addr),
1177 (long) (insn_addr));
1178 if (!((*info->callbacks->warning)
1179 (info, buf, name, input_bfd, input_section,
1185 if (phys_page != 0 && insn_page == 0)
1190 msg = _("reference to a banked address [%lx:%04lx] in the "
1191 "normal address space at %04lx");
1193 buf = alloca (strlen (msg) + 128);
1194 sprintf (buf, msg, phys_page, phys_addr, insn_addr);
1195 if (!((*info->callbacks->warning)
1196 (info, buf, name, input_bfd, input_section,
1200 relocation = phys_addr;
1204 /* If this is a banked address use the phys_addr so that
1205 we stay in the banked window. */
1206 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend))
1207 relocation = phys_addr;
1211 /* If we are linking an XGATE instruction against an S12 symbol, we
1212 need to change the offset of the symbol value so that it's correct
1213 from the XGATE's perspective. */
1214 if (!strcmp (howto->name, "R_XGATE_IMM8_LO")
1215 || !strcmp (howto->name, "R_XGATE_IMM8_HI"))
1217 /* We can only offset S12 addresses that lie within the non-paged
1219 if (!is_xgate_symbol && !is_section_symbol)
1221 /* The ram in the global space is mapped to 0x2000 and stops at
1222 0x4000 in the 16-bit address space for S12 and 0xE000 in the
1223 16-bit address space for XGATE. */
1224 if (relocation >= 0x2000 && relocation < 0x4000)
1225 /* We offset the address by the difference
1226 between these two mappings. */
1227 relocation += 0xC000;
1233 /* Get virtual address of instruction having the relocation. */
1234 insn_addr = input_section->output_section->vma
1235 + input_section->output_offset + rel->r_offset;
1237 msg = _("S12 address (%lx) is not within shared RAM"
1238 "(0x2000-0x4000), therefore you must manually "
1239 "offset the address in your code");
1240 buf = alloca (strlen (msg) + 128);
1241 sprintf (buf, msg, phys_addr);
1242 if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
1243 input_section, insn_addr)))
1250 if (r_type != R_M68HC11_NONE)
1252 if ((r_type == R_M68HC12_PCREL_9) || (r_type == R_M68HC12_PCREL_10))
1253 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1254 contents, rel->r_offset,
1255 relocation - 2, rel->r_addend);
1257 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1258 contents, rel->r_offset,
1259 relocation, rel->r_addend);
1262 if (r != bfd_reloc_ok)
1264 const char * msg = (const char *) 0;
1268 case bfd_reloc_overflow:
1269 if (!((*info->callbacks->reloc_overflow)
1270 (info, NULL, name, howto->name, (bfd_vma) 0,
1271 input_bfd, input_section, rel->r_offset)))
1275 case bfd_reloc_undefined:
1276 if (!((*info->callbacks->undefined_symbol)
1277 (info, name, input_bfd, input_section,
1278 rel->r_offset, TRUE)))
1282 case bfd_reloc_outofrange:
1283 msg = _ ("internal error: out of range error");
1286 case bfd_reloc_notsupported:
1287 msg = _ ("internal error: unsupported relocation error");
1290 case bfd_reloc_dangerous:
1291 msg = _ ("internal error: dangerous error");
1295 msg = _ ("internal error: unknown error");
1299 if (!((*info->callbacks->warning)
1300 (info, msg, name, input_bfd, input_section,
1313 /* Set and control ELF flags in ELF header. */
1316 _bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
1318 BFD_ASSERT (!elf_flags_init (abfd)
1319 || elf_elfheader (abfd)->e_flags == flags);
1321 elf_elfheader (abfd)->e_flags = flags;
1322 elf_flags_init (abfd) = TRUE;
1326 /* Merge backend specific data from an object file to the output
1327 object file when linking. */
1330 _bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
1334 bfd_boolean ok = TRUE;
1336 /* Check if we have the same endianness */
1337 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
1340 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1341 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1344 new_flags = elf_elfheader (ibfd)->e_flags;
1345 elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
1346 old_flags = elf_elfheader (obfd)->e_flags;
1348 if (! elf_flags_init (obfd))
1350 elf_flags_init (obfd) = TRUE;
1351 elf_elfheader (obfd)->e_flags = new_flags;
1352 elf_elfheader (obfd)->e_ident[EI_CLASS]
1353 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
1355 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1356 && bfd_get_arch_info (obfd)->the_default)
1358 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1359 bfd_get_mach (ibfd)))
1366 /* Check ABI compatibility. */
1367 if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
1369 (*_bfd_error_handler)
1370 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1371 "and others for 32-bit integers"), ibfd);
1374 if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
1376 (*_bfd_error_handler)
1377 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1378 "and others for 64-bit double"), ibfd);
1382 /* Processor compatibility. */
1383 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
1385 (*_bfd_error_handler)
1386 (_("%B: linking files compiled for HCS12 with "
1387 "others compiled for HC12"), ibfd);
1390 new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
1391 | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
1393 elf_elfheader (obfd)->e_flags = new_flags;
1395 new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1396 old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1398 /* Warn about any other mismatches */
1399 if (new_flags != old_flags)
1401 (*_bfd_error_handler)
1402 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1403 ibfd, (unsigned long) new_flags, (unsigned long) old_flags);
1409 bfd_set_error (bfd_error_bad_value);
1417 _bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
1419 FILE *file = (FILE *) ptr;
1421 BFD_ASSERT (abfd != NULL && ptr != NULL);
1423 /* Print normal ELF private data. */
1424 _bfd_elf_print_private_bfd_data (abfd, ptr);
1426 /* xgettext:c-format */
1427 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1429 if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
1430 fprintf (file, _("[abi=32-bit int, "));
1432 fprintf (file, _("[abi=16-bit int, "));
1434 if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
1435 fprintf (file, _("64-bit double, "));
1437 fprintf (file, _("32-bit double, "));
1439 if (strcmp (bfd_get_target (abfd), "elf32-m68hc11") == 0)
1440 fprintf (file, _("cpu=HC11]"));
1441 else if (elf_elfheader (abfd)->e_flags & EF_M68HCS12_MACH)
1442 fprintf (file, _("cpu=HCS12]"));
1444 fprintf (file, _("cpu=HC12]"));
1446 if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
1447 fprintf (file, _(" [memory=bank-model]"));
1449 fprintf (file, _(" [memory=flat]"));
1451 if (elf_elfheader (abfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
1452 fprintf (file, _(" [XGATE RAM offsetting]"));
1459 static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
1460 asection *asect, void *arg)
1462 struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
1464 if (asect->vma >= p->pinfo->bank_virtual)
1465 p->use_memory_banks = TRUE;
1468 /* Tweak the OSABI field of the elf header. */
1471 elf32_m68hc11_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
1473 struct m68hc11_scan_param param;
1474 struct m68hc11_elf_link_hash_table *htab;
1476 if (link_info == NULL)
1479 htab = m68hc11_elf_hash_table (link_info);
1483 m68hc11_elf_get_bank_parameters (link_info);
1485 param.use_memory_banks = FALSE;
1486 param.pinfo = & htab->pinfo;
1488 bfd_map_over_sections (abfd, scan_sections_for_abi, ¶m);
1490 if (param.use_memory_banks)
1492 Elf_Internal_Ehdr * i_ehdrp;
1494 i_ehdrp = elf_elfheader (abfd);
1495 i_ehdrp->e_flags |= E_M68HC12_BANKS;