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
;
876 switch (ELF32_R_TYPE (rel
->r_info
))
878 /* This relocation describes the C++ object vtable hierarchy.
879 Reconstruct it for later use during GC. */
880 case R_M68HC11_GNU_VTINHERIT
:
881 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
885 /* This relocation describes which C++ vtable entries are actually
886 used. Record for later use during GC. */
887 case R_M68HC11_GNU_VTENTRY
:
888 BFD_ASSERT (h
!= NULL
);
890 && !bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
899 /* Relocate a 68hc11/68hc12 ELF section. */
901 elf32_m68hc11_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
902 struct bfd_link_info
*info
,
903 bfd
*input_bfd
, asection
*input_section
,
904 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
905 Elf_Internal_Sym
*local_syms
,
906 asection
**local_sections
)
908 Elf_Internal_Shdr
*symtab_hdr
;
909 struct elf_link_hash_entry
**sym_hashes
;
910 Elf_Internal_Rela
*rel
, *relend
;
911 const char *name
= NULL
;
912 struct m68hc11_page_info
*pinfo
;
913 const struct elf_backend_data
* const ebd
= get_elf_backend_data (input_bfd
);
914 struct m68hc11_elf_link_hash_table
*htab
;
915 unsigned long e_flags
;
917 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
918 sym_hashes
= elf_sym_hashes (input_bfd
);
919 e_flags
= elf_elfheader (input_bfd
)->e_flags
;
921 htab
= m68hc11_elf_hash_table (info
);
925 /* Get memory bank parameters. */
926 m68hc11_elf_get_bank_parameters (info
);
928 pinfo
= & htab
->pinfo
;
930 relend
= relocs
+ input_section
->reloc_count
;
932 for (; rel
< relend
; rel
++)
936 reloc_howto_type
*howto
;
937 unsigned long r_symndx
;
938 Elf_Internal_Sym
*sym
;
940 bfd_vma relocation
= 0;
941 bfd_reloc_status_type r
= bfd_reloc_undefined
;
946 bfd_boolean is_far
= FALSE
;
947 bfd_boolean is_xgate_symbol
= FALSE
;
948 bfd_boolean is_section_symbol
= FALSE
;
949 struct elf_link_hash_entry
*h
;
952 r_symndx
= ELF32_R_SYM (rel
->r_info
);
953 r_type
= ELF32_R_TYPE (rel
->r_info
);
955 if (r_type
== R_M68HC11_GNU_VTENTRY
956 || r_type
== R_M68HC11_GNU_VTINHERIT
)
959 (*ebd
->elf_info_to_howto_rel
) (input_bfd
, &arel
, rel
);
965 if (r_symndx
< symtab_hdr
->sh_info
)
967 sym
= local_syms
+ r_symndx
;
968 sec
= local_sections
[r_symndx
];
969 relocation
= (sec
->output_section
->vma
972 is_far
= (sym
&& (sym
->st_other
& STO_M68HC12_FAR
));
973 is_xgate_symbol
= (sym
&& (sym
->st_target_internal
));
974 is_section_symbol
= ELF_ST_TYPE (sym
->st_info
) & STT_SECTION
;
978 bfd_boolean unresolved_reloc
, warned
;
980 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
981 r_symndx
, symtab_hdr
, sym_hashes
,
982 h
, sec
, relocation
, unresolved_reloc
,
985 is_far
= (h
&& (h
->other
& STO_M68HC12_FAR
));
986 is_xgate_symbol
= (h
&& (h
->target_internal
));
989 if (sec
!= NULL
&& discarded_section (sec
))
990 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
991 rel
, 1, relend
, howto
, 0, contents
);
993 if (info
->relocatable
)
995 /* This is a relocatable link. We don't have to change
996 anything, unless the reloc is against a section symbol,
997 in which case we have to adjust according to where the
998 section symbol winds up in the output section. */
999 if (sym
!= NULL
&& ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1000 rel
->r_addend
+= sec
->output_offset
;
1005 name
= h
->root
.root
.string
;
1008 name
= (bfd_elf_string_from_elf_section
1009 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1010 if (name
== NULL
|| *name
== '\0')
1011 name
= bfd_section_name (input_bfd
, sec
);
1014 if (is_far
&& ELF32_R_TYPE (rel
->r_info
) == R_M68HC11_16
)
1016 struct elf32_m68hc11_stub_hash_entry
* stub
;
1018 stub
= m68hc12_stub_hash_lookup (htab
->stub_hash_table
,
1019 name
, FALSE
, FALSE
);
1022 relocation
= stub
->stub_offset
1023 + stub
->stub_sec
->output_section
->vma
1024 + stub
->stub_sec
->output_offset
;
1029 /* Do the memory bank mapping. */
1030 phys_addr
= m68hc11_phys_addr (pinfo
, relocation
+ rel
->r_addend
);
1031 phys_page
= m68hc11_phys_page (pinfo
, relocation
+ rel
->r_addend
);
1034 case R_M68HC12_LO8XG
:
1035 /* This relocation is specific to XGATE IMM16 calls and will precede
1036 a HI8. tc-m68hc11 only generates them in pairs.
1037 Leave the relocation to the HI8XG step. */
1039 r_type
= R_M68HC11_NONE
;
1042 case R_M68HC12_HI8XG
:
1043 /* This relocation is specific to XGATE IMM16 calls and must follow
1044 a LO8XG. Does not actually check that it was a LO8XG.
1045 Adjusts high and low bytes. */
1046 relocation
= phys_addr
;
1047 if ((e_flags
& E_M68HC11_XGATE_RAMOFFSET
)
1048 && (relocation
>= 0x2000))
1049 relocation
+= 0xc000; /* HARDCODED RAM offset for XGATE. */
1051 /* Fetch 16 bit value including low byte in previous insn. */
1052 val
= (bfd_get_8 (input_bfd
, (bfd_byte
*) contents
+ rel
->r_offset
) << 8)
1053 | bfd_get_8 (input_bfd
, (bfd_byte
*) contents
+ rel
->r_offset
- 2);
1055 /* Add on value to preserve carry, then write zero to high byte. */
1058 /* Write out top byte. */
1059 bfd_put_8 (input_bfd
, (relocation
>> 8) & 0xff,
1060 (bfd_byte
*) contents
+ rel
->r_offset
);
1062 /* Write out low byte to previous instruction. */
1063 bfd_put_8 (input_bfd
, relocation
& 0xff,
1064 (bfd_byte
*) contents
+ rel
->r_offset
- 2);
1066 /* Mark as relocation completed. */
1068 r_type
= R_M68HC11_NONE
;
1071 /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1072 assembler directives. %hi does not support carry. */
1075 relocation
= phys_addr
;
1079 /* Reloc used by 68HC12 call instruction. */
1080 bfd_put_16 (input_bfd
, phys_addr
,
1081 (bfd_byte
*) contents
+ rel
->r_offset
);
1082 bfd_put_8 (input_bfd
, phys_page
,
1083 (bfd_byte
*) contents
+ rel
->r_offset
+ 2);
1085 r_type
= R_M68HC11_NONE
;
1088 case R_M68HC11_NONE
:
1092 case R_M68HC11_LO16
:
1093 /* Reloc generated by %addr(expr) gas to obtain the
1094 address as mapped in the memory bank window. */
1095 relocation
= phys_addr
;
1098 case R_M68HC11_PAGE
:
1099 /* Reloc generated by %page(expr) gas to obtain the
1100 page number associated with the address. */
1101 relocation
= phys_page
;
1105 /* Get virtual address of instruction having the relocation. */
1110 msg
= _("Reference to the far symbol `%s' using a wrong "
1111 "relocation may result in incorrect execution");
1112 buf
= alloca (strlen (msg
) + strlen (name
) + 10);
1113 sprintf (buf
, msg
, name
);
1115 (* info
->callbacks
->warning
)
1116 (info
, buf
, name
, input_bfd
, NULL
, rel
->r_offset
);
1119 /* Get virtual address of instruction having the relocation. */
1120 insn_addr
= input_section
->output_section
->vma
1121 + input_section
->output_offset
1124 insn_page
= m68hc11_phys_page (pinfo
, insn_addr
);
1126 /* If we are linking an S12 instruction against an XGATE symbol, we
1127 need to change the offset of the symbol value so that it's correct
1128 from the S12's perspective. */
1129 if (is_xgate_symbol
)
1131 /* The ram in the global space is mapped to 0x2000 in the 16-bit
1132 address space for S12 and 0xE000 in the 16-bit address space
1134 if (relocation
>= 0xE000)
1136 /* We offset the address by the difference
1137 between these two mappings. */
1138 relocation
-= 0xC000;
1146 msg
= _("XGATE address (%lx) is not within shared RAM"
1147 "(0xE000-0xFFFF), therefore you must manually offset "
1148 "the address, and possibly manage the page, in your "
1150 buf
= alloca (strlen (msg
) + 128);
1151 sprintf (buf
, msg
, phys_addr
);
1152 if (!((*info
->callbacks
->warning
) (info
, buf
, name
, input_bfd
,
1153 input_section
, insn_addr
)))
1159 if (m68hc11_addr_is_banked (pinfo
, relocation
+ rel
->r_addend
)
1160 && m68hc11_addr_is_banked (pinfo
, insn_addr
)
1161 && phys_page
!= insn_page
&& !(e_flags
& E_M68HC11_NO_BANK_WARNING
))
1166 msg
= _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1167 "as current banked address [%lx:%04lx] (%lx)");
1169 buf
= alloca (strlen (msg
) + 128);
1170 sprintf (buf
, msg
, phys_page
, phys_addr
,
1171 (long) (relocation
+ rel
->r_addend
),
1172 insn_page
, m68hc11_phys_addr (pinfo
, insn_addr
),
1173 (long) (insn_addr
));
1174 if (!((*info
->callbacks
->warning
)
1175 (info
, buf
, name
, input_bfd
, input_section
,
1181 if (phys_page
!= 0 && insn_page
== 0)
1186 msg
= _("reference to a banked address [%lx:%04lx] in the "
1187 "normal address space at %04lx");
1189 buf
= alloca (strlen (msg
) + 128);
1190 sprintf (buf
, msg
, phys_page
, phys_addr
, insn_addr
);
1191 if (!((*info
->callbacks
->warning
)
1192 (info
, buf
, name
, input_bfd
, input_section
,
1196 relocation
= phys_addr
;
1200 /* If this is a banked address use the phys_addr so that
1201 we stay in the banked window. */
1202 if (m68hc11_addr_is_banked (pinfo
, relocation
+ rel
->r_addend
))
1203 relocation
= phys_addr
;
1207 /* If we are linking an XGATE instruction against an S12 symbol, we
1208 need to change the offset of the symbol value so that it's correct
1209 from the XGATE's perspective. */
1210 if (!strcmp (howto
->name
, "R_XGATE_IMM8_LO")
1211 || !strcmp (howto
->name
, "R_XGATE_IMM8_HI"))
1213 /* We can only offset S12 addresses that lie within the non-paged
1215 if (!is_xgate_symbol
&& !is_section_symbol
)
1217 /* The ram in the global space is mapped to 0x2000 and stops at
1218 0x4000 in the 16-bit address space for S12 and 0xE000 in the
1219 16-bit address space for XGATE. */
1220 if (relocation
>= 0x2000 && relocation
< 0x4000)
1221 /* We offset the address by the difference
1222 between these two mappings. */
1223 relocation
+= 0xC000;
1229 /* Get virtual address of instruction having the relocation. */
1230 insn_addr
= input_section
->output_section
->vma
1231 + input_section
->output_offset
+ rel
->r_offset
;
1233 msg
= _("S12 address (%lx) is not within shared RAM"
1234 "(0x2000-0x4000), therefore you must manually "
1235 "offset the address in your code");
1236 buf
= alloca (strlen (msg
) + 128);
1237 sprintf (buf
, msg
, phys_addr
);
1238 if (!((*info
->callbacks
->warning
) (info
, buf
, name
, input_bfd
,
1239 input_section
, insn_addr
)))
1246 if (r_type
!= R_M68HC11_NONE
)
1248 if ((r_type
== R_M68HC12_PCREL_9
) || (r_type
== R_M68HC12_PCREL_10
))
1249 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1250 contents
, rel
->r_offset
,
1251 relocation
- 2, rel
->r_addend
);
1253 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1254 contents
, rel
->r_offset
,
1255 relocation
, rel
->r_addend
);
1258 if (r
!= bfd_reloc_ok
)
1260 const char * msg
= (const char *) 0;
1264 case bfd_reloc_overflow
:
1265 if (!((*info
->callbacks
->reloc_overflow
)
1266 (info
, NULL
, name
, howto
->name
, (bfd_vma
) 0,
1267 input_bfd
, input_section
, rel
->r_offset
)))
1271 case bfd_reloc_undefined
:
1272 if (!((*info
->callbacks
->undefined_symbol
)
1273 (info
, name
, input_bfd
, input_section
,
1274 rel
->r_offset
, TRUE
)))
1278 case bfd_reloc_outofrange
:
1279 msg
= _ ("internal error: out of range error");
1282 case bfd_reloc_notsupported
:
1283 msg
= _ ("internal error: unsupported relocation error");
1286 case bfd_reloc_dangerous
:
1287 msg
= _ ("internal error: dangerous error");
1291 msg
= _ ("internal error: unknown error");
1295 if (!((*info
->callbacks
->warning
)
1296 (info
, msg
, name
, input_bfd
, input_section
,
1309 /* Set and control ELF flags in ELF header. */
1312 _bfd_m68hc11_elf_set_private_flags (bfd
*abfd
, flagword flags
)
1314 BFD_ASSERT (!elf_flags_init (abfd
)
1315 || elf_elfheader (abfd
)->e_flags
== flags
);
1317 elf_elfheader (abfd
)->e_flags
= flags
;
1318 elf_flags_init (abfd
) = TRUE
;
1322 /* Merge backend specific data from an object file to the output
1323 object file when linking. */
1326 _bfd_m68hc11_elf_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1330 bfd_boolean ok
= TRUE
;
1332 /* Check if we have the same endianness */
1333 if (!_bfd_generic_verify_endian_match (ibfd
, obfd
))
1336 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1337 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1340 new_flags
= elf_elfheader (ibfd
)->e_flags
;
1341 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_M68HC11_ABI
;
1342 old_flags
= elf_elfheader (obfd
)->e_flags
;
1344 if (! elf_flags_init (obfd
))
1346 elf_flags_init (obfd
) = TRUE
;
1347 elf_elfheader (obfd
)->e_flags
= new_flags
;
1348 elf_elfheader (obfd
)->e_ident
[EI_CLASS
]
1349 = elf_elfheader (ibfd
)->e_ident
[EI_CLASS
];
1351 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
1352 && bfd_get_arch_info (obfd
)->the_default
)
1354 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
1355 bfd_get_mach (ibfd
)))
1362 /* Check ABI compatibility. */
1363 if ((new_flags
& E_M68HC11_I32
) != (old_flags
& E_M68HC11_I32
))
1365 (*_bfd_error_handler
)
1366 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1367 "and others for 32-bit integers"), ibfd
);
1370 if ((new_flags
& E_M68HC11_F64
) != (old_flags
& E_M68HC11_F64
))
1372 (*_bfd_error_handler
)
1373 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1374 "and others for 64-bit double"), ibfd
);
1378 /* Processor compatibility. */
1379 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags
, old_flags
))
1381 (*_bfd_error_handler
)
1382 (_("%B: linking files compiled for HCS12 with "
1383 "others compiled for HC12"), ibfd
);
1386 new_flags
= ((new_flags
& ~EF_M68HC11_MACH_MASK
)
1387 | (EF_M68HC11_MERGE_MACH (new_flags
, old_flags
)));
1389 elf_elfheader (obfd
)->e_flags
= new_flags
;
1391 new_flags
&= ~(EF_M68HC11_ABI
| EF_M68HC11_MACH_MASK
);
1392 old_flags
&= ~(EF_M68HC11_ABI
| EF_M68HC11_MACH_MASK
);
1394 /* Warn about any other mismatches */
1395 if (new_flags
!= old_flags
)
1397 (*_bfd_error_handler
)
1398 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1399 ibfd
, (unsigned long) new_flags
, (unsigned long) old_flags
);
1405 bfd_set_error (bfd_error_bad_value
);
1413 _bfd_m68hc11_elf_print_private_bfd_data (bfd
*abfd
, void *ptr
)
1415 FILE *file
= (FILE *) ptr
;
1417 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
1419 /* Print normal ELF private data. */
1420 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
1422 /* xgettext:c-format */
1423 fprintf (file
, _("private flags = %lx:"), elf_elfheader (abfd
)->e_flags
);
1425 if (elf_elfheader (abfd
)->e_flags
& E_M68HC11_I32
)
1426 fprintf (file
, _("[abi=32-bit int, "));
1428 fprintf (file
, _("[abi=16-bit int, "));
1430 if (elf_elfheader (abfd
)->e_flags
& E_M68HC11_F64
)
1431 fprintf (file
, _("64-bit double, "));
1433 fprintf (file
, _("32-bit double, "));
1435 if (strcmp (bfd_get_target (abfd
), "elf32-m68hc11") == 0)
1436 fprintf (file
, _("cpu=HC11]"));
1437 else if (elf_elfheader (abfd
)->e_flags
& EF_M68HCS12_MACH
)
1438 fprintf (file
, _("cpu=HCS12]"));
1440 fprintf (file
, _("cpu=HC12]"));
1442 if (elf_elfheader (abfd
)->e_flags
& E_M68HC12_BANKS
)
1443 fprintf (file
, _(" [memory=bank-model]"));
1445 fprintf (file
, _(" [memory=flat]"));
1447 if (elf_elfheader (abfd
)->e_flags
& E_M68HC11_XGATE_RAMOFFSET
)
1448 fprintf (file
, _(" [XGATE RAM offsetting]"));
1455 static void scan_sections_for_abi (bfd
*abfd ATTRIBUTE_UNUSED
,
1456 asection
*asect
, void *arg
)
1458 struct m68hc11_scan_param
* p
= (struct m68hc11_scan_param
*) arg
;
1460 if (asect
->vma
>= p
->pinfo
->bank_virtual
)
1461 p
->use_memory_banks
= TRUE
;
1464 /* Tweak the OSABI field of the elf header. */
1467 elf32_m68hc11_post_process_headers (bfd
*abfd
, struct bfd_link_info
*link_info
)
1469 struct m68hc11_scan_param param
;
1470 struct m68hc11_elf_link_hash_table
*htab
;
1472 if (link_info
== NULL
)
1475 htab
= m68hc11_elf_hash_table (link_info
);
1479 m68hc11_elf_get_bank_parameters (link_info
);
1481 param
.use_memory_banks
= FALSE
;
1482 param
.pinfo
= & htab
->pinfo
;
1484 bfd_map_over_sections (abfd
, scan_sections_for_abi
, ¶m
);
1486 if (param
.use_memory_banks
)
1488 Elf_Internal_Ehdr
* i_ehdrp
;
1490 i_ehdrp
= elf_elfheader (abfd
);
1491 i_ehdrp
->e_flags
|= E_M68HC12_BANKS
;