]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-m68hc1x.c
Add missing ld makefile dependency for nios.
[thirdparty/binutils-gdb.git] / bfd / elf32-m68hc1x.c
CommitLineData
3a65329d 1/* Motorola 68HC11/HC12-specific support for 32-bit ELF
4b95cf5c 2 Copyright (C) 1999-2014 Free Software Foundation, Inc.
3a65329d
SC
3 Contributed by Stephane Carrez (stcarrez@nerim.fr)
4
cd123cb7
NC
5 This file is part of BFD, the Binary File Descriptor library.
6
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.
11
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.
16
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. */
3a65329d 21
3a65329d 22#include "sysdep.h"
df7b86aa 23#include "alloca-conf.h"
3db64b00 24#include "bfd.h"
3a65329d
SC
25#include "bfdlink.h"
26#include "libbfd.h"
27#include "elf-bfd.h"
28#include "elf32-m68hc1x.h"
29#include "elf/m68hc11.h"
30#include "opcode/m68hc11.h"
31
32
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)))
36
37static struct elf32_m68hc11_stub_hash_entry* m68hc12_add_stub
0a6a3ebe
SC
38 (const char *stub_name,
39 asection *section,
40 struct m68hc11_elf_link_hash_table *htab);
3a65329d
SC
41
42static struct bfd_hash_entry *stub_hash_newfunc
0a6a3ebe 43 (struct bfd_hash_entry *, struct bfd_hash_table *, const char *);
3a65329d 44
0a6a3ebe
SC
45static void m68hc11_elf_set_symbol (bfd* abfd, struct bfd_link_info *info,
46 const char* name, bfd_vma value,
47 asection* sec);
3a65329d
SC
48
49static bfd_boolean m68hc11_elf_export_one_stub
0a6a3ebe 50 (struct bfd_hash_entry *gen_entry, void *in_arg);
3a65329d 51
2c3fc389 52static void scan_sections_for_abi (bfd*, asection*, void *);
3a65329d
SC
53
54struct m68hc11_scan_param
55{
56 struct m68hc11_page_info* pinfo;
57 bfd_boolean use_memory_banks;
58};
59
60
61/* Create a 68HC11/68HC12 ELF linker hash table. */
62
63struct m68hc11_elf_link_hash_table*
0a6a3ebe 64m68hc11_elf_hash_table_create (bfd *abfd)
3a65329d
SC
65{
66 struct m68hc11_elf_link_hash_table *ret;
67 bfd_size_type amt = sizeof (struct m68hc11_elf_link_hash_table);
68
7bf52ea2 69 ret = (struct m68hc11_elf_link_hash_table *) bfd_zmalloc (amt);
3a65329d
SC
70 if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
71 return NULL;
72
66eb6687
AM
73 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
74 _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
75 sizeof (struct elf_link_hash_entry),
76 M68HC11_ELF_DATA))
3a65329d 77 {
47247ced 78 free (ret);
3a65329d
SC
79 return NULL;
80 }
81
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)
86 {
47247ced 87 free (ret);
3a65329d
SC
88 return NULL;
89 }
66eb6687
AM
90 if (!bfd_hash_table_init (ret->stub_hash_table, stub_hash_newfunc,
91 sizeof (struct elf32_m68hc11_stub_hash_entry)))
3a65329d
SC
92 return NULL;
93
3a65329d
SC
94 return ret;
95}
96
97/* Free the derived linker hash table. */
98
99void
0a6a3ebe 100m68hc11_elf_bfd_link_hash_table_free (struct bfd_link_hash_table *hash)
3a65329d
SC
101{
102 struct m68hc11_elf_link_hash_table *ret
103 = (struct m68hc11_elf_link_hash_table *) hash;
104
105 bfd_hash_table_free (ret->stub_hash_table);
106 free (ret->stub_hash_table);
9f7c3e5e 107 _bfd_elf_link_hash_table_free (hash);
3a65329d
SC
108}
109
110/* Assorted hash table functions. */
111
112/* Initialize an entry in the stub hash table. */
113
114static struct bfd_hash_entry *
0a6a3ebe
SC
115stub_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
116 const char *string)
3a65329d
SC
117{
118 /* Allocate the structure if it has not already been allocated by a
119 subclass. */
120 if (entry == NULL)
121 {
122 entry = bfd_hash_allocate (table,
123 sizeof (struct elf32_m68hc11_stub_hash_entry));
124 if (entry == NULL)
125 return entry;
126 }
127
128 /* Call the allocation method of the superclass. */
129 entry = bfd_hash_newfunc (entry, table, string);
130 if (entry != NULL)
131 {
132 struct elf32_m68hc11_stub_hash_entry *eh;
133
134 /* Initialize the local fields. */
135 eh = (struct elf32_m68hc11_stub_hash_entry *) entry;
136 eh->stub_sec = NULL;
137 eh->stub_offset = 0;
138 eh->target_value = 0;
139 eh->target_section = NULL;
140 }
141
142 return entry;
143}
144
145/* Add a new stub entry to the stub hash. Not all fields of the new
146 stub entry are initialised. */
147
148static struct elf32_m68hc11_stub_hash_entry *
0a6a3ebe
SC
149m68hc12_add_stub (const char *stub_name, asection *section,
150 struct m68hc11_elf_link_hash_table *htab)
3a65329d
SC
151{
152 struct elf32_m68hc11_stub_hash_entry *stub_entry;
153
154 /* Enter this entry into the linker stub hash table. */
155 stub_entry = m68hc12_stub_hash_lookup (htab->stub_hash_table, stub_name,
156 TRUE, FALSE);
157 if (stub_entry == NULL)
158 {
d003868e
AM
159 (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
160 section->owner, stub_name);
3a65329d
SC
161 return NULL;
162 }
163
164 if (htab->stub_section == 0)
165 {
166 htab->stub_section = (*htab->add_stub_section) (".tramp",
167 htab->tramp_section);
168 }
169
170 stub_entry->stub_sec = htab->stub_section;
171 stub_entry->stub_offset = 0;
172 return stub_entry;
173}
174
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. */
178
179bfd_boolean
0a6a3ebe 180elf32_m68hc11_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 181 Elf_Internal_Sym *sym,
0a6a3ebe
SC
182 const char **namep ATTRIBUTE_UNUSED,
183 flagword *flagsp ATTRIBUTE_UNUSED,
184 asection **secp ATTRIBUTE_UNUSED,
185 bfd_vma *valp ATTRIBUTE_UNUSED)
3a65329d
SC
186{
187 if (sym->st_other & STO_M68HC12_FAR)
188 {
189 struct elf_link_hash_entry *h;
190
191 h = (struct elf_link_hash_entry *)
192 bfd_link_hash_lookup (info->hash, "__far_trampoline",
193 FALSE, FALSE, FALSE);
194 if (h == NULL)
195 {
196 struct bfd_link_hash_entry* entry = NULL;
197
198 _bfd_generic_link_add_one_symbol (info, abfd,
199 "__far_trampoline",
200 BSF_GLOBAL,
201 bfd_und_section_ptr,
202 (bfd_vma) 0, (const char*) NULL,
203 FALSE, FALSE, &entry);
204 }
205
206 }
207 return TRUE;
208}
209
5efbbc43
AM
210/* Merge non-visibility st_other attributes, STO_M68HC12_FAR and
211 STO_M68HC12_INTERRUPT. */
212
213void
214elf32_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)
218{
219 if (definition)
220 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
221 | ELF_ST_VISIBILITY (h->other));
222}
223
3a65329d
SC
224/* External entry points for sizing and building linker stubs. */
225
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. */
229
230int
0a6a3ebe 231elf32_m68hc11_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
3a65329d
SC
232{
233 bfd *input_bfd;
234 unsigned int bfd_count;
235 int top_id, top_index;
236 asection *section;
237 asection **input_list, **list;
238 bfd_size_type amt;
239 asection *text_section;
240 struct m68hc11_elf_link_hash_table *htab;
241
242 htab = m68hc11_elf_hash_table (info);
4dfe6ac6
NC
243 if (htab == NULL)
244 return -1;
3a65329d 245
f13a99db 246 if (bfd_get_flavour (info->output_bfd) != bfd_target_elf_flavour)
3a65329d
SC
247 return 0;
248
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
252 can't find it. */
253 htab->tramp_section = 0;
254 text_section = 0;
255 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
256 input_bfd != NULL;
257 input_bfd = input_bfd->link_next)
258 {
259 bfd_count += 1;
260 for (section = input_bfd->sections;
261 section != NULL;
262 section = section->next)
263 {
264 const char* name = bfd_get_section_name (input_bfd, section);
265
266 if (!strcmp (name, ".tramp"))
267 htab->tramp_section = section;
268
269 if (!strcmp (name, ".text"))
270 text_section = section;
271
272 if (top_id < section->id)
273 top_id = section->id;
274 }
275 }
276 htab->bfd_count = bfd_count;
277 if (htab->tramp_section == 0)
278 htab->tramp_section = text_section;
279
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
8423293d 282 strip_excluded_output_sections doesn't renumber the indices. */
3a65329d
SC
283 for (section = output_bfd->sections, top_index = 0;
284 section != NULL;
285 section = section->next)
286 {
287 if (top_index < section->index)
288 top_index = section->index;
289 }
290
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)
296 return -1;
297
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;
301 do
302 *list = bfd_abs_section_ptr;
303 while (list-- != input_list);
304
305 for (section = output_bfd->sections;
306 section != NULL;
307 section = section->next)
308 {
309 if ((section->flags & SEC_CODE) != 0)
310 input_list[section->index] = NULL;
311 }
312
313 return 1;
314}
315
316/* Determine and set the size of the stub section for a final link.
317
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"
320 instruction. */
321
322bfd_boolean
0a6a3ebe
SC
323elf32_m68hc11_size_stubs (bfd *output_bfd, bfd *stub_bfd,
324 struct bfd_link_info *info,
325 asection * (*add_stub_section) (const char*, asection*))
3a65329d
SC
326{
327 bfd *input_bfd;
328 asection *section;
329 Elf_Internal_Sym *local_syms, **all_local_syms;
330 unsigned int bfd_indx, bfd_count;
331 bfd_size_type amt;
332 asection *stub_sec;
3a65329d
SC
333 struct m68hc11_elf_link_hash_table *htab = m68hc11_elf_hash_table (info);
334
4dfe6ac6
NC
335 if (htab == NULL)
336 return FALSE;
337
3a65329d
SC
338 /* Stash our params away. */
339 htab->stub_bfd = stub_bfd;
340 htab->add_stub_section = add_stub_section;
341
342 /* Count the number of input BFDs and find the top input section id. */
343 for (input_bfd = info->input_bfds, bfd_count = 0;
344 input_bfd != NULL;
345 input_bfd = input_bfd->link_next)
4dfe6ac6 346 bfd_count += 1;
3a65329d
SC
347
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)
354 return FALSE;
355
356 /* Walk over all the input BFDs, swapping in local symbols. */
357 for (input_bfd = info->input_bfds, bfd_indx = 0;
358 input_bfd != NULL;
359 input_bfd = input_bfd->link_next, bfd_indx++)
360 {
361 Elf_Internal_Shdr *symtab_hdr;
3a65329d
SC
362
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)
366 continue;
367
2a0e29b4
SC
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)
371 {
372 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
373 symtab_hdr->sh_info, 0,
374 NULL, NULL, NULL);
375 /* Cache them for elf_link_input_bfd. */
376 symtab_hdr->contents = (unsigned char *) local_syms;
377 }
3a65329d 378 if (local_syms == NULL)
3a65329d 379 {
2a0e29b4
SC
380 free (all_local_syms);
381 return FALSE;
3a65329d
SC
382 }
383
2a0e29b4 384 all_local_syms[bfd_indx] = local_syms;
3a65329d
SC
385 }
386
387 for (input_bfd = info->input_bfds, bfd_indx = 0;
388 input_bfd != NULL;
389 input_bfd = input_bfd->link_next, bfd_indx++)
390 {
391 Elf_Internal_Shdr *symtab_hdr;
3a65329d
SC
392 struct elf_link_hash_entry ** sym_hashes;
393
394 sym_hashes = elf_sym_hashes (input_bfd);
395
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)
399 continue;
400
401 local_syms = all_local_syms[bfd_indx];
402
403 /* Walk over each section attached to the input bfd. */
404 for (section = input_bfd->sections;
405 section != NULL;
406 section = section->next)
407 {
408 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
409
410 /* If there aren't any relocs, then there's nothing more
411 to do. */
412 if ((section->flags & SEC_RELOC) == 0
413 || section->reloc_count == 0)
414 continue;
415
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)
420 continue;
421
422 /* Get the relocs. */
423 internal_relocs
45d6a902
AM
424 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
425 (Elf_Internal_Rela *) NULL,
426 info->keep_memory);
3a65329d
SC
427 if (internal_relocs == NULL)
428 goto error_ret_free_local;
429
430 /* Now examine each relocation. */
431 irela = internal_relocs;
432 irelaend = irela + section->reloc_count;
433 for (; irela < irelaend; irela++)
434 {
435 unsigned int r_type, r_indx;
436 struct elf32_m68hc11_stub_hash_entry *stub_entry;
437 asection *sym_sec;
438 bfd_vma sym_value;
439 struct elf_link_hash_entry *hash;
440 const char *stub_name;
441 Elf_Internal_Sym *sym;
442
443 r_type = ELF32_R_TYPE (irela->r_info);
444
445 /* Only look at 16-bit relocs. */
446 if (r_type != (unsigned int) R_M68HC11_16)
447 continue;
448
449 /* Now determine the call target, its name, value,
450 section. */
451 r_indx = ELF32_R_SYM (irela->r_info);
452 if (r_indx < symtab_hdr->sh_info)
453 {
454 /* It's a local symbol. */
455 Elf_Internal_Shdr *hdr;
456 bfd_boolean is_far;
457
458 sym = local_syms + r_indx;
3a65329d
SC
459 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
460 if (!is_far)
461 continue;
7f888330 462
4fbb74a6
AM
463 if (sym->st_shndx >= elf_numsections (input_bfd))
464 sym_sec = NULL;
465 else
466 {
467 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
468 sym_sec = hdr->bfd_section;
469 }
3a65329d
SC
470 stub_name = (bfd_elf_string_from_elf_section
471 (input_bfd, symtab_hdr->sh_link,
472 sym->st_name));
473 sym_value = sym->st_value;
474 hash = NULL;
475 }
476 else
477 {
478 /* It's an external symbol. */
479 int e_indx;
480
481 e_indx = r_indx - symtab_hdr->sh_info;
482 hash = (struct elf_link_hash_entry *)
483 (sym_hashes[e_indx]);
484
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);
489
490 if (hash->root.type == bfd_link_hash_defined
83774818
SC
491 || hash->root.type == bfd_link_hash_defweak
492 || hash->root.type == bfd_link_hash_new)
3a65329d
SC
493 {
494 if (!(hash->other & STO_M68HC12_FAR))
495 continue;
496 }
497 else if (hash->root.type == bfd_link_hash_undefweak)
498 {
499 continue;
500 }
501 else if (hash->root.type == bfd_link_hash_undefined)
502 {
503 continue;
504 }
505 else
506 {
507 bfd_set_error (bfd_error_bad_value);
508 goto error_ret_free_internal;
509 }
510 sym_sec = hash->root.u.def.section;
511 sym_value = hash->root.u.def.value;
512 stub_name = hash->root.root.string;
513 }
514
515 if (!stub_name)
516 goto error_ret_free_internal;
517
518 stub_entry = m68hc12_stub_hash_lookup
519 (htab->stub_hash_table,
520 stub_name,
521 FALSE, FALSE);
522 if (stub_entry == NULL)
523 {
524 if (add_stub_section == 0)
525 continue;
526
527 stub_entry = m68hc12_add_stub (stub_name, section, htab);
528 if (stub_entry == NULL)
529 {
530 error_ret_free_internal:
531 if (elf_section_data (section)->relocs == NULL)
532 free (internal_relocs);
533 goto error_ret_free_local;
534 }
535 }
536
537 stub_entry->target_value = sym_value;
538 stub_entry->target_section = sym_sec;
539 }
540
541 /* We're done with the internal relocs, free them. */
542 if (elf_section_data (section)->relocs == NULL)
543 free (internal_relocs);
544 }
545 }
546
547 if (add_stub_section)
548 {
549 /* OK, we've added some stubs. Find out the new size of the
550 stub sections. */
551 for (stub_sec = htab->stub_bfd->sections;
552 stub_sec != NULL;
553 stub_sec = stub_sec->next)
554 {
eea6121a 555 stub_sec->size = 0;
3a65329d
SC
556 }
557
558 bfd_hash_traverse (htab->stub_hash_table, htab->size_one_stub, htab);
559 }
2a0e29b4 560 free (all_local_syms);
3a65329d
SC
561 return TRUE;
562
563 error_ret_free_local:
2a0e29b4 564 free (all_local_syms);
3a65329d
SC
565 return FALSE;
566}
567
568/* Export the trampoline addresses in the symbol table. */
569static bfd_boolean
0a6a3ebe 570m68hc11_elf_export_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
3a65329d
SC
571{
572 struct bfd_link_info *info;
573 struct m68hc11_elf_link_hash_table *htab;
574 struct elf32_m68hc11_stub_hash_entry *stub_entry;
575 char* name;
576 bfd_boolean result;
577
578 info = (struct bfd_link_info *) in_arg;
579 htab = m68hc11_elf_hash_table (info);
4dfe6ac6
NC
580 if (htab == NULL)
581 return FALSE;
3a65329d
SC
582
583 /* Massage our args to the form they really have. */
584 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
585
586 /* Generate the trampoline according to HC11 or HC12. */
587 result = (* htab->build_one_stub) (gen_entry, in_arg);
588
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);
592
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);
597 return result;
598}
599
600/* Export a symbol or set its value and section. */
601static void
0a6a3ebe
SC
602m68hc11_elf_set_symbol (bfd *abfd, struct bfd_link_info *info,
603 const char *name, bfd_vma value, asection *sec)
3a65329d
SC
604{
605 struct elf_link_hash_entry *h;
606
607 h = (struct elf_link_hash_entry *)
608 bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
609 if (h == NULL)
610 {
611 _bfd_generic_link_add_one_symbol (info, abfd,
612 name,
613 BSF_GLOBAL,
614 sec,
615 value,
616 (const char*) NULL,
617 TRUE, FALSE, NULL);
618 }
619 else
620 {
621 h->root.type = bfd_link_hash_defined;
622 h->root.u.def.value = value;
623 h->root.u.def.section = sec;
624 }
625}
626
627
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
631 linker. */
632
633bfd_boolean
0a6a3ebe 634elf32_m68hc11_build_stubs (bfd *abfd, struct bfd_link_info *info)
3a65329d
SC
635{
636 asection *stub_sec;
637 struct bfd_hash_table *table;
638 struct m68hc11_elf_link_hash_table *htab;
639 struct m68hc11_scan_param param;
640
641 m68hc11_elf_get_bank_parameters (info);
642 htab = m68hc11_elf_hash_table (info);
4dfe6ac6
NC
643 if (htab == NULL)
644 return FALSE;
3a65329d
SC
645
646 for (stub_sec = htab->stub_bfd->sections;
647 stub_sec != NULL;
648 stub_sec = stub_sec->next)
649 {
650 bfd_size_type size;
651
652 /* Allocate memory to hold the linker stubs. */
eea6121a 653 size = stub_sec->size;
3a65329d
SC
654 stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
655 if (stub_sec->contents == NULL && size != 0)
656 return FALSE;
eea6121a 657 stub_sec->size = 0;
3a65329d
SC
658 }
659
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);
68ffbac6 663
3a65329d
SC
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, &param);
672 if (param.use_memory_banks)
673 {
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);
683 }
684
685 return TRUE;
686}
687
688void
0a6a3ebe 689m68hc11_elf_get_bank_parameters (struct bfd_link_info *info)
3a65329d
SC
690{
691 unsigned i;
692 struct m68hc11_page_info *pinfo;
693 struct bfd_link_hash_entry *h;
4dfe6ac6
NC
694 struct m68hc11_elf_link_hash_table *htab;
695
696 htab = m68hc11_elf_hash_table (info);
697 if (htab == NULL)
698 return;
3a65329d 699
4dfe6ac6 700 pinfo = & htab->pinfo;
3a65329d
SC
701 if (pinfo->bank_param_initialized)
702 return;
703
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;
709
710 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_START_NAME,
711 FALSE, FALSE, TRUE);
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);
717
718 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_VIRTUAL_NAME,
719 FALSE, FALSE, TRUE);
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);
725
726 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_SIZE_NAME,
727 FALSE, FALSE, TRUE);
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);
733
734 pinfo->bank_shift = 0;
735 for (i = pinfo->bank_size; i != 0; i >>= 1)
736 pinfo->bank_shift++;
737 pinfo->bank_shift--;
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;
741
742 h = bfd_link_hash_lookup (info->hash, "__far_trampoline", FALSE,
743 FALSE, TRUE);
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);
749}
750
751/* Return 1 if the address is in banked memory.
752 This can be applied to a virtual address and to a physical address. */
753int
0a6a3ebe 754m68hc11_addr_is_banked (struct m68hc11_page_info *pinfo, bfd_vma addr)
3a65329d
SC
755{
756 if (addr >= pinfo->bank_virtual)
757 return 1;
758
759 if (addr >= pinfo->bank_physical && addr <= pinfo->bank_physical_end)
760 return 1;
761
762 return 0;
763}
764
765/* Return the physical address seen by the processor, taking
766 into account banked memory. */
767bfd_vma
0a6a3ebe 768m68hc11_phys_addr (struct m68hc11_page_info *pinfo, bfd_vma addr)
3a65329d
SC
769{
770 if (addr < pinfo->bank_virtual)
771 return addr;
772
773 /* Map the address to the memory bank. */
774 addr -= pinfo->bank_virtual;
775 addr &= pinfo->bank_mask;
776 addr += pinfo->bank_physical;
777 return addr;
778}
779
780/* Return the page number corresponding to an address in banked memory. */
781bfd_vma
0a6a3ebe 782m68hc11_phys_page (struct m68hc11_page_info *pinfo, bfd_vma addr)
3a65329d
SC
783{
784 if (addr < pinfo->bank_virtual)
785 return 0;
786
787 /* Map the address to the memory bank. */
788 addr -= pinfo->bank_virtual;
789 addr >>= pinfo->bank_shift;
790 addr &= 0x0ff;
791 return addr;
792}
793
794/* This function is used for relocs which are only used for relaxing,
795 which the linker should otherwise ignore. */
796
797bfd_reloc_status_type
0a6a3ebe
SC
798m68hc11_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,
803 bfd *output_bfd,
804 char **error_message ATTRIBUTE_UNUSED)
3a65329d
SC
805{
806 if (output_bfd != NULL)
807 reloc_entry->address += input_section->output_offset;
808 return bfd_reloc_ok;
809}
810
811bfd_reloc_status_type
0a6a3ebe
SC
812m68hc11_elf_special_reloc (bfd *abfd ATTRIBUTE_UNUSED,
813 arelent *reloc_entry,
814 asymbol *symbol,
815 void *data ATTRIBUTE_UNUSED,
816 asection *input_section,
817 bfd *output_bfd,
818 char **error_message ATTRIBUTE_UNUSED)
3a65329d
SC
819{
820 if (output_bfd != (bfd *) NULL
821 && (symbol->flags & BSF_SECTION_SYM) == 0
822 && (! reloc_entry->howto->partial_inplace
823 || reloc_entry->addend == 0))
824 {
825 reloc_entry->address += input_section->output_offset;
826 return bfd_reloc_ok;
827 }
828
829 if (output_bfd != NULL)
830 return bfd_reloc_continue;
831
07515404 832 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
3a65329d
SC
833 return bfd_reloc_outofrange;
834
835 abort();
836}
837
3a65329d
SC
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. */
841
842bfd_boolean
0a6a3ebe
SC
843elf32_m68hc11_check_relocs (bfd *abfd, struct bfd_link_info *info,
844 asection *sec, const Elf_Internal_Rela *relocs)
3a65329d
SC
845{
846 Elf_Internal_Shdr * symtab_hdr;
847 struct elf_link_hash_entry ** sym_hashes;
3a65329d
SC
848 const Elf_Internal_Rela * rel;
849 const Elf_Internal_Rela * rel_end;
850
1049f94e 851 if (info->relocatable)
3a65329d
SC
852 return TRUE;
853
854 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
855 sym_hashes = elf_sym_hashes (abfd);
3a65329d
SC
856 rel_end = relocs + sec->reloc_count;
857
858 for (rel = relocs; rel < rel_end; rel++)
859 {
860 struct elf_link_hash_entry * h;
861 unsigned long r_symndx;
862
863 r_symndx = ELF32_R_SYM (rel->r_info);
864
865 if (r_symndx < symtab_hdr->sh_info)
866 h = NULL;
867 else
973a3492
L
868 {
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;
81fbe831
AM
873
874 /* PR15323, ref flags aren't set for references in the same
875 object. */
876 h->root.non_ir_ref = 1;
973a3492 877 }
3a65329d
SC
878
879 switch (ELF32_R_TYPE (rel->r_info))
880 {
881 /* This relocation describes the C++ object vtable hierarchy.
882 Reconstruct it for later use during GC. */
883 case R_M68HC11_GNU_VTINHERIT:
c152c796 884 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3a65329d
SC
885 return FALSE;
886 break;
887
888 /* This relocation describes which C++ vtable entries are actually
889 used. Record for later use during GC. */
890 case R_M68HC11_GNU_VTENTRY:
d17e0c6e
JB
891 BFD_ASSERT (h != NULL);
892 if (h != NULL
893 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3a65329d
SC
894 return FALSE;
895 break;
896 }
897 }
898
899 return TRUE;
900}
901
3a65329d
SC
902/* Relocate a 68hc11/68hc12 ELF section. */
903bfd_boolean
0a6a3ebe
SC
904elf32_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)
3a65329d
SC
910{
911 Elf_Internal_Shdr *symtab_hdr;
912 struct elf_link_hash_entry **sym_hashes;
913 Elf_Internal_Rela *rel, *relend;
9b69b847 914 const char *name = NULL;
3a65329d 915 struct m68hc11_page_info *pinfo;
9c5bfbb7 916 const struct elf_backend_data * const ebd = get_elf_backend_data (input_bfd);
4dfe6ac6 917 struct m68hc11_elf_link_hash_table *htab;
9a92d0ce 918 unsigned long e_flags;
3a65329d
SC
919
920 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
921 sym_hashes = elf_sym_hashes (input_bfd);
9a92d0ce 922 e_flags = elf_elfheader (input_bfd)->e_flags;
3a65329d 923
4dfe6ac6
NC
924 htab = m68hc11_elf_hash_table (info);
925 if (htab == NULL)
926 return FALSE;
927
3a65329d
SC
928 /* Get memory bank parameters. */
929 m68hc11_elf_get_bank_parameters (info);
3a65329d 930
4dfe6ac6 931 pinfo = & htab->pinfo;
3a65329d
SC
932 rel = relocs;
933 relend = relocs + input_section->reloc_count;
4dfe6ac6 934
3a65329d
SC
935 for (; rel < relend; rel++)
936 {
937 int r_type;
938 arelent arel;
939 reloc_howto_type *howto;
940 unsigned long r_symndx;
941 Elf_Internal_Sym *sym;
942 asection *sec;
9b69b847 943 bfd_vma relocation = 0;
3a65329d
SC
944 bfd_reloc_status_type r = bfd_reloc_undefined;
945 bfd_vma phys_page;
946 bfd_vma phys_addr;
947 bfd_vma insn_addr;
948 bfd_vma insn_page;
9b69b847 949 bfd_boolean is_far = FALSE;
082d1122
NC
950 bfd_boolean is_xgate_symbol = FALSE;
951 bfd_boolean is_section_symbol = FALSE;
ab96bf03 952 struct elf_link_hash_entry *h;
6927f982 953 bfd_vma val;
3a65329d
SC
954
955 r_symndx = ELF32_R_SYM (rel->r_info);
956 r_type = ELF32_R_TYPE (rel->r_info);
957
958 if (r_type == R_M68HC11_GNU_VTENTRY
082d1122 959 || r_type == R_M68HC11_GNU_VTINHERIT)
3a65329d
SC
960 continue;
961
ab96bf03
AM
962 (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel);
963 howto = arel.howto;
964
965 h = NULL;
966 sym = NULL;
967 sec = NULL;
968 if (r_symndx < symtab_hdr->sh_info)
969 {
970 sym = local_syms + r_symndx;
971 sec = local_sections[r_symndx];
972 relocation = (sec->output_section->vma
973 + sec->output_offset
974 + sym->st_value);
975 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
082d1122
NC
976 is_xgate_symbol = (sym && (sym->st_target_internal));
977 is_section_symbol = ELF_ST_TYPE (sym->st_info) & STT_SECTION;
ab96bf03
AM
978 }
979 else
980 {
62d887d4 981 bfd_boolean unresolved_reloc, warned, ignored;
ab96bf03
AM
982
983 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
984 r_symndx, symtab_hdr, sym_hashes,
985 h, sec, relocation, unresolved_reloc,
62d887d4 986 warned, ignored);
ab96bf03
AM
987
988 is_far = (h && (h->other & STO_M68HC12_FAR));
082d1122 989 is_xgate_symbol = (h && (h->target_internal));
ab96bf03
AM
990 }
991
dbaa2011 992 if (sec != NULL && discarded_section (sec))
e4067dbb 993 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b 994 rel, 1, relend, howto, 0, contents);
ab96bf03 995
1049f94e 996 if (info->relocatable)
3a65329d 997 {
1049f94e 998 /* This is a relocatable link. We don't have to change
3a65329d
SC
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. */
ab96bf03
AM
1002 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1003 rel->r_addend += sec->output_offset;
3a65329d
SC
1004 continue;
1005 }
3a65329d 1006
ab96bf03
AM
1007 if (h != NULL)
1008 name = h->root.root.string;
1009 else
1010 {
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);
1015 }
1016
1017 if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
1018 {
1019 struct elf32_m68hc11_stub_hash_entry* stub;
ab96bf03 1020
ab96bf03
AM
1021 stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
1022 name, FALSE, FALSE);
1023 if (stub)
1024 {
1025 relocation = stub->stub_offset
1026 + stub->stub_sec->output_section->vma
1027 + stub->stub_sec->output_offset;
1028 is_far = FALSE;
1029 }
1030 }
3a65329d
SC
1031
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);
1035 switch (r_type)
1036 {
6927f982
NC
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. */
1041 r = bfd_reloc_ok;
1042 r_type = R_M68HC11_NONE;
1043 break;
1044
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;
9a92d0ce 1050 if ((e_flags & E_M68HC11_XGATE_RAMOFFSET)
6927f982
NC
1051 && (relocation >= 0x2000))
1052 relocation += 0xc000; /* HARDCODED RAM offset for XGATE. */
1053
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);
1057
1058 /* Add on value to preserve carry, then write zero to high byte. */
1059 relocation += val;
1060
1061 /* Write out top byte. */
1062 bfd_put_8 (input_bfd, (relocation >> 8) & 0xff,
1063 (bfd_byte*) contents + rel->r_offset);
1064
1065 /* Write out low byte to previous instruction. */
1066 bfd_put_8 (input_bfd, relocation & 0xff,
1067 (bfd_byte*) contents + rel->r_offset - 2);
1068
1069 /* Mark as relocation completed. */
1070 r = bfd_reloc_ok;
1071 r_type = R_M68HC11_NONE;
1072 break;
1073
1074 /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1075 assembler directives. %hi does not support carry. */
1076 case R_M68HC11_HI8:
1077 case R_M68HC11_LO8:
1078 relocation = phys_addr;
1079 break;
1080
3a65329d
SC
1081 case R_M68HC11_24:
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);
1087 r = bfd_reloc_ok;
1088 r_type = R_M68HC11_NONE;
1089 break;
1090
1091 case R_M68HC11_NONE:
1092 r = bfd_reloc_ok;
1093 break;
1094
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;
1099 break;
1100
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;
1105 break;
1106
1107 case R_M68HC11_16:
1108 /* Get virtual address of instruction having the relocation. */
1109 if (is_far)
1110 {
1111 const char* msg;
1112 char* buf;
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);
68ffbac6 1117
3a65329d
SC
1118 (* info->callbacks->warning)
1119 (info, buf, name, input_bfd, NULL, rel->r_offset);
1120 }
1121
1122 /* Get virtual address of instruction having the relocation. */
1123 insn_addr = input_section->output_section->vma
1124 + input_section->output_offset
1125 + rel->r_offset;
1126
1127 insn_page = m68hc11_phys_page (pinfo, insn_addr);
1128
082d1122
NC
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)
1133 {
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
1136 for XGATE. */
1137 if (relocation >= 0xE000)
1138 {
1139 /* We offset the address by the difference
1140 between these two mappings. */
1141 relocation -= 0xC000;
1142 break;
1143 }
1144 else
1145 {
1146 const char * msg;
1147 char * buf;
1148
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 "
1152 "code.");
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)))
1157 return FALSE;
1158 break;
1159 }
1160 }
1161
3a65329d
SC
1162 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
1163 && m68hc11_addr_is_banked (pinfo, insn_addr)
9a92d0ce 1164 && phys_page != insn_page && !(e_flags & E_M68HC11_NO_BANK_WARNING))
3a65329d 1165 {
082d1122
NC
1166 const char * msg;
1167 char * buf;
3a65329d
SC
1168
1169 msg = _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1170 "as current banked address [%lx:%04lx] (%lx)");
1171
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,
1179 rel->r_offset)))
1180 return FALSE;
1181 break;
1182 }
082d1122 1183
3a65329d
SC
1184 if (phys_page != 0 && insn_page == 0)
1185 {
082d1122
NC
1186 const char * msg;
1187 char * buf;
3a65329d
SC
1188
1189 msg = _("reference to a banked address [%lx:%04lx] in the "
1190 "normal address space at %04lx");
1191
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,
1196 insn_addr)))
1197 return FALSE;
1198
1199 relocation = phys_addr;
1200 break;
1201 }
1202
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;
1207 break;
1208 }
6927f982 1209
082d1122
NC
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"))
1215 {
1216 /* We can only offset S12 addresses that lie within the non-paged
1217 area of RAM. */
1218 if (!is_xgate_symbol && !is_section_symbol)
1219 {
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;
1227 else
1228 {
1229 const char * msg;
1230 char * buf;
1231
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;
1235
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)))
1243 return FALSE;
1244 break;
1245 }
1246 }
1247 }
1248
3a65329d 1249 if (r_type != R_M68HC11_NONE)
6927f982
NC
1250 {
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,
3a65329d 1253 contents, rel->r_offset,
6927f982
NC
1254 relocation - 2, rel->r_addend);
1255 else
1256 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1257 contents, rel->r_offset,
1258 relocation, rel->r_addend);
1259 }
3a65329d
SC
1260
1261 if (r != bfd_reloc_ok)
1262 {
1263 const char * msg = (const char *) 0;
1264
1265 switch (r)
1266 {
1267 case bfd_reloc_overflow:
1268 if (!((*info->callbacks->reloc_overflow)
dfeffb9f 1269 (info, NULL, name, howto->name, (bfd_vma) 0,
3a65329d
SC
1270 input_bfd, input_section, rel->r_offset)))
1271 return FALSE;
1272 break;
1273
1274 case bfd_reloc_undefined:
1275 if (!((*info->callbacks->undefined_symbol)
1276 (info, name, input_bfd, input_section,
1277 rel->r_offset, TRUE)))
1278 return FALSE;
1279 break;
1280
1281 case bfd_reloc_outofrange:
1282 msg = _ ("internal error: out of range error");
1283 goto common_error;
1284
1285 case bfd_reloc_notsupported:
1286 msg = _ ("internal error: unsupported relocation error");
1287 goto common_error;
1288
1289 case bfd_reloc_dangerous:
1290 msg = _ ("internal error: dangerous error");
1291 goto common_error;
1292
1293 default:
1294 msg = _ ("internal error: unknown error");
1295 /* fall through */
1296
1297 common_error:
1298 if (!((*info->callbacks->warning)
1299 (info, msg, name, input_bfd, input_section,
1300 rel->r_offset)))
1301 return FALSE;
1302 break;
1303 }
1304 }
1305 }
1306
1307 return TRUE;
1308}
1309
1310
1311\f
1312/* Set and control ELF flags in ELF header. */
1313
1314bfd_boolean
0a6a3ebe 1315_bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
3a65329d
SC
1316{
1317 BFD_ASSERT (!elf_flags_init (abfd)
1318 || elf_elfheader (abfd)->e_flags == flags);
1319
1320 elf_elfheader (abfd)->e_flags = flags;
1321 elf_flags_init (abfd) = TRUE;
1322 return TRUE;
1323}
1324
1325/* Merge backend specific data from an object file to the output
1326 object file when linking. */
1327
1328bfd_boolean
0a6a3ebe 1329_bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3a65329d
SC
1330{
1331 flagword old_flags;
1332 flagword new_flags;
1333 bfd_boolean ok = TRUE;
1334
cc643b88 1335 /* Check if we have the same endianness */
3a65329d
SC
1336 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
1337 return FALSE;
1338
1339 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1340 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1341 return TRUE;
1342
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;
1346
1347 if (! elf_flags_init (obfd))
1348 {
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];
1353
1354 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1355 && bfd_get_arch_info (obfd)->the_default)
1356 {
1357 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1358 bfd_get_mach (ibfd)))
1359 return FALSE;
1360 }
1361
1362 return TRUE;
1363 }
1364
1365 /* Check ABI compatibility. */
1366 if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
1367 {
1368 (*_bfd_error_handler)
d003868e
AM
1369 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1370 "and others for 32-bit integers"), ibfd);
3a65329d
SC
1371 ok = FALSE;
1372 }
1373 if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
1374 {
1375 (*_bfd_error_handler)
d003868e
AM
1376 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1377 "and others for 64-bit double"), ibfd);
3a65329d
SC
1378 ok = FALSE;
1379 }
47247ced
SC
1380
1381 /* Processor compatibility. */
1382 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
1383 {
1384 (*_bfd_error_handler)
d003868e
AM
1385 (_("%B: linking files compiled for HCS12 with "
1386 "others compiled for HC12"), ibfd);
47247ced
SC
1387 ok = FALSE;
1388 }
1389 new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
1390 | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
1391
1392 elf_elfheader (obfd)->e_flags = new_flags;
1393
17e58af0
SC
1394 new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1395 old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
3a65329d
SC
1396
1397 /* Warn about any other mismatches */
1398 if (new_flags != old_flags)
1399 {
1400 (*_bfd_error_handler)
d003868e
AM
1401 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1402 ibfd, (unsigned long) new_flags, (unsigned long) old_flags);
3a65329d
SC
1403 ok = FALSE;
1404 }
1405
1406 if (! ok)
1407 {
1408 bfd_set_error (bfd_error_bad_value);
1409 return FALSE;
1410 }
1411
1412 return TRUE;
1413}
1414
1415bfd_boolean
0a6a3ebe 1416_bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
3a65329d
SC
1417{
1418 FILE *file = (FILE *) ptr;
1419
1420 BFD_ASSERT (abfd != NULL && ptr != NULL);
1421
1422 /* Print normal ELF private data. */
1423 _bfd_elf_print_private_bfd_data (abfd, ptr);
1424
1425 /* xgettext:c-format */
1426 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1427
1428 if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
1429 fprintf (file, _("[abi=32-bit int, "));
1430 else
1431 fprintf (file, _("[abi=16-bit int, "));
1432
1433 if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
1434 fprintf (file, _("64-bit double, "));
1435 else
1436 fprintf (file, _("32-bit double, "));
1437
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]"));
1442 else
68ffbac6 1443 fprintf (file, _("cpu=HC12]"));
3a65329d
SC
1444
1445 if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
1446 fprintf (file, _(" [memory=bank-model]"));
1447 else
1448 fprintf (file, _(" [memory=flat]"));
1449
6927f982
NC
1450 if (elf_elfheader (abfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
1451 fprintf (file, _(" [XGATE RAM offsetting]"));
1452
3a65329d
SC
1453 fputc ('\n', file);
1454
1455 return TRUE;
1456}
1457
0a6a3ebe
SC
1458static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
1459 asection *asect, void *arg)
3a65329d
SC
1460{
1461 struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
1462
1463 if (asect->vma >= p->pinfo->bank_virtual)
1464 p->use_memory_banks = TRUE;
1465}
68ffbac6 1466
3a65329d
SC
1467/* Tweak the OSABI field of the elf header. */
1468
1469void
0a6a3ebe 1470elf32_m68hc11_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
3a65329d
SC
1471{
1472 struct m68hc11_scan_param param;
4dfe6ac6
NC
1473 struct m68hc11_elf_link_hash_table *htab;
1474
1475 if (link_info == NULL)
1476 return;
3a65329d 1477
4dfe6ac6
NC
1478 htab = m68hc11_elf_hash_table (link_info);
1479 if (htab == NULL)
3a65329d
SC
1480 return;
1481
1482 m68hc11_elf_get_bank_parameters (link_info);
1483
1484 param.use_memory_banks = FALSE;
4dfe6ac6
NC
1485 param.pinfo = & htab->pinfo;
1486
3a65329d 1487 bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
4dfe6ac6 1488
3a65329d
SC
1489 if (param.use_memory_banks)
1490 {
1491 Elf_Internal_Ehdr * i_ehdrp;
1492
1493 i_ehdrp = elf_elfheader (abfd);
1494 i_ehdrp->e_flags |= E_M68HC12_BANKS;
1495 }
1496}