]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - bfd/elf32-m68hc1x.c
Remove spurious whitespace introduced by previous delta.
[thirdparty/binutils-gdb.git] / bfd / elf32-m68hc1x.c
CommitLineData
3a65329d 1/* Motorola 68HC11/HC12-specific support for 32-bit ELF
6f2750fe 2 Copyright (C) 1999-2016 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"
e1fa0163 31#include "libiberty.h"
3a65329d
SC
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
68faa637
AM
61/* Destroy a 68HC11/68HC12 ELF linker hash table. */
62
d495ab0d
AM
63static void
64m68hc11_elf_bfd_link_hash_table_free (bfd *obfd)
68faa637
AM
65{
66 struct m68hc11_elf_link_hash_table *ret
d495ab0d 67 = (struct m68hc11_elf_link_hash_table *) obfd->link.hash;
68faa637
AM
68
69 bfd_hash_table_free (ret->stub_hash_table);
70 free (ret->stub_hash_table);
d495ab0d 71 _bfd_elf_link_hash_table_free (obfd);
68faa637
AM
72}
73
3a65329d
SC
74/* Create a 68HC11/68HC12 ELF linker hash table. */
75
76struct m68hc11_elf_link_hash_table*
0a6a3ebe 77m68hc11_elf_hash_table_create (bfd *abfd)
3a65329d
SC
78{
79 struct m68hc11_elf_link_hash_table *ret;
80 bfd_size_type amt = sizeof (struct m68hc11_elf_link_hash_table);
81
7bf52ea2 82 ret = (struct m68hc11_elf_link_hash_table *) bfd_zmalloc (amt);
3a65329d
SC
83 if (ret == (struct m68hc11_elf_link_hash_table *) NULL)
84 return NULL;
85
66eb6687
AM
86 if (!_bfd_elf_link_hash_table_init (&ret->root, abfd,
87 _bfd_elf_link_hash_newfunc,
4dfe6ac6
NC
88 sizeof (struct elf_link_hash_entry),
89 M68HC11_ELF_DATA))
3a65329d 90 {
47247ced 91 free (ret);
3a65329d
SC
92 return NULL;
93 }
94
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)
99 {
d495ab0d 100 _bfd_elf_link_hash_table_free (abfd);
3a65329d
SC
101 return NULL;
102 }
66eb6687
AM
103 if (!bfd_hash_table_init (ret->stub_hash_table, stub_hash_newfunc,
104 sizeof (struct elf32_m68hc11_stub_hash_entry)))
d495ab0d
AM
105 {
106 free (ret->stub_hash_table);
107 _bfd_elf_link_hash_table_free (abfd);
108 return NULL;
109 }
110 ret->root.root.hash_table_free = m68hc11_elf_bfd_link_hash_table_free;
3a65329d 111
3a65329d
SC
112 return ret;
113}
114
3a65329d
SC
115/* Assorted hash table functions. */
116
117/* Initialize an entry in the stub hash table. */
118
119static struct bfd_hash_entry *
0a6a3ebe
SC
120stub_hash_newfunc (struct bfd_hash_entry *entry, struct bfd_hash_table *table,
121 const char *string)
3a65329d
SC
122{
123 /* Allocate the structure if it has not already been allocated by a
124 subclass. */
125 if (entry == NULL)
126 {
127 entry = bfd_hash_allocate (table,
128 sizeof (struct elf32_m68hc11_stub_hash_entry));
129 if (entry == NULL)
130 return entry;
131 }
132
133 /* Call the allocation method of the superclass. */
134 entry = bfd_hash_newfunc (entry, table, string);
135 if (entry != NULL)
136 {
137 struct elf32_m68hc11_stub_hash_entry *eh;
138
139 /* Initialize the local fields. */
140 eh = (struct elf32_m68hc11_stub_hash_entry *) entry;
141 eh->stub_sec = NULL;
142 eh->stub_offset = 0;
143 eh->target_value = 0;
144 eh->target_section = NULL;
145 }
146
147 return entry;
148}
149
150/* Add a new stub entry to the stub hash. Not all fields of the new
151 stub entry are initialised. */
152
153static struct elf32_m68hc11_stub_hash_entry *
0a6a3ebe
SC
154m68hc12_add_stub (const char *stub_name, asection *section,
155 struct m68hc11_elf_link_hash_table *htab)
3a65329d
SC
156{
157 struct elf32_m68hc11_stub_hash_entry *stub_entry;
158
159 /* Enter this entry into the linker stub hash table. */
160 stub_entry = m68hc12_stub_hash_lookup (htab->stub_hash_table, stub_name,
161 TRUE, FALSE);
162 if (stub_entry == NULL)
163 {
4eca0228
AM
164 _bfd_error_handler (_("%B: cannot create stub entry %s"),
165 section->owner, stub_name);
3a65329d
SC
166 return NULL;
167 }
168
169 if (htab->stub_section == 0)
170 {
171 htab->stub_section = (*htab->add_stub_section) (".tramp",
172 htab->tramp_section);
173 }
174
175 stub_entry->stub_sec = htab->stub_section;
176 stub_entry->stub_offset = 0;
177 return stub_entry;
178}
179
180/* Hook called by the linker routine which adds symbols from an object
181 file. We use it for identify far symbols and force a loading of
182 the trampoline handler. */
183
184bfd_boolean
0a6a3ebe 185elf32_m68hc11_add_symbol_hook (bfd *abfd, struct bfd_link_info *info,
555cd476 186 Elf_Internal_Sym *sym,
0a6a3ebe
SC
187 const char **namep ATTRIBUTE_UNUSED,
188 flagword *flagsp ATTRIBUTE_UNUSED,
189 asection **secp ATTRIBUTE_UNUSED,
190 bfd_vma *valp ATTRIBUTE_UNUSED)
3a65329d
SC
191{
192 if (sym->st_other & STO_M68HC12_FAR)
193 {
194 struct elf_link_hash_entry *h;
195
196 h = (struct elf_link_hash_entry *)
197 bfd_link_hash_lookup (info->hash, "__far_trampoline",
198 FALSE, FALSE, FALSE);
199 if (h == NULL)
200 {
201 struct bfd_link_hash_entry* entry = NULL;
202
203 _bfd_generic_link_add_one_symbol (info, abfd,
204 "__far_trampoline",
205 BSF_GLOBAL,
206 bfd_und_section_ptr,
207 (bfd_vma) 0, (const char*) NULL,
208 FALSE, FALSE, &entry);
209 }
210
211 }
212 return TRUE;
213}
214
5efbbc43
AM
215/* Merge non-visibility st_other attributes, STO_M68HC12_FAR and
216 STO_M68HC12_INTERRUPT. */
217
218void
219elf32_m68hc11_merge_symbol_attribute (struct elf_link_hash_entry *h,
220 const Elf_Internal_Sym *isym,
221 bfd_boolean definition,
222 bfd_boolean dynamic ATTRIBUTE_UNUSED)
223{
224 if (definition)
225 h->other = ((isym->st_other & ~ELF_ST_VISIBILITY (-1))
226 | ELF_ST_VISIBILITY (h->other));
227}
228
3a65329d
SC
229/* External entry points for sizing and building linker stubs. */
230
231/* Set up various things so that we can make a list of input sections
232 for each output section included in the link. Returns -1 on error,
233 0 when no stubs will be needed, and 1 on success. */
234
235int
0a6a3ebe 236elf32_m68hc11_setup_section_lists (bfd *output_bfd, struct bfd_link_info *info)
3a65329d
SC
237{
238 bfd *input_bfd;
239 unsigned int bfd_count;
7292b3ac 240 unsigned int top_id, top_index;
3a65329d
SC
241 asection *section;
242 asection **input_list, **list;
243 bfd_size_type amt;
244 asection *text_section;
245 struct m68hc11_elf_link_hash_table *htab;
246
247 htab = m68hc11_elf_hash_table (info);
4dfe6ac6
NC
248 if (htab == NULL)
249 return -1;
3a65329d 250
f13a99db 251 if (bfd_get_flavour (info->output_bfd) != bfd_target_elf_flavour)
3a65329d
SC
252 return 0;
253
254 /* Count the number of input BFDs and find the top input section id.
255 Also search for an existing ".tramp" section so that we know
256 where generated trampolines must go. Default to ".text" if we
257 can't find it. */
258 htab->tramp_section = 0;
259 text_section = 0;
260 for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
261 input_bfd != NULL;
c72f2fb2 262 input_bfd = input_bfd->link.next)
3a65329d
SC
263 {
264 bfd_count += 1;
265 for (section = input_bfd->sections;
266 section != NULL;
267 section = section->next)
268 {
269 const char* name = bfd_get_section_name (input_bfd, section);
270
271 if (!strcmp (name, ".tramp"))
272 htab->tramp_section = section;
273
274 if (!strcmp (name, ".text"))
275 text_section = section;
276
277 if (top_id < section->id)
278 top_id = section->id;
279 }
280 }
281 htab->bfd_count = bfd_count;
282 if (htab->tramp_section == 0)
283 htab->tramp_section = text_section;
284
285 /* We can't use output_bfd->section_count here to find the top output
286 section index as some sections may have been removed, and
8423293d 287 strip_excluded_output_sections doesn't renumber the indices. */
3a65329d
SC
288 for (section = output_bfd->sections, top_index = 0;
289 section != NULL;
290 section = section->next)
291 {
292 if (top_index < section->index)
293 top_index = section->index;
294 }
295
296 htab->top_index = top_index;
297 amt = sizeof (asection *) * (top_index + 1);
298 input_list = (asection **) bfd_malloc (amt);
299 htab->input_list = input_list;
300 if (input_list == NULL)
301 return -1;
302
303 /* For sections we aren't interested in, mark their entries with a
304 value we can check later. */
305 list = input_list + top_index;
306 do
307 *list = bfd_abs_section_ptr;
308 while (list-- != input_list);
309
310 for (section = output_bfd->sections;
311 section != NULL;
312 section = section->next)
313 {
314 if ((section->flags & SEC_CODE) != 0)
315 input_list[section->index] = NULL;
316 }
317
318 return 1;
319}
320
321/* Determine and set the size of the stub section for a final link.
322
323 The basic idea here is to examine all the relocations looking for
324 PC-relative calls to a target that is unreachable with a "bl"
325 instruction. */
326
327bfd_boolean
0a6a3ebe
SC
328elf32_m68hc11_size_stubs (bfd *output_bfd, bfd *stub_bfd,
329 struct bfd_link_info *info,
330 asection * (*add_stub_section) (const char*, asection*))
3a65329d
SC
331{
332 bfd *input_bfd;
333 asection *section;
334 Elf_Internal_Sym *local_syms, **all_local_syms;
335 unsigned int bfd_indx, bfd_count;
336 bfd_size_type amt;
337 asection *stub_sec;
3a65329d
SC
338 struct m68hc11_elf_link_hash_table *htab = m68hc11_elf_hash_table (info);
339
4dfe6ac6
NC
340 if (htab == NULL)
341 return FALSE;
342
3a65329d
SC
343 /* Stash our params away. */
344 htab->stub_bfd = stub_bfd;
345 htab->add_stub_section = add_stub_section;
346
347 /* Count the number of input BFDs and find the top input section id. */
348 for (input_bfd = info->input_bfds, bfd_count = 0;
349 input_bfd != NULL;
c72f2fb2 350 input_bfd = input_bfd->link.next)
4dfe6ac6 351 bfd_count += 1;
3a65329d
SC
352
353 /* We want to read in symbol extension records only once. To do this
354 we need to read in the local symbols in parallel and save them for
355 later use; so hold pointers to the local symbols in an array. */
356 amt = sizeof (Elf_Internal_Sym *) * bfd_count;
357 all_local_syms = (Elf_Internal_Sym **) bfd_zmalloc (amt);
358 if (all_local_syms == NULL)
359 return FALSE;
360
361 /* Walk over all the input BFDs, swapping in local symbols. */
362 for (input_bfd = info->input_bfds, bfd_indx = 0;
363 input_bfd != NULL;
c72f2fb2 364 input_bfd = input_bfd->link.next, bfd_indx++)
3a65329d
SC
365 {
366 Elf_Internal_Shdr *symtab_hdr;
3a65329d
SC
367
368 /* We'll need the symbol table in a second. */
369 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
370 if (symtab_hdr->sh_info == 0)
371 continue;
372
2a0e29b4
SC
373 /* We need an array of the local symbols attached to the input bfd. */
374 local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
375 if (local_syms == NULL)
376 {
377 local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
378 symtab_hdr->sh_info, 0,
379 NULL, NULL, NULL);
380 /* Cache them for elf_link_input_bfd. */
381 symtab_hdr->contents = (unsigned char *) local_syms;
382 }
3a65329d 383 if (local_syms == NULL)
3a65329d 384 {
2a0e29b4
SC
385 free (all_local_syms);
386 return FALSE;
3a65329d
SC
387 }
388
2a0e29b4 389 all_local_syms[bfd_indx] = local_syms;
3a65329d
SC
390 }
391
392 for (input_bfd = info->input_bfds, bfd_indx = 0;
393 input_bfd != NULL;
c72f2fb2 394 input_bfd = input_bfd->link.next, bfd_indx++)
3a65329d
SC
395 {
396 Elf_Internal_Shdr *symtab_hdr;
3a65329d
SC
397 struct elf_link_hash_entry ** sym_hashes;
398
399 sym_hashes = elf_sym_hashes (input_bfd);
400
401 /* We'll need the symbol table in a second. */
402 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
403 if (symtab_hdr->sh_info == 0)
404 continue;
405
406 local_syms = all_local_syms[bfd_indx];
407
408 /* Walk over each section attached to the input bfd. */
409 for (section = input_bfd->sections;
410 section != NULL;
411 section = section->next)
412 {
413 Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
414
415 /* If there aren't any relocs, then there's nothing more
416 to do. */
417 if ((section->flags & SEC_RELOC) == 0
418 || section->reloc_count == 0)
419 continue;
420
421 /* If this section is a link-once section that will be
422 discarded, then don't create any stubs. */
423 if (section->output_section == NULL
424 || section->output_section->owner != output_bfd)
425 continue;
426
427 /* Get the relocs. */
428 internal_relocs
45d6a902
AM
429 = _bfd_elf_link_read_relocs (input_bfd, section, NULL,
430 (Elf_Internal_Rela *) NULL,
431 info->keep_memory);
3a65329d
SC
432 if (internal_relocs == NULL)
433 goto error_ret_free_local;
434
435 /* Now examine each relocation. */
436 irela = internal_relocs;
437 irelaend = irela + section->reloc_count;
438 for (; irela < irelaend; irela++)
439 {
440 unsigned int r_type, r_indx;
441 struct elf32_m68hc11_stub_hash_entry *stub_entry;
442 asection *sym_sec;
443 bfd_vma sym_value;
444 struct elf_link_hash_entry *hash;
445 const char *stub_name;
446 Elf_Internal_Sym *sym;
447
448 r_type = ELF32_R_TYPE (irela->r_info);
449
450 /* Only look at 16-bit relocs. */
451 if (r_type != (unsigned int) R_M68HC11_16)
452 continue;
453
454 /* Now determine the call target, its name, value,
455 section. */
456 r_indx = ELF32_R_SYM (irela->r_info);
457 if (r_indx < symtab_hdr->sh_info)
458 {
459 /* It's a local symbol. */
460 Elf_Internal_Shdr *hdr;
461 bfd_boolean is_far;
462
463 sym = local_syms + r_indx;
3a65329d
SC
464 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
465 if (!is_far)
466 continue;
7f888330 467
4fbb74a6
AM
468 if (sym->st_shndx >= elf_numsections (input_bfd))
469 sym_sec = NULL;
470 else
471 {
472 hdr = elf_elfsections (input_bfd)[sym->st_shndx];
473 sym_sec = hdr->bfd_section;
474 }
3a65329d
SC
475 stub_name = (bfd_elf_string_from_elf_section
476 (input_bfd, symtab_hdr->sh_link,
477 sym->st_name));
478 sym_value = sym->st_value;
479 hash = NULL;
480 }
481 else
482 {
483 /* It's an external symbol. */
484 int e_indx;
485
486 e_indx = r_indx - symtab_hdr->sh_info;
487 hash = (struct elf_link_hash_entry *)
488 (sym_hashes[e_indx]);
489
490 while (hash->root.type == bfd_link_hash_indirect
491 || hash->root.type == bfd_link_hash_warning)
492 hash = ((struct elf_link_hash_entry *)
493 hash->root.u.i.link);
494
495 if (hash->root.type == bfd_link_hash_defined
83774818
SC
496 || hash->root.type == bfd_link_hash_defweak
497 || hash->root.type == bfd_link_hash_new)
3a65329d
SC
498 {
499 if (!(hash->other & STO_M68HC12_FAR))
500 continue;
501 }
502 else if (hash->root.type == bfd_link_hash_undefweak)
503 {
504 continue;
505 }
506 else if (hash->root.type == bfd_link_hash_undefined)
507 {
508 continue;
509 }
510 else
511 {
512 bfd_set_error (bfd_error_bad_value);
513 goto error_ret_free_internal;
514 }
515 sym_sec = hash->root.u.def.section;
516 sym_value = hash->root.u.def.value;
517 stub_name = hash->root.root.string;
518 }
519
520 if (!stub_name)
521 goto error_ret_free_internal;
522
523 stub_entry = m68hc12_stub_hash_lookup
524 (htab->stub_hash_table,
525 stub_name,
526 FALSE, FALSE);
527 if (stub_entry == NULL)
528 {
529 if (add_stub_section == 0)
530 continue;
531
532 stub_entry = m68hc12_add_stub (stub_name, section, htab);
533 if (stub_entry == NULL)
534 {
535 error_ret_free_internal:
536 if (elf_section_data (section)->relocs == NULL)
537 free (internal_relocs);
538 goto error_ret_free_local;
539 }
540 }
541
542 stub_entry->target_value = sym_value;
543 stub_entry->target_section = sym_sec;
544 }
545
546 /* We're done with the internal relocs, free them. */
547 if (elf_section_data (section)->relocs == NULL)
548 free (internal_relocs);
549 }
550 }
551
552 if (add_stub_section)
553 {
554 /* OK, we've added some stubs. Find out the new size of the
555 stub sections. */
556 for (stub_sec = htab->stub_bfd->sections;
557 stub_sec != NULL;
558 stub_sec = stub_sec->next)
559 {
eea6121a 560 stub_sec->size = 0;
3a65329d
SC
561 }
562
563 bfd_hash_traverse (htab->stub_hash_table, htab->size_one_stub, htab);
564 }
2a0e29b4 565 free (all_local_syms);
3a65329d
SC
566 return TRUE;
567
568 error_ret_free_local:
2a0e29b4 569 free (all_local_syms);
3a65329d
SC
570 return FALSE;
571}
572
573/* Export the trampoline addresses in the symbol table. */
574static bfd_boolean
0a6a3ebe 575m68hc11_elf_export_one_stub (struct bfd_hash_entry *gen_entry, void *in_arg)
3a65329d
SC
576{
577 struct bfd_link_info *info;
578 struct m68hc11_elf_link_hash_table *htab;
579 struct elf32_m68hc11_stub_hash_entry *stub_entry;
580 char* name;
581 bfd_boolean result;
582
583 info = (struct bfd_link_info *) in_arg;
584 htab = m68hc11_elf_hash_table (info);
4dfe6ac6
NC
585 if (htab == NULL)
586 return FALSE;
3a65329d
SC
587
588 /* Massage our args to the form they really have. */
589 stub_entry = (struct elf32_m68hc11_stub_hash_entry *) gen_entry;
590
591 /* Generate the trampoline according to HC11 or HC12. */
592 result = (* htab->build_one_stub) (gen_entry, in_arg);
593
594 /* Make a printable name that does not conflict with the real function. */
e1fa0163 595 name = concat ("tramp.", stub_entry->root.string, NULL);
3a65329d
SC
596
597 /* Export the symbol for debugging/disassembling. */
598 m68hc11_elf_set_symbol (htab->stub_bfd, info, name,
599 stub_entry->stub_offset,
600 stub_entry->stub_sec);
e1fa0163 601 free (name);
3a65329d
SC
602 return result;
603}
604
605/* Export a symbol or set its value and section. */
606static void
0a6a3ebe
SC
607m68hc11_elf_set_symbol (bfd *abfd, struct bfd_link_info *info,
608 const char *name, bfd_vma value, asection *sec)
3a65329d
SC
609{
610 struct elf_link_hash_entry *h;
611
612 h = (struct elf_link_hash_entry *)
613 bfd_link_hash_lookup (info->hash, name, FALSE, FALSE, FALSE);
614 if (h == NULL)
615 {
616 _bfd_generic_link_add_one_symbol (info, abfd,
617 name,
618 BSF_GLOBAL,
619 sec,
620 value,
621 (const char*) NULL,
622 TRUE, FALSE, NULL);
623 }
624 else
625 {
626 h->root.type = bfd_link_hash_defined;
627 h->root.u.def.value = value;
628 h->root.u.def.section = sec;
629 }
630}
631
632
633/* Build all the stubs associated with the current output file. The
634 stubs are kept in a hash table attached to the main linker hash
635 table. This function is called via m68hc12elf_finish in the
636 linker. */
637
638bfd_boolean
0a6a3ebe 639elf32_m68hc11_build_stubs (bfd *abfd, struct bfd_link_info *info)
3a65329d
SC
640{
641 asection *stub_sec;
642 struct bfd_hash_table *table;
643 struct m68hc11_elf_link_hash_table *htab;
644 struct m68hc11_scan_param param;
645
646 m68hc11_elf_get_bank_parameters (info);
647 htab = m68hc11_elf_hash_table (info);
4dfe6ac6
NC
648 if (htab == NULL)
649 return FALSE;
3a65329d
SC
650
651 for (stub_sec = htab->stub_bfd->sections;
652 stub_sec != NULL;
653 stub_sec = stub_sec->next)
654 {
655 bfd_size_type size;
656
657 /* Allocate memory to hold the linker stubs. */
eea6121a 658 size = stub_sec->size;
3a65329d
SC
659 stub_sec->contents = (unsigned char *) bfd_zalloc (htab->stub_bfd, size);
660 if (stub_sec->contents == NULL && size != 0)
661 return FALSE;
eea6121a 662 stub_sec->size = 0;
3a65329d
SC
663 }
664
665 /* Build the stubs as directed by the stub hash table. */
666 table = htab->stub_hash_table;
667 bfd_hash_traverse (table, m68hc11_elf_export_one_stub, info);
68ffbac6 668
3a65329d
SC
669 /* Scan the output sections to see if we use the memory banks.
670 If so, export the symbols that define how the memory banks
671 are mapped. This is used by gdb and the simulator to obtain
672 the information. It can be used by programs to burn the eprom
673 at the good addresses. */
674 param.use_memory_banks = FALSE;
675 param.pinfo = &htab->pinfo;
676 bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
677 if (param.use_memory_banks)
678 {
679 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_START_NAME,
680 htab->pinfo.bank_physical,
681 bfd_abs_section_ptr);
682 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_VIRTUAL_NAME,
683 htab->pinfo.bank_virtual,
684 bfd_abs_section_ptr);
685 m68hc11_elf_set_symbol (abfd, info, BFD_M68HC11_BANK_SIZE_NAME,
686 htab->pinfo.bank_size,
687 bfd_abs_section_ptr);
688 }
689
690 return TRUE;
691}
692
693void
0a6a3ebe 694m68hc11_elf_get_bank_parameters (struct bfd_link_info *info)
3a65329d
SC
695{
696 unsigned i;
697 struct m68hc11_page_info *pinfo;
698 struct bfd_link_hash_entry *h;
4dfe6ac6
NC
699 struct m68hc11_elf_link_hash_table *htab;
700
701 htab = m68hc11_elf_hash_table (info);
702 if (htab == NULL)
703 return;
3a65329d 704
4dfe6ac6 705 pinfo = & htab->pinfo;
3a65329d
SC
706 if (pinfo->bank_param_initialized)
707 return;
708
709 pinfo->bank_virtual = M68HC12_BANK_VIRT;
710 pinfo->bank_mask = M68HC12_BANK_MASK;
711 pinfo->bank_physical = M68HC12_BANK_BASE;
712 pinfo->bank_shift = M68HC12_BANK_SHIFT;
713 pinfo->bank_size = 1 << M68HC12_BANK_SHIFT;
714
715 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_START_NAME,
716 FALSE, FALSE, TRUE);
717 if (h != (struct bfd_link_hash_entry*) NULL
718 && h->type == bfd_link_hash_defined)
719 pinfo->bank_physical = (h->u.def.value
720 + h->u.def.section->output_section->vma
721 + h->u.def.section->output_offset);
722
723 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_VIRTUAL_NAME,
724 FALSE, FALSE, TRUE);
725 if (h != (struct bfd_link_hash_entry*) NULL
726 && h->type == bfd_link_hash_defined)
727 pinfo->bank_virtual = (h->u.def.value
728 + h->u.def.section->output_section->vma
729 + h->u.def.section->output_offset);
730
731 h = bfd_link_hash_lookup (info->hash, BFD_M68HC11_BANK_SIZE_NAME,
732 FALSE, FALSE, TRUE);
733 if (h != (struct bfd_link_hash_entry*) NULL
734 && h->type == bfd_link_hash_defined)
735 pinfo->bank_size = (h->u.def.value
736 + h->u.def.section->output_section->vma
737 + h->u.def.section->output_offset);
738
739 pinfo->bank_shift = 0;
740 for (i = pinfo->bank_size; i != 0; i >>= 1)
741 pinfo->bank_shift++;
742 pinfo->bank_shift--;
743 pinfo->bank_mask = (1 << pinfo->bank_shift) - 1;
744 pinfo->bank_physical_end = pinfo->bank_physical + pinfo->bank_size;
745 pinfo->bank_param_initialized = 1;
746
747 h = bfd_link_hash_lookup (info->hash, "__far_trampoline", FALSE,
748 FALSE, TRUE);
749 if (h != (struct bfd_link_hash_entry*) NULL
750 && h->type == bfd_link_hash_defined)
751 pinfo->trampoline_addr = (h->u.def.value
752 + h->u.def.section->output_section->vma
753 + h->u.def.section->output_offset);
754}
755
756/* Return 1 if the address is in banked memory.
757 This can be applied to a virtual address and to a physical address. */
758int
0a6a3ebe 759m68hc11_addr_is_banked (struct m68hc11_page_info *pinfo, bfd_vma addr)
3a65329d
SC
760{
761 if (addr >= pinfo->bank_virtual)
762 return 1;
763
764 if (addr >= pinfo->bank_physical && addr <= pinfo->bank_physical_end)
765 return 1;
766
767 return 0;
768}
769
770/* Return the physical address seen by the processor, taking
771 into account banked memory. */
772bfd_vma
0a6a3ebe 773m68hc11_phys_addr (struct m68hc11_page_info *pinfo, bfd_vma addr)
3a65329d
SC
774{
775 if (addr < pinfo->bank_virtual)
776 return addr;
777
778 /* Map the address to the memory bank. */
779 addr -= pinfo->bank_virtual;
780 addr &= pinfo->bank_mask;
781 addr += pinfo->bank_physical;
782 return addr;
783}
784
785/* Return the page number corresponding to an address in banked memory. */
786bfd_vma
0a6a3ebe 787m68hc11_phys_page (struct m68hc11_page_info *pinfo, bfd_vma addr)
3a65329d
SC
788{
789 if (addr < pinfo->bank_virtual)
790 return 0;
791
792 /* Map the address to the memory bank. */
793 addr -= pinfo->bank_virtual;
794 addr >>= pinfo->bank_shift;
795 addr &= 0x0ff;
796 return addr;
797}
798
799/* This function is used for relocs which are only used for relaxing,
800 which the linker should otherwise ignore. */
801
802bfd_reloc_status_type
0a6a3ebe
SC
803m68hc11_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED,
804 arelent *reloc_entry,
805 asymbol *symbol ATTRIBUTE_UNUSED,
806 void *data ATTRIBUTE_UNUSED,
807 asection *input_section,
808 bfd *output_bfd,
809 char **error_message ATTRIBUTE_UNUSED)
3a65329d
SC
810{
811 if (output_bfd != NULL)
812 reloc_entry->address += input_section->output_offset;
813 return bfd_reloc_ok;
814}
815
816bfd_reloc_status_type
0a6a3ebe
SC
817m68hc11_elf_special_reloc (bfd *abfd ATTRIBUTE_UNUSED,
818 arelent *reloc_entry,
819 asymbol *symbol,
820 void *data ATTRIBUTE_UNUSED,
821 asection *input_section,
822 bfd *output_bfd,
823 char **error_message ATTRIBUTE_UNUSED)
3a65329d
SC
824{
825 if (output_bfd != (bfd *) NULL
826 && (symbol->flags & BSF_SECTION_SYM) == 0
827 && (! reloc_entry->howto->partial_inplace
828 || reloc_entry->addend == 0))
829 {
830 reloc_entry->address += input_section->output_offset;
831 return bfd_reloc_ok;
832 }
833
834 if (output_bfd != NULL)
835 return bfd_reloc_continue;
836
07515404 837 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
3a65329d
SC
838 return bfd_reloc_outofrange;
839
840 abort();
841}
842
3a65329d
SC
843/* Look through the relocs for a section during the first phase.
844 Since we don't do .gots or .plts, we just need to consider the
845 virtual table relocs for gc. */
846
847bfd_boolean
0a6a3ebe
SC
848elf32_m68hc11_check_relocs (bfd *abfd, struct bfd_link_info *info,
849 asection *sec, const Elf_Internal_Rela *relocs)
3a65329d
SC
850{
851 Elf_Internal_Shdr * symtab_hdr;
852 struct elf_link_hash_entry ** sym_hashes;
3a65329d
SC
853 const Elf_Internal_Rela * rel;
854 const Elf_Internal_Rela * rel_end;
855
0e1862bb 856 if (bfd_link_relocatable (info))
3a65329d
SC
857 return TRUE;
858
859 symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
860 sym_hashes = elf_sym_hashes (abfd);
3a65329d
SC
861 rel_end = relocs + sec->reloc_count;
862
863 for (rel = relocs; rel < rel_end; rel++)
864 {
865 struct elf_link_hash_entry * h;
866 unsigned long r_symndx;
867
868 r_symndx = ELF32_R_SYM (rel->r_info);
869
870 if (r_symndx < symtab_hdr->sh_info)
871 h = NULL;
872 else
973a3492
L
873 {
874 h = sym_hashes [r_symndx - symtab_hdr->sh_info];
875 while (h->root.type == bfd_link_hash_indirect
876 || h->root.type == bfd_link_hash_warning)
877 h = (struct elf_link_hash_entry *) h->root.u.i.link;
81fbe831
AM
878
879 /* PR15323, ref flags aren't set for references in the same
880 object. */
881 h->root.non_ir_ref = 1;
973a3492 882 }
3a65329d
SC
883
884 switch (ELF32_R_TYPE (rel->r_info))
885 {
886 /* This relocation describes the C++ object vtable hierarchy.
887 Reconstruct it for later use during GC. */
888 case R_M68HC11_GNU_VTINHERIT:
c152c796 889 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
3a65329d
SC
890 return FALSE;
891 break;
892
893 /* This relocation describes which C++ vtable entries are actually
894 used. Record for later use during GC. */
895 case R_M68HC11_GNU_VTENTRY:
d17e0c6e
JB
896 BFD_ASSERT (h != NULL);
897 if (h != NULL
898 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
3a65329d
SC
899 return FALSE;
900 break;
901 }
902 }
903
904 return TRUE;
905}
906
3a65329d
SC
907/* Relocate a 68hc11/68hc12 ELF section. */
908bfd_boolean
0a6a3ebe
SC
909elf32_m68hc11_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
910 struct bfd_link_info *info,
911 bfd *input_bfd, asection *input_section,
912 bfd_byte *contents, Elf_Internal_Rela *relocs,
913 Elf_Internal_Sym *local_syms,
914 asection **local_sections)
3a65329d
SC
915{
916 Elf_Internal_Shdr *symtab_hdr;
917 struct elf_link_hash_entry **sym_hashes;
918 Elf_Internal_Rela *rel, *relend;
9b69b847 919 const char *name = NULL;
3a65329d 920 struct m68hc11_page_info *pinfo;
9c5bfbb7 921 const struct elf_backend_data * const ebd = get_elf_backend_data (input_bfd);
4dfe6ac6 922 struct m68hc11_elf_link_hash_table *htab;
9a92d0ce 923 unsigned long e_flags;
3a65329d
SC
924
925 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
926 sym_hashes = elf_sym_hashes (input_bfd);
9a92d0ce 927 e_flags = elf_elfheader (input_bfd)->e_flags;
3a65329d 928
4dfe6ac6
NC
929 htab = m68hc11_elf_hash_table (info);
930 if (htab == NULL)
931 return FALSE;
932
3a65329d
SC
933 /* Get memory bank parameters. */
934 m68hc11_elf_get_bank_parameters (info);
3a65329d 935
4dfe6ac6 936 pinfo = & htab->pinfo;
3a65329d
SC
937 rel = relocs;
938 relend = relocs + input_section->reloc_count;
4dfe6ac6 939
3a65329d
SC
940 for (; rel < relend; rel++)
941 {
942 int r_type;
943 arelent arel;
944 reloc_howto_type *howto;
945 unsigned long r_symndx;
946 Elf_Internal_Sym *sym;
947 asection *sec;
9b69b847 948 bfd_vma relocation = 0;
3a65329d
SC
949 bfd_reloc_status_type r = bfd_reloc_undefined;
950 bfd_vma phys_page;
951 bfd_vma phys_addr;
952 bfd_vma insn_addr;
953 bfd_vma insn_page;
9b69b847 954 bfd_boolean is_far = FALSE;
082d1122
NC
955 bfd_boolean is_xgate_symbol = FALSE;
956 bfd_boolean is_section_symbol = FALSE;
ab96bf03 957 struct elf_link_hash_entry *h;
6927f982 958 bfd_vma val;
e1fa0163
NC
959 const char * msg;
960 char * buf;
3a65329d
SC
961
962 r_symndx = ELF32_R_SYM (rel->r_info);
963 r_type = ELF32_R_TYPE (rel->r_info);
964
965 if (r_type == R_M68HC11_GNU_VTENTRY
082d1122 966 || r_type == R_M68HC11_GNU_VTINHERIT)
3a65329d
SC
967 continue;
968
ab96bf03
AM
969 (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel);
970 howto = arel.howto;
971
972 h = NULL;
973 sym = NULL;
974 sec = NULL;
975 if (r_symndx < symtab_hdr->sh_info)
976 {
977 sym = local_syms + r_symndx;
978 sec = local_sections[r_symndx];
979 relocation = (sec->output_section->vma
980 + sec->output_offset
981 + sym->st_value);
982 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
082d1122
NC
983 is_xgate_symbol = (sym && (sym->st_target_internal));
984 is_section_symbol = ELF_ST_TYPE (sym->st_info) & STT_SECTION;
ab96bf03
AM
985 }
986 else
987 {
62d887d4 988 bfd_boolean unresolved_reloc, warned, ignored;
ab96bf03
AM
989
990 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
991 r_symndx, symtab_hdr, sym_hashes,
992 h, sec, relocation, unresolved_reloc,
62d887d4 993 warned, ignored);
ab96bf03
AM
994
995 is_far = (h && (h->other & STO_M68HC12_FAR));
082d1122 996 is_xgate_symbol = (h && (h->target_internal));
ab96bf03
AM
997 }
998
dbaa2011 999 if (sec != NULL && discarded_section (sec))
e4067dbb 1000 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b 1001 rel, 1, relend, howto, 0, contents);
ab96bf03 1002
0e1862bb 1003 if (bfd_link_relocatable (info))
3a65329d 1004 {
1049f94e 1005 /* This is a relocatable link. We don't have to change
3a65329d
SC
1006 anything, unless the reloc is against a section symbol,
1007 in which case we have to adjust according to where the
1008 section symbol winds up in the output section. */
ab96bf03
AM
1009 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1010 rel->r_addend += sec->output_offset;
3a65329d
SC
1011 continue;
1012 }
3a65329d 1013
ab96bf03
AM
1014 if (h != NULL)
1015 name = h->root.root.string;
1016 else
1017 {
1018 name = (bfd_elf_string_from_elf_section
1019 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1020 if (name == NULL || *name == '\0')
1021 name = bfd_section_name (input_bfd, sec);
1022 }
1023
1024 if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
1025 {
1026 struct elf32_m68hc11_stub_hash_entry* stub;
ab96bf03 1027
ab96bf03
AM
1028 stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
1029 name, FALSE, FALSE);
1030 if (stub)
1031 {
1032 relocation = stub->stub_offset
1033 + stub->stub_sec->output_section->vma
1034 + stub->stub_sec->output_offset;
1035 is_far = FALSE;
1036 }
1037 }
3a65329d
SC
1038
1039 /* Do the memory bank mapping. */
1040 phys_addr = m68hc11_phys_addr (pinfo, relocation + rel->r_addend);
1041 phys_page = m68hc11_phys_page (pinfo, relocation + rel->r_addend);
1042 switch (r_type)
1043 {
6927f982
NC
1044 case R_M68HC12_LO8XG:
1045 /* This relocation is specific to XGATE IMM16 calls and will precede
1046 a HI8. tc-m68hc11 only generates them in pairs.
1047 Leave the relocation to the HI8XG step. */
1048 r = bfd_reloc_ok;
1049 r_type = R_M68HC11_NONE;
1050 break;
1051
1052 case R_M68HC12_HI8XG:
1053 /* This relocation is specific to XGATE IMM16 calls and must follow
1054 a LO8XG. Does not actually check that it was a LO8XG.
1055 Adjusts high and low bytes. */
1056 relocation = phys_addr;
9a92d0ce 1057 if ((e_flags & E_M68HC11_XGATE_RAMOFFSET)
6927f982
NC
1058 && (relocation >= 0x2000))
1059 relocation += 0xc000; /* HARDCODED RAM offset for XGATE. */
1060
1061 /* Fetch 16 bit value including low byte in previous insn. */
1062 val = (bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset) << 8)
1063 | bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset - 2);
1064
1065 /* Add on value to preserve carry, then write zero to high byte. */
1066 relocation += val;
1067
1068 /* Write out top byte. */
1069 bfd_put_8 (input_bfd, (relocation >> 8) & 0xff,
1070 (bfd_byte*) contents + rel->r_offset);
1071
1072 /* Write out low byte to previous instruction. */
1073 bfd_put_8 (input_bfd, relocation & 0xff,
1074 (bfd_byte*) contents + rel->r_offset - 2);
1075
1076 /* Mark as relocation completed. */
1077 r = bfd_reloc_ok;
1078 r_type = R_M68HC11_NONE;
1079 break;
1080
1081 /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1082 assembler directives. %hi does not support carry. */
1083 case R_M68HC11_HI8:
1084 case R_M68HC11_LO8:
1085 relocation = phys_addr;
1086 break;
1087
3a65329d
SC
1088 case R_M68HC11_24:
1089 /* Reloc used by 68HC12 call instruction. */
1090 bfd_put_16 (input_bfd, phys_addr,
1091 (bfd_byte*) contents + rel->r_offset);
1092 bfd_put_8 (input_bfd, phys_page,
1093 (bfd_byte*) contents + rel->r_offset + 2);
1094 r = bfd_reloc_ok;
1095 r_type = R_M68HC11_NONE;
1096 break;
1097
1098 case R_M68HC11_NONE:
1099 r = bfd_reloc_ok;
1100 break;
1101
1102 case R_M68HC11_LO16:
1103 /* Reloc generated by %addr(expr) gas to obtain the
1104 address as mapped in the memory bank window. */
1105 relocation = phys_addr;
1106 break;
1107
1108 case R_M68HC11_PAGE:
1109 /* Reloc generated by %page(expr) gas to obtain the
1110 page number associated with the address. */
1111 relocation = phys_page;
1112 break;
1113
1114 case R_M68HC11_16:
1115 /* Get virtual address of instruction having the relocation. */
1116 if (is_far)
1117 {
3a65329d
SC
1118 msg = _("Reference to the far symbol `%s' using a wrong "
1119 "relocation may result in incorrect execution");
e1fa0163 1120 buf = xmalloc (strlen (msg) + strlen (name) + 10);
3a65329d 1121 sprintf (buf, msg, name);
68ffbac6 1122
1a72702b
AM
1123 (*info->callbacks->warning)
1124 (info, buf, name, input_bfd, NULL, rel->r_offset);
e1fa0163 1125 free (buf);
3a65329d
SC
1126 }
1127
1128 /* Get virtual address of instruction having the relocation. */
1129 insn_addr = input_section->output_section->vma
1130 + input_section->output_offset
1131 + rel->r_offset;
1132
1133 insn_page = m68hc11_phys_page (pinfo, insn_addr);
1134
082d1122
NC
1135 /* If we are linking an S12 instruction against an XGATE symbol, we
1136 need to change the offset of the symbol value so that it's correct
1137 from the S12's perspective. */
1138 if (is_xgate_symbol)
1139 {
1140 /* The ram in the global space is mapped to 0x2000 in the 16-bit
1141 address space for S12 and 0xE000 in the 16-bit address space
1142 for XGATE. */
1143 if (relocation >= 0xE000)
1144 {
1145 /* We offset the address by the difference
1146 between these two mappings. */
1147 relocation -= 0xC000;
1148 break;
1149 }
1150 else
1151 {
082d1122
NC
1152 msg = _("XGATE address (%lx) is not within shared RAM"
1153 "(0xE000-0xFFFF), therefore you must manually offset "
1154 "the address, and possibly manage the page, in your "
1155 "code.");
e1fa0163 1156 buf = xmalloc (strlen (msg) + 128);
082d1122 1157 sprintf (buf, msg, phys_addr);
1a72702b
AM
1158 (*info->callbacks->warning) (info, buf, name, input_bfd,
1159 input_section, insn_addr);
e1fa0163 1160 free (buf);
082d1122
NC
1161 break;
1162 }
1163 }
1164
3a65329d
SC
1165 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
1166 && m68hc11_addr_is_banked (pinfo, insn_addr)
9a92d0ce 1167 && phys_page != insn_page && !(e_flags & E_M68HC11_NO_BANK_WARNING))
3a65329d 1168 {
3a65329d
SC
1169 msg = _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1170 "as current banked address [%lx:%04lx] (%lx)");
e1fa0163 1171 buf = xmalloc (strlen (msg) + 128);
3a65329d
SC
1172 sprintf (buf, msg, phys_page, phys_addr,
1173 (long) (relocation + rel->r_addend),
1174 insn_page, m68hc11_phys_addr (pinfo, insn_addr),
1175 (long) (insn_addr));
1a72702b
AM
1176 (*info->callbacks->warning) (info, buf, name, input_bfd,
1177 input_section, rel->r_offset);
e1fa0163 1178 free (buf);
3a65329d
SC
1179 break;
1180 }
082d1122 1181
3a65329d
SC
1182 if (phys_page != 0 && insn_page == 0)
1183 {
3a65329d
SC
1184 msg = _("reference to a banked address [%lx:%04lx] in the "
1185 "normal address space at %04lx");
e1fa0163 1186 buf = xmalloc (strlen (msg) + 128);
3a65329d 1187 sprintf (buf, msg, phys_page, phys_addr, insn_addr);
1a72702b
AM
1188 (*info->callbacks->warning) (info, buf, name, input_bfd,
1189 input_section, insn_addr);
e1fa0163 1190 free (buf);
3a65329d
SC
1191 relocation = phys_addr;
1192 break;
1193 }
1194
1195 /* If this is a banked address use the phys_addr so that
1196 we stay in the banked window. */
1197 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend))
1198 relocation = phys_addr;
1199 break;
1200 }
6927f982 1201
082d1122
NC
1202 /* If we are linking an XGATE instruction against an S12 symbol, we
1203 need to change the offset of the symbol value so that it's correct
1204 from the XGATE's perspective. */
1205 if (!strcmp (howto->name, "R_XGATE_IMM8_LO")
1206 || !strcmp (howto->name, "R_XGATE_IMM8_HI"))
1207 {
1208 /* We can only offset S12 addresses that lie within the non-paged
1209 area of RAM. */
1210 if (!is_xgate_symbol && !is_section_symbol)
1211 {
1212 /* The ram in the global space is mapped to 0x2000 and stops at
1213 0x4000 in the 16-bit address space for S12 and 0xE000 in the
1214 16-bit address space for XGATE. */
1215 if (relocation >= 0x2000 && relocation < 0x4000)
1216 /* We offset the address by the difference
1217 between these two mappings. */
1218 relocation += 0xC000;
1219 else
1220 {
082d1122
NC
1221 /* Get virtual address of instruction having the relocation. */
1222 insn_addr = input_section->output_section->vma
1223 + input_section->output_offset + rel->r_offset;
1224
1225 msg = _("S12 address (%lx) is not within shared RAM"
1226 "(0x2000-0x4000), therefore you must manually "
1227 "offset the address in your code");
e1fa0163 1228 buf = xmalloc (strlen (msg) + 128);
082d1122 1229 sprintf (buf, msg, phys_addr);
1a72702b
AM
1230 (*info->callbacks->warning) (info, buf, name, input_bfd,
1231 input_section, insn_addr);
e1fa0163 1232 free (buf);
082d1122
NC
1233 break;
1234 }
1235 }
1236 }
1237
3a65329d 1238 if (r_type != R_M68HC11_NONE)
6927f982
NC
1239 {
1240 if ((r_type == R_M68HC12_PCREL_9) || (r_type == R_M68HC12_PCREL_10))
1241 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3a65329d 1242 contents, rel->r_offset,
6927f982
NC
1243 relocation - 2, rel->r_addend);
1244 else
1245 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1246 contents, rel->r_offset,
1247 relocation, rel->r_addend);
1248 }
3a65329d
SC
1249
1250 if (r != bfd_reloc_ok)
1251 {
3a65329d
SC
1252 switch (r)
1253 {
1254 case bfd_reloc_overflow:
1a72702b
AM
1255 (*info->callbacks->reloc_overflow)
1256 (info, NULL, name, howto->name, (bfd_vma) 0,
1257 input_bfd, input_section, rel->r_offset);
3a65329d
SC
1258 break;
1259
1260 case bfd_reloc_undefined:
1a72702b
AM
1261 (*info->callbacks->undefined_symbol)
1262 (info, name, input_bfd, input_section, rel->r_offset, TRUE);
3a65329d
SC
1263 break;
1264
1265 case bfd_reloc_outofrange:
1266 msg = _ ("internal error: out of range error");
1267 goto common_error;
1268
1269 case bfd_reloc_notsupported:
1270 msg = _ ("internal error: unsupported relocation error");
1271 goto common_error;
1272
1273 case bfd_reloc_dangerous:
1274 msg = _ ("internal error: dangerous error");
1275 goto common_error;
1276
1277 default:
1278 msg = _ ("internal error: unknown error");
1279 /* fall through */
1280
1281 common_error:
1a72702b
AM
1282 (*info->callbacks->warning) (info, msg, name, input_bfd,
1283 input_section, rel->r_offset);
3a65329d
SC
1284 break;
1285 }
1286 }
1287 }
1288
1289 return TRUE;
1290}
1291
1292
1293\f
1294/* Set and control ELF flags in ELF header. */
1295
1296bfd_boolean
0a6a3ebe 1297_bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
3a65329d
SC
1298{
1299 BFD_ASSERT (!elf_flags_init (abfd)
1300 || elf_elfheader (abfd)->e_flags == flags);
1301
1302 elf_elfheader (abfd)->e_flags = flags;
1303 elf_flags_init (abfd) = TRUE;
1304 return TRUE;
1305}
1306
1307/* Merge backend specific data from an object file to the output
1308 object file when linking. */
1309
1310bfd_boolean
50e03d47 1311_bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
3a65329d 1312{
50e03d47 1313 bfd *obfd = info->output_bfd;
3a65329d
SC
1314 flagword old_flags;
1315 flagword new_flags;
1316 bfd_boolean ok = TRUE;
1317
cc643b88 1318 /* Check if we have the same endianness */
50e03d47 1319 if (!_bfd_generic_verify_endian_match (ibfd, info))
3a65329d
SC
1320 return FALSE;
1321
1322 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1323 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1324 return TRUE;
1325
1326 new_flags = elf_elfheader (ibfd)->e_flags;
1327 elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
1328 old_flags = elf_elfheader (obfd)->e_flags;
1329
1330 if (! elf_flags_init (obfd))
1331 {
1332 elf_flags_init (obfd) = TRUE;
1333 elf_elfheader (obfd)->e_flags = new_flags;
1334 elf_elfheader (obfd)->e_ident[EI_CLASS]
1335 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
1336
1337 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1338 && bfd_get_arch_info (obfd)->the_default)
1339 {
1340 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1341 bfd_get_mach (ibfd)))
1342 return FALSE;
1343 }
1344
1345 return TRUE;
1346 }
1347
1348 /* Check ABI compatibility. */
1349 if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
1350 {
4eca0228 1351 _bfd_error_handler
d003868e
AM
1352 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1353 "and others for 32-bit integers"), ibfd);
3a65329d
SC
1354 ok = FALSE;
1355 }
1356 if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
1357 {
4eca0228 1358 _bfd_error_handler
d003868e
AM
1359 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1360 "and others for 64-bit double"), ibfd);
3a65329d
SC
1361 ok = FALSE;
1362 }
47247ced
SC
1363
1364 /* Processor compatibility. */
1365 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
1366 {
4eca0228 1367 _bfd_error_handler
d003868e
AM
1368 (_("%B: linking files compiled for HCS12 with "
1369 "others compiled for HC12"), ibfd);
47247ced
SC
1370 ok = FALSE;
1371 }
1372 new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
1373 | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
1374
1375 elf_elfheader (obfd)->e_flags = new_flags;
1376
17e58af0
SC
1377 new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1378 old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
3a65329d
SC
1379
1380 /* Warn about any other mismatches */
1381 if (new_flags != old_flags)
1382 {
4eca0228 1383 _bfd_error_handler
d003868e
AM
1384 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1385 ibfd, (unsigned long) new_flags, (unsigned long) old_flags);
3a65329d
SC
1386 ok = FALSE;
1387 }
1388
1389 if (! ok)
1390 {
1391 bfd_set_error (bfd_error_bad_value);
1392 return FALSE;
1393 }
1394
1395 return TRUE;
1396}
1397
1398bfd_boolean
0a6a3ebe 1399_bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
3a65329d
SC
1400{
1401 FILE *file = (FILE *) ptr;
1402
1403 BFD_ASSERT (abfd != NULL && ptr != NULL);
1404
1405 /* Print normal ELF private data. */
1406 _bfd_elf_print_private_bfd_data (abfd, ptr);
1407
1408 /* xgettext:c-format */
1409 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1410
1411 if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
1412 fprintf (file, _("[abi=32-bit int, "));
1413 else
1414 fprintf (file, _("[abi=16-bit int, "));
1415
1416 if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
1417 fprintf (file, _("64-bit double, "));
1418 else
1419 fprintf (file, _("32-bit double, "));
1420
1421 if (strcmp (bfd_get_target (abfd), "elf32-m68hc11") == 0)
1422 fprintf (file, _("cpu=HC11]"));
1423 else if (elf_elfheader (abfd)->e_flags & EF_M68HCS12_MACH)
1424 fprintf (file, _("cpu=HCS12]"));
1425 else
68ffbac6 1426 fprintf (file, _("cpu=HC12]"));
3a65329d
SC
1427
1428 if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
1429 fprintf (file, _(" [memory=bank-model]"));
1430 else
1431 fprintf (file, _(" [memory=flat]"));
1432
6927f982
NC
1433 if (elf_elfheader (abfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
1434 fprintf (file, _(" [XGATE RAM offsetting]"));
1435
3a65329d
SC
1436 fputc ('\n', file);
1437
1438 return TRUE;
1439}
1440
0a6a3ebe
SC
1441static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
1442 asection *asect, void *arg)
3a65329d
SC
1443{
1444 struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
1445
1446 if (asect->vma >= p->pinfo->bank_virtual)
1447 p->use_memory_banks = TRUE;
1448}
68ffbac6 1449
3a65329d
SC
1450/* Tweak the OSABI field of the elf header. */
1451
1452void
0a6a3ebe 1453elf32_m68hc11_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
3a65329d
SC
1454{
1455 struct m68hc11_scan_param param;
4dfe6ac6
NC
1456 struct m68hc11_elf_link_hash_table *htab;
1457
1458 if (link_info == NULL)
1459 return;
3a65329d 1460
4dfe6ac6
NC
1461 htab = m68hc11_elf_hash_table (link_info);
1462 if (htab == NULL)
3a65329d
SC
1463 return;
1464
1465 m68hc11_elf_get_bank_parameters (link_info);
1466
1467 param.use_memory_banks = FALSE;
4dfe6ac6
NC
1468 param.pinfo = & htab->pinfo;
1469
3a65329d 1470 bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
4dfe6ac6 1471
3a65329d
SC
1472 if (param.use_memory_banks)
1473 {
1474 Elf_Internal_Ehdr * i_ehdrp;
1475
1476 i_ehdrp = elf_elfheader (abfd);
1477 i_ehdrp->e_flags |= E_M68HC12_BANKS;
1478 }
1479}