1 /* Motorola 68HC11/HC12-specific support for 32-bit ELF
2 Copyright (C) 1999-2017 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"
31 #include "libiberty.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 /* Destroy a 68HC11/68HC12 ELF linker hash table. */
64 m68hc11_elf_bfd_link_hash_table_free (bfd
*obfd
)
66 struct m68hc11_elf_link_hash_table
*ret
67 = (struct m68hc11_elf_link_hash_table
*) obfd
->link
.hash
;
69 bfd_hash_table_free (ret
->stub_hash_table
);
70 free (ret
->stub_hash_table
);
71 _bfd_elf_link_hash_table_free (obfd
);
74 /* Create a 68HC11/68HC12 ELF linker hash table. */
76 struct m68hc11_elf_link_hash_table
*
77 m68hc11_elf_hash_table_create (bfd
*abfd
)
79 struct m68hc11_elf_link_hash_table
*ret
;
80 bfd_size_type amt
= sizeof (struct m68hc11_elf_link_hash_table
);
82 ret
= (struct m68hc11_elf_link_hash_table
*) bfd_zmalloc (amt
);
83 if (ret
== (struct m68hc11_elf_link_hash_table
*) NULL
)
86 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
87 _bfd_elf_link_hash_newfunc
,
88 sizeof (struct elf_link_hash_entry
),
95 /* Init the stub hash table too. */
96 amt
= sizeof (struct bfd_hash_table
);
97 ret
->stub_hash_table
= (struct bfd_hash_table
*) bfd_malloc (amt
);
98 if (ret
->stub_hash_table
== NULL
)
100 _bfd_elf_link_hash_table_free (abfd
);
103 if (!bfd_hash_table_init (ret
->stub_hash_table
, stub_hash_newfunc
,
104 sizeof (struct elf32_m68hc11_stub_hash_entry
)))
106 free (ret
->stub_hash_table
);
107 _bfd_elf_link_hash_table_free (abfd
);
110 ret
->root
.root
.hash_table_free
= m68hc11_elf_bfd_link_hash_table_free
;
115 /* Assorted hash table functions. */
117 /* Initialize an entry in the stub hash table. */
119 static struct bfd_hash_entry
*
120 stub_hash_newfunc (struct bfd_hash_entry
*entry
, struct bfd_hash_table
*table
,
123 /* Allocate the structure if it has not already been allocated by a
127 entry
= bfd_hash_allocate (table
,
128 sizeof (struct elf32_m68hc11_stub_hash_entry
));
133 /* Call the allocation method of the superclass. */
134 entry
= bfd_hash_newfunc (entry
, table
, string
);
137 struct elf32_m68hc11_stub_hash_entry
*eh
;
139 /* Initialize the local fields. */
140 eh
= (struct elf32_m68hc11_stub_hash_entry
*) entry
;
143 eh
->target_value
= 0;
144 eh
->target_section
= NULL
;
150 /* Add a new stub entry to the stub hash. Not all fields of the new
151 stub entry are initialised. */
153 static struct elf32_m68hc11_stub_hash_entry
*
154 m68hc12_add_stub (const char *stub_name
, asection
*section
,
155 struct m68hc11_elf_link_hash_table
*htab
)
157 struct elf32_m68hc11_stub_hash_entry
*stub_entry
;
159 /* Enter this entry into the linker stub hash table. */
160 stub_entry
= m68hc12_stub_hash_lookup (htab
->stub_hash_table
, stub_name
,
162 if (stub_entry
== NULL
)
164 /* xgettext:c-format */
165 _bfd_error_handler (_("%B: cannot create stub entry %s"),
166 section
->owner
, stub_name
);
170 if (htab
->stub_section
== 0)
172 htab
->stub_section
= (*htab
->add_stub_section
) (".tramp",
173 htab
->tramp_section
);
176 stub_entry
->stub_sec
= htab
->stub_section
;
177 stub_entry
->stub_offset
= 0;
181 /* Hook called by the linker routine which adds symbols from an object
182 file. We use it for identify far symbols and force a loading of
183 the trampoline handler. */
186 elf32_m68hc11_add_symbol_hook (bfd
*abfd
, struct bfd_link_info
*info
,
187 Elf_Internal_Sym
*sym
,
188 const char **namep ATTRIBUTE_UNUSED
,
189 flagword
*flagsp ATTRIBUTE_UNUSED
,
190 asection
**secp ATTRIBUTE_UNUSED
,
191 bfd_vma
*valp ATTRIBUTE_UNUSED
)
193 if (sym
->st_other
& STO_M68HC12_FAR
)
195 struct elf_link_hash_entry
*h
;
197 h
= (struct elf_link_hash_entry
*)
198 bfd_link_hash_lookup (info
->hash
, "__far_trampoline",
199 FALSE
, FALSE
, FALSE
);
202 struct bfd_link_hash_entry
* entry
= NULL
;
204 _bfd_generic_link_add_one_symbol (info
, abfd
,
208 (bfd_vma
) 0, (const char*) NULL
,
209 FALSE
, FALSE
, &entry
);
216 /* Merge non-visibility st_other attributes, STO_M68HC12_FAR and
217 STO_M68HC12_INTERRUPT. */
220 elf32_m68hc11_merge_symbol_attribute (struct elf_link_hash_entry
*h
,
221 const Elf_Internal_Sym
*isym
,
222 bfd_boolean definition
,
223 bfd_boolean dynamic ATTRIBUTE_UNUSED
)
226 h
->other
= ((isym
->st_other
& ~ELF_ST_VISIBILITY (-1))
227 | ELF_ST_VISIBILITY (h
->other
));
230 /* External entry points for sizing and building linker stubs. */
232 /* Set up various things so that we can make a list of input sections
233 for each output section included in the link. Returns -1 on error,
234 0 when no stubs will be needed, and 1 on success. */
237 elf32_m68hc11_setup_section_lists (bfd
*output_bfd
, struct bfd_link_info
*info
)
240 unsigned int bfd_count
;
241 unsigned int top_id
, top_index
;
243 asection
**input_list
, **list
;
245 asection
*text_section
;
246 struct m68hc11_elf_link_hash_table
*htab
;
248 htab
= m68hc11_elf_hash_table (info
);
252 if (bfd_get_flavour (info
->output_bfd
) != bfd_target_elf_flavour
)
255 /* Count the number of input BFDs and find the top input section id.
256 Also search for an existing ".tramp" section so that we know
257 where generated trampolines must go. Default to ".text" if we
259 htab
->tramp_section
= 0;
261 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
263 input_bfd
= input_bfd
->link
.next
)
266 for (section
= input_bfd
->sections
;
268 section
= section
->next
)
270 const char* name
= bfd_get_section_name (input_bfd
, section
);
272 if (!strcmp (name
, ".tramp"))
273 htab
->tramp_section
= section
;
275 if (!strcmp (name
, ".text"))
276 text_section
= section
;
278 if (top_id
< section
->id
)
279 top_id
= section
->id
;
282 htab
->bfd_count
= bfd_count
;
283 if (htab
->tramp_section
== 0)
284 htab
->tramp_section
= text_section
;
286 /* We can't use output_bfd->section_count here to find the top output
287 section index as some sections may have been removed, and
288 strip_excluded_output_sections doesn't renumber the indices. */
289 for (section
= output_bfd
->sections
, top_index
= 0;
291 section
= section
->next
)
293 if (top_index
< section
->index
)
294 top_index
= section
->index
;
297 htab
->top_index
= top_index
;
298 amt
= sizeof (asection
*) * (top_index
+ 1);
299 input_list
= (asection
**) bfd_malloc (amt
);
300 htab
->input_list
= input_list
;
301 if (input_list
== NULL
)
304 /* For sections we aren't interested in, mark their entries with a
305 value we can check later. */
306 list
= input_list
+ top_index
;
308 *list
= bfd_abs_section_ptr
;
309 while (list
-- != input_list
);
311 for (section
= output_bfd
->sections
;
313 section
= section
->next
)
315 if ((section
->flags
& SEC_CODE
) != 0)
316 input_list
[section
->index
] = NULL
;
322 /* Determine and set the size of the stub section for a final link.
324 The basic idea here is to examine all the relocations looking for
325 PC-relative calls to a target that is unreachable with a "bl"
329 elf32_m68hc11_size_stubs (bfd
*output_bfd
, bfd
*stub_bfd
,
330 struct bfd_link_info
*info
,
331 asection
* (*add_stub_section
) (const char*, asection
*))
335 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
336 unsigned int bfd_indx
, bfd_count
;
339 struct m68hc11_elf_link_hash_table
*htab
= m68hc11_elf_hash_table (info
);
344 /* Stash our params away. */
345 htab
->stub_bfd
= stub_bfd
;
346 htab
->add_stub_section
= add_stub_section
;
348 /* Count the number of input BFDs and find the top input section id. */
349 for (input_bfd
= info
->input_bfds
, bfd_count
= 0;
351 input_bfd
= input_bfd
->link
.next
)
354 /* We want to read in symbol extension records only once. To do this
355 we need to read in the local symbols in parallel and save them for
356 later use; so hold pointers to the local symbols in an array. */
357 amt
= sizeof (Elf_Internal_Sym
*) * bfd_count
;
358 all_local_syms
= (Elf_Internal_Sym
**) bfd_zmalloc (amt
);
359 if (all_local_syms
== NULL
)
362 /* Walk over all the input BFDs, swapping in local symbols. */
363 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
365 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
367 Elf_Internal_Shdr
*symtab_hdr
;
369 /* We'll need the symbol table in a second. */
370 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
371 if (symtab_hdr
->sh_info
== 0)
374 /* We need an array of the local symbols attached to the input bfd. */
375 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
376 if (local_syms
== NULL
)
378 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
379 symtab_hdr
->sh_info
, 0,
381 /* Cache them for elf_link_input_bfd. */
382 symtab_hdr
->contents
= (unsigned char *) local_syms
;
384 if (local_syms
== NULL
)
386 free (all_local_syms
);
390 all_local_syms
[bfd_indx
] = local_syms
;
393 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
395 input_bfd
= input_bfd
->link
.next
, bfd_indx
++)
397 Elf_Internal_Shdr
*symtab_hdr
;
398 struct elf_link_hash_entry
** sym_hashes
;
400 sym_hashes
= elf_sym_hashes (input_bfd
);
402 /* We'll need the symbol table in a second. */
403 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
404 if (symtab_hdr
->sh_info
== 0)
407 local_syms
= all_local_syms
[bfd_indx
];
409 /* Walk over each section attached to the input bfd. */
410 for (section
= input_bfd
->sections
;
412 section
= section
->next
)
414 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
416 /* If there aren't any relocs, then there's nothing more
418 if ((section
->flags
& SEC_RELOC
) == 0
419 || section
->reloc_count
== 0)
422 /* If this section is a link-once section that will be
423 discarded, then don't create any stubs. */
424 if (section
->output_section
== NULL
425 || section
->output_section
->owner
!= output_bfd
)
428 /* Get the relocs. */
430 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
,
431 (Elf_Internal_Rela
*) NULL
,
433 if (internal_relocs
== NULL
)
434 goto error_ret_free_local
;
436 /* Now examine each relocation. */
437 irela
= internal_relocs
;
438 irelaend
= irela
+ section
->reloc_count
;
439 for (; irela
< irelaend
; irela
++)
441 unsigned int r_type
, r_indx
;
442 struct elf32_m68hc11_stub_hash_entry
*stub_entry
;
445 struct elf_link_hash_entry
*hash
;
446 const char *stub_name
;
447 Elf_Internal_Sym
*sym
;
449 r_type
= ELF32_R_TYPE (irela
->r_info
);
451 /* Only look at 16-bit relocs. */
452 if (r_type
!= (unsigned int) R_M68HC11_16
)
455 /* Now determine the call target, its name, value,
457 r_indx
= ELF32_R_SYM (irela
->r_info
);
458 if (r_indx
< symtab_hdr
->sh_info
)
460 /* It's a local symbol. */
461 Elf_Internal_Shdr
*hdr
;
464 sym
= local_syms
+ r_indx
;
465 is_far
= (sym
&& (sym
->st_other
& STO_M68HC12_FAR
));
469 if (sym
->st_shndx
>= elf_numsections (input_bfd
))
473 hdr
= elf_elfsections (input_bfd
)[sym
->st_shndx
];
474 sym_sec
= hdr
->bfd_section
;
476 stub_name
= (bfd_elf_string_from_elf_section
477 (input_bfd
, symtab_hdr
->sh_link
,
479 sym_value
= sym
->st_value
;
484 /* It's an external symbol. */
487 e_indx
= r_indx
- symtab_hdr
->sh_info
;
488 hash
= (struct elf_link_hash_entry
*)
489 (sym_hashes
[e_indx
]);
491 while (hash
->root
.type
== bfd_link_hash_indirect
492 || hash
->root
.type
== bfd_link_hash_warning
)
493 hash
= ((struct elf_link_hash_entry
*)
494 hash
->root
.u
.i
.link
);
496 if (hash
->root
.type
== bfd_link_hash_defined
497 || hash
->root
.type
== bfd_link_hash_defweak
498 || hash
->root
.type
== bfd_link_hash_new
)
500 if (!(hash
->other
& STO_M68HC12_FAR
))
503 else if (hash
->root
.type
== bfd_link_hash_undefweak
)
507 else if (hash
->root
.type
== bfd_link_hash_undefined
)
513 bfd_set_error (bfd_error_bad_value
);
514 goto error_ret_free_internal
;
516 sym_sec
= hash
->root
.u
.def
.section
;
517 sym_value
= hash
->root
.u
.def
.value
;
518 stub_name
= hash
->root
.root
.string
;
522 goto error_ret_free_internal
;
524 stub_entry
= m68hc12_stub_hash_lookup
525 (htab
->stub_hash_table
,
528 if (stub_entry
== NULL
)
530 if (add_stub_section
== 0)
533 stub_entry
= m68hc12_add_stub (stub_name
, section
, htab
);
534 if (stub_entry
== NULL
)
536 error_ret_free_internal
:
537 if (elf_section_data (section
)->relocs
== NULL
)
538 free (internal_relocs
);
539 goto error_ret_free_local
;
543 stub_entry
->target_value
= sym_value
;
544 stub_entry
->target_section
= sym_sec
;
547 /* We're done with the internal relocs, free them. */
548 if (elf_section_data (section
)->relocs
== NULL
)
549 free (internal_relocs
);
553 if (add_stub_section
)
555 /* OK, we've added some stubs. Find out the new size of the
557 for (stub_sec
= htab
->stub_bfd
->sections
;
559 stub_sec
= stub_sec
->next
)
564 bfd_hash_traverse (htab
->stub_hash_table
, htab
->size_one_stub
, htab
);
566 free (all_local_syms
);
569 error_ret_free_local
:
570 free (all_local_syms
);
574 /* Export the trampoline addresses in the symbol table. */
576 m68hc11_elf_export_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg
)
578 struct bfd_link_info
*info
;
579 struct m68hc11_elf_link_hash_table
*htab
;
580 struct elf32_m68hc11_stub_hash_entry
*stub_entry
;
584 info
= (struct bfd_link_info
*) in_arg
;
585 htab
= m68hc11_elf_hash_table (info
);
589 /* Massage our args to the form they really have. */
590 stub_entry
= (struct elf32_m68hc11_stub_hash_entry
*) gen_entry
;
592 /* Generate the trampoline according to HC11 or HC12. */
593 result
= (* htab
->build_one_stub
) (gen_entry
, in_arg
);
595 /* Make a printable name that does not conflict with the real function. */
596 name
= concat ("tramp.", stub_entry
->root
.string
, NULL
);
598 /* Export the symbol for debugging/disassembling. */
599 m68hc11_elf_set_symbol (htab
->stub_bfd
, info
, name
,
600 stub_entry
->stub_offset
,
601 stub_entry
->stub_sec
);
606 /* Export a symbol or set its value and section. */
608 m68hc11_elf_set_symbol (bfd
*abfd
, struct bfd_link_info
*info
,
609 const char *name
, bfd_vma value
, asection
*sec
)
611 struct elf_link_hash_entry
*h
;
613 h
= (struct elf_link_hash_entry
*)
614 bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
617 _bfd_generic_link_add_one_symbol (info
, abfd
,
627 h
->root
.type
= bfd_link_hash_defined
;
628 h
->root
.u
.def
.value
= value
;
629 h
->root
.u
.def
.section
= sec
;
634 /* Build all the stubs associated with the current output file. The
635 stubs are kept in a hash table attached to the main linker hash
636 table. This function is called via m68hc12elf_finish in the
640 elf32_m68hc11_build_stubs (bfd
*abfd
, struct bfd_link_info
*info
)
643 struct bfd_hash_table
*table
;
644 struct m68hc11_elf_link_hash_table
*htab
;
645 struct m68hc11_scan_param param
;
647 m68hc11_elf_get_bank_parameters (info
);
648 htab
= m68hc11_elf_hash_table (info
);
652 for (stub_sec
= htab
->stub_bfd
->sections
;
654 stub_sec
= stub_sec
->next
)
658 /* Allocate memory to hold the linker stubs. */
659 size
= stub_sec
->size
;
660 stub_sec
->contents
= (unsigned char *) bfd_zalloc (htab
->stub_bfd
, size
);
661 if (stub_sec
->contents
== NULL
&& size
!= 0)
666 /* Build the stubs as directed by the stub hash table. */
667 table
= htab
->stub_hash_table
;
668 bfd_hash_traverse (table
, m68hc11_elf_export_one_stub
, info
);
670 /* Scan the output sections to see if we use the memory banks.
671 If so, export the symbols that define how the memory banks
672 are mapped. This is used by gdb and the simulator to obtain
673 the information. It can be used by programs to burn the eprom
674 at the good addresses. */
675 param
.use_memory_banks
= FALSE
;
676 param
.pinfo
= &htab
->pinfo
;
677 bfd_map_over_sections (abfd
, scan_sections_for_abi
, ¶m
);
678 if (param
.use_memory_banks
)
680 m68hc11_elf_set_symbol (abfd
, info
, BFD_M68HC11_BANK_START_NAME
,
681 htab
->pinfo
.bank_physical
,
682 bfd_abs_section_ptr
);
683 m68hc11_elf_set_symbol (abfd
, info
, BFD_M68HC11_BANK_VIRTUAL_NAME
,
684 htab
->pinfo
.bank_virtual
,
685 bfd_abs_section_ptr
);
686 m68hc11_elf_set_symbol (abfd
, info
, BFD_M68HC11_BANK_SIZE_NAME
,
687 htab
->pinfo
.bank_size
,
688 bfd_abs_section_ptr
);
695 m68hc11_elf_get_bank_parameters (struct bfd_link_info
*info
)
698 struct m68hc11_page_info
*pinfo
;
699 struct bfd_link_hash_entry
*h
;
700 struct m68hc11_elf_link_hash_table
*htab
;
702 htab
= m68hc11_elf_hash_table (info
);
706 pinfo
= & htab
->pinfo
;
707 if (pinfo
->bank_param_initialized
)
710 pinfo
->bank_virtual
= M68HC12_BANK_VIRT
;
711 pinfo
->bank_mask
= M68HC12_BANK_MASK
;
712 pinfo
->bank_physical
= M68HC12_BANK_BASE
;
713 pinfo
->bank_shift
= M68HC12_BANK_SHIFT
;
714 pinfo
->bank_size
= 1 << M68HC12_BANK_SHIFT
;
716 h
= bfd_link_hash_lookup (info
->hash
, BFD_M68HC11_BANK_START_NAME
,
718 if (h
!= (struct bfd_link_hash_entry
*) NULL
719 && h
->type
== bfd_link_hash_defined
)
720 pinfo
->bank_physical
= (h
->u
.def
.value
721 + h
->u
.def
.section
->output_section
->vma
722 + h
->u
.def
.section
->output_offset
);
724 h
= bfd_link_hash_lookup (info
->hash
, BFD_M68HC11_BANK_VIRTUAL_NAME
,
726 if (h
!= (struct bfd_link_hash_entry
*) NULL
727 && h
->type
== bfd_link_hash_defined
)
728 pinfo
->bank_virtual
= (h
->u
.def
.value
729 + h
->u
.def
.section
->output_section
->vma
730 + h
->u
.def
.section
->output_offset
);
732 h
= bfd_link_hash_lookup (info
->hash
, BFD_M68HC11_BANK_SIZE_NAME
,
734 if (h
!= (struct bfd_link_hash_entry
*) NULL
735 && h
->type
== bfd_link_hash_defined
)
736 pinfo
->bank_size
= (h
->u
.def
.value
737 + h
->u
.def
.section
->output_section
->vma
738 + h
->u
.def
.section
->output_offset
);
740 pinfo
->bank_shift
= 0;
741 for (i
= pinfo
->bank_size
; i
!= 0; i
>>= 1)
744 pinfo
->bank_mask
= (1 << pinfo
->bank_shift
) - 1;
745 pinfo
->bank_physical_end
= pinfo
->bank_physical
+ pinfo
->bank_size
;
746 pinfo
->bank_param_initialized
= 1;
748 h
= bfd_link_hash_lookup (info
->hash
, "__far_trampoline", FALSE
,
750 if (h
!= (struct bfd_link_hash_entry
*) NULL
751 && h
->type
== bfd_link_hash_defined
)
752 pinfo
->trampoline_addr
= (h
->u
.def
.value
753 + h
->u
.def
.section
->output_section
->vma
754 + h
->u
.def
.section
->output_offset
);
757 /* Return 1 if the address is in banked memory.
758 This can be applied to a virtual address and to a physical address. */
760 m68hc11_addr_is_banked (struct m68hc11_page_info
*pinfo
, bfd_vma addr
)
762 if (addr
>= pinfo
->bank_virtual
)
765 if (addr
>= pinfo
->bank_physical
&& addr
<= pinfo
->bank_physical_end
)
771 /* Return the physical address seen by the processor, taking
772 into account banked memory. */
774 m68hc11_phys_addr (struct m68hc11_page_info
*pinfo
, bfd_vma addr
)
776 if (addr
< pinfo
->bank_virtual
)
779 /* Map the address to the memory bank. */
780 addr
-= pinfo
->bank_virtual
;
781 addr
&= pinfo
->bank_mask
;
782 addr
+= pinfo
->bank_physical
;
786 /* Return the page number corresponding to an address in banked memory. */
788 m68hc11_phys_page (struct m68hc11_page_info
*pinfo
, bfd_vma addr
)
790 if (addr
< pinfo
->bank_virtual
)
793 /* Map the address to the memory bank. */
794 addr
-= pinfo
->bank_virtual
;
795 addr
>>= pinfo
->bank_shift
;
800 /* This function is used for relocs which are only used for relaxing,
801 which the linker should otherwise ignore. */
803 bfd_reloc_status_type
804 m68hc11_elf_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
805 arelent
*reloc_entry
,
806 asymbol
*symbol ATTRIBUTE_UNUSED
,
807 void *data ATTRIBUTE_UNUSED
,
808 asection
*input_section
,
810 char **error_message ATTRIBUTE_UNUSED
)
812 if (output_bfd
!= NULL
)
813 reloc_entry
->address
+= input_section
->output_offset
;
817 bfd_reloc_status_type
818 m68hc11_elf_special_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
819 arelent
*reloc_entry
,
821 void *data ATTRIBUTE_UNUSED
,
822 asection
*input_section
,
824 char **error_message ATTRIBUTE_UNUSED
)
826 if (output_bfd
!= (bfd
*) NULL
827 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
828 && (! reloc_entry
->howto
->partial_inplace
829 || reloc_entry
->addend
== 0))
831 reloc_entry
->address
+= input_section
->output_offset
;
835 if (output_bfd
!= NULL
)
836 return bfd_reloc_continue
;
838 if (reloc_entry
->address
> bfd_get_section_limit (abfd
, input_section
))
839 return bfd_reloc_outofrange
;
844 /* Look through the relocs for a section during the first phase.
845 Since we don't do .gots or .plts, we just need to consider the
846 virtual table relocs for gc. */
849 elf32_m68hc11_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
850 asection
*sec
, const Elf_Internal_Rela
*relocs
)
852 Elf_Internal_Shdr
* symtab_hdr
;
853 struct elf_link_hash_entry
** sym_hashes
;
854 const Elf_Internal_Rela
* rel
;
855 const Elf_Internal_Rela
* rel_end
;
857 if (bfd_link_relocatable (info
))
860 symtab_hdr
= & elf_tdata (abfd
)->symtab_hdr
;
861 sym_hashes
= elf_sym_hashes (abfd
);
862 rel_end
= relocs
+ sec
->reloc_count
;
864 for (rel
= relocs
; rel
< rel_end
; rel
++)
866 struct elf_link_hash_entry
* h
;
867 unsigned long r_symndx
;
869 r_symndx
= ELF32_R_SYM (rel
->r_info
);
871 if (r_symndx
< symtab_hdr
->sh_info
)
875 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
876 while (h
->root
.type
== bfd_link_hash_indirect
877 || h
->root
.type
== bfd_link_hash_warning
)
878 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
880 /* PR15323, ref flags aren't set for references in the same
882 h
->root
.non_ir_ref_regular
= 1;
885 switch (ELF32_R_TYPE (rel
->r_info
))
887 /* This relocation describes the C++ object vtable hierarchy.
888 Reconstruct it for later use during GC. */
889 case R_M68HC11_GNU_VTINHERIT
:
890 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
894 /* This relocation describes which C++ vtable entries are actually
895 used. Record for later use during GC. */
896 case R_M68HC11_GNU_VTENTRY
:
897 BFD_ASSERT (h
!= NULL
);
899 && !bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
908 /* Relocate a 68hc11/68hc12 ELF section. */
910 elf32_m68hc11_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
911 struct bfd_link_info
*info
,
912 bfd
*input_bfd
, asection
*input_section
,
913 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
914 Elf_Internal_Sym
*local_syms
,
915 asection
**local_sections
)
917 Elf_Internal_Shdr
*symtab_hdr
;
918 struct elf_link_hash_entry
**sym_hashes
;
919 Elf_Internal_Rela
*rel
, *relend
;
920 const char *name
= NULL
;
921 struct m68hc11_page_info
*pinfo
;
922 const struct elf_backend_data
* const ebd
= get_elf_backend_data (input_bfd
);
923 struct m68hc11_elf_link_hash_table
*htab
;
924 unsigned long e_flags
;
926 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
927 sym_hashes
= elf_sym_hashes (input_bfd
);
928 e_flags
= elf_elfheader (input_bfd
)->e_flags
;
930 htab
= m68hc11_elf_hash_table (info
);
934 /* Get memory bank parameters. */
935 m68hc11_elf_get_bank_parameters (info
);
937 pinfo
= & htab
->pinfo
;
939 relend
= relocs
+ input_section
->reloc_count
;
941 for (; rel
< relend
; rel
++)
945 reloc_howto_type
*howto
;
946 unsigned long r_symndx
;
947 Elf_Internal_Sym
*sym
;
949 bfd_vma relocation
= 0;
950 bfd_reloc_status_type r
= bfd_reloc_undefined
;
955 bfd_boolean is_far
= FALSE
;
956 bfd_boolean is_xgate_symbol
= FALSE
;
957 bfd_boolean is_section_symbol
= FALSE
;
958 struct elf_link_hash_entry
*h
;
963 r_symndx
= ELF32_R_SYM (rel
->r_info
);
964 r_type
= ELF32_R_TYPE (rel
->r_info
);
966 if (r_type
== R_M68HC11_GNU_VTENTRY
967 || r_type
== R_M68HC11_GNU_VTINHERIT
)
970 (*ebd
->elf_info_to_howto_rel
) (input_bfd
, &arel
, rel
);
976 if (r_symndx
< symtab_hdr
->sh_info
)
978 sym
= local_syms
+ r_symndx
;
979 sec
= local_sections
[r_symndx
];
980 relocation
= (sec
->output_section
->vma
983 is_far
= (sym
&& (sym
->st_other
& STO_M68HC12_FAR
));
984 is_xgate_symbol
= (sym
&& (sym
->st_target_internal
));
985 is_section_symbol
= ELF_ST_TYPE (sym
->st_info
) & STT_SECTION
;
989 bfd_boolean unresolved_reloc
, warned
, ignored
;
991 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
992 r_symndx
, symtab_hdr
, sym_hashes
,
993 h
, sec
, relocation
, unresolved_reloc
,
996 is_far
= (h
&& (h
->other
& STO_M68HC12_FAR
));
997 is_xgate_symbol
= (h
&& (h
->target_internal
));
1000 if (sec
!= NULL
&& discarded_section (sec
))
1001 RELOC_AGAINST_DISCARDED_SECTION (info
, input_bfd
, input_section
,
1002 rel
, 1, relend
, howto
, 0, contents
);
1004 if (bfd_link_relocatable (info
))
1006 /* This is a relocatable link. We don't have to change
1007 anything, unless the reloc is against a section symbol,
1008 in which case we have to adjust according to where the
1009 section symbol winds up in the output section. */
1010 if (sym
!= NULL
&& ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
1011 rel
->r_addend
+= sec
->output_offset
;
1016 name
= h
->root
.root
.string
;
1019 name
= (bfd_elf_string_from_elf_section
1020 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1021 if (name
== NULL
|| *name
== '\0')
1022 name
= bfd_section_name (input_bfd
, sec
);
1025 if (is_far
&& ELF32_R_TYPE (rel
->r_info
) == R_M68HC11_16
)
1027 struct elf32_m68hc11_stub_hash_entry
* stub
;
1029 stub
= m68hc12_stub_hash_lookup (htab
->stub_hash_table
,
1030 name
, FALSE
, FALSE
);
1033 relocation
= stub
->stub_offset
1034 + stub
->stub_sec
->output_section
->vma
1035 + stub
->stub_sec
->output_offset
;
1040 /* Do the memory bank mapping. */
1041 phys_addr
= m68hc11_phys_addr (pinfo
, relocation
+ rel
->r_addend
);
1042 phys_page
= m68hc11_phys_page (pinfo
, relocation
+ rel
->r_addend
);
1045 case R_M68HC12_LO8XG
:
1046 /* This relocation is specific to XGATE IMM16 calls and will precede
1047 a HI8. tc-m68hc11 only generates them in pairs.
1048 Leave the relocation to the HI8XG step. */
1050 r_type
= R_M68HC11_NONE
;
1053 case R_M68HC12_HI8XG
:
1054 /* This relocation is specific to XGATE IMM16 calls and must follow
1055 a LO8XG. Does not actually check that it was a LO8XG.
1056 Adjusts high and low bytes. */
1057 relocation
= phys_addr
;
1058 if ((e_flags
& E_M68HC11_XGATE_RAMOFFSET
)
1059 && (relocation
>= 0x2000))
1060 relocation
+= 0xc000; /* HARDCODED RAM offset for XGATE. */
1062 /* Fetch 16 bit value including low byte in previous insn. */
1063 val
= (bfd_get_8 (input_bfd
, (bfd_byte
*) contents
+ rel
->r_offset
) << 8)
1064 | bfd_get_8 (input_bfd
, (bfd_byte
*) contents
+ rel
->r_offset
- 2);
1066 /* Add on value to preserve carry, then write zero to high byte. */
1069 /* Write out top byte. */
1070 bfd_put_8 (input_bfd
, (relocation
>> 8) & 0xff,
1071 (bfd_byte
*) contents
+ rel
->r_offset
);
1073 /* Write out low byte to previous instruction. */
1074 bfd_put_8 (input_bfd
, relocation
& 0xff,
1075 (bfd_byte
*) contents
+ rel
->r_offset
- 2);
1077 /* Mark as relocation completed. */
1079 r_type
= R_M68HC11_NONE
;
1082 /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1083 assembler directives. %hi does not support carry. */
1086 relocation
= phys_addr
;
1090 /* Reloc used by 68HC12 call instruction. */
1091 bfd_put_16 (input_bfd
, phys_addr
,
1092 (bfd_byte
*) contents
+ rel
->r_offset
);
1093 bfd_put_8 (input_bfd
, phys_page
,
1094 (bfd_byte
*) contents
+ rel
->r_offset
+ 2);
1096 r_type
= R_M68HC11_NONE
;
1099 case R_M68HC11_NONE
:
1103 case R_M68HC11_LO16
:
1104 /* Reloc generated by %addr(expr) gas to obtain the
1105 address as mapped in the memory bank window. */
1106 relocation
= phys_addr
;
1109 case R_M68HC11_PAGE
:
1110 /* Reloc generated by %page(expr) gas to obtain the
1111 page number associated with the address. */
1112 relocation
= phys_page
;
1116 /* Get virtual address of instruction having the relocation. */
1119 msg
= _("Reference to the far symbol `%s' using a wrong "
1120 "relocation may result in incorrect execution");
1121 buf
= xmalloc (strlen (msg
) + strlen (name
) + 10);
1122 sprintf (buf
, msg
, name
);
1124 (*info
->callbacks
->warning
)
1125 (info
, buf
, name
, input_bfd
, NULL
, rel
->r_offset
);
1129 /* Get virtual address of instruction having the relocation. */
1130 insn_addr
= input_section
->output_section
->vma
1131 + input_section
->output_offset
1134 insn_page
= m68hc11_phys_page (pinfo
, insn_addr
);
1136 /* If we are linking an S12 instruction against an XGATE symbol, we
1137 need to change the offset of the symbol value so that it's correct
1138 from the S12's perspective. */
1139 if (is_xgate_symbol
)
1141 /* The ram in the global space is mapped to 0x2000 in the 16-bit
1142 address space for S12 and 0xE000 in the 16-bit address space
1144 if (relocation
>= 0xE000)
1146 /* We offset the address by the difference
1147 between these two mappings. */
1148 relocation
-= 0xC000;
1153 msg
= _("XGATE address (%lx) is not within shared RAM"
1154 "(0xE000-0xFFFF), therefore you must manually offset "
1155 "the address, and possibly manage the page, in your "
1157 buf
= xmalloc (strlen (msg
) + 128);
1158 sprintf (buf
, msg
, phys_addr
);
1159 (*info
->callbacks
->warning
) (info
, buf
, name
, input_bfd
,
1160 input_section
, insn_addr
);
1166 if (m68hc11_addr_is_banked (pinfo
, relocation
+ rel
->r_addend
)
1167 && m68hc11_addr_is_banked (pinfo
, insn_addr
)
1168 && phys_page
!= insn_page
&& !(e_flags
& E_M68HC11_NO_BANK_WARNING
))
1170 /* xgettext:c-format */
1171 msg
= _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1172 "as current banked address [%lx:%04lx] (%lx)");
1173 buf
= xmalloc (strlen (msg
) + 128);
1174 sprintf (buf
, msg
, phys_page
, phys_addr
,
1175 (long) (relocation
+ rel
->r_addend
),
1176 insn_page
, m68hc11_phys_addr (pinfo
, insn_addr
),
1177 (long) (insn_addr
));
1178 (*info
->callbacks
->warning
) (info
, buf
, name
, input_bfd
,
1179 input_section
, rel
->r_offset
);
1184 if (phys_page
!= 0 && insn_page
== 0)
1186 /* xgettext:c-format */
1187 msg
= _("reference to a banked address [%lx:%04lx] in the "
1188 "normal address space at %04lx");
1189 buf
= xmalloc (strlen (msg
) + 128);
1190 sprintf (buf
, msg
, phys_page
, phys_addr
, insn_addr
);
1191 (*info
->callbacks
->warning
) (info
, buf
, name
, input_bfd
,
1192 input_section
, insn_addr
);
1194 relocation
= phys_addr
;
1198 /* If this is a banked address use the phys_addr so that
1199 we stay in the banked window. */
1200 if (m68hc11_addr_is_banked (pinfo
, relocation
+ rel
->r_addend
))
1201 relocation
= phys_addr
;
1205 /* If we are linking an XGATE instruction against an S12 symbol, we
1206 need to change the offset of the symbol value so that it's correct
1207 from the XGATE's perspective. */
1208 if (!strcmp (howto
->name
, "R_XGATE_IMM8_LO")
1209 || !strcmp (howto
->name
, "R_XGATE_IMM8_HI"))
1211 /* We can only offset S12 addresses that lie within the non-paged
1213 if (!is_xgate_symbol
&& !is_section_symbol
)
1215 /* The ram in the global space is mapped to 0x2000 and stops at
1216 0x4000 in the 16-bit address space for S12 and 0xE000 in the
1217 16-bit address space for XGATE. */
1218 if (relocation
>= 0x2000 && relocation
< 0x4000)
1219 /* We offset the address by the difference
1220 between these two mappings. */
1221 relocation
+= 0xC000;
1224 /* Get virtual address of instruction having the relocation. */
1225 insn_addr
= input_section
->output_section
->vma
1226 + input_section
->output_offset
+ rel
->r_offset
;
1228 msg
= _("S12 address (%lx) is not within shared RAM"
1229 "(0x2000-0x4000), therefore you must manually "
1230 "offset the address in your code");
1231 buf
= xmalloc (strlen (msg
) + 128);
1232 sprintf (buf
, msg
, phys_addr
);
1233 (*info
->callbacks
->warning
) (info
, buf
, name
, input_bfd
,
1234 input_section
, insn_addr
);
1241 if (r_type
!= R_M68HC11_NONE
)
1243 if ((r_type
== R_M68HC12_PCREL_9
) || (r_type
== R_M68HC12_PCREL_10
))
1244 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1245 contents
, rel
->r_offset
,
1246 relocation
- 2, rel
->r_addend
);
1248 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1249 contents
, rel
->r_offset
,
1250 relocation
, rel
->r_addend
);
1253 if (r
!= bfd_reloc_ok
)
1257 case bfd_reloc_overflow
:
1258 (*info
->callbacks
->reloc_overflow
)
1259 (info
, NULL
, name
, howto
->name
, (bfd_vma
) 0,
1260 input_bfd
, input_section
, rel
->r_offset
);
1263 case bfd_reloc_undefined
:
1264 (*info
->callbacks
->undefined_symbol
)
1265 (info
, name
, input_bfd
, input_section
, rel
->r_offset
, TRUE
);
1268 case bfd_reloc_outofrange
:
1269 msg
= _ ("internal error: out of range error");
1272 case bfd_reloc_notsupported
:
1273 msg
= _ ("internal error: unsupported relocation error");
1276 case bfd_reloc_dangerous
:
1277 msg
= _ ("internal error: dangerous error");
1281 msg
= _ ("internal error: unknown error");
1285 (*info
->callbacks
->warning
) (info
, msg
, name
, input_bfd
,
1286 input_section
, rel
->r_offset
);
1297 /* Set and control ELF flags in ELF header. */
1300 _bfd_m68hc11_elf_set_private_flags (bfd
*abfd
, flagword flags
)
1302 BFD_ASSERT (!elf_flags_init (abfd
)
1303 || elf_elfheader (abfd
)->e_flags
== flags
);
1305 elf_elfheader (abfd
)->e_flags
= flags
;
1306 elf_flags_init (abfd
) = TRUE
;
1310 /* Merge backend specific data from an object file to the output
1311 object file when linking. */
1314 _bfd_m68hc11_elf_merge_private_bfd_data (bfd
*ibfd
, struct bfd_link_info
*info
)
1316 bfd
*obfd
= info
->output_bfd
;
1319 bfd_boolean ok
= TRUE
;
1321 /* Check if we have the same endianness */
1322 if (!_bfd_generic_verify_endian_match (ibfd
, info
))
1325 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1326 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1329 new_flags
= elf_elfheader (ibfd
)->e_flags
;
1330 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_M68HC11_ABI
;
1331 old_flags
= elf_elfheader (obfd
)->e_flags
;
1333 if (! elf_flags_init (obfd
))
1335 elf_flags_init (obfd
) = TRUE
;
1336 elf_elfheader (obfd
)->e_flags
= new_flags
;
1337 elf_elfheader (obfd
)->e_ident
[EI_CLASS
]
1338 = elf_elfheader (ibfd
)->e_ident
[EI_CLASS
];
1340 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
1341 && bfd_get_arch_info (obfd
)->the_default
)
1343 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
1344 bfd_get_mach (ibfd
)))
1351 /* Check ABI compatibility. */
1352 if ((new_flags
& E_M68HC11_I32
) != (old_flags
& E_M68HC11_I32
))
1355 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1356 "and others for 32-bit integers"), ibfd
);
1359 if ((new_flags
& E_M68HC11_F64
) != (old_flags
& E_M68HC11_F64
))
1362 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1363 "and others for 64-bit double"), ibfd
);
1367 /* Processor compatibility. */
1368 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags
, old_flags
))
1371 (_("%B: linking files compiled for HCS12 with "
1372 "others compiled for HC12"), ibfd
);
1375 new_flags
= ((new_flags
& ~EF_M68HC11_MACH_MASK
)
1376 | (EF_M68HC11_MERGE_MACH (new_flags
, old_flags
)));
1378 elf_elfheader (obfd
)->e_flags
= new_flags
;
1380 new_flags
&= ~(EF_M68HC11_ABI
| EF_M68HC11_MACH_MASK
);
1381 old_flags
&= ~(EF_M68HC11_ABI
| EF_M68HC11_MACH_MASK
);
1383 /* Warn about any other mismatches */
1384 if (new_flags
!= old_flags
)
1387 /* xgettext:c-format */
1388 (_("%B: uses different e_flags (%#x) fields than previous modules (%#x)"),
1389 ibfd
, new_flags
, old_flags
);
1395 bfd_set_error (bfd_error_bad_value
);
1403 _bfd_m68hc11_elf_print_private_bfd_data (bfd
*abfd
, void *ptr
)
1405 FILE *file
= (FILE *) ptr
;
1407 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
1409 /* Print normal ELF private data. */
1410 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
1412 /* xgettext:c-format */
1413 fprintf (file
, _("private flags = %lx:"), elf_elfheader (abfd
)->e_flags
);
1415 if (elf_elfheader (abfd
)->e_flags
& E_M68HC11_I32
)
1416 fprintf (file
, _("[abi=32-bit int, "));
1418 fprintf (file
, _("[abi=16-bit int, "));
1420 if (elf_elfheader (abfd
)->e_flags
& E_M68HC11_F64
)
1421 fprintf (file
, _("64-bit double, "));
1423 fprintf (file
, _("32-bit double, "));
1425 if (strcmp (bfd_get_target (abfd
), "elf32-m68hc11") == 0)
1426 fprintf (file
, _("cpu=HC11]"));
1427 else if (elf_elfheader (abfd
)->e_flags
& EF_M68HCS12_MACH
)
1428 fprintf (file
, _("cpu=HCS12]"));
1430 fprintf (file
, _("cpu=HC12]"));
1432 if (elf_elfheader (abfd
)->e_flags
& E_M68HC12_BANKS
)
1433 fprintf (file
, _(" [memory=bank-model]"));
1435 fprintf (file
, _(" [memory=flat]"));
1437 if (elf_elfheader (abfd
)->e_flags
& E_M68HC11_XGATE_RAMOFFSET
)
1438 fprintf (file
, _(" [XGATE RAM offsetting]"));
1445 static void scan_sections_for_abi (bfd
*abfd ATTRIBUTE_UNUSED
,
1446 asection
*asect
, void *arg
)
1448 struct m68hc11_scan_param
* p
= (struct m68hc11_scan_param
*) arg
;
1450 if (asect
->vma
>= p
->pinfo
->bank_virtual
)
1451 p
->use_memory_banks
= TRUE
;
1454 /* Tweak the OSABI field of the elf header. */
1457 elf32_m68hc11_post_process_headers (bfd
*abfd
, struct bfd_link_info
*link_info
)
1459 struct m68hc11_scan_param param
;
1460 struct m68hc11_elf_link_hash_table
*htab
;
1462 if (link_info
== NULL
)
1465 htab
= m68hc11_elf_hash_table (link_info
);
1469 m68hc11_elf_get_bank_parameters (link_info
);
1471 param
.use_memory_banks
= FALSE
;
1472 param
.pinfo
= & htab
->pinfo
;
1474 bfd_map_over_sections (abfd
, scan_sections_for_abi
, ¶m
);
1476 if (param
.use_memory_banks
)
1478 Elf_Internal_Ehdr
* i_ehdrp
;
1480 i_ehdrp
= elf_elfheader (abfd
);
1481 i_ehdrp
->e_flags
|= E_M68HC12_BANKS
;