1 /* Motorola 68HC11/HC12-specific support for 32-bit ELF
2 Copyright (C) 1999-2014 Free Software Foundation, Inc.
3 Contributed by Stephane Carrez (stcarrez@nerim.fr)
5 This file is part of BFD, the Binary File Descriptor library.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
23 #include "alloca-conf.h"
28 #include "elf32-m68hc1x.h"
29 #include "elf/m68hc11.h"
30 #include "opcode/m68hc11.h"
33 #define m68hc12_stub_hash_lookup(table, string, create, copy) \
34 ((struct elf32_m68hc11_stub_hash_entry *) \
35 bfd_hash_lookup ((table), (string), (create), (copy)))
37 static struct elf32_m68hc11_stub_hash_entry
* m68hc12_add_stub
38 (const char *stub_name
,
40 struct m68hc11_elf_link_hash_table
*htab
);
42 static struct bfd_hash_entry
*stub_hash_newfunc
43 (struct bfd_hash_entry
*, struct bfd_hash_table
*, const char *);
45 static void m68hc11_elf_set_symbol (bfd
* abfd
, struct bfd_link_info
*info
,
46 const char* name
, bfd_vma value
,
49 static bfd_boolean m68hc11_elf_export_one_stub
50 (struct bfd_hash_entry
*gen_entry
, void *in_arg
);
52 static void scan_sections_for_abi (bfd
*, asection
*, void *);
54 struct m68hc11_scan_param
56 struct m68hc11_page_info
* pinfo
;
57 bfd_boolean use_memory_banks
;
61 /* Create a 68HC11/68HC12 ELF linker hash table. */
63 struct m68hc11_elf_link_hash_table
*
64 m68hc11_elf_hash_table_create (bfd
*abfd
)
66 struct m68hc11_elf_link_hash_table
*ret
;
67 bfd_size_type amt
= sizeof (struct m68hc11_elf_link_hash_table
);
69 ret
= (struct m68hc11_elf_link_hash_table
*) bfd_zmalloc (amt
);
70 if (ret
== (struct m68hc11_elf_link_hash_table
*) NULL
)
73 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
74 _bfd_elf_link_hash_newfunc
,
75 sizeof (struct elf_link_hash_entry
),
82 /* Init the stub hash table too. */
83 amt
= sizeof (struct bfd_hash_table
);
84 ret
->stub_hash_table
= (struct bfd_hash_table
*) bfd_malloc (amt
);
85 if (ret
->stub_hash_table
== NULL
)
90 if (!bfd_hash_table_init (ret
->stub_hash_table
, stub_hash_newfunc
,
91 sizeof (struct elf32_m68hc11_stub_hash_entry
)))
97 /* Free the derived linker hash table. */
100 m68hc11_elf_bfd_link_hash_table_free (struct bfd_link_hash_table
*hash
)
102 struct m68hc11_elf_link_hash_table
*ret
103 = (struct m68hc11_elf_link_hash_table
*) hash
;
105 bfd_hash_table_free (ret
->stub_hash_table
);
106 free (ret
->stub_hash_table
);
107 _bfd_elf_link_hash_table_free (hash
);
110 /* Assorted hash table functions. */
112 /* Initialize an entry in the stub hash table. */
114 static struct bfd_hash_entry
*
115 stub_hash_newfunc (struct bfd_hash_entry
*entry
, struct bfd_hash_table
*table
,
118 /* Allocate the structure if it has not already been allocated by a
122 entry
= bfd_hash_allocate (table
,
123 sizeof (struct elf32_m68hc11_stub_hash_entry
));
128 /* Call the allocation method of the superclass. */
129 entry
= bfd_hash_newfunc (entry
, table
, string
);
132 struct elf32_m68hc11_stub_hash_entry
*eh
;
134 /* Initialize the local fields. */
135 eh
= (struct elf32_m68hc11_stub_hash_entry
*) entry
;
138 eh
->target_value
= 0;
139 eh
->target_section
= NULL
;
145 /* Add a new stub entry to the stub hash. Not all fields of the new
146 stub entry are initialised. */
148 static struct elf32_m68hc11_stub_hash_entry
*
149 m68hc12_add_stub (const char *stub_name
, asection
*section
,
150 struct m68hc11_elf_link_hash_table
*htab
)
152 struct elf32_m68hc11_stub_hash_entry
*stub_entry
;
154 /* Enter this entry into the linker stub hash table. */
155 stub_entry
= m68hc12_stub_hash_lookup (htab
->stub_hash_table
, stub_name
,
157 if (stub_entry
== NULL
)
159 (*_bfd_error_handler
) (_("%B: cannot create stub entry %s"),
160 section
->owner
, stub_name
);
164 if (htab
->stub_section
== 0)
166 htab
->stub_section
= (*htab
->add_stub_section
) (".tramp",
167 htab
->tramp_section
);
170 stub_entry
->stub_sec
= htab
->stub_section
;
171 stub_entry
->stub_offset
= 0;
175 /* Hook called by the linker routine which adds symbols from an object
176 file. We use it for identify far symbols and force a loading of
177 the trampoline handler. */
180 elf32_m68hc11_add_symbol_hook (bfd
*abfd
, struct bfd_link_info
*info
,
181 Elf_Internal_Sym
*sym
,
182 const char **namep ATTRIBUTE_UNUSED
,
183 flagword
*flagsp ATTRIBUTE_UNUSED
,
184 asection
**secp ATTRIBUTE_UNUSED
,
185 bfd_vma
*valp ATTRIBUTE_UNUSED
)
187 if (sym
->st_other
& STO_M68HC12_FAR
)
189 struct elf_link_hash_entry
*h
;
191 h
= (struct elf_link_hash_entry
*)
192 bfd_link_hash_lookup (info
->hash
, "__far_trampoline",
193 FALSE
, FALSE
, FALSE
);
196 struct bfd_link_hash_entry
* entry
= NULL
;
198 _bfd_generic_link_add_one_symbol (info
, abfd
,
202 (bfd_vma
) 0, (const char*) NULL
,
203 FALSE
, FALSE
, &entry
);
210 /* Merge non-visibility st_other attributes, STO_M68HC12_FAR and
211 STO_M68HC12_INTERRUPT. */
214 elf32_m68hc11_merge_symbol_attribute (struct elf_link_hash_entry
*h
,
215 const Elf_Internal_Sym
*isym
,
216 bfd_boolean definition
,
217 bfd_boolean dynamic ATTRIBUTE_UNUSED
)
220 h
->other
= ((isym
->st_other
& ~ELF_ST_VISIBILITY (-1))
221 | ELF_ST_VISIBILITY (h
->other
));
224 /* External entry points for sizing and building linker stubs. */
226 /* Set up various things so that we can make a list of input sections
227 for each output section included in the link. Returns -1 on error,
228 0 when no stubs will be needed, and 1 on success. */
231 elf32_m68hc11_setup_section_lists (bfd
*output_bfd
, struct bfd_link_info
*info
)
234 unsigned int bfd_count
;
235 int top_id
, top_index
;
237 asection
**input_list
, **list
;
239 asection
*text_section
;
240 struct m68hc11_elf_link_hash_table
*htab
;
242 htab
= m68hc11_elf_hash_table (info
);
246 if (bfd_get_flavour (info
->output_bfd
) != bfd_target_elf_flavour
)
249 /* Count the number of input BFDs and find the top input section id.
250 Also search for an existing ".tramp" section so that we know
251 where generated trampolines must go. Default to ".text" if we
253 htab
->tramp_section
= 0;
255 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
257 input_bfd
= input_bfd
->link_next
)
260 for (section
= input_bfd
->sections
;
262 section
= section
->next
)
264 const char* name
= bfd_get_section_name (input_bfd
, section
);
266 if (!strcmp (name
, ".tramp"))
267 htab
->tramp_section
= section
;
269 if (!strcmp (name
, ".text"))
270 text_section
= section
;
272 if (top_id
< section
->id
)
273 top_id
= section
->id
;
276 htab
->bfd_count
= bfd_count
;
277 if (htab
->tramp_section
== 0)
278 htab
->tramp_section
= text_section
;
280 /* We can't use output_bfd->section_count here to find the top output
281 section index as some sections may have been removed, and
282 strip_excluded_output_sections doesn't renumber the indices. */
283 for (section
= output_bfd
->sections
, top_index
= 0;
285 section
= section
->next
)
287 if (top_index
< section
->index
)
288 top_index
= section
->index
;
291 htab
->top_index
= top_index
;
292 amt
= sizeof (asection
*) * (top_index
+ 1);
293 input_list
= (asection
**) bfd_malloc (amt
);
294 htab
->input_list
= input_list
;
295 if (input_list
== NULL
)
298 /* For sections we aren't interested in, mark their entries with a
299 value we can check later. */
300 list
= input_list
+ top_index
;
302 *list
= bfd_abs_section_ptr
;
303 while (list
-- != input_list
);
305 for (section
= output_bfd
->sections
;
307 section
= section
->next
)
309 if ((section
->flags
& SEC_CODE
) != 0)
310 input_list
[section
->index
] = NULL
;
316 /* Determine and set the size of the stub section for a final link.
318 The basic idea here is to examine all the relocations looking for
319 PC-relative calls to a target that is unreachable with a "bl"
323 elf32_m68hc11_size_stubs (bfd
*output_bfd
, bfd
*stub_bfd
,
324 struct bfd_link_info
*info
,
325 asection
* (*add_stub_section
) (const char*, asection
*))
329 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
330 unsigned int bfd_indx
, bfd_count
;
333 struct m68hc11_elf_link_hash_table
*htab
= m68hc11_elf_hash_table (info
);
338 /* Stash our params away. */
339 htab
->stub_bfd
= stub_bfd
;
340 htab
->add_stub_section
= add_stub_section
;
342 /* Count the number of input BFDs and find the top input section id. */
343 for (input_bfd
= info
->input_bfds
, bfd_count
= 0;
345 input_bfd
= input_bfd
->link_next
)
348 /* We want to read in symbol extension records only once. To do this
349 we need to read in the local symbols in parallel and save them for
350 later use; so hold pointers to the local symbols in an array. */
351 amt
= sizeof (Elf_Internal_Sym
*) * bfd_count
;
352 all_local_syms
= (Elf_Internal_Sym
**) bfd_zmalloc (amt
);
353 if (all_local_syms
== NULL
)
356 /* Walk over all the input BFDs, swapping in local symbols. */
357 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
359 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
361 Elf_Internal_Shdr
*symtab_hdr
;
363 /* We'll need the symbol table in a second. */
364 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
365 if (symtab_hdr
->sh_info
== 0)
368 /* We need an array of the local symbols attached to the input bfd. */
369 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
370 if (local_syms
== NULL
)
372 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
373 symtab_hdr
->sh_info
, 0,
375 /* Cache them for elf_link_input_bfd. */
376 symtab_hdr
->contents
= (unsigned char *) local_syms
;
378 if (local_syms
== NULL
)
380 free (all_local_syms
);
384 all_local_syms
[bfd_indx
] = local_syms
;
387 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
389 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
391 Elf_Internal_Shdr
*symtab_hdr
;
392 struct elf_link_hash_entry
** sym_hashes
;
394 sym_hashes
= elf_sym_hashes (input_bfd
);
396 /* We'll need the symbol table in a second. */
397 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
398 if (symtab_hdr
->sh_info
== 0)
401 local_syms
= all_local_syms
[bfd_indx
];
403 /* Walk over each section attached to the input bfd. */
404 for (section
= input_bfd
->sections
;
406 section
= section
->next
)
408 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
410 /* If there aren't any relocs, then there's nothing more
412 if ((section
->flags
& SEC_RELOC
) == 0
413 || section
->reloc_count
== 0)
416 /* If this section is a link-once section that will be
417 discarded, then don't create any stubs. */
418 if (section
->output_section
== NULL
419 || section
->output_section
->owner
!= output_bfd
)
422 /* Get the relocs. */
424 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
,
425 (Elf_Internal_Rela
*) NULL
,
427 if (internal_relocs
== NULL
)
428 goto error_ret_free_local
;
430 /* Now examine each relocation. */
431 irela
= internal_relocs
;
432 irelaend
= irela
+ section
->reloc_count
;
433 for (; irela
< irelaend
; irela
++)
435 unsigned int r_type
, r_indx
;
436 struct elf32_m68hc11_stub_hash_entry
*stub_entry
;
439 struct elf_link_hash_entry
*hash
;
440 const char *stub_name
;
441 Elf_Internal_Sym
*sym
;
443 r_type
= ELF32_R_TYPE (irela
->r_info
);
445 /* Only look at 16-bit relocs. */
446 if (r_type
!= (unsigned int) R_M68HC11_16
)
449 /* Now determine the call target, its name, value,
451 r_indx
= ELF32_R_SYM (irela
->r_info
);
452 if (r_indx
< symtab_hdr
->sh_info
)
454 /* It's a local symbol. */
455 Elf_Internal_Shdr
*hdr
;
458 sym
= local_syms
+ r_indx
;
459 is_far
= (sym
&& (sym
->st_other
& STO_M68HC12_FAR
));
463 if (sym
->st_shndx
>= elf_numsections (input_bfd
))
467 hdr
= elf_elfsections (input_bfd
)[sym
->st_shndx
];
468 sym_sec
= hdr
->bfd_section
;
470 stub_name
= (bfd_elf_string_from_elf_section
471 (input_bfd
, symtab_hdr
->sh_link
,
473 sym_value
= sym
->st_value
;
478 /* It's an external symbol. */
481 e_indx
= r_indx
- symtab_hdr
->sh_info
;
482 hash
= (struct elf_link_hash_entry
*)
483 (sym_hashes
[e_indx
]);
485 while (hash
->root
.type
== bfd_link_hash_indirect
486 || hash
->root
.type
== bfd_link_hash_warning
)
487 hash
= ((struct elf_link_hash_entry
*)
488 hash
->root
.u
.i
.link
);
490 if (hash
->root
.type
== bfd_link_hash_defined
491 || hash
->root
.type
== bfd_link_hash_defweak
492 || hash
->root
.type
== bfd_link_hash_new
)
494 if (!(hash
->other
& STO_M68HC12_FAR
))
497 else if (hash
->root
.type
== bfd_link_hash_undefweak
)
501 else if (hash
->root
.type
== bfd_link_hash_undefined
)
507 bfd_set_error (bfd_error_bad_value
);
508 goto error_ret_free_internal
;
510 sym_sec
= hash
->root
.u
.def
.section
;
511 sym_value
= hash
->root
.u
.def
.value
;
512 stub_name
= hash
->root
.root
.string
;
516 goto error_ret_free_internal
;
518 stub_entry
= m68hc12_stub_hash_lookup
519 (htab
->stub_hash_table
,
522 if (stub_entry
== NULL
)
524 if (add_stub_section
== 0)
527 stub_entry
= m68hc12_add_stub (stub_name
, section
, htab
);
528 if (stub_entry
== NULL
)
530 error_ret_free_internal
:
531 if (elf_section_data (section
)->relocs
== NULL
)
532 free (internal_relocs
);
533 goto error_ret_free_local
;
537 stub_entry
->target_value
= sym_value
;
538 stub_entry
->target_section
= sym_sec
;
541 /* We're done with the internal relocs, free them. */
542 if (elf_section_data (section
)->relocs
== NULL
)
543 free (internal_relocs
);
547 if (add_stub_section
)
549 /* OK, we've added some stubs. Find out the new size of the
551 for (stub_sec
= htab
->stub_bfd
->sections
;
553 stub_sec
= stub_sec
->next
)
558 bfd_hash_traverse (htab
->stub_hash_table
, htab
->size_one_stub
, htab
);
560 free (all_local_syms
);
563 error_ret_free_local
:
564 free (all_local_syms
);
568 /* Export the trampoline addresses in the symbol table. */
570 m68hc11_elf_export_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg
)
572 struct bfd_link_info
*info
;
573 struct m68hc11_elf_link_hash_table
*htab
;
574 struct elf32_m68hc11_stub_hash_entry
*stub_entry
;
578 info
= (struct bfd_link_info
*) in_arg
;
579 htab
= m68hc11_elf_hash_table (info
);
583 /* Massage our args to the form they really have. */
584 stub_entry
= (struct elf32_m68hc11_stub_hash_entry
*) gen_entry
;
586 /* Generate the trampoline according to HC11 or HC12. */
587 result
= (* htab
->build_one_stub
) (gen_entry
, in_arg
);
589 /* Make a printable name that does not conflict with the real function. */
590 name
= alloca (strlen (stub_entry
->root
.string
) + 16);
591 sprintf (name
, "tramp.%s", stub_entry
->root
.string
);
593 /* Export the symbol for debugging/disassembling. */
594 m68hc11_elf_set_symbol (htab
->stub_bfd
, info
, name
,
595 stub_entry
->stub_offset
,
596 stub_entry
->stub_sec
);
600 /* Export a symbol or set its value and section. */
602 m68hc11_elf_set_symbol (bfd
*abfd
, struct bfd_link_info
*info
,
603 const char *name
, bfd_vma value
, asection
*sec
)
605 struct elf_link_hash_entry
*h
;
607 h
= (struct elf_link_hash_entry
*)
608 bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
611 _bfd_generic_link_add_one_symbol (info
, abfd
,
621 h
->root
.type
= bfd_link_hash_defined
;
622 h
->root
.u
.def
.value
= value
;
623 h
->root
.u
.def
.section
= sec
;
628 /* Build all the stubs associated with the current output file. The
629 stubs are kept in a hash table attached to the main linker hash
630 table. This function is called via m68hc12elf_finish in the
634 elf32_m68hc11_build_stubs (bfd
*abfd
, struct bfd_link_info
*info
)
637 struct bfd_hash_table
*table
;
638 struct m68hc11_elf_link_hash_table
*htab
;
639 struct m68hc11_scan_param param
;
641 m68hc11_elf_get_bank_parameters (info
);
642 htab
= m68hc11_elf_hash_table (info
);
646 for (stub_sec
= htab
->stub_bfd
->sections
;
648 stub_sec
= stub_sec
->next
)
652 /* Allocate memory to hold the linker stubs. */
653 size
= stub_sec
->size
;
654 stub_sec
->contents
= (unsigned char *) bfd_zalloc (htab
->stub_bfd
, size
);
655 if (stub_sec
->contents
== NULL
&& size
!= 0)
660 /* Build the stubs as directed by the stub hash table. */
661 table
= htab
->stub_hash_table
;
662 bfd_hash_traverse (table
, m68hc11_elf_export_one_stub
, info
);
664 /* Scan the output sections to see if we use the memory banks.
665 If so, export the symbols that define how the memory banks
666 are mapped. This is used by gdb and the simulator to obtain
667 the information. It can be used by programs to burn the eprom
668 at the good addresses. */
669 param
.use_memory_banks
= FALSE
;
670 param
.pinfo
= &htab
->pinfo
;
671 bfd_map_over_sections (abfd
, scan_sections_for_abi
, ¶m
);
672 if (param
.use_memory_banks
)
674 m68hc11_elf_set_symbol (abfd
, info
, BFD_M68HC11_BANK_START_NAME
,
675 htab
->pinfo
.bank_physical
,
676 bfd_abs_section_ptr
);
677 m68hc11_elf_set_symbol (abfd
, info
, BFD_M68HC11_BANK_VIRTUAL_NAME
,
678 htab
->pinfo
.bank_virtual
,
679 bfd_abs_section_ptr
);
680 m68hc11_elf_set_symbol (abfd
, info
, BFD_M68HC11_BANK_SIZE_NAME
,
681 htab
->pinfo
.bank_size
,
682 bfd_abs_section_ptr
);
689 m68hc11_elf_get_bank_parameters (struct bfd_link_info
*info
)
692 struct m68hc11_page_info
*pinfo
;
693 struct bfd_link_hash_entry
*h
;
694 struct m68hc11_elf_link_hash_table
*htab
;
696 htab
= m68hc11_elf_hash_table (info
);
700 pinfo
= & htab
->pinfo
;
701 if (pinfo
->bank_param_initialized
)
704 pinfo
->bank_virtual
= M68HC12_BANK_VIRT
;
705 pinfo
->bank_mask
= M68HC12_BANK_MASK
;
706 pinfo
->bank_physical
= M68HC12_BANK_BASE
;
707 pinfo
->bank_shift
= M68HC12_BANK_SHIFT
;
708 pinfo
->bank_size
= 1 << M68HC12_BANK_SHIFT
;
710 h
= bfd_link_hash_lookup (info
->hash
, BFD_M68HC11_BANK_START_NAME
,
712 if (h
!= (struct bfd_link_hash_entry
*) NULL
713 && h
->type
== bfd_link_hash_defined
)
714 pinfo
->bank_physical
= (h
->u
.def
.value
715 + h
->u
.def
.section
->output_section
->vma
716 + h
->u
.def
.section
->output_offset
);
718 h
= bfd_link_hash_lookup (info
->hash
, BFD_M68HC11_BANK_VIRTUAL_NAME
,
720 if (h
!= (struct bfd_link_hash_entry
*) NULL
721 && h
->type
== bfd_link_hash_defined
)
722 pinfo
->bank_virtual
= (h
->u
.def
.value
723 + h
->u
.def
.section
->output_section
->vma
724 + h
->u
.def
.section
->output_offset
);
726 h
= bfd_link_hash_lookup (info
->hash
, BFD_M68HC11_BANK_SIZE_NAME
,
728 if (h
!= (struct bfd_link_hash_entry
*) NULL
729 && h
->type
== bfd_link_hash_defined
)
730 pinfo
->bank_size
= (h
->u
.def
.value
731 + h
->u
.def
.section
->output_section
->vma
732 + h
->u
.def
.section
->output_offset
);
734 pinfo
->bank_shift
= 0;
735 for (i
= pinfo
->bank_size
; i
!= 0; i
>>= 1)
738 pinfo
->bank_mask
= (1 << pinfo
->bank_shift
) - 1;
739 pinfo
->bank_physical_end
= pinfo
->bank_physical
+ pinfo
->bank_size
;
740 pinfo
->bank_param_initialized
= 1;
742 h
= bfd_link_hash_lookup (info
->hash
, "__far_trampoline", FALSE
,
744 if (h
!= (struct bfd_link_hash_entry
*) NULL
745 && h
->type
== bfd_link_hash_defined
)
746 pinfo
->trampoline_addr
= (h
->u
.def
.value
747 + h
->u
.def
.section
->output_section
->vma
748 + h
->u
.def
.section
->output_offset
);
751 /* Return 1 if the address is in banked memory.
752 This can be applied to a virtual address and to a physical address. */
754 m68hc11_addr_is_banked (struct m68hc11_page_info
*pinfo
, bfd_vma addr
)
756 if (addr
>= pinfo
->bank_virtual
)
759 if (addr
>= pinfo
->bank_physical
&& addr
<= pinfo
->bank_physical_end
)
765 /* Return the physical address seen by the processor, taking
766 into account banked memory. */
768 m68hc11_phys_addr (struct m68hc11_page_info
*pinfo
, bfd_vma addr
)
770 if (addr
< pinfo
->bank_virtual
)
773 /* Map the address to the memory bank. */
774 addr
-= pinfo
->bank_virtual
;
775 addr
&= pinfo
->bank_mask
;
776 addr
+= pinfo
->bank_physical
;
780 /* Return the page number corresponding to an address in banked memory. */
782 m68hc11_phys_page (struct m68hc11_page_info
*pinfo
, bfd_vma addr
)
784 if (addr
< pinfo
->bank_virtual
)
787 /* Map the address to the memory bank. */
788 addr
-= pinfo
->bank_virtual
;
789 addr
>>= pinfo
->bank_shift
;
794 /* This function is used for relocs which are only used for relaxing,
795 which the linker should otherwise ignore. */
797 bfd_reloc_status_type
798 m68hc11_elf_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
799 arelent
*reloc_entry
,
800 asymbol
*symbol ATTRIBUTE_UNUSED
,
801 void *data ATTRIBUTE_UNUSED
,
802 asection
*input_section
,
804 char **error_message ATTRIBUTE_UNUSED
)
806 if (output_bfd
!= NULL
)
807 reloc_entry
->address
+= input_section
->output_offset
;
811 bfd_reloc_status_type
812 m68hc11_elf_special_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
813 arelent
*reloc_entry
,
815 void *data ATTRIBUTE_UNUSED
,
816 asection
*input_section
,
818 char **error_message ATTRIBUTE_UNUSED
)
820 if (output_bfd
!= (bfd
*) NULL
821 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
822 && (! reloc_entry
->howto
->partial_inplace
823 || reloc_entry
->addend
== 0))
825 reloc_entry
->address
+= input_section
->output_offset
;
829 if (output_bfd
!= NULL
)
830 return bfd_reloc_continue
;
832 if (reloc_entry
->address
> bfd_get_section_limit (abfd
, input_section
))
833 return bfd_reloc_outofrange
;
838 /* Look through the relocs for a section during the first phase.
839 Since we don't do .gots or .plts, we just need to consider the
840 virtual table relocs for gc. */
843 elf32_m68hc11_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
844 asection
*sec
, const Elf_Internal_Rela
*relocs
)
846 Elf_Internal_Shdr
* symtab_hdr
;
847 struct elf_link_hash_entry
** sym_hashes
;
848 const Elf_Internal_Rela
* rel
;
849 const Elf_Internal_Rela
* rel_end
;
851 if (info
->relocatable
)
854 symtab_hdr
= & elf_tdata (abfd
)->symtab_hdr
;
855 sym_hashes
= elf_sym_hashes (abfd
);
856 rel_end
= relocs
+ sec
->reloc_count
;
858 for (rel
= relocs
; rel
< rel_end
; rel
++)
860 struct elf_link_hash_entry
* h
;
861 unsigned long r_symndx
;
863 r_symndx
= ELF32_R_SYM (rel
->r_info
);
865 if (r_symndx
< symtab_hdr
->sh_info
)
869 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
870 while (h
->root
.type
== bfd_link_hash_indirect
871 || h
->root
.type
== bfd_link_hash_warning
)
872 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
874 /* PR15323, ref flags aren't set for references in the same
876 h
->root
.non_ir_ref
= 1;
879 switch (ELF32_R_TYPE (rel
->r_info
))
881 /* This relocation describes the C++ object vtable hierarchy.
882 Reconstruct it for later use during GC. */
883 case R_M68HC11_GNU_VTINHERIT
:
884 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
888 /* This relocation describes which C++ vtable entries are actually
889 used. Record for later use during GC. */
890 case R_M68HC11_GNU_VTENTRY
:
891 BFD_ASSERT (h
!= NULL
);
893 && !bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
902 /* Relocate a 68hc11/68hc12 ELF section. */
904 elf32_m68hc11_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
905 struct bfd_link_info
*info
,
906 bfd
*input_bfd
, asection
*input_section
,
907 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
908 Elf_Internal_Sym
*local_syms
,
909 asection
**local_sections
)
911 Elf_Internal_Shdr
*symtab_hdr
;
912 struct elf_link_hash_entry
**sym_hashes
;
913 Elf_Internal_Rela
*rel
, *relend
;
914 const char *name
= NULL
;
915 struct m68hc11_page_info
*pinfo
;
916 const struct elf_backend_data
* const ebd
= get_elf_backend_data (input_bfd
);
917 struct m68hc11_elf_link_hash_table
*htab
;
918 unsigned long e_flags
;
920 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
921 sym_hashes
= elf_sym_hashes (input_bfd
);
922 e_flags
= elf_elfheader (input_bfd
)->e_flags
;
924 htab
= m68hc11_elf_hash_table (info
);
928 /* Get memory bank parameters. */
929 m68hc11_elf_get_bank_parameters (info
);
931 pinfo
= & htab
->pinfo
;
933 relend
= relocs
+ input_section
->reloc_count
;
935 for (; rel
< relend
; rel
++)
939 reloc_howto_type
*howto
;
940 unsigned long r_symndx
;
941 Elf_Internal_Sym
*sym
;
943 bfd_vma relocation
= 0;
944 bfd_reloc_status_type r
= bfd_reloc_undefined
;
949 bfd_boolean is_far
= FALSE
;
950 bfd_boolean is_xgate_symbol
= FALSE
;
951 bfd_boolean is_section_symbol
= FALSE
;
952 struct elf_link_hash_entry
*h
;
955 r_symndx
= ELF32_R_SYM (rel
->r_info
);
956 r_type
= ELF32_R_TYPE (rel
->r_info
);
958 if (r_type
== R_M68HC11_GNU_VTENTRY
959 || r_type
== R_M68HC11_GNU_VTINHERIT
)
962 (*ebd
->elf_info_to_howto_rel
) (input_bfd
, &arel
, rel
);
968 if (r_symndx
< symtab_hdr
->sh_info
)
970 sym
= local_syms
+ r_symndx
;
971 sec
= local_sections
[r_symndx
];
972 relocation
= (sec
->output_section
->vma
975 is_far
= (sym
&& (sym
->st_other
& STO_M68HC12_FAR
));
976 is_xgate_symbol
= (sym
&& (sym
->st_target_internal
));
977 is_section_symbol
= ELF_ST_TYPE (sym
->st_info
) & STT_SECTION
;
981 bfd_boolean unresolved_reloc
, warned
, ignored
;
983 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
984 r_symndx
, symtab_hdr
, sym_hashes
,
985 h
, sec
, relocation
, unresolved_reloc
,
988 is_far
= (h
&& (h
->other
& STO_M68HC12_FAR
));
989 is_xgate_symbol
= (h
&& (h
->target_internal
));
992 if (sec
!= NULL
&& discarded_section (sec
))
993 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
994 rel
, 1, relend
, howto
, 0, contents
);
996 if (info
->relocatable
)
998 /* This is a relocatable link. We don't have to change
999 anything, unless the reloc is against a section symbol,
1000 in which case we have to adjust according to where the
1001 section symbol winds up in the output section. */
1002 if (sym
!= NULL
&& ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1003 rel
->r_addend
+= sec
->output_offset
;
1008 name
= h
->root
.root
.string
;
1011 name
= (bfd_elf_string_from_elf_section
1012 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1013 if (name
== NULL
|| *name
== '\0')
1014 name
= bfd_section_name (input_bfd
, sec
);
1017 if (is_far
&& ELF32_R_TYPE (rel
->r_info
) == R_M68HC11_16
)
1019 struct elf32_m68hc11_stub_hash_entry
* stub
;
1021 stub
= m68hc12_stub_hash_lookup (htab
->stub_hash_table
,
1022 name
, FALSE
, FALSE
);
1025 relocation
= stub
->stub_offset
1026 + stub
->stub_sec
->output_section
->vma
1027 + stub
->stub_sec
->output_offset
;
1032 /* Do the memory bank mapping. */
1033 phys_addr
= m68hc11_phys_addr (pinfo
, relocation
+ rel
->r_addend
);
1034 phys_page
= m68hc11_phys_page (pinfo
, relocation
+ rel
->r_addend
);
1037 case R_M68HC12_LO8XG
:
1038 /* This relocation is specific to XGATE IMM16 calls and will precede
1039 a HI8. tc-m68hc11 only generates them in pairs.
1040 Leave the relocation to the HI8XG step. */
1042 r_type
= R_M68HC11_NONE
;
1045 case R_M68HC12_HI8XG
:
1046 /* This relocation is specific to XGATE IMM16 calls and must follow
1047 a LO8XG. Does not actually check that it was a LO8XG.
1048 Adjusts high and low bytes. */
1049 relocation
= phys_addr
;
1050 if ((e_flags
& E_M68HC11_XGATE_RAMOFFSET
)
1051 && (relocation
>= 0x2000))
1052 relocation
+= 0xc000; /* HARDCODED RAM offset for XGATE. */
1054 /* Fetch 16 bit value including low byte in previous insn. */
1055 val
= (bfd_get_8 (input_bfd
, (bfd_byte
*) contents
+ rel
->r_offset
) << 8)
1056 | bfd_get_8 (input_bfd
, (bfd_byte
*) contents
+ rel
->r_offset
- 2);
1058 /* Add on value to preserve carry, then write zero to high byte. */
1061 /* Write out top byte. */
1062 bfd_put_8 (input_bfd
, (relocation
>> 8) & 0xff,
1063 (bfd_byte
*) contents
+ rel
->r_offset
);
1065 /* Write out low byte to previous instruction. */
1066 bfd_put_8 (input_bfd
, relocation
& 0xff,
1067 (bfd_byte
*) contents
+ rel
->r_offset
- 2);
1069 /* Mark as relocation completed. */
1071 r_type
= R_M68HC11_NONE
;
1074 /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1075 assembler directives. %hi does not support carry. */
1078 relocation
= phys_addr
;
1082 /* Reloc used by 68HC12 call instruction. */
1083 bfd_put_16 (input_bfd
, phys_addr
,
1084 (bfd_byte
*) contents
+ rel
->r_offset
);
1085 bfd_put_8 (input_bfd
, phys_page
,
1086 (bfd_byte
*) contents
+ rel
->r_offset
+ 2);
1088 r_type
= R_M68HC11_NONE
;
1091 case R_M68HC11_NONE
:
1095 case R_M68HC11_LO16
:
1096 /* Reloc generated by %addr(expr) gas to obtain the
1097 address as mapped in the memory bank window. */
1098 relocation
= phys_addr
;
1101 case R_M68HC11_PAGE
:
1102 /* Reloc generated by %page(expr) gas to obtain the
1103 page number associated with the address. */
1104 relocation
= phys_page
;
1108 /* Get virtual address of instruction having the relocation. */
1113 msg
= _("Reference to the far symbol `%s' using a wrong "
1114 "relocation may result in incorrect execution");
1115 buf
= alloca (strlen (msg
) + strlen (name
) + 10);
1116 sprintf (buf
, msg
, name
);
1118 (* info
->callbacks
->warning
)
1119 (info
, buf
, name
, input_bfd
, NULL
, rel
->r_offset
);
1122 /* Get virtual address of instruction having the relocation. */
1123 insn_addr
= input_section
->output_section
->vma
1124 + input_section
->output_offset
1127 insn_page
= m68hc11_phys_page (pinfo
, insn_addr
);
1129 /* If we are linking an S12 instruction against an XGATE symbol, we
1130 need to change the offset of the symbol value so that it's correct
1131 from the S12's perspective. */
1132 if (is_xgate_symbol
)
1134 /* The ram in the global space is mapped to 0x2000 in the 16-bit
1135 address space for S12 and 0xE000 in the 16-bit address space
1137 if (relocation
>= 0xE000)
1139 /* We offset the address by the difference
1140 between these two mappings. */
1141 relocation
-= 0xC000;
1149 msg
= _("XGATE address (%lx) is not within shared RAM"
1150 "(0xE000-0xFFFF), therefore you must manually offset "
1151 "the address, and possibly manage the page, in your "
1153 buf
= alloca (strlen (msg
) + 128);
1154 sprintf (buf
, msg
, phys_addr
);
1155 if (!((*info
->callbacks
->warning
) (info
, buf
, name
, input_bfd
,
1156 input_section
, insn_addr
)))
1162 if (m68hc11_addr_is_banked (pinfo
, relocation
+ rel
->r_addend
)
1163 && m68hc11_addr_is_banked (pinfo
, insn_addr
)
1164 && phys_page
!= insn_page
&& !(e_flags
& E_M68HC11_NO_BANK_WARNING
))
1169 msg
= _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1170 "as current banked address [%lx:%04lx] (%lx)");
1172 buf
= alloca (strlen (msg
) + 128);
1173 sprintf (buf
, msg
, phys_page
, phys_addr
,
1174 (long) (relocation
+ rel
->r_addend
),
1175 insn_page
, m68hc11_phys_addr (pinfo
, insn_addr
),
1176 (long) (insn_addr
));
1177 if (!((*info
->callbacks
->warning
)
1178 (info
, buf
, name
, input_bfd
, input_section
,
1184 if (phys_page
!= 0 && insn_page
== 0)
1189 msg
= _("reference to a banked address [%lx:%04lx] in the "
1190 "normal address space at %04lx");
1192 buf
= alloca (strlen (msg
) + 128);
1193 sprintf (buf
, msg
, phys_page
, phys_addr
, insn_addr
);
1194 if (!((*info
->callbacks
->warning
)
1195 (info
, buf
, name
, input_bfd
, input_section
,
1199 relocation
= phys_addr
;
1203 /* If this is a banked address use the phys_addr so that
1204 we stay in the banked window. */
1205 if (m68hc11_addr_is_banked (pinfo
, relocation
+ rel
->r_addend
))
1206 relocation
= phys_addr
;
1210 /* If we are linking an XGATE instruction against an S12 symbol, we
1211 need to change the offset of the symbol value so that it's correct
1212 from the XGATE's perspective. */
1213 if (!strcmp (howto
->name
, "R_XGATE_IMM8_LO")
1214 || !strcmp (howto
->name
, "R_XGATE_IMM8_HI"))
1216 /* We can only offset S12 addresses that lie within the non-paged
1218 if (!is_xgate_symbol
&& !is_section_symbol
)
1220 /* The ram in the global space is mapped to 0x2000 and stops at
1221 0x4000 in the 16-bit address space for S12 and 0xE000 in the
1222 16-bit address space for XGATE. */
1223 if (relocation
>= 0x2000 && relocation
< 0x4000)
1224 /* We offset the address by the difference
1225 between these two mappings. */
1226 relocation
+= 0xC000;
1232 /* Get virtual address of instruction having the relocation. */
1233 insn_addr
= input_section
->output_section
->vma
1234 + input_section
->output_offset
+ rel
->r_offset
;
1236 msg
= _("S12 address (%lx) is not within shared RAM"
1237 "(0x2000-0x4000), therefore you must manually "
1238 "offset the address in your code");
1239 buf
= alloca (strlen (msg
) + 128);
1240 sprintf (buf
, msg
, phys_addr
);
1241 if (!((*info
->callbacks
->warning
) (info
, buf
, name
, input_bfd
,
1242 input_section
, insn_addr
)))
1249 if (r_type
!= R_M68HC11_NONE
)
1251 if ((r_type
== R_M68HC12_PCREL_9
) || (r_type
== R_M68HC12_PCREL_10
))
1252 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1253 contents
, rel
->r_offset
,
1254 relocation
- 2, rel
->r_addend
);
1256 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1257 contents
, rel
->r_offset
,
1258 relocation
, rel
->r_addend
);
1261 if (r
!= bfd_reloc_ok
)
1263 const char * msg
= (const char *) 0;
1267 case bfd_reloc_overflow
:
1268 if (!((*info
->callbacks
->reloc_overflow
)
1269 (info
, NULL
, name
, howto
->name
, (bfd_vma
) 0,
1270 input_bfd
, input_section
, rel
->r_offset
)))
1274 case bfd_reloc_undefined
:
1275 if (!((*info
->callbacks
->undefined_symbol
)
1276 (info
, name
, input_bfd
, input_section
,
1277 rel
->r_offset
, TRUE
)))
1281 case bfd_reloc_outofrange
:
1282 msg
= _ ("internal error: out of range error");
1285 case bfd_reloc_notsupported
:
1286 msg
= _ ("internal error: unsupported relocation error");
1289 case bfd_reloc_dangerous
:
1290 msg
= _ ("internal error: dangerous error");
1294 msg
= _ ("internal error: unknown error");
1298 if (!((*info
->callbacks
->warning
)
1299 (info
, msg
, name
, input_bfd
, input_section
,
1312 /* Set and control ELF flags in ELF header. */
1315 _bfd_m68hc11_elf_set_private_flags (bfd
*abfd
, flagword flags
)
1317 BFD_ASSERT (!elf_flags_init (abfd
)
1318 || elf_elfheader (abfd
)->e_flags
== flags
);
1320 elf_elfheader (abfd
)->e_flags
= flags
;
1321 elf_flags_init (abfd
) = TRUE
;
1325 /* Merge backend specific data from an object file to the output
1326 object file when linking. */
1329 _bfd_m68hc11_elf_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1333 bfd_boolean ok
= TRUE
;
1335 /* Check if we have the same endianness */
1336 if (!_bfd_generic_verify_endian_match (ibfd
, obfd
))
1339 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1340 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1343 new_flags
= elf_elfheader (ibfd
)->e_flags
;
1344 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_M68HC11_ABI
;
1345 old_flags
= elf_elfheader (obfd
)->e_flags
;
1347 if (! elf_flags_init (obfd
))
1349 elf_flags_init (obfd
) = TRUE
;
1350 elf_elfheader (obfd
)->e_flags
= new_flags
;
1351 elf_elfheader (obfd
)->e_ident
[EI_CLASS
]
1352 = elf_elfheader (ibfd
)->e_ident
[EI_CLASS
];
1354 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
1355 && bfd_get_arch_info (obfd
)->the_default
)
1357 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
1358 bfd_get_mach (ibfd
)))
1365 /* Check ABI compatibility. */
1366 if ((new_flags
& E_M68HC11_I32
) != (old_flags
& E_M68HC11_I32
))
1368 (*_bfd_error_handler
)
1369 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1370 "and others for 32-bit integers"), ibfd
);
1373 if ((new_flags
& E_M68HC11_F64
) != (old_flags
& E_M68HC11_F64
))
1375 (*_bfd_error_handler
)
1376 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1377 "and others for 64-bit double"), ibfd
);
1381 /* Processor compatibility. */
1382 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags
, old_flags
))
1384 (*_bfd_error_handler
)
1385 (_("%B: linking files compiled for HCS12 with "
1386 "others compiled for HC12"), ibfd
);
1389 new_flags
= ((new_flags
& ~EF_M68HC11_MACH_MASK
)
1390 | (EF_M68HC11_MERGE_MACH (new_flags
, old_flags
)));
1392 elf_elfheader (obfd
)->e_flags
= new_flags
;
1394 new_flags
&= ~(EF_M68HC11_ABI
| EF_M68HC11_MACH_MASK
);
1395 old_flags
&= ~(EF_M68HC11_ABI
| EF_M68HC11_MACH_MASK
);
1397 /* Warn about any other mismatches */
1398 if (new_flags
!= old_flags
)
1400 (*_bfd_error_handler
)
1401 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1402 ibfd
, (unsigned long) new_flags
, (unsigned long) old_flags
);
1408 bfd_set_error (bfd_error_bad_value
);
1416 _bfd_m68hc11_elf_print_private_bfd_data (bfd
*abfd
, void *ptr
)
1418 FILE *file
= (FILE *) ptr
;
1420 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
1422 /* Print normal ELF private data. */
1423 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
1425 /* xgettext:c-format */
1426 fprintf (file
, _("private flags = %lx:"), elf_elfheader (abfd
)->e_flags
);
1428 if (elf_elfheader (abfd
)->e_flags
& E_M68HC11_I32
)
1429 fprintf (file
, _("[abi=32-bit int, "));
1431 fprintf (file
, _("[abi=16-bit int, "));
1433 if (elf_elfheader (abfd
)->e_flags
& E_M68HC11_F64
)
1434 fprintf (file
, _("64-bit double, "));
1436 fprintf (file
, _("32-bit double, "));
1438 if (strcmp (bfd_get_target (abfd
), "elf32-m68hc11") == 0)
1439 fprintf (file
, _("cpu=HC11]"));
1440 else if (elf_elfheader (abfd
)->e_flags
& EF_M68HCS12_MACH
)
1441 fprintf (file
, _("cpu=HCS12]"));
1443 fprintf (file
, _("cpu=HC12]"));
1445 if (elf_elfheader (abfd
)->e_flags
& E_M68HC12_BANKS
)
1446 fprintf (file
, _(" [memory=bank-model]"));
1448 fprintf (file
, _(" [memory=flat]"));
1450 if (elf_elfheader (abfd
)->e_flags
& E_M68HC11_XGATE_RAMOFFSET
)
1451 fprintf (file
, _(" [XGATE RAM offsetting]"));
1458 static void scan_sections_for_abi (bfd
*abfd ATTRIBUTE_UNUSED
,
1459 asection
*asect
, void *arg
)
1461 struct m68hc11_scan_param
* p
= (struct m68hc11_scan_param
*) arg
;
1463 if (asect
->vma
>= p
->pinfo
->bank_virtual
)
1464 p
->use_memory_banks
= TRUE
;
1467 /* Tweak the OSABI field of the elf header. */
1470 elf32_m68hc11_post_process_headers (bfd
*abfd
, struct bfd_link_info
*link_info
)
1472 struct m68hc11_scan_param param
;
1473 struct m68hc11_elf_link_hash_table
*htab
;
1475 if (link_info
== NULL
)
1478 htab
= m68hc11_elf_hash_table (link_info
);
1482 m68hc11_elf_get_bank_parameters (link_info
);
1484 param
.use_memory_banks
= FALSE
;
1485 param
.pinfo
= & htab
->pinfo
;
1487 bfd_map_over_sections (abfd
, scan_sections_for_abi
, ¶m
);
1489 if (param
.use_memory_banks
)
1491 Elf_Internal_Ehdr
* i_ehdrp
;
1493 i_ehdrp
= elf_elfheader (abfd
);
1494 i_ehdrp
->e_flags
|= E_M68HC12_BANKS
;