1 /* Motorola 68HC11/HC12-specific support for 32-bit ELF
2 Copyright 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
3 2009, 2010 Free Software Foundation, Inc.
4 Contributed by Stephane Carrez (stcarrez@nerim.fr)
6 This file is part of BFD, the Binary File Descriptor library.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 MA 02110-1301, USA. */
23 #include "alloca-conf.h"
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
*, PTR
);
55 struct m68hc11_scan_param
57 struct m68hc11_page_info
* pinfo
;
58 bfd_boolean use_memory_banks
;
62 /* Create a 68HC11/68HC12 ELF linker hash table. */
64 struct m68hc11_elf_link_hash_table
*
65 m68hc11_elf_hash_table_create (bfd
*abfd
)
67 struct m68hc11_elf_link_hash_table
*ret
;
68 bfd_size_type amt
= sizeof (struct m68hc11_elf_link_hash_table
);
70 ret
= (struct m68hc11_elf_link_hash_table
*) bfd_malloc (amt
);
71 if (ret
== (struct m68hc11_elf_link_hash_table
*) NULL
)
75 if (!_bfd_elf_link_hash_table_init (&ret
->root
, abfd
,
76 _bfd_elf_link_hash_newfunc
,
77 sizeof (struct elf_link_hash_entry
),
84 /* Init the stub hash table too. */
85 amt
= sizeof (struct bfd_hash_table
);
86 ret
->stub_hash_table
= (struct bfd_hash_table
*) bfd_malloc (amt
);
87 if (ret
->stub_hash_table
== NULL
)
92 if (!bfd_hash_table_init (ret
->stub_hash_table
, stub_hash_newfunc
,
93 sizeof (struct elf32_m68hc11_stub_hash_entry
)))
97 ret
->stub_section
= 0;
98 ret
->add_stub_section
= NULL
;
99 ret
->sym_cache
.abfd
= NULL
;
104 /* Free the derived linker hash table. */
107 m68hc11_elf_bfd_link_hash_table_free (struct bfd_link_hash_table
*hash
)
109 struct m68hc11_elf_link_hash_table
*ret
110 = (struct m68hc11_elf_link_hash_table
*) hash
;
112 bfd_hash_table_free (ret
->stub_hash_table
);
113 free (ret
->stub_hash_table
);
114 _bfd_generic_link_hash_table_free (hash
);
117 /* Assorted hash table functions. */
119 /* Initialize an entry in the stub hash table. */
121 static struct bfd_hash_entry
*
122 stub_hash_newfunc (struct bfd_hash_entry
*entry
, struct bfd_hash_table
*table
,
125 /* Allocate the structure if it has not already been allocated by a
129 entry
= bfd_hash_allocate (table
,
130 sizeof (struct elf32_m68hc11_stub_hash_entry
));
135 /* Call the allocation method of the superclass. */
136 entry
= bfd_hash_newfunc (entry
, table
, string
);
139 struct elf32_m68hc11_stub_hash_entry
*eh
;
141 /* Initialize the local fields. */
142 eh
= (struct elf32_m68hc11_stub_hash_entry
*) entry
;
145 eh
->target_value
= 0;
146 eh
->target_section
= NULL
;
152 /* Add a new stub entry to the stub hash. Not all fields of the new
153 stub entry are initialised. */
155 static struct elf32_m68hc11_stub_hash_entry
*
156 m68hc12_add_stub (const char *stub_name
, asection
*section
,
157 struct m68hc11_elf_link_hash_table
*htab
)
159 struct elf32_m68hc11_stub_hash_entry
*stub_entry
;
161 /* Enter this entry into the linker stub hash table. */
162 stub_entry
= m68hc12_stub_hash_lookup (htab
->stub_hash_table
, stub_name
,
164 if (stub_entry
== NULL
)
166 (*_bfd_error_handler
) (_("%B: cannot create stub entry %s"),
167 section
->owner
, stub_name
);
171 if (htab
->stub_section
== 0)
173 htab
->stub_section
= (*htab
->add_stub_section
) (".tramp",
174 htab
->tramp_section
);
177 stub_entry
->stub_sec
= htab
->stub_section
;
178 stub_entry
->stub_offset
= 0;
182 /* Hook called by the linker routine which adds symbols from an object
183 file. We use it for identify far symbols and force a loading of
184 the trampoline handler. */
187 elf32_m68hc11_add_symbol_hook (bfd
*abfd
, struct bfd_link_info
*info
,
188 Elf_Internal_Sym
*sym
,
189 const char **namep ATTRIBUTE_UNUSED
,
190 flagword
*flagsp ATTRIBUTE_UNUSED
,
191 asection
**secp ATTRIBUTE_UNUSED
,
192 bfd_vma
*valp ATTRIBUTE_UNUSED
)
194 if (sym
->st_other
& STO_M68HC12_FAR
)
196 struct elf_link_hash_entry
*h
;
198 h
= (struct elf_link_hash_entry
*)
199 bfd_link_hash_lookup (info
->hash
, "__far_trampoline",
200 FALSE
, FALSE
, FALSE
);
203 struct bfd_link_hash_entry
* entry
= NULL
;
205 _bfd_generic_link_add_one_symbol (info
, abfd
,
209 (bfd_vma
) 0, (const char*) NULL
,
210 FALSE
, FALSE
, &entry
);
217 /* External entry points for sizing and building linker stubs. */
219 /* Set up various things so that we can make a list of input sections
220 for each output section included in the link. Returns -1 on error,
221 0 when no stubs will be needed, and 1 on success. */
224 elf32_m68hc11_setup_section_lists (bfd
*output_bfd
, struct bfd_link_info
*info
)
227 unsigned int bfd_count
;
228 int top_id
, top_index
;
230 asection
**input_list
, **list
;
232 asection
*text_section
;
233 struct m68hc11_elf_link_hash_table
*htab
;
235 htab
= m68hc11_elf_hash_table (info
);
239 if (bfd_get_flavour (info
->output_bfd
) != bfd_target_elf_flavour
)
242 /* Count the number of input BFDs and find the top input section id.
243 Also search for an existing ".tramp" section so that we know
244 where generated trampolines must go. Default to ".text" if we
246 htab
->tramp_section
= 0;
248 for (input_bfd
= info
->input_bfds
, bfd_count
= 0, top_id
= 0;
250 input_bfd
= input_bfd
->link_next
)
253 for (section
= input_bfd
->sections
;
255 section
= section
->next
)
257 const char* name
= bfd_get_section_name (input_bfd
, section
);
259 if (!strcmp (name
, ".tramp"))
260 htab
->tramp_section
= section
;
262 if (!strcmp (name
, ".text"))
263 text_section
= section
;
265 if (top_id
< section
->id
)
266 top_id
= section
->id
;
269 htab
->bfd_count
= bfd_count
;
270 if (htab
->tramp_section
== 0)
271 htab
->tramp_section
= text_section
;
273 /* We can't use output_bfd->section_count here to find the top output
274 section index as some sections may have been removed, and
275 strip_excluded_output_sections doesn't renumber the indices. */
276 for (section
= output_bfd
->sections
, top_index
= 0;
278 section
= section
->next
)
280 if (top_index
< section
->index
)
281 top_index
= section
->index
;
284 htab
->top_index
= top_index
;
285 amt
= sizeof (asection
*) * (top_index
+ 1);
286 input_list
= (asection
**) bfd_malloc (amt
);
287 htab
->input_list
= input_list
;
288 if (input_list
== NULL
)
291 /* For sections we aren't interested in, mark their entries with a
292 value we can check later. */
293 list
= input_list
+ top_index
;
295 *list
= bfd_abs_section_ptr
;
296 while (list
-- != input_list
);
298 for (section
= output_bfd
->sections
;
300 section
= section
->next
)
302 if ((section
->flags
& SEC_CODE
) != 0)
303 input_list
[section
->index
] = NULL
;
309 /* Determine and set the size of the stub section for a final link.
311 The basic idea here is to examine all the relocations looking for
312 PC-relative calls to a target that is unreachable with a "bl"
316 elf32_m68hc11_size_stubs (bfd
*output_bfd
, bfd
*stub_bfd
,
317 struct bfd_link_info
*info
,
318 asection
* (*add_stub_section
) (const char*, asection
*))
322 Elf_Internal_Sym
*local_syms
, **all_local_syms
;
323 unsigned int bfd_indx
, bfd_count
;
326 struct m68hc11_elf_link_hash_table
*htab
= m68hc11_elf_hash_table (info
);
331 /* Stash our params away. */
332 htab
->stub_bfd
= stub_bfd
;
333 htab
->add_stub_section
= add_stub_section
;
335 /* Count the number of input BFDs and find the top input section id. */
336 for (input_bfd
= info
->input_bfds
, bfd_count
= 0;
338 input_bfd
= input_bfd
->link_next
)
341 /* We want to read in symbol extension records only once. To do this
342 we need to read in the local symbols in parallel and save them for
343 later use; so hold pointers to the local symbols in an array. */
344 amt
= sizeof (Elf_Internal_Sym
*) * bfd_count
;
345 all_local_syms
= (Elf_Internal_Sym
**) bfd_zmalloc (amt
);
346 if (all_local_syms
== NULL
)
349 /* Walk over all the input BFDs, swapping in local symbols. */
350 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
352 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
354 Elf_Internal_Shdr
*symtab_hdr
;
356 /* We'll need the symbol table in a second. */
357 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
358 if (symtab_hdr
->sh_info
== 0)
361 /* We need an array of the local symbols attached to the input bfd. */
362 local_syms
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
363 if (local_syms
== NULL
)
365 local_syms
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
366 symtab_hdr
->sh_info
, 0,
368 /* Cache them for elf_link_input_bfd. */
369 symtab_hdr
->contents
= (unsigned char *) local_syms
;
371 if (local_syms
== NULL
)
373 free (all_local_syms
);
377 all_local_syms
[bfd_indx
] = local_syms
;
380 for (input_bfd
= info
->input_bfds
, bfd_indx
= 0;
382 input_bfd
= input_bfd
->link_next
, bfd_indx
++)
384 Elf_Internal_Shdr
*symtab_hdr
;
385 struct elf_link_hash_entry
** sym_hashes
;
387 sym_hashes
= elf_sym_hashes (input_bfd
);
389 /* We'll need the symbol table in a second. */
390 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
391 if (symtab_hdr
->sh_info
== 0)
394 local_syms
= all_local_syms
[bfd_indx
];
396 /* Walk over each section attached to the input bfd. */
397 for (section
= input_bfd
->sections
;
399 section
= section
->next
)
401 Elf_Internal_Rela
*internal_relocs
, *irelaend
, *irela
;
403 /* If there aren't any relocs, then there's nothing more
405 if ((section
->flags
& SEC_RELOC
) == 0
406 || section
->reloc_count
== 0)
409 /* If this section is a link-once section that will be
410 discarded, then don't create any stubs. */
411 if (section
->output_section
== NULL
412 || section
->output_section
->owner
!= output_bfd
)
415 /* Get the relocs. */
417 = _bfd_elf_link_read_relocs (input_bfd
, section
, NULL
,
418 (Elf_Internal_Rela
*) NULL
,
420 if (internal_relocs
== NULL
)
421 goto error_ret_free_local
;
423 /* Now examine each relocation. */
424 irela
= internal_relocs
;
425 irelaend
= irela
+ section
->reloc_count
;
426 for (; irela
< irelaend
; irela
++)
428 unsigned int r_type
, r_indx
;
429 struct elf32_m68hc11_stub_hash_entry
*stub_entry
;
432 struct elf_link_hash_entry
*hash
;
433 const char *stub_name
;
434 Elf_Internal_Sym
*sym
;
436 r_type
= ELF32_R_TYPE (irela
->r_info
);
438 /* Only look at 16-bit relocs. */
439 if (r_type
!= (unsigned int) R_M68HC11_16
)
442 /* Now determine the call target, its name, value,
444 r_indx
= ELF32_R_SYM (irela
->r_info
);
445 if (r_indx
< symtab_hdr
->sh_info
)
447 /* It's a local symbol. */
448 Elf_Internal_Shdr
*hdr
;
451 sym
= local_syms
+ r_indx
;
452 is_far
= (sym
&& (sym
->st_other
& STO_M68HC12_FAR
));
456 if (sym
->st_shndx
>= elf_numsections (input_bfd
))
460 hdr
= elf_elfsections (input_bfd
)[sym
->st_shndx
];
461 sym_sec
= hdr
->bfd_section
;
463 stub_name
= (bfd_elf_string_from_elf_section
464 (input_bfd
, symtab_hdr
->sh_link
,
466 sym_value
= sym
->st_value
;
471 /* It's an external symbol. */
474 e_indx
= r_indx
- symtab_hdr
->sh_info
;
475 hash
= (struct elf_link_hash_entry
*)
476 (sym_hashes
[e_indx
]);
478 while (hash
->root
.type
== bfd_link_hash_indirect
479 || hash
->root
.type
== bfd_link_hash_warning
)
480 hash
= ((struct elf_link_hash_entry
*)
481 hash
->root
.u
.i
.link
);
483 if (hash
->root
.type
== bfd_link_hash_defined
484 || hash
->root
.type
== bfd_link_hash_defweak
485 || hash
->root
.type
== bfd_link_hash_new
)
487 if (!(hash
->other
& STO_M68HC12_FAR
))
490 else if (hash
->root
.type
== bfd_link_hash_undefweak
)
494 else if (hash
->root
.type
== bfd_link_hash_undefined
)
500 bfd_set_error (bfd_error_bad_value
);
501 goto error_ret_free_internal
;
503 sym_sec
= hash
->root
.u
.def
.section
;
504 sym_value
= hash
->root
.u
.def
.value
;
505 stub_name
= hash
->root
.root
.string
;
509 goto error_ret_free_internal
;
511 stub_entry
= m68hc12_stub_hash_lookup
512 (htab
->stub_hash_table
,
515 if (stub_entry
== NULL
)
517 if (add_stub_section
== 0)
520 stub_entry
= m68hc12_add_stub (stub_name
, section
, htab
);
521 if (stub_entry
== NULL
)
523 error_ret_free_internal
:
524 if (elf_section_data (section
)->relocs
== NULL
)
525 free (internal_relocs
);
526 goto error_ret_free_local
;
530 stub_entry
->target_value
= sym_value
;
531 stub_entry
->target_section
= sym_sec
;
534 /* We're done with the internal relocs, free them. */
535 if (elf_section_data (section
)->relocs
== NULL
)
536 free (internal_relocs
);
540 if (add_stub_section
)
542 /* OK, we've added some stubs. Find out the new size of the
544 for (stub_sec
= htab
->stub_bfd
->sections
;
546 stub_sec
= stub_sec
->next
)
551 bfd_hash_traverse (htab
->stub_hash_table
, htab
->size_one_stub
, htab
);
553 free (all_local_syms
);
556 error_ret_free_local
:
557 free (all_local_syms
);
561 /* Export the trampoline addresses in the symbol table. */
563 m68hc11_elf_export_one_stub (struct bfd_hash_entry
*gen_entry
, void *in_arg
)
565 struct bfd_link_info
*info
;
566 struct m68hc11_elf_link_hash_table
*htab
;
567 struct elf32_m68hc11_stub_hash_entry
*stub_entry
;
571 info
= (struct bfd_link_info
*) in_arg
;
572 htab
= m68hc11_elf_hash_table (info
);
576 /* Massage our args to the form they really have. */
577 stub_entry
= (struct elf32_m68hc11_stub_hash_entry
*) gen_entry
;
579 /* Generate the trampoline according to HC11 or HC12. */
580 result
= (* htab
->build_one_stub
) (gen_entry
, in_arg
);
582 /* Make a printable name that does not conflict with the real function. */
583 name
= alloca (strlen (stub_entry
->root
.string
) + 16);
584 sprintf (name
, "tramp.%s", stub_entry
->root
.string
);
586 /* Export the symbol for debugging/disassembling. */
587 m68hc11_elf_set_symbol (htab
->stub_bfd
, info
, name
,
588 stub_entry
->stub_offset
,
589 stub_entry
->stub_sec
);
593 /* Export a symbol or set its value and section. */
595 m68hc11_elf_set_symbol (bfd
*abfd
, struct bfd_link_info
*info
,
596 const char *name
, bfd_vma value
, asection
*sec
)
598 struct elf_link_hash_entry
*h
;
600 h
= (struct elf_link_hash_entry
*)
601 bfd_link_hash_lookup (info
->hash
, name
, FALSE
, FALSE
, FALSE
);
604 _bfd_generic_link_add_one_symbol (info
, abfd
,
614 h
->root
.type
= bfd_link_hash_defined
;
615 h
->root
.u
.def
.value
= value
;
616 h
->root
.u
.def
.section
= sec
;
621 /* Build all the stubs associated with the current output file. The
622 stubs are kept in a hash table attached to the main linker hash
623 table. This function is called via m68hc12elf_finish in the
627 elf32_m68hc11_build_stubs (bfd
*abfd
, struct bfd_link_info
*info
)
630 struct bfd_hash_table
*table
;
631 struct m68hc11_elf_link_hash_table
*htab
;
632 struct m68hc11_scan_param param
;
634 m68hc11_elf_get_bank_parameters (info
);
635 htab
= m68hc11_elf_hash_table (info
);
639 for (stub_sec
= htab
->stub_bfd
->sections
;
641 stub_sec
= stub_sec
->next
)
645 /* Allocate memory to hold the linker stubs. */
646 size
= stub_sec
->size
;
647 stub_sec
->contents
= (unsigned char *) bfd_zalloc (htab
->stub_bfd
, size
);
648 if (stub_sec
->contents
== NULL
&& size
!= 0)
653 /* Build the stubs as directed by the stub hash table. */
654 table
= htab
->stub_hash_table
;
655 bfd_hash_traverse (table
, m68hc11_elf_export_one_stub
, info
);
657 /* Scan the output sections to see if we use the memory banks.
658 If so, export the symbols that define how the memory banks
659 are mapped. This is used by gdb and the simulator to obtain
660 the information. It can be used by programs to burn the eprom
661 at the good addresses. */
662 param
.use_memory_banks
= FALSE
;
663 param
.pinfo
= &htab
->pinfo
;
664 bfd_map_over_sections (abfd
, scan_sections_for_abi
, ¶m
);
665 if (param
.use_memory_banks
)
667 m68hc11_elf_set_symbol (abfd
, info
, BFD_M68HC11_BANK_START_NAME
,
668 htab
->pinfo
.bank_physical
,
669 bfd_abs_section_ptr
);
670 m68hc11_elf_set_symbol (abfd
, info
, BFD_M68HC11_BANK_VIRTUAL_NAME
,
671 htab
->pinfo
.bank_virtual
,
672 bfd_abs_section_ptr
);
673 m68hc11_elf_set_symbol (abfd
, info
, BFD_M68HC11_BANK_SIZE_NAME
,
674 htab
->pinfo
.bank_size
,
675 bfd_abs_section_ptr
);
682 m68hc11_elf_get_bank_parameters (struct bfd_link_info
*info
)
685 struct m68hc11_page_info
*pinfo
;
686 struct bfd_link_hash_entry
*h
;
687 struct m68hc11_elf_link_hash_table
*htab
;
689 htab
= m68hc11_elf_hash_table (info
);
693 pinfo
= & htab
->pinfo
;
694 if (pinfo
->bank_param_initialized
)
697 pinfo
->bank_virtual
= M68HC12_BANK_VIRT
;
698 pinfo
->bank_mask
= M68HC12_BANK_MASK
;
699 pinfo
->bank_physical
= M68HC12_BANK_BASE
;
700 pinfo
->bank_shift
= M68HC12_BANK_SHIFT
;
701 pinfo
->bank_size
= 1 << M68HC12_BANK_SHIFT
;
703 h
= bfd_link_hash_lookup (info
->hash
, BFD_M68HC11_BANK_START_NAME
,
705 if (h
!= (struct bfd_link_hash_entry
*) NULL
706 && h
->type
== bfd_link_hash_defined
)
707 pinfo
->bank_physical
= (h
->u
.def
.value
708 + h
->u
.def
.section
->output_section
->vma
709 + h
->u
.def
.section
->output_offset
);
711 h
= bfd_link_hash_lookup (info
->hash
, BFD_M68HC11_BANK_VIRTUAL_NAME
,
713 if (h
!= (struct bfd_link_hash_entry
*) NULL
714 && h
->type
== bfd_link_hash_defined
)
715 pinfo
->bank_virtual
= (h
->u
.def
.value
716 + h
->u
.def
.section
->output_section
->vma
717 + h
->u
.def
.section
->output_offset
);
719 h
= bfd_link_hash_lookup (info
->hash
, BFD_M68HC11_BANK_SIZE_NAME
,
721 if (h
!= (struct bfd_link_hash_entry
*) NULL
722 && h
->type
== bfd_link_hash_defined
)
723 pinfo
->bank_size
= (h
->u
.def
.value
724 + h
->u
.def
.section
->output_section
->vma
725 + h
->u
.def
.section
->output_offset
);
727 pinfo
->bank_shift
= 0;
728 for (i
= pinfo
->bank_size
; i
!= 0; i
>>= 1)
731 pinfo
->bank_mask
= (1 << pinfo
->bank_shift
) - 1;
732 pinfo
->bank_physical_end
= pinfo
->bank_physical
+ pinfo
->bank_size
;
733 pinfo
->bank_param_initialized
= 1;
735 h
= bfd_link_hash_lookup (info
->hash
, "__far_trampoline", FALSE
,
737 if (h
!= (struct bfd_link_hash_entry
*) NULL
738 && h
->type
== bfd_link_hash_defined
)
739 pinfo
->trampoline_addr
= (h
->u
.def
.value
740 + h
->u
.def
.section
->output_section
->vma
741 + h
->u
.def
.section
->output_offset
);
744 /* Return 1 if the address is in banked memory.
745 This can be applied to a virtual address and to a physical address. */
747 m68hc11_addr_is_banked (struct m68hc11_page_info
*pinfo
, bfd_vma addr
)
749 if (addr
>= pinfo
->bank_virtual
)
752 if (addr
>= pinfo
->bank_physical
&& addr
<= pinfo
->bank_physical_end
)
758 /* Return the physical address seen by the processor, taking
759 into account banked memory. */
761 m68hc11_phys_addr (struct m68hc11_page_info
*pinfo
, bfd_vma addr
)
763 if (addr
< pinfo
->bank_virtual
)
766 /* Map the address to the memory bank. */
767 addr
-= pinfo
->bank_virtual
;
768 addr
&= pinfo
->bank_mask
;
769 addr
+= pinfo
->bank_physical
;
773 /* Return the page number corresponding to an address in banked memory. */
775 m68hc11_phys_page (struct m68hc11_page_info
*pinfo
, bfd_vma addr
)
777 if (addr
< pinfo
->bank_virtual
)
780 /* Map the address to the memory bank. */
781 addr
-= pinfo
->bank_virtual
;
782 addr
>>= pinfo
->bank_shift
;
787 /* This function is used for relocs which are only used for relaxing,
788 which the linker should otherwise ignore. */
790 bfd_reloc_status_type
791 m68hc11_elf_ignore_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
792 arelent
*reloc_entry
,
793 asymbol
*symbol ATTRIBUTE_UNUSED
,
794 void *data ATTRIBUTE_UNUSED
,
795 asection
*input_section
,
797 char **error_message ATTRIBUTE_UNUSED
)
799 if (output_bfd
!= NULL
)
800 reloc_entry
->address
+= input_section
->output_offset
;
804 bfd_reloc_status_type
805 m68hc11_elf_special_reloc (bfd
*abfd ATTRIBUTE_UNUSED
,
806 arelent
*reloc_entry
,
808 void *data ATTRIBUTE_UNUSED
,
809 asection
*input_section
,
811 char **error_message ATTRIBUTE_UNUSED
)
813 if (output_bfd
!= (bfd
*) NULL
814 && (symbol
->flags
& BSF_SECTION_SYM
) == 0
815 && (! reloc_entry
->howto
->partial_inplace
816 || reloc_entry
->addend
== 0))
818 reloc_entry
->address
+= input_section
->output_offset
;
822 if (output_bfd
!= NULL
)
823 return bfd_reloc_continue
;
825 if (reloc_entry
->address
> bfd_get_section_limit (abfd
, input_section
))
826 return bfd_reloc_outofrange
;
831 /* Look through the relocs for a section during the first phase.
832 Since we don't do .gots or .plts, we just need to consider the
833 virtual table relocs for gc. */
836 elf32_m68hc11_check_relocs (bfd
*abfd
, struct bfd_link_info
*info
,
837 asection
*sec
, const Elf_Internal_Rela
*relocs
)
839 Elf_Internal_Shdr
* symtab_hdr
;
840 struct elf_link_hash_entry
** sym_hashes
;
841 const Elf_Internal_Rela
* rel
;
842 const Elf_Internal_Rela
* rel_end
;
844 if (info
->relocatable
)
847 symtab_hdr
= & elf_tdata (abfd
)->symtab_hdr
;
848 sym_hashes
= elf_sym_hashes (abfd
);
849 rel_end
= relocs
+ sec
->reloc_count
;
851 for (rel
= relocs
; rel
< rel_end
; rel
++)
853 struct elf_link_hash_entry
* h
;
854 unsigned long r_symndx
;
856 r_symndx
= ELF32_R_SYM (rel
->r_info
);
858 if (r_symndx
< symtab_hdr
->sh_info
)
862 h
= sym_hashes
[r_symndx
- symtab_hdr
->sh_info
];
863 while (h
->root
.type
== bfd_link_hash_indirect
864 || h
->root
.type
== bfd_link_hash_warning
)
865 h
= (struct elf_link_hash_entry
*) h
->root
.u
.i
.link
;
868 switch (ELF32_R_TYPE (rel
->r_info
))
870 /* This relocation describes the C++ object vtable hierarchy.
871 Reconstruct it for later use during GC. */
872 case R_M68HC11_GNU_VTINHERIT
:
873 if (!bfd_elf_gc_record_vtinherit (abfd
, sec
, h
, rel
->r_offset
))
877 /* This relocation describes which C++ vtable entries are actually
878 used. Record for later use during GC. */
879 case R_M68HC11_GNU_VTENTRY
:
880 BFD_ASSERT (h
!= NULL
);
882 && !bfd_elf_gc_record_vtentry (abfd
, sec
, h
, rel
->r_addend
))
891 /* Relocate a 68hc11/68hc12 ELF section. */
893 elf32_m68hc11_relocate_section (bfd
*output_bfd ATTRIBUTE_UNUSED
,
894 struct bfd_link_info
*info
,
895 bfd
*input_bfd
, asection
*input_section
,
896 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
897 Elf_Internal_Sym
*local_syms
,
898 asection
**local_sections
)
900 Elf_Internal_Shdr
*symtab_hdr
;
901 struct elf_link_hash_entry
**sym_hashes
;
902 Elf_Internal_Rela
*rel
, *relend
;
903 const char *name
= NULL
;
904 struct m68hc11_page_info
*pinfo
;
905 const struct elf_backend_data
* const ebd
= get_elf_backend_data (input_bfd
);
906 struct m68hc11_elf_link_hash_table
*htab
;
908 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
909 sym_hashes
= elf_sym_hashes (input_bfd
);
911 htab
= m68hc11_elf_hash_table (info
);
915 /* Get memory bank parameters. */
916 m68hc11_elf_get_bank_parameters (info
);
918 pinfo
= & htab
->pinfo
;
920 relend
= relocs
+ input_section
->reloc_count
;
922 for (; rel
< relend
; rel
++)
926 reloc_howto_type
*howto
;
927 unsigned long r_symndx
;
928 Elf_Internal_Sym
*sym
;
930 bfd_vma relocation
= 0;
931 bfd_reloc_status_type r
= bfd_reloc_undefined
;
936 bfd_boolean is_far
= FALSE
;
937 struct elf_link_hash_entry
*h
;
939 r_symndx
= ELF32_R_SYM (rel
->r_info
);
940 r_type
= ELF32_R_TYPE (rel
->r_info
);
942 if (r_type
== R_M68HC11_GNU_VTENTRY
943 || r_type
== R_M68HC11_GNU_VTINHERIT
)
946 (*ebd
->elf_info_to_howto_rel
) (input_bfd
, &arel
, rel
);
952 if (r_symndx
< symtab_hdr
->sh_info
)
954 sym
= local_syms
+ r_symndx
;
955 sec
= local_sections
[r_symndx
];
956 relocation
= (sec
->output_section
->vma
959 is_far
= (sym
&& (sym
->st_other
& STO_M68HC12_FAR
));
963 bfd_boolean unresolved_reloc
, warned
;
965 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
966 r_symndx
, symtab_hdr
, sym_hashes
,
967 h
, sec
, relocation
, unresolved_reloc
,
970 is_far
= (h
&& (h
->other
& STO_M68HC12_FAR
));
973 if (sec
!= NULL
&& elf_discarded_section (sec
))
975 /* For relocs against symbols from removed linkonce sections,
976 or sections discarded by a linker script, we just want the
977 section contents zeroed. Avoid any special processing. */
978 _bfd_clear_contents (howto
, input_bfd
, contents
+ rel
->r_offset
);
984 if (info
->relocatable
)
986 /* This is a relocatable link. We don't have to change
987 anything, unless the reloc is against a section symbol,
988 in which case we have to adjust according to where the
989 section symbol winds up in the output section. */
990 if (sym
!= NULL
&& ELF_ST_TYPE (sym
->st_info
) == STT_SECTION
)
991 rel
->r_addend
+= sec
->output_offset
;
996 name
= h
->root
.root
.string
;
999 name
= (bfd_elf_string_from_elf_section
1000 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
1001 if (name
== NULL
|| *name
== '\0')
1002 name
= bfd_section_name (input_bfd
, sec
);
1005 if (is_far
&& ELF32_R_TYPE (rel
->r_info
) == R_M68HC11_16
)
1007 struct elf32_m68hc11_stub_hash_entry
* stub
;
1009 stub
= m68hc12_stub_hash_lookup (htab
->stub_hash_table
,
1010 name
, FALSE
, FALSE
);
1013 relocation
= stub
->stub_offset
1014 + stub
->stub_sec
->output_section
->vma
1015 + stub
->stub_sec
->output_offset
;
1020 /* Do the memory bank mapping. */
1021 phys_addr
= m68hc11_phys_addr (pinfo
, relocation
+ rel
->r_addend
);
1022 phys_page
= m68hc11_phys_page (pinfo
, relocation
+ rel
->r_addend
);
1026 /* Reloc used by 68HC12 call instruction. */
1027 bfd_put_16 (input_bfd
, phys_addr
,
1028 (bfd_byte
*) contents
+ rel
->r_offset
);
1029 bfd_put_8 (input_bfd
, phys_page
,
1030 (bfd_byte
*) contents
+ rel
->r_offset
+ 2);
1032 r_type
= R_M68HC11_NONE
;
1035 case R_M68HC11_NONE
:
1039 case R_M68HC11_LO16
:
1040 /* Reloc generated by %addr(expr) gas to obtain the
1041 address as mapped in the memory bank window. */
1042 relocation
= phys_addr
;
1045 case R_M68HC11_PAGE
:
1046 /* Reloc generated by %page(expr) gas to obtain the
1047 page number associated with the address. */
1048 relocation
= phys_page
;
1052 /* Get virtual address of instruction having the relocation. */
1057 msg
= _("Reference to the far symbol `%s' using a wrong "
1058 "relocation may result in incorrect execution");
1059 buf
= alloca (strlen (msg
) + strlen (name
) + 10);
1060 sprintf (buf
, msg
, name
);
1062 (* info
->callbacks
->warning
)
1063 (info
, buf
, name
, input_bfd
, NULL
, rel
->r_offset
);
1066 /* Get virtual address of instruction having the relocation. */
1067 insn_addr
= input_section
->output_section
->vma
1068 + input_section
->output_offset
1071 insn_page
= m68hc11_phys_page (pinfo
, insn_addr
);
1073 if (m68hc11_addr_is_banked (pinfo
, relocation
+ rel
->r_addend
)
1074 && m68hc11_addr_is_banked (pinfo
, insn_addr
)
1075 && phys_page
!= insn_page
)
1080 msg
= _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1081 "as current banked address [%lx:%04lx] (%lx)");
1083 buf
= alloca (strlen (msg
) + 128);
1084 sprintf (buf
, msg
, phys_page
, phys_addr
,
1085 (long) (relocation
+ rel
->r_addend
),
1086 insn_page
, m68hc11_phys_addr (pinfo
, insn_addr
),
1087 (long) (insn_addr
));
1088 if (!((*info
->callbacks
->warning
)
1089 (info
, buf
, name
, input_bfd
, input_section
,
1094 if (phys_page
!= 0 && insn_page
== 0)
1099 msg
= _("reference to a banked address [%lx:%04lx] in the "
1100 "normal address space at %04lx");
1102 buf
= alloca (strlen (msg
) + 128);
1103 sprintf (buf
, msg
, phys_page
, phys_addr
, insn_addr
);
1104 if (!((*info
->callbacks
->warning
)
1105 (info
, buf
, name
, input_bfd
, input_section
,
1109 relocation
= phys_addr
;
1113 /* If this is a banked address use the phys_addr so that
1114 we stay in the banked window. */
1115 if (m68hc11_addr_is_banked (pinfo
, relocation
+ rel
->r_addend
))
1116 relocation
= phys_addr
;
1119 if (r_type
!= R_M68HC11_NONE
)
1120 r
= _bfd_final_link_relocate (howto
, input_bfd
, input_section
,
1121 contents
, rel
->r_offset
,
1122 relocation
, rel
->r_addend
);
1124 if (r
!= bfd_reloc_ok
)
1126 const char * msg
= (const char *) 0;
1130 case bfd_reloc_overflow
:
1131 if (!((*info
->callbacks
->reloc_overflow
)
1132 (info
, NULL
, name
, howto
->name
, (bfd_vma
) 0,
1133 input_bfd
, input_section
, rel
->r_offset
)))
1137 case bfd_reloc_undefined
:
1138 if (!((*info
->callbacks
->undefined_symbol
)
1139 (info
, name
, input_bfd
, input_section
,
1140 rel
->r_offset
, TRUE
)))
1144 case bfd_reloc_outofrange
:
1145 msg
= _ ("internal error: out of range error");
1148 case bfd_reloc_notsupported
:
1149 msg
= _ ("internal error: unsupported relocation error");
1152 case bfd_reloc_dangerous
:
1153 msg
= _ ("internal error: dangerous error");
1157 msg
= _ ("internal error: unknown error");
1161 if (!((*info
->callbacks
->warning
)
1162 (info
, msg
, name
, input_bfd
, input_section
,
1175 /* Set and control ELF flags in ELF header. */
1178 _bfd_m68hc11_elf_set_private_flags (bfd
*abfd
, flagword flags
)
1180 BFD_ASSERT (!elf_flags_init (abfd
)
1181 || elf_elfheader (abfd
)->e_flags
== flags
);
1183 elf_elfheader (abfd
)->e_flags
= flags
;
1184 elf_flags_init (abfd
) = TRUE
;
1188 /* Merge backend specific data from an object file to the output
1189 object file when linking. */
1192 _bfd_m68hc11_elf_merge_private_bfd_data (bfd
*ibfd
, bfd
*obfd
)
1196 bfd_boolean ok
= TRUE
;
1198 /* Check if we have the same endianess */
1199 if (!_bfd_generic_verify_endian_match (ibfd
, obfd
))
1202 if (bfd_get_flavour (ibfd
) != bfd_target_elf_flavour
1203 || bfd_get_flavour (obfd
) != bfd_target_elf_flavour
)
1206 new_flags
= elf_elfheader (ibfd
)->e_flags
;
1207 elf_elfheader (obfd
)->e_flags
|= new_flags
& EF_M68HC11_ABI
;
1208 old_flags
= elf_elfheader (obfd
)->e_flags
;
1210 if (! elf_flags_init (obfd
))
1212 elf_flags_init (obfd
) = TRUE
;
1213 elf_elfheader (obfd
)->e_flags
= new_flags
;
1214 elf_elfheader (obfd
)->e_ident
[EI_CLASS
]
1215 = elf_elfheader (ibfd
)->e_ident
[EI_CLASS
];
1217 if (bfd_get_arch (obfd
) == bfd_get_arch (ibfd
)
1218 && bfd_get_arch_info (obfd
)->the_default
)
1220 if (! bfd_set_arch_mach (obfd
, bfd_get_arch (ibfd
),
1221 bfd_get_mach (ibfd
)))
1228 /* Check ABI compatibility. */
1229 if ((new_flags
& E_M68HC11_I32
) != (old_flags
& E_M68HC11_I32
))
1231 (*_bfd_error_handler
)
1232 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1233 "and others for 32-bit integers"), ibfd
);
1236 if ((new_flags
& E_M68HC11_F64
) != (old_flags
& E_M68HC11_F64
))
1238 (*_bfd_error_handler
)
1239 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1240 "and others for 64-bit double"), ibfd
);
1244 /* Processor compatibility. */
1245 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags
, old_flags
))
1247 (*_bfd_error_handler
)
1248 (_("%B: linking files compiled for HCS12 with "
1249 "others compiled for HC12"), ibfd
);
1252 new_flags
= ((new_flags
& ~EF_M68HC11_MACH_MASK
)
1253 | (EF_M68HC11_MERGE_MACH (new_flags
, old_flags
)));
1255 elf_elfheader (obfd
)->e_flags
= new_flags
;
1257 new_flags
&= ~(EF_M68HC11_ABI
| EF_M68HC11_MACH_MASK
);
1258 old_flags
&= ~(EF_M68HC11_ABI
| EF_M68HC11_MACH_MASK
);
1260 /* Warn about any other mismatches */
1261 if (new_flags
!= old_flags
)
1263 (*_bfd_error_handler
)
1264 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1265 ibfd
, (unsigned long) new_flags
, (unsigned long) old_flags
);
1271 bfd_set_error (bfd_error_bad_value
);
1279 _bfd_m68hc11_elf_print_private_bfd_data (bfd
*abfd
, void *ptr
)
1281 FILE *file
= (FILE *) ptr
;
1283 BFD_ASSERT (abfd
!= NULL
&& ptr
!= NULL
);
1285 /* Print normal ELF private data. */
1286 _bfd_elf_print_private_bfd_data (abfd
, ptr
);
1288 /* xgettext:c-format */
1289 fprintf (file
, _("private flags = %lx:"), elf_elfheader (abfd
)->e_flags
);
1291 if (elf_elfheader (abfd
)->e_flags
& E_M68HC11_I32
)
1292 fprintf (file
, _("[abi=32-bit int, "));
1294 fprintf (file
, _("[abi=16-bit int, "));
1296 if (elf_elfheader (abfd
)->e_flags
& E_M68HC11_F64
)
1297 fprintf (file
, _("64-bit double, "));
1299 fprintf (file
, _("32-bit double, "));
1301 if (strcmp (bfd_get_target (abfd
), "elf32-m68hc11") == 0)
1302 fprintf (file
, _("cpu=HC11]"));
1303 else if (elf_elfheader (abfd
)->e_flags
& EF_M68HCS12_MACH
)
1304 fprintf (file
, _("cpu=HCS12]"));
1306 fprintf (file
, _("cpu=HC12]"));
1308 if (elf_elfheader (abfd
)->e_flags
& E_M68HC12_BANKS
)
1309 fprintf (file
, _(" [memory=bank-model]"));
1311 fprintf (file
, _(" [memory=flat]"));
1318 static void scan_sections_for_abi (bfd
*abfd ATTRIBUTE_UNUSED
,
1319 asection
*asect
, void *arg
)
1321 struct m68hc11_scan_param
* p
= (struct m68hc11_scan_param
*) arg
;
1323 if (asect
->vma
>= p
->pinfo
->bank_virtual
)
1324 p
->use_memory_banks
= TRUE
;
1327 /* Tweak the OSABI field of the elf header. */
1330 elf32_m68hc11_post_process_headers (bfd
*abfd
, struct bfd_link_info
*link_info
)
1332 struct m68hc11_scan_param param
;
1333 struct m68hc11_elf_link_hash_table
*htab
;
1335 if (link_info
== NULL
)
1338 htab
= m68hc11_elf_hash_table (link_info
);
1342 m68hc11_elf_get_bank_parameters (link_info
);
1344 param
.use_memory_banks
= FALSE
;
1345 param
.pinfo
= & htab
->pinfo
;
1347 bfd_map_over_sections (abfd
, scan_sections_for_abi
, ¶m
);
1349 if (param
.use_memory_banks
)
1351 Elf_Internal_Ehdr
* i_ehdrp
;
1353 i_ehdrp
= elf_elfheader (abfd
);
1354 i_ehdrp
->e_flags
|= E_M68HC12_BANKS
;