2012-08-02 Sergio Durigan Junior <sergiodj@redhat.com>
[deliverable/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;
3a65329d
SC
921
922 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
923 sym_hashes = elf_sym_hashes (input_bfd);
924
4dfe6ac6
NC
925 htab = m68hc11_elf_hash_table (info);
926 if (htab == NULL)
927 return FALSE;
928
3a65329d
SC
929 /* Get memory bank parameters. */
930 m68hc11_elf_get_bank_parameters (info);
3a65329d 931
4dfe6ac6 932 pinfo = & htab->pinfo;
3a65329d
SC
933 rel = relocs;
934 relend = relocs + input_section->reloc_count;
4dfe6ac6 935
3a65329d
SC
936 for (; rel < relend; rel++)
937 {
938 int r_type;
939 arelent arel;
940 reloc_howto_type *howto;
941 unsigned long r_symndx;
942 Elf_Internal_Sym *sym;
943 asection *sec;
9b69b847 944 bfd_vma relocation = 0;
3a65329d
SC
945 bfd_reloc_status_type r = bfd_reloc_undefined;
946 bfd_vma phys_page;
947 bfd_vma phys_addr;
948 bfd_vma insn_addr;
949 bfd_vma insn_page;
9b69b847 950 bfd_boolean is_far = FALSE;
082d1122
NC
951 bfd_boolean is_xgate_symbol = FALSE;
952 bfd_boolean is_section_symbol = FALSE;
ab96bf03 953 struct elf_link_hash_entry *h;
6927f982 954 bfd_vma val;
3a65329d
SC
955
956 r_symndx = ELF32_R_SYM (rel->r_info);
957 r_type = ELF32_R_TYPE (rel->r_info);
958
959 if (r_type == R_M68HC11_GNU_VTENTRY
082d1122 960 || r_type == R_M68HC11_GNU_VTINHERIT)
3a65329d
SC
961 continue;
962
ab96bf03
AM
963 (*ebd->elf_info_to_howto_rel) (input_bfd, &arel, rel);
964 howto = arel.howto;
965
966 h = NULL;
967 sym = NULL;
968 sec = NULL;
969 if (r_symndx < symtab_hdr->sh_info)
970 {
971 sym = local_syms + r_symndx;
972 sec = local_sections[r_symndx];
973 relocation = (sec->output_section->vma
974 + sec->output_offset
975 + sym->st_value);
976 is_far = (sym && (sym->st_other & STO_M68HC12_FAR));
082d1122
NC
977 is_xgate_symbol = (sym && (sym->st_target_internal));
978 is_section_symbol = ELF_ST_TYPE (sym->st_info) & STT_SECTION;
ab96bf03
AM
979 }
980 else
981 {
982 bfd_boolean unresolved_reloc, warned;
983
984 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
985 r_symndx, symtab_hdr, sym_hashes,
986 h, sec, relocation, unresolved_reloc,
987 warned);
988
989 is_far = (h && (h->other & STO_M68HC12_FAR));
082d1122 990 is_xgate_symbol = (h && (h->target_internal));
ab96bf03
AM
991 }
992
dbaa2011 993 if (sec != NULL && discarded_section (sec))
e4067dbb 994 RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
545fd46b 995 rel, 1, relend, howto, 0, contents);
ab96bf03 996
1049f94e 997 if (info->relocatable)
3a65329d 998 {
1049f94e 999 /* This is a relocatable link. We don't have to change
3a65329d
SC
1000 anything, unless the reloc is against a section symbol,
1001 in which case we have to adjust according to where the
1002 section symbol winds up in the output section. */
ab96bf03
AM
1003 if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1004 rel->r_addend += sec->output_offset;
3a65329d
SC
1005 continue;
1006 }
3a65329d 1007
ab96bf03
AM
1008 if (h != NULL)
1009 name = h->root.root.string;
1010 else
1011 {
1012 name = (bfd_elf_string_from_elf_section
1013 (input_bfd, symtab_hdr->sh_link, sym->st_name));
1014 if (name == NULL || *name == '\0')
1015 name = bfd_section_name (input_bfd, sec);
1016 }
1017
1018 if (is_far && ELF32_R_TYPE (rel->r_info) == R_M68HC11_16)
1019 {
1020 struct elf32_m68hc11_stub_hash_entry* stub;
ab96bf03 1021
ab96bf03
AM
1022 stub = m68hc12_stub_hash_lookup (htab->stub_hash_table,
1023 name, FALSE, FALSE);
1024 if (stub)
1025 {
1026 relocation = stub->stub_offset
1027 + stub->stub_sec->output_section->vma
1028 + stub->stub_sec->output_offset;
1029 is_far = FALSE;
1030 }
1031 }
3a65329d
SC
1032
1033 /* Do the memory bank mapping. */
1034 phys_addr = m68hc11_phys_addr (pinfo, relocation + rel->r_addend);
1035 phys_page = m68hc11_phys_page (pinfo, relocation + rel->r_addend);
1036 switch (r_type)
1037 {
6927f982
NC
1038 case R_M68HC12_LO8XG:
1039 /* This relocation is specific to XGATE IMM16 calls and will precede
1040 a HI8. tc-m68hc11 only generates them in pairs.
1041 Leave the relocation to the HI8XG step. */
1042 r = bfd_reloc_ok;
1043 r_type = R_M68HC11_NONE;
1044 break;
1045
1046 case R_M68HC12_HI8XG:
1047 /* This relocation is specific to XGATE IMM16 calls and must follow
1048 a LO8XG. Does not actually check that it was a LO8XG.
1049 Adjusts high and low bytes. */
1050 relocation = phys_addr;
1051 if ((elf_elfheader (input_bfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
1052 && (relocation >= 0x2000))
1053 relocation += 0xc000; /* HARDCODED RAM offset for XGATE. */
1054
1055 /* Fetch 16 bit value including low byte in previous insn. */
1056 val = (bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset) << 8)
1057 | bfd_get_8 (input_bfd, (bfd_byte*) contents + rel->r_offset - 2);
1058
1059 /* Add on value to preserve carry, then write zero to high byte. */
1060 relocation += val;
1061
1062 /* Write out top byte. */
1063 bfd_put_8 (input_bfd, (relocation >> 8) & 0xff,
1064 (bfd_byte*) contents + rel->r_offset);
1065
1066 /* Write out low byte to previous instruction. */
1067 bfd_put_8 (input_bfd, relocation & 0xff,
1068 (bfd_byte*) contents + rel->r_offset - 2);
1069
1070 /* Mark as relocation completed. */
1071 r = bfd_reloc_ok;
1072 r_type = R_M68HC11_NONE;
1073 break;
1074
1075 /* The HI8 and LO8 relocs are generated by %hi(expr) %lo(expr)
1076 assembler directives. %hi does not support carry. */
1077 case R_M68HC11_HI8:
1078 case R_M68HC11_LO8:
1079 relocation = phys_addr;
1080 break;
1081
3a65329d
SC
1082 case R_M68HC11_24:
1083 /* Reloc used by 68HC12 call instruction. */
1084 bfd_put_16 (input_bfd, phys_addr,
1085 (bfd_byte*) contents + rel->r_offset);
1086 bfd_put_8 (input_bfd, phys_page,
1087 (bfd_byte*) contents + rel->r_offset + 2);
1088 r = bfd_reloc_ok;
1089 r_type = R_M68HC11_NONE;
1090 break;
1091
1092 case R_M68HC11_NONE:
1093 r = bfd_reloc_ok;
1094 break;
1095
1096 case R_M68HC11_LO16:
1097 /* Reloc generated by %addr(expr) gas to obtain the
1098 address as mapped in the memory bank window. */
1099 relocation = phys_addr;
1100 break;
1101
1102 case R_M68HC11_PAGE:
1103 /* Reloc generated by %page(expr) gas to obtain the
1104 page number associated with the address. */
1105 relocation = phys_page;
1106 break;
1107
1108 case R_M68HC11_16:
1109 /* Get virtual address of instruction having the relocation. */
1110 if (is_far)
1111 {
1112 const char* msg;
1113 char* buf;
1114 msg = _("Reference to the far symbol `%s' using a wrong "
1115 "relocation may result in incorrect execution");
1116 buf = alloca (strlen (msg) + strlen (name) + 10);
1117 sprintf (buf, msg, name);
1118
1119 (* info->callbacks->warning)
1120 (info, buf, name, input_bfd, NULL, rel->r_offset);
1121 }
1122
1123 /* Get virtual address of instruction having the relocation. */
1124 insn_addr = input_section->output_section->vma
1125 + input_section->output_offset
1126 + rel->r_offset;
1127
1128 insn_page = m68hc11_phys_page (pinfo, insn_addr);
1129
082d1122
NC
1130 /* If we are linking an S12 instruction against an XGATE symbol, we
1131 need to change the offset of the symbol value so that it's correct
1132 from the S12's perspective. */
1133 if (is_xgate_symbol)
1134 {
1135 /* The ram in the global space is mapped to 0x2000 in the 16-bit
1136 address space for S12 and 0xE000 in the 16-bit address space
1137 for XGATE. */
1138 if (relocation >= 0xE000)
1139 {
1140 /* We offset the address by the difference
1141 between these two mappings. */
1142 relocation -= 0xC000;
1143 break;
1144 }
1145 else
1146 {
1147 const char * msg;
1148 char * buf;
1149
1150 msg = _("XGATE address (%lx) is not within shared RAM"
1151 "(0xE000-0xFFFF), therefore you must manually offset "
1152 "the address, and possibly manage the page, in your "
1153 "code.");
1154 buf = alloca (strlen (msg) + 128);
1155 sprintf (buf, msg, phys_addr);
1156 if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
1157 input_section, insn_addr)))
1158 return FALSE;
1159 break;
1160 }
1161 }
1162
3a65329d
SC
1163 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend)
1164 && m68hc11_addr_is_banked (pinfo, insn_addr)
1165 && phys_page != insn_page)
1166 {
082d1122
NC
1167 const char * msg;
1168 char * buf;
3a65329d
SC
1169
1170 msg = _("banked address [%lx:%04lx] (%lx) is not in the same bank "
1171 "as current banked address [%lx:%04lx] (%lx)");
1172
1173 buf = alloca (strlen (msg) + 128);
1174 sprintf (buf, msg, phys_page, phys_addr,
1175 (long) (relocation + rel->r_addend),
1176 insn_page, m68hc11_phys_addr (pinfo, insn_addr),
1177 (long) (insn_addr));
1178 if (!((*info->callbacks->warning)
1179 (info, buf, name, input_bfd, input_section,
1180 rel->r_offset)))
1181 return FALSE;
1182 break;
1183 }
082d1122 1184
3a65329d
SC
1185 if (phys_page != 0 && insn_page == 0)
1186 {
082d1122
NC
1187 const char * msg;
1188 char * buf;
3a65329d
SC
1189
1190 msg = _("reference to a banked address [%lx:%04lx] in the "
1191 "normal address space at %04lx");
1192
1193 buf = alloca (strlen (msg) + 128);
1194 sprintf (buf, msg, phys_page, phys_addr, insn_addr);
1195 if (!((*info->callbacks->warning)
1196 (info, buf, name, input_bfd, input_section,
1197 insn_addr)))
1198 return FALSE;
1199
1200 relocation = phys_addr;
1201 break;
1202 }
1203
1204 /* If this is a banked address use the phys_addr so that
1205 we stay in the banked window. */
1206 if (m68hc11_addr_is_banked (pinfo, relocation + rel->r_addend))
1207 relocation = phys_addr;
1208 break;
1209 }
6927f982 1210
082d1122
NC
1211 /* If we are linking an XGATE instruction against an S12 symbol, we
1212 need to change the offset of the symbol value so that it's correct
1213 from the XGATE's perspective. */
1214 if (!strcmp (howto->name, "R_XGATE_IMM8_LO")
1215 || !strcmp (howto->name, "R_XGATE_IMM8_HI"))
1216 {
1217 /* We can only offset S12 addresses that lie within the non-paged
1218 area of RAM. */
1219 if (!is_xgate_symbol && !is_section_symbol)
1220 {
1221 /* The ram in the global space is mapped to 0x2000 and stops at
1222 0x4000 in the 16-bit address space for S12 and 0xE000 in the
1223 16-bit address space for XGATE. */
1224 if (relocation >= 0x2000 && relocation < 0x4000)
1225 /* We offset the address by the difference
1226 between these two mappings. */
1227 relocation += 0xC000;
1228 else
1229 {
1230 const char * msg;
1231 char * buf;
1232
1233 /* Get virtual address of instruction having the relocation. */
1234 insn_addr = input_section->output_section->vma
1235 + input_section->output_offset + rel->r_offset;
1236
1237 msg = _("S12 address (%lx) is not within shared RAM"
1238 "(0x2000-0x4000), therefore you must manually "
1239 "offset the address in your code");
1240 buf = alloca (strlen (msg) + 128);
1241 sprintf (buf, msg, phys_addr);
1242 if (!((*info->callbacks->warning) (info, buf, name, input_bfd,
1243 input_section, insn_addr)))
1244 return FALSE;
1245 break;
1246 }
1247 }
1248 }
1249
3a65329d 1250 if (r_type != R_M68HC11_NONE)
6927f982
NC
1251 {
1252 if ((r_type == R_M68HC12_PCREL_9) || (r_type == R_M68HC12_PCREL_10))
1253 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
3a65329d 1254 contents, rel->r_offset,
6927f982
NC
1255 relocation - 2, rel->r_addend);
1256 else
1257 r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1258 contents, rel->r_offset,
1259 relocation, rel->r_addend);
1260 }
3a65329d
SC
1261
1262 if (r != bfd_reloc_ok)
1263 {
1264 const char * msg = (const char *) 0;
1265
1266 switch (r)
1267 {
1268 case bfd_reloc_overflow:
1269 if (!((*info->callbacks->reloc_overflow)
dfeffb9f 1270 (info, NULL, name, howto->name, (bfd_vma) 0,
3a65329d
SC
1271 input_bfd, input_section, rel->r_offset)))
1272 return FALSE;
1273 break;
1274
1275 case bfd_reloc_undefined:
1276 if (!((*info->callbacks->undefined_symbol)
1277 (info, name, input_bfd, input_section,
1278 rel->r_offset, TRUE)))
1279 return FALSE;
1280 break;
1281
1282 case bfd_reloc_outofrange:
1283 msg = _ ("internal error: out of range error");
1284 goto common_error;
1285
1286 case bfd_reloc_notsupported:
1287 msg = _ ("internal error: unsupported relocation error");
1288 goto common_error;
1289
1290 case bfd_reloc_dangerous:
1291 msg = _ ("internal error: dangerous error");
1292 goto common_error;
1293
1294 default:
1295 msg = _ ("internal error: unknown error");
1296 /* fall through */
1297
1298 common_error:
1299 if (!((*info->callbacks->warning)
1300 (info, msg, name, input_bfd, input_section,
1301 rel->r_offset)))
1302 return FALSE;
1303 break;
1304 }
1305 }
1306 }
1307
1308 return TRUE;
1309}
1310
1311
1312\f
1313/* Set and control ELF flags in ELF header. */
1314
1315bfd_boolean
0a6a3ebe 1316_bfd_m68hc11_elf_set_private_flags (bfd *abfd, flagword flags)
3a65329d
SC
1317{
1318 BFD_ASSERT (!elf_flags_init (abfd)
1319 || elf_elfheader (abfd)->e_flags == flags);
1320
1321 elf_elfheader (abfd)->e_flags = flags;
1322 elf_flags_init (abfd) = TRUE;
1323 return TRUE;
1324}
1325
1326/* Merge backend specific data from an object file to the output
1327 object file when linking. */
1328
1329bfd_boolean
0a6a3ebe 1330_bfd_m68hc11_elf_merge_private_bfd_data (bfd *ibfd, bfd *obfd)
3a65329d
SC
1331{
1332 flagword old_flags;
1333 flagword new_flags;
1334 bfd_boolean ok = TRUE;
1335
cc643b88 1336 /* Check if we have the same endianness */
3a65329d
SC
1337 if (!_bfd_generic_verify_endian_match (ibfd, obfd))
1338 return FALSE;
1339
1340 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1341 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1342 return TRUE;
1343
1344 new_flags = elf_elfheader (ibfd)->e_flags;
1345 elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
1346 old_flags = elf_elfheader (obfd)->e_flags;
1347
1348 if (! elf_flags_init (obfd))
1349 {
1350 elf_flags_init (obfd) = TRUE;
1351 elf_elfheader (obfd)->e_flags = new_flags;
1352 elf_elfheader (obfd)->e_ident[EI_CLASS]
1353 = elf_elfheader (ibfd)->e_ident[EI_CLASS];
1354
1355 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1356 && bfd_get_arch_info (obfd)->the_default)
1357 {
1358 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1359 bfd_get_mach (ibfd)))
1360 return FALSE;
1361 }
1362
1363 return TRUE;
1364 }
1365
1366 /* Check ABI compatibility. */
1367 if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
1368 {
1369 (*_bfd_error_handler)
d003868e
AM
1370 (_("%B: linking files compiled for 16-bit integers (-mshort) "
1371 "and others for 32-bit integers"), ibfd);
3a65329d
SC
1372 ok = FALSE;
1373 }
1374 if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
1375 {
1376 (*_bfd_error_handler)
d003868e
AM
1377 (_("%B: linking files compiled for 32-bit double (-fshort-double) "
1378 "and others for 64-bit double"), ibfd);
3a65329d
SC
1379 ok = FALSE;
1380 }
47247ced
SC
1381
1382 /* Processor compatibility. */
1383 if (!EF_M68HC11_CAN_MERGE_MACH (new_flags, old_flags))
1384 {
1385 (*_bfd_error_handler)
d003868e
AM
1386 (_("%B: linking files compiled for HCS12 with "
1387 "others compiled for HC12"), ibfd);
47247ced
SC
1388 ok = FALSE;
1389 }
1390 new_flags = ((new_flags & ~EF_M68HC11_MACH_MASK)
1391 | (EF_M68HC11_MERGE_MACH (new_flags, old_flags)));
1392
1393 elf_elfheader (obfd)->e_flags = new_flags;
1394
17e58af0
SC
1395 new_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
1396 old_flags &= ~(EF_M68HC11_ABI | EF_M68HC11_MACH_MASK);
3a65329d
SC
1397
1398 /* Warn about any other mismatches */
1399 if (new_flags != old_flags)
1400 {
1401 (*_bfd_error_handler)
d003868e
AM
1402 (_("%B: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1403 ibfd, (unsigned long) new_flags, (unsigned long) old_flags);
3a65329d
SC
1404 ok = FALSE;
1405 }
1406
1407 if (! ok)
1408 {
1409 bfd_set_error (bfd_error_bad_value);
1410 return FALSE;
1411 }
1412
1413 return TRUE;
1414}
1415
1416bfd_boolean
0a6a3ebe 1417_bfd_m68hc11_elf_print_private_bfd_data (bfd *abfd, void *ptr)
3a65329d
SC
1418{
1419 FILE *file = (FILE *) ptr;
1420
1421 BFD_ASSERT (abfd != NULL && ptr != NULL);
1422
1423 /* Print normal ELF private data. */
1424 _bfd_elf_print_private_bfd_data (abfd, ptr);
1425
1426 /* xgettext:c-format */
1427 fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1428
1429 if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
1430 fprintf (file, _("[abi=32-bit int, "));
1431 else
1432 fprintf (file, _("[abi=16-bit int, "));
1433
1434 if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
1435 fprintf (file, _("64-bit double, "));
1436 else
1437 fprintf (file, _("32-bit double, "));
1438
1439 if (strcmp (bfd_get_target (abfd), "elf32-m68hc11") == 0)
1440 fprintf (file, _("cpu=HC11]"));
1441 else if (elf_elfheader (abfd)->e_flags & EF_M68HCS12_MACH)
1442 fprintf (file, _("cpu=HCS12]"));
1443 else
1444 fprintf (file, _("cpu=HC12]"));
1445
1446 if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
1447 fprintf (file, _(" [memory=bank-model]"));
1448 else
1449 fprintf (file, _(" [memory=flat]"));
1450
6927f982
NC
1451 if (elf_elfheader (abfd)->e_flags & E_M68HC11_XGATE_RAMOFFSET)
1452 fprintf (file, _(" [XGATE RAM offsetting]"));
1453
3a65329d
SC
1454 fputc ('\n', file);
1455
1456 return TRUE;
1457}
1458
0a6a3ebe
SC
1459static void scan_sections_for_abi (bfd *abfd ATTRIBUTE_UNUSED,
1460 asection *asect, void *arg)
3a65329d
SC
1461{
1462 struct m68hc11_scan_param* p = (struct m68hc11_scan_param*) arg;
1463
1464 if (asect->vma >= p->pinfo->bank_virtual)
1465 p->use_memory_banks = TRUE;
1466}
1467
1468/* Tweak the OSABI field of the elf header. */
1469
1470void
0a6a3ebe 1471elf32_m68hc11_post_process_headers (bfd *abfd, struct bfd_link_info *link_info)
3a65329d
SC
1472{
1473 struct m68hc11_scan_param param;
4dfe6ac6
NC
1474 struct m68hc11_elf_link_hash_table *htab;
1475
1476 if (link_info == NULL)
1477 return;
3a65329d 1478
4dfe6ac6
NC
1479 htab = m68hc11_elf_hash_table (link_info);
1480 if (htab == NULL)
3a65329d
SC
1481 return;
1482
1483 m68hc11_elf_get_bank_parameters (link_info);
1484
1485 param.use_memory_banks = FALSE;
4dfe6ac6
NC
1486 param.pinfo = & htab->pinfo;
1487
3a65329d 1488 bfd_map_over_sections (abfd, scan_sections_for_abi, &param);
4dfe6ac6 1489
3a65329d
SC
1490 if (param.use_memory_banks)
1491 {
1492 Elf_Internal_Ehdr * i_ehdrp;
1493
1494 i_ehdrp = elf_elfheader (abfd);
1495 i_ehdrp->e_flags |= E_M68HC12_BANKS;
1496 }
1497}
This page took 0.982412 seconds and 4 git commands to generate.