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