* config/tc-mips.c (mips_fix_loongson2f, mips_fix_loongson2f_nop,
[binutils/dougsmingw.git] / bfd / elf32-m68hc1x.c
blob9199f69361b74a3ff1db9d97d0347b89a0787cda
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 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. */
23 #include "alloca-conf.h"
24 #include "sysdep.h"
25 #include "bfd.h"
26 #include "bfdlink.h"
27 #include "libbfd.h"
28 #include "elf-bfd.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,
40 asection *section,
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,
48 asection* sec);
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)
72 return NULL;
74 memset (ret, 0, amt);
75 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
76 _bfd_elf_link_hash_newfunc,
77 sizeof (struct elf_link_hash_entry),
78 M68HC11_ELF_DATA))
80 free (ret);
81 return NULL;
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)
89 free (ret);
90 return NULL;
92 if (!bfd_hash_table_init (ret->stub_hash_table, stub_hash_newfunc,
93 sizeof (struct elf32_m68hc11_stub_hash_entry)))
94 return NULL;
96 ret->stub_bfd = NULL;
97 ret->stub_section = 0;
98 ret->add_stub_section = NULL;
99 ret->sym_cache.abfd = NULL;
101 return ret;
104 /* Free the derived linker hash table. */
106 void
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,
123 const char *string)
125 /* Allocate the structure if it has not already been allocated by a
126 subclass. */
127 if (entry == NULL)
129 entry = bfd_hash_allocate (table,
130 sizeof (struct elf32_m68hc11_stub_hash_entry));
131 if (entry == NULL)
132 return entry;
135 /* Call the allocation method of the superclass. */
136 entry = bfd_hash_newfunc (entry, table, string);
137 if (entry != NULL)
139 struct elf32_m68hc11_stub_hash_entry *eh;
141 /* Initialize the local fields. */
142 eh = (struct elf32_m68hc11_stub_hash_entry *) entry;
143 eh->stub_sec = NULL;
144 eh->stub_offset = 0;
145 eh->target_value = 0;
146 eh->target_section = NULL;
149 return entry;
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,
163 TRUE, FALSE);
164 if (stub_entry == NULL)
166 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
167 section->owner, stub_name);
168 return NULL;
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;
179 return stub_entry;
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. */
186 bfd_boolean
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);
201 if (h == NULL)
203 struct bfd_link_hash_entry* entry = NULL;
205 _bfd_generic_link_add_one_symbol (info, abfd,
206 "__far_trampoline",
207 BSF_GLOBAL,
208 bfd_und_section_ptr,
209 (bfd_vma) 0, (const char*) NULL,
210 FALSE, FALSE, &entry);
214 return TRUE;
217 /* External entry points for sizing and building linker stubs. */
219 /* Set up various things so that we can make a list of input sections
220 for each output section included in the link. Returns -1 on error,
221 0 when no stubs will be needed, and 1 on success. */
224 elf32_m68hc11_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
226 bfd *input_bfd;
227 unsigned int bfd_count;
228 int top_id, top_index;
229 asection *section;
230 asection **input_list, **list;
231 bfd_size_type amt;
232 asection *text_section;
233 struct m68hc11_elf_link_hash_table *htab;
235 htab = m68hc11_elf_hash_table (info);
236 if (htab == NULL)
237 return -1;
239 if (bfd_get_flavour (info->output_bfd) != bfd_target_elf_flavour)
240 return 0;
242 /* Count the number of input BFDs and find the top input section id.
243 Also search for an existing ".tramp" section so that we know
244 where generated trampolines must go. Default to ".text" if we
245 can't find it. */
246 htab->tramp_section = 0;
247 text_section = 0;
248 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
249 input_bfd != NULL;
250 input_bfd = input_bfd->link_next)
252 bfd_count += 1;
253 for (section = input_bfd->sections;
254 section != NULL;
255 section = section->next)
257 const char* name = bfd_get_section_name (input_bfd, section);
259 if (!strcmp (name, ".tramp"))
260 htab->tramp_section = section;
262 if (!strcmp (name, ".text"))
263 text_section = section;
265 if (top_id < section->id)
266 top_id = section->id;
269 htab->bfd_count = bfd_count;
270 if (htab->tramp_section == 0)
271 htab->tramp_section = text_section;
273 /* We can't use output_bfd->section_count here to find the top output
274 section index as some sections may have been removed, and
275 strip_excluded_output_sections doesn't renumber the indices. */
276 for (section = output_bfd->sections, top_index = 0;
277 section != NULL;
278 section = section->next)
280 if (top_index < section->index)
281 top_index = section->index;
284 htab->top_index = top_index;
285 amt = sizeof (asection *) * (top_index + 1);
286 input_list = (asection **) bfd_malloc (amt);
287 htab->input_list = input_list;
288 if (input_list == NULL)
289 return -1;
291 /* For sections we aren't interested in, mark their entries with a
292 value we can check later. */
293 list = input_list + top_index;
295 *list = bfd_abs_section_ptr;
296 while (list-- != input_list);
298 for (section = output_bfd->sections;
299 section != NULL;
300 section = section->next)
302 if ((section->flags & SEC_CODE) != 0)
303 input_list[section->index] = NULL;
306 return 1;
309 /* Determine and set the size of the stub section for a final link.
311 The basic idea here is to examine all the relocations looking for
312 PC-relative calls to a target that is unreachable with a "bl"
313 instruction. */
315 bfd_boolean
316 elf32_m68hc11_size_stubs (bfd *output_bfd, bfd *stub_bfd,
317 struct bfd_link_info *info,
318 asection * (*add_stub_section) (const char*, asection*))
320 bfd *input_bfd;
321 asection *section;
322 Elf_Internal_Sym *local_syms, **all_local_syms;
323 unsigned int bfd_indx, bfd_count;
324 bfd_size_type amt;
325 asection *stub_sec;
326 struct m68hc11_elf_link_hash_table *htab = m68hc11_elf_hash_table (info);
328 if (htab == NULL)
329 return FALSE;
331 /* Stash our params away. */
332 htab->stub_bfd = stub_bfd;
333 htab->add_stub_section = add_stub_section;
335 /* Count the number of input BFDs and find the top input section id. */
336 for (input_bfd = info->input_bfds, bfd_count = 0;
337 input_bfd != NULL;
338 input_bfd = input_bfd->link_next)
339 bfd_count += 1;
341 /* We want to read in symbol extension records only once. To do this
342 we need to read in the local symbols in parallel and save them for
343 later use; so hold pointers to the local symbols in an array. */
344 amt = sizeof (Elf_Internal_Sym *) * bfd_count;
345 all_local_syms = (Elf_Internal_Sym **) bfd_zmalloc (amt);
346 if (all_local_syms == NULL)
347 return FALSE;
349 /* Walk over all the input BFDs, swapping in local symbols. */
350 for (input_bfd = info->input_bfds, bfd_indx = 0;
351 input_bfd != NULL;
352 input_bfd = input_bfd->link_next, bfd_indx++)
354 Elf_Internal_Shdr *symtab_hdr;
356 /* We'll need the symbol table in a second. */
357 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
358 if (symtab_hdr->sh_info == 0)
359 continue;
361 /* We need an array of the local symbols attached to the input bfd. */
362 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
363 if (local_syms == NULL)
365 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
366 symtab_hdr->sh_info, 0,
367 NULL, NULL, NULL);
368 /* Cache them for elf_link_input_bfd. */
369 symtab_hdr->contents = (unsigned char *) local_syms;
371 if (local_syms == NULL)
373 free (all_local_syms);
374 return FALSE;
377 all_local_syms[bfd_indx] = local_syms;
380 for (input_bfd = info->input_bfds, bfd_indx = 0;
381 input_bfd != NULL;
382 input_bfd = input_bfd->link_next, bfd_indx++)
384 Elf_Internal_Shdr *symtab_hdr;
385 struct elf_link_hash_entry ** sym_hashes;
387 sym_hashes = elf_sym_hashes (input_bfd);
389 /* We'll need the symbol table in a second. */
390 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
391 if (symtab_hdr->sh_info == 0)
392 continue;
394 local_syms = all_local_syms[bfd_indx];
396 /* Walk over each section attached to the input bfd. */
397 for (section = input_bfd->sections;
398 section != NULL;
399 section = section->next)
401 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
403 /* If there aren't any relocs, then there's nothing more
404 to do. */
405 if ((section->flags & SEC_RELOC) == 0
406 || section->reloc_count == 0)
407 continue;
409 /* If this section is a link-once section that will be
410 discarded, then don't create any stubs. */
411 if (section->output_section == NULL
412 || section->output_section->owner != output_bfd)
413 continue;
415 /* Get the relocs. */
416 internal_relocs
417 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
418 (Elf_Internal_Rela *) NULL,
419 info->keep_memory);
420 if (internal_relocs == NULL)
421 goto error_ret_free_local;
423 /* Now examine each relocation. */
424 irela = internal_relocs;
425 irelaend = irela + section->reloc_count;
426 for (; irela < irelaend; irela++)
428 unsigned int r_type, r_indx;
429 struct elf32_m68hc11_stub_hash_entry *stub_entry;
430 asection *sym_sec;
431 bfd_vma sym_value;
432 struct elf_link_hash_entry *hash;
433 const char *stub_name;
434 Elf_Internal_Sym *sym;
436 r_type = ELF32_R_TYPE (irela->r_info);
438 /* Only look at 16-bit relocs. */
439 if (r_type != (unsigned int) R_M68HC11_16)
440 continue;
442 /* Now determine the call target, its name, value,
443 section. */
444 r_indx = ELF32_R_SYM (irela->r_info);
445 if (r_indx < symtab_hdr->sh_info)
447 /* It's a local symbol. */
448 Elf_Internal_Shdr *hdr;
449 bfd_boolean is_far;
451 sym = local_syms + r_indx;
452 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
453 if (!is_far)
454 continue;
456 if (sym->st_shndx >= elf_numsections (input_bfd))
457 sym_sec = NULL;
458 else
460 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
461 sym_sec = hdr->bfd_section;
463 stub_name = (bfd_elf_string_from_elf_section
464 (input_bfd, symtab_hdr->sh_link,
465 sym->st_name));
466 sym_value = sym->st_value;
467 hash = NULL;
469 else
471 /* It's an external symbol. */
472 int e_indx;
474 e_indx = r_indx - symtab_hdr->sh_info;
475 hash = (struct elf_link_hash_entry *)
476 (sym_hashes[e_indx]);
478 while (hash->root.type == bfd_link_hash_indirect
479 || hash->root.type == bfd_link_hash_warning)
480 hash = ((struct elf_link_hash_entry *)
481 hash->root.u.i.link);
483 if (hash->root.type == bfd_link_hash_defined
484 || hash->root.type == bfd_link_hash_defweak
485 || hash->root.type == bfd_link_hash_new)
487 if (!(hash->other & STO_M68HC12_FAR))
488 continue;
490 else if (hash->root.type == bfd_link_hash_undefweak)
492 continue;
494 else if (hash->root.type == bfd_link_hash_undefined)
496 continue;
498 else
500 bfd_set_error (bfd_error_bad_value);
501 goto error_ret_free_internal;
503 sym_sec = hash->root.u.def.section;
504 sym_value = hash->root.u.def.value;
505 stub_name = hash->root.root.string;
508 if (!stub_name)
509 goto error_ret_free_internal;
511 stub_entry = m68hc12_stub_hash_lookup
512 (htab->stub_hash_table,
513 stub_name,
514 FALSE, FALSE);
515 if (stub_entry == NULL)
517 if (add_stub_section == 0)
518 continue;
520 stub_entry = m68hc12_add_stub (stub_name, section, htab);
521 if (stub_entry == NULL)
523 error_ret_free_internal:
524 if (elf_section_data (section)->relocs == NULL)
525 free (internal_relocs);
526 goto error_ret_free_local;
530 stub_entry->target_value = sym_value;
531 stub_entry->target_section = sym_sec;
534 /* We're done with the internal relocs, free them. */
535 if (elf_section_data (section)->relocs == NULL)
536 free (internal_relocs);
540 if (add_stub_section)
542 /* OK, we've added some stubs. Find out the new size of the
543 stub sections. */
544 for (stub_sec = htab->stub_bfd->sections;
545 stub_sec != NULL;
546 stub_sec = stub_sec->next)
548 stub_sec->size = 0;
551 bfd_hash_traverse (htab->stub_hash_table, htab->size_one_stub, htab);
553 free (all_local_syms);
554 return TRUE;
556 error_ret_free_local:
557 free (all_local_syms);
558 return FALSE;
561 /* Export the trampoline addresses in the symbol table. */
562 static bfd_boolean
563 m68hc11_elf_export_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
565 struct bfd_link_info *info;
566 struct m68hc11_elf_link_hash_table *htab;
567 struct elf32_m68hc11_stub_hash_entry *stub_entry;
568 char* name;
569 bfd_boolean result;
571 info = (struct bfd_link_info *) in_arg;
572 htab = m68hc11_elf_hash_table (info);
573 if (htab == NULL)
574 return FALSE;
576 /* Massage our args to the form they really have. */
577 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
579 /* Generate the trampoline according to HC11 or HC12. */
580 result = (* htab->build_one_stub) (gen_entry, in_arg);
582 /* Make a printable name that does not conflict with the real function. */
583 name = alloca (strlen (stub_entry->root.string) + 16);
584 sprintf (name, "tramp.%s", stub_entry->root.string);
586 /* Export the symbol for debugging/disassembling. */
587 m68hc11_elf_set_symbol (htab->stub_bfd, info, name,
588 stub_entry->stub_offset,
589 stub_entry->stub_sec);
590 return result;
593 /* Export a symbol or set its value and section. */
594 static void
595 m68hc11_elf_set_symbol (bfd *abfd, struct bfd_link_info *info,
596 const char *name, bfd_vma value, asection *sec)
598 struct elf_link_hash_entry *h;
600 h = (struct elf_link_hash_entry *)
601 bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
602 if (h == NULL)
604 _bfd_generic_link_add_one_symbol (info, abfd,
605 name,
606 BSF_GLOBAL,
607 sec,
608 value,
609 (const char*) NULL,
610 TRUE, FALSE, NULL);
612 else
614 h->root.type = bfd_link_hash_defined;
615 h->root.u.def.value = value;
616 h->root.u.def.section = sec;
621 /* Build all the stubs associated with the current output file. The
622 stubs are kept in a hash table attached to the main linker hash
623 table. This function is called via m68hc12elf_finish in the
624 linker. */
626 bfd_boolean
627 elf32_m68hc11_build_stubs (bfd *abfd, struct bfd_link_info *info)
629 asection *stub_sec;
630 struct bfd_hash_table *table;
631 struct m68hc11_elf_link_hash_table *htab;
632 struct m68hc11_scan_param param;
634 m68hc11_elf_get_bank_parameters (info);
635 htab = m68hc11_elf_hash_table (info);
636 if (htab == NULL)
637 return FALSE;
639 for (stub_sec = htab->stub_bfd->sections;
640 stub_sec != NULL;
641 stub_sec = stub_sec->next)
643 bfd_size_type size;
645 /* Allocate memory to hold the linker stubs. */
646 size = stub_sec->size;
647 stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
648 if (stub_sec->contents == NULL && size != 0)
649 return FALSE;
650 stub_sec->size = 0;
653 /* Build the stubs as directed by the stub hash table. */
654 table = htab->stub_hash_table;
655 bfd_hash_traverse (table, m68hc11_elf_export_one_stub, info);
657 /* Scan the output sections to see if we use the memory banks.
658 If so, export the symbols that define how the memory banks
659 are mapped. This is used by gdb and the simulator to obtain
660 the information. It can be used by programs to burn the eprom
661 at the good addresses. */
662 param.use_memory_banks = FALSE;
663 param.pinfo = &htab->pinfo;
664 bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
665 if (param.use_memory_banks)
667 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_START_NAME,
668 htab->pinfo.bank_physical,
669 bfd_abs_section_ptr);
670 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_VIRTUAL_NAME,
671 htab->pinfo.bank_virtual,
672 bfd_abs_section_ptr);
673 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_SIZE_NAME,
674 htab->pinfo.bank_size,
675 bfd_abs_section_ptr);
678 return TRUE;
681 void
682 m68hc11_elf_get_bank_parameters (struct bfd_link_info *info)
684 unsigned i;
685 struct m68hc11_page_info *pinfo;
686 struct bfd_link_hash_entry *h;
687 struct m68hc11_elf_link_hash_table *htab;
689 htab = m68hc11_elf_hash_table (info);
690 if (htab == NULL)
691 return;
693 pinfo = & htab->pinfo;
694 if (pinfo->bank_param_initialized)
695 return;
697 pinfo->bank_virtual = M68HC12_BANK_VIRT;
698 pinfo->bank_mask = M68HC12_BANK_MASK;
699 pinfo->bank_physical = M68HC12_BANK_BASE;
700 pinfo->bank_shift = M68HC12_BANK_SHIFT;
701 pinfo->bank_size = 1 << M68HC12_BANK_SHIFT;
703 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_START_NAME,
704 FALSE, FALSE, TRUE);
705 if (h != (struct bfd_link_hash_entry*) NULL
706 && h->type == bfd_link_hash_defined)
707 pinfo->bank_physical = (h->u.def.value
708 + h->u.def.section->output_section->vma
709 + h->u.def.section->output_offset);
711 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_VIRTUAL_NAME,
712 FALSE, FALSE, TRUE);
713 if (h != (struct bfd_link_hash_entry*) NULL
714 && h->type == bfd_link_hash_defined)
715 pinfo->bank_virtual = (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_SIZE_NAME,
720 FALSE, FALSE, TRUE);
721 if (h != (struct bfd_link_hash_entry*) NULL
722 && h->type == bfd_link_hash_defined)
723 pinfo->bank_size = (h->u.def.value
724 + h->u.def.section->output_section->vma
725 + h->u.def.section->output_offset);
727 pinfo->bank_shift = 0;
728 for (i = pinfo->bank_size; i != 0; i >>= 1)
729 pinfo->bank_shift++;
730 pinfo->bank_shift--;
731 pinfo->bank_mask = (1 << pinfo->bank_shift) - 1;
732 pinfo->bank_physical_end = pinfo->bank_physical + pinfo->bank_size;
733 pinfo->bank_param_initialized = 1;
735 h = bfd_link_hash_lookup (info->hash, "__far_trampoline", FALSE,
736 FALSE, TRUE);
737 if (h != (struct bfd_link_hash_entry*) NULL
738 && h->type == bfd_link_hash_defined)
739 pinfo->trampoline_addr = (h->u.def.value
740 + h->u.def.section->output_section->vma
741 + h->u.def.section->output_offset);
744 /* Return 1 if the address is in banked memory.
745 This can be applied to a virtual address and to a physical address. */
747 m68hc11_addr_is_banked (struct m68hc11_page_info *pinfo, bfd_vma addr)
749 if (addr >= pinfo->bank_virtual)
750 return 1;
752 if (addr >= pinfo->bank_physical && addr <= pinfo->bank_physical_end)
753 return 1;
755 return 0;
758 /* Return the physical address seen by the processor, taking
759 into account banked memory. */
760 bfd_vma
761 m68hc11_phys_addr (struct m68hc11_page_info *pinfo, bfd_vma addr)
763 if (addr < pinfo->bank_virtual)
764 return addr;
766 /* Map the address to the memory bank. */
767 addr -= pinfo->bank_virtual;
768 addr &= pinfo->bank_mask;
769 addr += pinfo->bank_physical;
770 return addr;
773 /* Return the page number corresponding to an address in banked memory. */
774 bfd_vma
775 m68hc11_phys_page (struct m68hc11_page_info *pinfo, bfd_vma addr)
777 if (addr < pinfo->bank_virtual)
778 return 0;
780 /* Map the address to the memory bank. */
781 addr -= pinfo->bank_virtual;
782 addr >>= pinfo->bank_shift;
783 addr &= 0x0ff;
784 return addr;
787 /* This function is used for relocs which are only used for relaxing,
788 which the linker should otherwise ignore. */
790 bfd_reloc_status_type
791 m68hc11_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED,
792 arelent *reloc_entry,
793 asymbol *symbol ATTRIBUTE_UNUSED,
794 void *data ATTRIBUTE_UNUSED,
795 asection *input_section,
796 bfd *output_bfd,
797 char **error_message ATTRIBUTE_UNUSED)
799 if (output_bfd != NULL)
800 reloc_entry->address += input_section->output_offset;
801 return bfd_reloc_ok;
804 bfd_reloc_status_type
805 m68hc11_elf_special_reloc (bfd *abfd ATTRIBUTE_UNUSED,
806 arelent *reloc_entry,
807 asymbol *symbol,
808 void *data ATTRIBUTE_UNUSED,
809 asection *input_section,
810 bfd *output_bfd,
811 char **error_message ATTRIBUTE_UNUSED)
813 if (output_bfd != (bfd *) NULL
814 && (symbol->flags & BSF_SECTION_SYM) == 0
815 && (! reloc_entry->howto->partial_inplace
816 || reloc_entry->addend == 0))
818 reloc_entry->address += input_section->output_offset;
819 return bfd_reloc_ok;
822 if (output_bfd != NULL)
823 return bfd_reloc_continue;
825 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
826 return bfd_reloc_outofrange;
828 abort();
831 /* Look through the relocs for a section during the first phase.
832 Since we don't do .gots or .plts, we just need to consider the
833 virtual table relocs for gc. */
835 bfd_boolean
836 elf32_m68hc11_check_relocs (bfd *abfd, struct bfd_link_info *info,
837 asection *sec, const Elf_Internal_Rela *relocs)
839 Elf_Internal_Shdr * symtab_hdr;
840 struct elf_link_hash_entry ** sym_hashes;
841 const Elf_Internal_Rela * rel;
842 const Elf_Internal_Rela * rel_end;
844 if (info->relocatable)
845 return TRUE;
847 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
848 sym_hashes = elf_sym_hashes (abfd);
849 rel_end = relocs + sec->reloc_count;
851 for (rel = relocs; rel < rel_end; rel++)
853 struct elf_link_hash_entry * h;
854 unsigned long r_symndx;
856 r_symndx = ELF32_R_SYM (rel->r_info);
858 if (r_symndx < symtab_hdr->sh_info)
859 h = NULL;
860 else
862 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
863 while (h->root.type == bfd_link_hash_indirect
864 || h->root.type == bfd_link_hash_warning)
865 h = (struct elf_link_hash_entry *) h->root.u.i.link;
868 switch (ELF32_R_TYPE (rel->r_info))
870 /* This relocation describes the C++ object vtable hierarchy.
871 Reconstruct it for later use during GC. */
872 case R_M68HC11_GNU_VTINHERIT:
873 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
874 return FALSE;
875 break;
877 /* This relocation describes which C++ vtable entries are actually
878 used. Record for later use during GC. */
879 case R_M68HC11_GNU_VTENTRY:
880 BFD_ASSERT (h != NULL);
881 if (h != NULL
882 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
883 return FALSE;
884 break;
888 return TRUE;
891 /* Relocate a 68hc11/68hc12 ELF section. */
892 bfd_boolean
893 elf32_m68hc11_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
894 struct bfd_link_info *info,
895 bfd *input_bfd, asection *input_section,
896 bfd_byte *contents, Elf_Internal_Rela *relocs,
897 Elf_Internal_Sym *local_syms,
898 asection **local_sections)
900 Elf_Internal_Shdr *symtab_hdr;
901 struct elf_link_hash_entry **sym_hashes;
902 Elf_Internal_Rela *rel, *relend;
903 const char *name = NULL;
904 struct m68hc11_page_info *pinfo;
905 const struct elf_backend_data * const ebd = get_elf_backend_data (input_bfd);
906 struct m68hc11_elf_link_hash_table *htab;
908 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
909 sym_hashes = elf_sym_hashes (input_bfd);
911 htab = m68hc11_elf_hash_table (info);
912 if (htab == NULL)
913 return FALSE;
915 /* Get memory bank parameters. */
916 m68hc11_elf_get_bank_parameters (info);
918 pinfo = & htab->pinfo;
919 rel = relocs;
920 relend = relocs + input_section->reloc_count;
922 for (; rel < relend; rel++)
924 int r_type;
925 arelent arel;
926 reloc_howto_type *howto;
927 unsigned long r_symndx;
928 Elf_Internal_Sym *sym;
929 asection *sec;
930 bfd_vma relocation = 0;
931 bfd_reloc_status_type r = bfd_reloc_undefined;
932 bfd_vma phys_page;
933 bfd_vma phys_addr;
934 bfd_vma insn_addr;
935 bfd_vma insn_page;
936 bfd_boolean is_far = FALSE;
937 struct elf_link_hash_entry *h;
938 const char* stub_name = 0;
940 r_symndx = ELF32_R_SYM (rel->r_info);
941 r_type = ELF32_R_TYPE (rel->r_info);
943 if (r_type == R_M68HC11_GNU_VTENTRY
944 || r_type == R_M68HC11_GNU_VTINHERIT )
945 continue;
947 (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel);
948 howto = arel.howto;
950 h = NULL;
951 sym = NULL;
952 sec = NULL;
953 if (r_symndx < symtab_hdr->sh_info)
955 sym = local_syms + r_symndx;
956 sec = local_sections[r_symndx];
957 relocation = (sec->output_section->vma
958 + sec->output_offset
959 + sym->st_value);
960 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
961 if (is_far)
962 stub_name = (bfd_elf_string_from_elf_section
963 (input_bfd, symtab_hdr->sh_link,
964 sym->st_name));
966 else
968 bfd_boolean unresolved_reloc, warned;
970 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
971 r_symndx, symtab_hdr, sym_hashes,
972 h, sec, relocation, unresolved_reloc,
973 warned);
975 is_far = (h && (h->other & STO_M68HC12_FAR));
976 stub_name = h->root.root.string;
979 if (sec != NULL && elf_discarded_section (sec))
981 /* For relocs against symbols from removed linkonce sections,
982 or sections discarded by a linker script, we just want the
983 section contents zeroed. Avoid any special processing. */
984 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
985 rel->r_info = 0;
986 rel->r_addend = 0;
987 continue;
990 if (info->relocatable)
992 /* This is a relocatable link. We don't have to change
993 anything, unless the reloc is against a section symbol,
994 in which case we have to adjust according to where the
995 section symbol winds up in the output section. */
996 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
997 rel->r_addend += sec->output_offset;
998 continue;
1001 if (h != NULL)
1002 name = h->root.root.string;
1003 else
1005 name = (bfd_elf_string_from_elf_section
1006 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1007 if (name == NULL || *name == '\0')
1008 name = bfd_section_name (input_bfd, sec);
1011 if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
1013 struct elf32_m68hc11_stub_hash_entry* stub;
1015 stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
1016 name, FALSE, FALSE);
1017 if (stub)
1019 relocation = stub->stub_offset
1020 + stub->stub_sec->output_section->vma
1021 + stub->stub_sec->output_offset;
1022 is_far = FALSE;
1026 /* Do the memory bank mapping. */
1027 phys_addr = m68hc11_phys_addr (pinfo, relocation + rel->r_addend);
1028 phys_page = m68hc11_phys_page (pinfo, relocation + rel->r_addend);
1029 switch (r_type)
1031 case R_M68HC11_24:
1032 /* Reloc used by 68HC12 call instruction. */
1033 bfd_put_16 (input_bfd, phys_addr,
1034 (bfd_byte*) contents + rel->r_offset);
1035 bfd_put_8 (input_bfd, phys_page,
1036 (bfd_byte*) contents + rel->r_offset + 2);
1037 r = bfd_reloc_ok;
1038 r_type = R_M68HC11_NONE;
1039 break;
1041 case R_M68HC11_NONE:
1042 r = bfd_reloc_ok;
1043 break;
1045 case R_M68HC11_LO16:
1046 /* Reloc generated by %addr(expr) gas to obtain the
1047 address as mapped in the memory bank window. */
1048 relocation = phys_addr;
1049 break;
1051 case R_M68HC11_PAGE:
1052 /* Reloc generated by %page(expr) gas to obtain the
1053 page number associated with the address. */
1054 relocation = phys_page;
1055 break;
1057 case R_M68HC11_16:
1058 /* Get virtual address of instruction having the relocation. */
1059 if (is_far)
1061 const char* msg;
1062 char* buf;
1063 msg = _("Reference to the far symbol `%s' using a wrong "
1064 "relocation may result in incorrect execution");
1065 buf = alloca (strlen (msg) + strlen (name) + 10);
1066 sprintf (buf, msg, name);
1068 (* info->callbacks->warning)
1069 (info, buf, name, input_bfd, NULL, rel->r_offset);
1072 /* Get virtual address of instruction having the relocation. */
1073 insn_addr = input_section->output_section->vma
1074 + input_section->output_offset
1075 + rel->r_offset;
1077 insn_page = m68hc11_phys_page (pinfo, insn_addr);
1079 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
1080 && m68hc11_addr_is_banked (pinfo, insn_addr)
1081 && phys_page != insn_page)
1083 const char* msg;
1084 char* buf;
1086 msg = _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1087 "as current banked address [%lx:%04lx] (%lx)");
1089 buf = alloca (strlen (msg) + 128);
1090 sprintf (buf, msg, phys_page, phys_addr,
1091 (long) (relocation + rel->r_addend),
1092 insn_page, m68hc11_phys_addr (pinfo, insn_addr),
1093 (long) (insn_addr));
1094 if (!((*info->callbacks->warning)
1095 (info, buf, name, input_bfd, input_section,
1096 rel->r_offset)))
1097 return FALSE;
1098 break;
1100 if (phys_page != 0 && insn_page == 0)
1102 const char* msg;
1103 char* buf;
1105 msg = _("reference to a banked address [%lx:%04lx] in the "
1106 "normal address space at %04lx");
1108 buf = alloca (strlen (msg) + 128);
1109 sprintf (buf, msg, phys_page, phys_addr, insn_addr);
1110 if (!((*info->callbacks->warning)
1111 (info, buf, name, input_bfd, input_section,
1112 insn_addr)))
1113 return FALSE;
1115 relocation = phys_addr;
1116 break;
1119 /* If this is a banked address use the phys_addr so that
1120 we stay in the banked window. */
1121 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend))
1122 relocation = phys_addr;
1123 break;
1125 if (r_type != R_M68HC11_NONE)
1126 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1127 contents, rel->r_offset,
1128 relocation, rel->r_addend);
1130 if (r != bfd_reloc_ok)
1132 const char * msg = (const char *) 0;
1134 switch (r)
1136 case bfd_reloc_overflow:
1137 if (!((*info->callbacks->reloc_overflow)
1138 (info, NULL, name, howto->name, (bfd_vma) 0,
1139 input_bfd, input_section, rel->r_offset)))
1140 return FALSE;
1141 break;
1143 case bfd_reloc_undefined:
1144 if (!((*info->callbacks->undefined_symbol)
1145 (info, name, input_bfd, input_section,
1146 rel->r_offset, TRUE)))
1147 return FALSE;
1148 break;
1150 case bfd_reloc_outofrange:
1151 msg = _ ("internal error: out of range error");
1152 goto common_error;
1154 case bfd_reloc_notsupported:
1155 msg = _ ("internal error: unsupported relocation error");
1156 goto common_error;
1158 case bfd_reloc_dangerous:
1159 msg = _ ("internal error: dangerous error");
1160 goto common_error;
1162 default:
1163 msg = _ ("internal error: unknown error");
1164 /* fall through */
1166 common_error:
1167 if (!((*info->callbacks->warning)
1168 (info, msg, name, input_bfd, input_section,
1169 rel->r_offset)))
1170 return FALSE;
1171 break;
1176 return TRUE;
1181 /* Set and control ELF flags in ELF header. */
1183 bfd_boolean
1184 _bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
1186 BFD_ASSERT (!elf_flags_init (abfd)
1187 || elf_elfheader (abfd)->e_flags == flags);
1189 elf_elfheader (abfd)->e_flags = flags;
1190 elf_flags_init (abfd) = TRUE;
1191 return TRUE;
1194 /* Merge backend specific data from an object file to the output
1195 object file when linking. */
1197 bfd_boolean
1198 _bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
1200 flagword old_flags;
1201 flagword new_flags;
1202 bfd_boolean ok = TRUE;
1204 /* Check if we have the same endianess */
1205 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
1206 return FALSE;
1208 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1209 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1210 return TRUE;
1212 new_flags = elf_elfheader (ibfd)->e_flags;
1213 elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
1214 old_flags = elf_elfheader (obfd)->e_flags;
1216 if (! elf_flags_init (obfd))
1218 elf_flags_init (obfd) = TRUE;
1219 elf_elfheader (obfd)->e_flags = new_flags;
1220 elf_elfheader (obfd)->e_ident[EI_CLASS]
1221 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
1223 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1224 && bfd_get_arch_info (obfd)->the_default)
1226 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1227 bfd_get_mach (ibfd)))
1228 return FALSE;
1231 return TRUE;
1234 /* Check ABI compatibility. */
1235 if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
1237 (*_bfd_error_handler)
1238 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1239 "and others for 32-bit integers"), ibfd);
1240 ok = FALSE;
1242 if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
1244 (*_bfd_error_handler)
1245 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1246 "and others for 64-bit double"), ibfd);
1247 ok = FALSE;
1250 /* Processor compatibility. */
1251 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
1253 (*_bfd_error_handler)
1254 (_("%B: linking files compiled for HCS12 with "
1255 "others compiled for HC12"), ibfd);
1256 ok = FALSE;
1258 new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
1259 | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
1261 elf_elfheader (obfd)->e_flags = new_flags;
1263 new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1264 old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1266 /* Warn about any other mismatches */
1267 if (new_flags != old_flags)
1269 (*_bfd_error_handler)
1270 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1271 ibfd, (unsigned long) new_flags, (unsigned long) old_flags);
1272 ok = FALSE;
1275 if (! ok)
1277 bfd_set_error (bfd_error_bad_value);
1278 return FALSE;
1281 return TRUE;
1284 bfd_boolean
1285 _bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
1287 FILE *file = (FILE *) ptr;
1289 BFD_ASSERT (abfd != NULL && ptr != NULL);
1291 /* Print normal ELF private data. */
1292 _bfd_elf_print_private_bfd_data (abfd, ptr);
1294 /* xgettext:c-format */
1295 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1297 if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
1298 fprintf (file, _("[abi=32-bit int, "));
1299 else
1300 fprintf (file, _("[abi=16-bit int, "));
1302 if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
1303 fprintf (file, _("64-bit double, "));
1304 else
1305 fprintf (file, _("32-bit double, "));
1307 if (strcmp (bfd_get_target (abfd), "elf32-m68hc11") == 0)
1308 fprintf (file, _("cpu=HC11]"));
1309 else if (elf_elfheader (abfd)->e_flags & EF_M68HCS12_MACH)
1310 fprintf (file, _("cpu=HCS12]"));
1311 else
1312 fprintf (file, _("cpu=HC12]"));
1314 if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
1315 fprintf (file, _(" [memory=bank-model]"));
1316 else
1317 fprintf (file, _(" [memory=flat]"));
1319 fputc ('\n', file);
1321 return TRUE;
1324 static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
1325 asection *asect, void *arg)
1327 struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
1329 if (asect->vma >= p->pinfo->bank_virtual)
1330 p->use_memory_banks = TRUE;
1333 /* Tweak the OSABI field of the elf header. */
1335 void
1336 elf32_m68hc11_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
1338 struct m68hc11_scan_param param;
1339 struct m68hc11_elf_link_hash_table *htab;
1341 if (link_info == NULL)
1342 return;
1344 htab = m68hc11_elf_hash_table (link_info);
1345 if (htab == NULL)
1346 return;
1348 m68hc11_elf_get_bank_parameters (link_info);
1350 param.use_memory_banks = FALSE;
1351 param.pinfo = & htab->pinfo;
1353 bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
1355 if (param.use_memory_banks)
1357 Elf_Internal_Ehdr * i_ehdrp;
1359 i_ehdrp = elf_elfheader (abfd);
1360 i_ehdrp->e_flags |= E_M68HC12_BANKS;