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