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