* elf32-m68hc11.c (m68hc11_elf_relax_section): Update symbols
[external/binutils.git] / bfd / elf32-m68hc11.c
1 /* Motorola 68HC11-specific support for 32-bit ELF
2    Copyright 1999, 2000, 2001, 2002 Free Software Foundation, Inc.
3    Contributed by Stephane Carrez (stcarrez@nerim.fr)
4    (Heavily copied from the D10V port by Martin Hunt (hunt@cygnus.com))
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 "elf/m68hc11.h"
28
29 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
30   PARAMS ((bfd *, bfd_reloc_code_real_type));
31 static void m68hc11_info_to_howto_rel
32   PARAMS ((bfd *, arelent *, Elf32_Internal_Rel *));
33
34 static bfd_reloc_status_type m68hc11_elf_ignore_reloc
35   PARAMS ((bfd *, arelent *, asymbol *, PTR, asection *, bfd *, char **));
36
37 /* GC mark and sweep.  */
38 static asection *elf32_m68hc11_gc_mark_hook
39   PARAMS ((asection *, struct bfd_link_info *, Elf_Internal_Rela *,
40            struct elf_link_hash_entry *, Elf_Internal_Sym *));
41 static boolean elf32_m68hc11_gc_sweep_hook
42   PARAMS ((bfd *, struct bfd_link_info *, asection *,
43            const Elf_Internal_Rela *));
44 static boolean elf32_m68hc11_check_relocs
45   PARAMS ((bfd *, struct bfd_link_info *, asection *,
46            const Elf_Internal_Rela *));
47 static boolean elf32_m68hc11_relocate_section
48   PARAMS ((bfd *, struct bfd_link_info *, bfd *, asection *, bfd_byte *,
49            Elf_Internal_Rela *, Elf_Internal_Sym *, asection **));
50 static boolean m68hc11_elf_relax_section
51   PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
52 static void m68hc11_elf_relax_delete_bytes
53   PARAMS ((bfd *, asection *, bfd_vma, int));
54 static void m68hc11_relax_group
55   PARAMS ((bfd *, asection *, bfd_byte *, unsigned,
56            unsigned long, unsigned long));
57 static int compare_reloc PARAMS ((const void *, const void *));
58
59
60 boolean _bfd_m68hc11_elf_merge_private_bfd_data PARAMS ((bfd *, bfd *));
61 boolean _bfd_m68hc11_elf_set_private_flags PARAMS ((bfd *, flagword));
62 boolean _bfd_m68hc11_elf_print_private_bfd_data PARAMS ((bfd *, PTR));
63
64 /* Use REL instead of RELA to save space */
65 #define USE_REL
66
67 /* The Motorola 68HC11 microcontroler only addresses 64Kb.
68    We must handle 8 and 16-bit relocations.  The 32-bit relocation
69    is defined but not used except by gas when -gstabs is used (which
70    is wrong).
71    The 3-bit and 16-bit PC rel relocation is only used by 68HC12.  */
72 static reloc_howto_type elf_m68hc11_howto_table[] = {
73   /* This reloc does nothing.  */
74   HOWTO (R_M68HC11_NONE,        /* type */
75          0,                     /* rightshift */
76          2,                     /* size (0 = byte, 1 = short, 2 = long) */
77          32,                    /* bitsize */
78          false,                 /* pc_relative */
79          0,                     /* bitpos */
80          complain_overflow_dont,/* complain_on_overflow */
81          bfd_elf_generic_reloc, /* special_function */
82          "R_M68HC11_NONE",      /* name */
83          false,                 /* partial_inplace */
84          0,                     /* src_mask */
85          0,                     /* dst_mask */
86          false),                /* pcrel_offset */
87
88   /* A 8 bit absolute relocation */
89   HOWTO (R_M68HC11_8,           /* type */
90          0,                     /* rightshift */
91          0,                     /* size (0 = byte, 1 = short, 2 = long) */
92          8,                     /* bitsize */
93          false,                 /* pc_relative */
94          0,                     /* bitpos */
95          complain_overflow_bitfield,    /* complain_on_overflow */
96          bfd_elf_generic_reloc, /* special_function */
97          "R_M68HC11_8",         /* name */
98          false,                 /* partial_inplace */
99          0x00ff,                /* src_mask */
100          0x00ff,                /* dst_mask */
101          false),                /* pcrel_offset */
102
103   /* A 8 bit absolute relocation (upper address) */
104   HOWTO (R_M68HC11_HI8,         /* type */
105          8,                     /* rightshift */
106          0,                     /* size (0 = byte, 1 = short, 2 = long) */
107          8,                     /* bitsize */
108          false,                 /* pc_relative */
109          0,                     /* bitpos */
110          complain_overflow_bitfield,    /* complain_on_overflow */
111          bfd_elf_generic_reloc, /* special_function */
112          "R_M68HC11_HI8",       /* name */
113          false,                 /* partial_inplace */
114          0x00ff,                /* src_mask */
115          0x00ff,                /* dst_mask */
116          false),                /* pcrel_offset */
117
118   /* A 8 bit absolute relocation (upper address) */
119   HOWTO (R_M68HC11_LO8,         /* type */
120          0,                     /* rightshift */
121          0,                     /* size (0 = byte, 1 = short, 2 = long) */
122          8,                     /* bitsize */
123          false,                 /* pc_relative */
124          0,                     /* bitpos */
125          complain_overflow_dont,        /* complain_on_overflow */
126          bfd_elf_generic_reloc, /* special_function */
127          "R_M68HC11_LO8",       /* name */
128          false,                 /* partial_inplace */
129          0x00ff,                /* src_mask */
130          0x00ff,                /* dst_mask */
131          false),                /* pcrel_offset */
132
133   /* A 8 bit PC-rel relocation */
134   HOWTO (R_M68HC11_PCREL_8,     /* type */
135          0,                     /* rightshift */
136          0,                     /* size (0 = byte, 1 = short, 2 = long) */
137          8,                     /* bitsize */
138          true,                  /* pc_relative */
139          0,                     /* bitpos */
140          complain_overflow_bitfield,    /* complain_on_overflow */
141          bfd_elf_generic_reloc, /* special_function */
142          "R_M68HC11_PCREL_8",   /* name */
143          false,                 /* partial_inplace */
144          0x00ff,                /* src_mask */
145          0x00ff,                /* dst_mask */
146          false),                /* pcrel_offset */
147
148   /* A 16 bit absolute relocation */
149   HOWTO (R_M68HC11_16,          /* type */
150          0,                     /* rightshift */
151          1,                     /* size (0 = byte, 1 = short, 2 = long) */
152          16,                    /* bitsize */
153          false,                 /* pc_relative */
154          0,                     /* bitpos */
155          complain_overflow_dont /*bitfield */ , /* complain_on_overflow */
156          bfd_elf_generic_reloc, /* special_function */
157          "R_M68HC11_16",        /* name */
158          false,                 /* partial_inplace */
159          0xffff,                /* src_mask */
160          0xffff,                /* dst_mask */
161          false),                /* pcrel_offset */
162
163   /* A 32 bit absolute relocation.  This one is never used for the
164      code relocation.  It's used by gas for -gstabs generation.  */
165   HOWTO (R_M68HC11_32,          /* type */
166          0,                     /* rightshift */
167          2,                     /* size (0 = byte, 1 = short, 2 = long) */
168          32,                    /* bitsize */
169          false,                 /* pc_relative */
170          0,                     /* bitpos */
171          complain_overflow_bitfield,    /* complain_on_overflow */
172          bfd_elf_generic_reloc, /* special_function */
173          "R_M68HC11_32",        /* name */
174          false,                 /* partial_inplace */
175          0xffffffff,            /* src_mask */
176          0xffffffff,            /* dst_mask */
177          false),                /* pcrel_offset */
178
179   /* A 3 bit absolute relocation */
180   HOWTO (R_M68HC11_3B,          /* type */
181          0,                     /* rightshift */
182          0,                     /* size (0 = byte, 1 = short, 2 = long) */
183          3,                     /* bitsize */
184          false,                 /* pc_relative */
185          0,                     /* bitpos */
186          complain_overflow_bitfield,    /* complain_on_overflow */
187          bfd_elf_generic_reloc, /* special_function */
188          "R_M68HC11_4B",        /* name */
189          false,                 /* partial_inplace */
190          0x003,                 /* src_mask */
191          0x003,                 /* dst_mask */
192          false),                /* pcrel_offset */
193
194   /* A 16 bit PC-rel relocation */
195   HOWTO (R_M68HC11_PCREL_16,    /* type */
196          0,                     /* rightshift */
197          1,                     /* size (0 = byte, 1 = short, 2 = long) */
198          16,                    /* bitsize */
199          true,                  /* pc_relative */
200          0,                     /* bitpos */
201          complain_overflow_dont,        /* complain_on_overflow */
202          bfd_elf_generic_reloc, /* special_function */
203          "R_M68HC11_PCREL_16",  /* name */
204          false,                 /* partial_inplace */
205          0xffff,                /* src_mask */
206          0xffff,                /* dst_mask */
207          false),                /* pcrel_offset */
208
209   /* GNU extension to record C++ vtable hierarchy */
210   HOWTO (R_M68HC11_GNU_VTINHERIT,       /* type */
211          0,                     /* rightshift */
212          1,                     /* size (0 = byte, 1 = short, 2 = long) */
213          0,                     /* bitsize */
214          false,                 /* pc_relative */
215          0,                     /* bitpos */
216          complain_overflow_dont,        /* complain_on_overflow */
217          NULL,                  /* special_function */
218          "R_M68HC11_GNU_VTINHERIT",     /* name */
219          false,                 /* partial_inplace */
220          0,                     /* src_mask */
221          0,                     /* dst_mask */
222          false),                /* pcrel_offset */
223
224   /* GNU extension to record C++ vtable member usage */
225   HOWTO (R_M68HC11_GNU_VTENTRY, /* type */
226          0,                     /* rightshift */
227          1,                     /* size (0 = byte, 1 = short, 2 = long) */
228          0,                     /* bitsize */
229          false,                 /* pc_relative */
230          0,                     /* bitpos */
231          complain_overflow_dont,        /* complain_on_overflow */
232          _bfd_elf_rel_vtable_reloc_fn,  /* special_function */
233          "R_M68HC11_GNU_VTENTRY",       /* name */
234          false,                 /* partial_inplace */
235          0,                     /* src_mask */
236          0,                     /* dst_mask */
237          false),                /* pcrel_offset */
238
239   /* A 24 bit relocation */
240   HOWTO (R_M68HC11_24,          /* type */
241          0,                     /* rightshift */
242          1,                     /* size (0 = byte, 1 = short, 2 = long) */
243          24,                    /* bitsize */
244          false,                 /* pc_relative */
245          0,                     /* bitpos */
246          complain_overflow_bitfield,    /* complain_on_overflow */
247          bfd_elf_generic_reloc, /* special_function */
248          "R_M68HC11_24",        /* name */
249          false,                 /* partial_inplace */
250          0xffff,                /* src_mask */
251          0xffff,                /* dst_mask */
252          false),                /* pcrel_offset */
253   
254   /* A 16-bit low relocation */
255   HOWTO (R_M68HC11_LO16,        /* type */
256          0,                     /* rightshift */
257          1,                     /* size (0 = byte, 1 = short, 2 = long) */
258          16,                    /* bitsize */
259          false,                 /* pc_relative */
260          0,                     /* bitpos */
261          complain_overflow_bitfield,    /* complain_on_overflow */
262          bfd_elf_generic_reloc, /* special_function */
263          "R_M68HC11_LO16",      /* name */
264          false,                 /* partial_inplace */
265          0xffff,                /* src_mask */
266          0xffff,                /* dst_mask */
267          false),                /* pcrel_offset */
268
269   /* A page relocation */
270   HOWTO (R_M68HC11_PAGE,        /* type */
271          0,                     /* rightshift */
272          0,                     /* size (0 = byte, 1 = short, 2 = long) */
273          8,                     /* bitsize */
274          false,                 /* pc_relative */
275          0,                     /* bitpos */
276          complain_overflow_bitfield,    /* complain_on_overflow */
277          bfd_elf_generic_reloc, /* special_function */
278          "R_M68HC11_PAGE",      /* name */
279          false,                 /* partial_inplace */
280          0x00ff,                /* src_mask */
281          0x00ff,                /* dst_mask */
282          false),                /* pcrel_offset */
283
284   EMPTY_HOWTO (14),
285   EMPTY_HOWTO (15),
286   EMPTY_HOWTO (16),
287   EMPTY_HOWTO (17),
288   EMPTY_HOWTO (18),
289   EMPTY_HOWTO (19),
290   
291   /* Mark beginning of a jump instruction (any form).  */
292   HOWTO (R_M68HC11_RL_JUMP,     /* type */
293          0,                     /* rightshift */
294          1,                     /* size (0 = byte, 1 = short, 2 = long) */
295          0,                     /* bitsize */
296          false,                 /* pc_relative */
297          0,                     /* bitpos */
298          complain_overflow_dont,        /* complain_on_overflow */
299          m68hc11_elf_ignore_reloc,      /* special_function */
300          "R_M68HC11_RL_JUMP",   /* name */
301          true,                  /* partial_inplace */
302          0,                     /* src_mask */
303          0,                     /* dst_mask */
304          true),                 /* pcrel_offset */
305
306   /* Mark beginning of Gcc relaxation group instruction.  */
307   HOWTO (R_M68HC11_RL_GROUP,    /* type */
308          0,                     /* rightshift */
309          1,                     /* size (0 = byte, 1 = short, 2 = long) */
310          0,                     /* bitsize */
311          false,                 /* pc_relative */
312          0,                     /* bitpos */
313          complain_overflow_dont,        /* complain_on_overflow */
314          m68hc11_elf_ignore_reloc,      /* special_function */
315          "R_M68HC11_RL_GROUP",  /* name */
316          true,                  /* partial_inplace */
317          0,                     /* src_mask */
318          0,                     /* dst_mask */
319          true),                 /* pcrel_offset */
320 };
321
322 /* Map BFD reloc types to M68HC11 ELF reloc types.  */
323
324 struct m68hc11_reloc_map
325 {
326   bfd_reloc_code_real_type bfd_reloc_val;
327   unsigned char elf_reloc_val;
328 };
329
330 static const struct m68hc11_reloc_map m68hc11_reloc_map[] = {
331   {BFD_RELOC_NONE, R_M68HC11_NONE,},
332   {BFD_RELOC_8, R_M68HC11_8},
333   {BFD_RELOC_M68HC11_HI8, R_M68HC11_HI8},
334   {BFD_RELOC_M68HC11_LO8, R_M68HC11_LO8},
335   {BFD_RELOC_8_PCREL, R_M68HC11_PCREL_8},
336   {BFD_RELOC_16_PCREL, R_M68HC11_PCREL_16},
337   {BFD_RELOC_16, R_M68HC11_16},
338   {BFD_RELOC_32, R_M68HC11_32},
339   {BFD_RELOC_M68HC11_3B, R_M68HC11_3B},
340
341   {BFD_RELOC_VTABLE_INHERIT, R_M68HC11_GNU_VTINHERIT},
342   {BFD_RELOC_VTABLE_ENTRY, R_M68HC11_GNU_VTENTRY},
343
344   {BFD_RELOC_M68HC11_LO16, R_M68HC11_LO16},
345   {BFD_RELOC_M68HC11_PAGE, R_M68HC11_PAGE},
346   {BFD_RELOC_M68HC11_24, R_M68HC11_24},
347
348   {BFD_RELOC_M68HC11_RL_JUMP, R_M68HC11_RL_JUMP},
349   {BFD_RELOC_M68HC11_RL_GROUP, R_M68HC11_RL_GROUP},
350 };
351
352 static reloc_howto_type *
353 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
354      bfd *abfd ATTRIBUTE_UNUSED;
355      bfd_reloc_code_real_type code;
356 {
357   unsigned int i;
358
359   for (i = 0;
360        i < sizeof (m68hc11_reloc_map) / sizeof (struct m68hc11_reloc_map);
361        i++)
362     {
363       if (m68hc11_reloc_map[i].bfd_reloc_val == code)
364         return &elf_m68hc11_howto_table[m68hc11_reloc_map[i].elf_reloc_val];
365     }
366
367   return NULL;
368 }
369
370 /* This function is used for relocs which are only used for relaxing,
371    which the linker should otherwise ignore.  */
372
373 static bfd_reloc_status_type
374 m68hc11_elf_ignore_reloc (abfd, reloc_entry, symbol, data, input_section,
375                           output_bfd, error_message)
376      bfd *abfd ATTRIBUTE_UNUSED;
377      arelent *reloc_entry;
378      asymbol *symbol ATTRIBUTE_UNUSED;
379      PTR data ATTRIBUTE_UNUSED;
380      asection *input_section;
381      bfd *output_bfd;
382      char **error_message ATTRIBUTE_UNUSED;
383 {
384   if (output_bfd != NULL)
385     reloc_entry->address += input_section->output_offset;
386   return bfd_reloc_ok;
387 }
388
389 /* Set the howto pointer for an M68HC11 ELF reloc.  */
390
391 static void
392 m68hc11_info_to_howto_rel (abfd, cache_ptr, dst)
393      bfd *abfd ATTRIBUTE_UNUSED;
394      arelent *cache_ptr;
395      Elf32_Internal_Rel *dst;
396 {
397   unsigned int r_type;
398
399   r_type = ELF32_R_TYPE (dst->r_info);
400   BFD_ASSERT (r_type < (unsigned int) R_M68HC11_max);
401   cache_ptr->howto = &elf_m68hc11_howto_table[r_type];
402 }
403
404 static asection *
405 elf32_m68hc11_gc_mark_hook (sec, info, rel, h, sym)
406      asection *sec;
407      struct bfd_link_info *info ATTRIBUTE_UNUSED;
408      Elf_Internal_Rela *rel;
409      struct elf_link_hash_entry *h;
410      Elf_Internal_Sym *sym;
411 {
412   if (h != NULL)
413     {
414       switch (ELF32_R_TYPE (rel->r_info))
415         {
416         default:
417           switch (h->root.type)
418             {
419             case bfd_link_hash_defined:
420             case bfd_link_hash_defweak:
421               return h->root.u.def.section;
422
423             case bfd_link_hash_common:
424               return h->root.u.c.p->section;
425
426             default:
427               break;
428             }
429         }
430     }
431   else
432     return bfd_section_from_elf_index (sec->owner, sym->st_shndx);
433
434   return NULL;
435 }
436
437 static boolean
438 elf32_m68hc11_gc_sweep_hook (abfd, info, sec, relocs)
439      bfd *abfd ATTRIBUTE_UNUSED;
440      struct bfd_link_info *info ATTRIBUTE_UNUSED;
441      asection *sec ATTRIBUTE_UNUSED;
442      const Elf_Internal_Rela *relocs ATTRIBUTE_UNUSED;
443 {
444   /* We don't use got and plt entries for 68hc11/68hc12.  */
445   return true;
446 }
447
448 struct m68hc11_direct_relax 
449 {
450   const char *name;
451   unsigned char code;
452   unsigned char direct_code;
453 } m68hc11_direct_relax_table[] = {
454   { "adca", 0xB9, 0x99 },
455   { "adcb", 0xF9, 0xD9 },
456   { "adda", 0xBB, 0x9B },
457   { "addb", 0xFB, 0xDB },
458   { "addd", 0xF3, 0xD3 },
459   { "anda", 0xB4, 0x94 },
460   { "andb", 0xF4, 0xD4 },
461   { "cmpa", 0xB1, 0x91 },
462   { "cmpb", 0xF1, 0xD1 },
463   { "cpd",  0xB3, 0x93 },
464   { "cpxy", 0xBC, 0x9C },
465 /* { "cpy",  0xBC, 0x9C }, */
466   { "eora", 0xB8, 0x98 },
467   { "eorb", 0xF8, 0xD8 },
468   { "jsr",  0xBD, 0x9D },
469   { "ldaa", 0xB6, 0x96 },
470   { "ldab", 0xF6, 0xD6 },
471   { "ldd",  0xFC, 0xDC },
472   { "lds",  0xBE, 0x9E },
473   { "ldxy", 0xFE, 0xDE },
474   /*  { "ldy",  0xFE, 0xDE },*/
475   { "oraa", 0xBA, 0x9A },
476   { "orab", 0xFA, 0xDA },
477   { "sbca", 0xB2, 0x92 },
478   { "sbcb", 0xF2, 0xD2 },
479   { "staa", 0xB7, 0x97 },
480   { "stab", 0xF7, 0xD7 },
481   { "std",  0xFD, 0xDD },
482   { "sts",  0xBF, 0x9F },
483   { "stxy", 0xFF, 0xDF },
484   /*  { "sty",  0xFF, 0xDF },*/
485   { "suba", 0xB0, 0x90 },
486   { "subb", 0xF0, 0xD0 },
487   { "subd", 0xB3, 0x93 },
488   { 0, 0, 0 }
489 };
490
491 static struct m68hc11_direct_relax *
492 find_relaxable_insn (unsigned char code)
493 {
494   int i;
495
496   for (i = 0; m68hc11_direct_relax_table[i].name; i++)
497     if (m68hc11_direct_relax_table[i].code == code)
498       return &m68hc11_direct_relax_table[i];
499
500   return 0;
501 }
502
503 static int
504 compare_reloc (e1, e2)
505      const void *e1;
506      const void *e2;
507 {
508   const Elf_Internal_Rela *i1 = (const Elf_Internal_Rela *) e1;
509   const Elf_Internal_Rela *i2 = (const Elf_Internal_Rela *) e2;
510
511   if (i1->r_offset == i2->r_offset)
512     return 0;
513   else
514     return i1->r_offset < i2->r_offset ? -1 : 1;
515 }
516
517 #define M6811_OP_LDX_IMMEDIATE (0xCE)
518
519 static void
520 m68hc11_relax_group (abfd, sec, contents, value, offset, end_group)
521      bfd *abfd;
522      asection *sec;
523      bfd_byte *contents;
524      unsigned value;
525      unsigned long offset;
526      unsigned long end_group;
527 {
528   unsigned char code;
529   unsigned long start_offset;
530   unsigned long ldx_offset = offset;
531   unsigned long ldx_size;
532   int can_delete_ldx;
533   int relax_ldy = 0;
534
535   /* First instruction of the relax group must be a
536      LDX #value or LDY #value.  If this is not the case,
537      ignore the relax group.  */
538   code = bfd_get_8 (abfd, contents + offset);
539   if (code == 0x18)
540     {
541       relax_ldy++;
542       offset++;
543       code = bfd_get_8 (abfd, contents + offset);
544     }
545   ldx_size = offset - ldx_offset + 3;
546   offset += 3;
547   if (code != M6811_OP_LDX_IMMEDIATE || offset >= end_group)
548     return;
549
550
551   /* We can remove the LDX/LDY only when all bset/brclr instructions
552      of the relax group have been converted to use direct addressing
553      mode.  */
554   can_delete_ldx = 1;
555   while (offset < end_group)
556     {
557       unsigned isize;
558       unsigned new_value;
559       int bset_use_y;
560
561       bset_use_y = 0;
562       start_offset = offset;
563       code = bfd_get_8 (abfd, contents + offset);
564       if (code == 0x18)
565         {
566           bset_use_y++;
567           offset++;
568           code = bfd_get_8 (abfd, contents + offset);
569         }
570
571       /* Check the instruction and translate to use direct addressing mode.  */
572       switch (code)
573         {
574           /* bset */
575         case 0x1C:
576           code = 0x14;
577           isize = 3;
578           break;
579
580           /* brclr */
581         case 0x1F:
582           code = 0x13;
583           isize = 4;
584           break;
585
586           /* brset */
587         case 0x1E:
588           code = 0x12;
589           isize = 4;
590           break;
591
592           /* bclr */
593         case 0x1D:
594           code = 0x15;
595           isize = 3;
596           break;
597
598           /* This instruction is not recognized and we are not
599              at end of the relax group.  Ignore and don't remove
600              the first LDX (we don't know what it is used for...).  */
601         default:
602           return;
603         }
604       new_value = (unsigned) bfd_get_8 (abfd, contents + offset + 1);
605       new_value += value;
606       if ((new_value & 0xff00) == 0 && bset_use_y == relax_ldy)
607         {
608           bfd_put_8 (abfd, code, contents + offset);
609           bfd_put_8 (abfd, new_value, contents + offset + 1);
610           if (start_offset != offset)
611             {
612               m68hc11_elf_relax_delete_bytes (abfd, sec, start_offset,
613                                               offset - start_offset);
614               end_group--;
615             }
616         }
617       else
618         {
619           can_delete_ldx = 0;
620         }
621       offset = start_offset + isize;
622     }
623   if (can_delete_ldx)
624     {
625       /* Remove the move instruction (3 or 4 bytes win).  */
626       m68hc11_elf_relax_delete_bytes (abfd, sec, ldx_offset, ldx_size);
627     }
628 }
629
630 /* This function handles relaxing for the 68HC11.
631
632    
633         and somewhat more difficult to support.  */
634
635 static boolean
636 m68hc11_elf_relax_section (abfd, sec, link_info, again)
637      bfd *abfd;
638      asection *sec;
639      struct bfd_link_info *link_info;
640      boolean *again;
641 {
642   Elf_Internal_Shdr *symtab_hdr;
643   Elf_Internal_Shdr *shndx_hdr;
644   Elf_Internal_Rela *internal_relocs;
645   Elf_Internal_Rela *free_relocs = NULL;
646   Elf_Internal_Rela *irel, *irelend;
647   bfd_byte *contents = NULL;
648   bfd_byte *free_contents = NULL;
649   Elf32_External_Sym *free_extsyms = NULL;
650   Elf_Internal_Rela *prev_insn_branch = NULL;
651   Elf_Internal_Rela *prev_insn_group = NULL;
652   unsigned insn_group_value = 0;
653   Elf_Internal_Sym *isymbuf = NULL;
654
655   /* Assume nothing changes.  */
656   *again = false;
657
658   /* We don't have to do anything for a relocateable link, if
659      this section does not have relocs, or if this is not a
660      code section.  */
661   if (link_info->relocateable
662       || (sec->flags & SEC_RELOC) == 0
663       || sec->reloc_count == 0
664       || (sec->flags & SEC_CODE) == 0)
665     return true;
666
667   /* If this is the first time we have been called for this section,
668      initialize the cooked size.  */
669   if (sec->_cooked_size == 0)
670     sec->_cooked_size = sec->_raw_size;
671
672   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
673   shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
674
675   /* Get a copy of the native relocations.  */
676   internal_relocs = (_bfd_elf32_link_read_relocs
677                      (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
678                       link_info->keep_memory));
679   if (internal_relocs == NULL)
680     goto error_return;
681   if (! link_info->keep_memory)
682     free_relocs = internal_relocs;
683
684   /* Checking for branch relaxation relies on the relocations to
685      be sorted on 'r_offset'.  This is not guaranteed so we must sort.  */
686   qsort (internal_relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
687          compare_reloc);
688
689   /* Walk through them looking for relaxing opportunities.  */
690   irelend = internal_relocs + sec->reloc_count;
691   for (irel = internal_relocs; irel < irelend; irel++)
692     {
693       bfd_vma symval;
694       bfd_vma value;
695       Elf_Internal_Sym *isym;
696
697       /* If this isn't something that can be relaxed, then ignore
698          this reloc.  */
699       if (ELF32_R_TYPE (irel->r_info) != (int) R_M68HC11_16
700           && ELF32_R_TYPE (irel->r_info) != (int) R_M68HC11_RL_JUMP
701           && ELF32_R_TYPE (irel->r_info) != (int) R_M68HC11_RL_GROUP)
702         {
703           prev_insn_branch = 0;
704           prev_insn_group = 0;
705           continue;
706         }
707
708       /* Get the section contents if we haven't done so already.  */
709       if (contents == NULL)
710         {
711           /* Get cached copy if it exists.  */
712           if (elf_section_data (sec)->this_hdr.contents != NULL)
713             contents = elf_section_data (sec)->this_hdr.contents;
714           else
715             {
716               /* Go get them off disk.  */
717               contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
718               if (contents == NULL)
719                 goto error_return;
720               free_contents = contents;
721
722               if (! bfd_get_section_contents (abfd, sec, contents,
723                                               (file_ptr) 0, sec->_raw_size))
724                 goto error_return;
725             }
726         }
727
728       /* Try to eliminate an unconditional 8 bit pc-relative branch
729          which immediately follows a conditional 8 bit pc-relative
730          branch around the unconditional branch.
731
732             original:           new:
733             bCC lab1            bCC' lab2
734             bra lab2
735            lab1:               lab1:
736
737          This happens when the bCC can't reach lab2 at assembly time,
738          but due to other relaxations it can reach at link time.  */
739       if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_RL_JUMP)
740         {
741           Elf_Internal_Rela *nrel;
742           unsigned char code;
743           unsigned char roffset;
744
745           prev_insn_branch = 0;
746           prev_insn_group = 0;
747           
748           /* Do nothing if this reloc is the last byte in the section.  */
749           if (irel->r_offset == sec->_cooked_size)
750             continue;
751
752           /* See if the next instruction is an unconditional pc-relative
753              branch, more often than not this test will fail, so we
754              test it first to speed things up.  */
755           code = bfd_get_8 (abfd, contents + irel->r_offset + 2);
756           if (code != 0x7e)
757             continue;
758
759           /* Also make sure the next relocation applies to the next
760              instruction and that it's a pc-relative 8 bit branch.  */
761           nrel = irel + 1;
762           if (nrel == irelend
763               || irel->r_offset + 3 != nrel->r_offset
764               || ELF32_R_TYPE (nrel->r_info) != (int) R_M68HC11_16)
765             continue;
766
767           /* Make sure our destination immediately follows the
768              unconditional branch.  */
769           roffset = bfd_get_8 (abfd, contents + irel->r_offset + 1);
770           if (roffset != 3)
771             continue;
772
773           prev_insn_branch = irel;
774           prev_insn_group = 0;
775           continue;
776         }
777
778       /* Read this BFD's symbols if we haven't done so already.  */
779       if (isymbuf == NULL && symtab_hdr->sh_info != 0)
780         {
781           isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
782           if (isymbuf == NULL)
783             isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
784                                             symtab_hdr->sh_info, 0,
785                                             NULL, NULL, NULL);
786           if (isymbuf == NULL)
787             goto error_return;
788         }
789
790       /* Get the value of the symbol referred to by the reloc.  */
791       if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
792         {
793           /* A local symbol.  */
794           asection *sym_sec;
795
796           isym = isymbuf + ELF32_R_SYM (irel->r_info);
797           sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
798           symval = (isym->st_value
799                     + sym_sec->output_section->vma
800                     + sym_sec->output_offset);
801         }
802       else
803         {
804           unsigned long indx;
805           struct elf_link_hash_entry *h;
806
807           /* An external symbol.  */
808           indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
809           h = elf_sym_hashes (abfd)[indx];
810           BFD_ASSERT (h != NULL);
811           if (h->root.type != bfd_link_hash_defined
812               && h->root.type != bfd_link_hash_defweak)
813             {
814               /* This appears to be a reference to an undefined
815                  symbol.  Just ignore it--it will be caught by the
816                  regular reloc processing.  */
817               prev_insn_branch = 0;
818               prev_insn_group = 0;
819               continue;
820             }
821
822           symval = (h->root.u.def.value
823                     + h->root.u.def.section->output_section->vma
824                     + h->root.u.def.section->output_offset);
825         }
826
827       if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_RL_GROUP)
828         {
829           prev_insn_branch = 0;
830           prev_insn_group = 0;
831           
832           /* Do nothing if this reloc is the last byte in the section.  */
833           if (irel->r_offset == sec->_cooked_size)
834             continue;
835
836           prev_insn_group = irel;
837           insn_group_value = isym->st_value;
838           continue;
839         }
840
841       value = symval;
842       /* Try to turn a far branch to a near branch.  */
843       if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_16
844           && prev_insn_branch)
845         {
846           bfd_vma offset;
847           unsigned char code;
848
849           offset = value - (prev_insn_branch->r_offset
850                             + sec->output_section->vma
851                             + sec->output_offset + 2);
852
853           /* If the offset is still out of -128..+127 range,
854              leave that far branch unchanged.  */
855           if ((offset & 0xff80) != 0 && (offset & 0xff80) != 0xff80)
856             {
857               prev_insn_branch = 0;
858               continue;
859             }
860
861           /* Shrink the branch.  */
862           code = bfd_get_8 (abfd, contents + prev_insn_branch->r_offset);
863           if (code == 0x7e)
864             {
865               code = 0x20;
866               bfd_put_8 (abfd, code, contents + prev_insn_branch->r_offset);
867               bfd_put_8 (abfd, offset,
868                          contents + prev_insn_branch->r_offset + 1);
869               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
870                                            R_M68HC11_NONE);
871               m68hc11_elf_relax_delete_bytes (abfd, sec,
872                                               irel->r_offset, 1);
873             }
874           else
875             {
876               code ^= 0x1;
877               bfd_put_8 (abfd, code, contents + prev_insn_branch->r_offset);
878               bfd_put_8 (abfd, offset,
879                          contents + prev_insn_branch->r_offset + 1);
880               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
881                                            R_M68HC11_NONE);
882               m68hc11_elf_relax_delete_bytes (abfd, sec,
883                                               irel->r_offset - 1, 3);
884             }
885           prev_insn_branch = 0;
886         }
887
888       /* Try to turn a 16 bit address into a 8 bit page0 address.  */
889       else if (ELF32_R_TYPE (irel->r_info) == (int) R_M68HC11_16
890                && (value & 0xff00) == 0)
891         {
892           unsigned char code;
893           unsigned short offset;
894           struct m68hc11_direct_relax *rinfo;
895
896           prev_insn_branch = 0;
897           offset = bfd_get_16 (abfd, contents + irel->r_offset);
898           offset += value;
899           if ((offset & 0xff00) != 0)
900             {
901               prev_insn_group = 0;
902               continue;
903             }
904
905           if (prev_insn_group)
906             {
907               /* Note that we've changed the reldection contents, etc.  */
908               elf_section_data (sec)->relocs = internal_relocs;
909               free_relocs = NULL;
910
911               elf_section_data (sec)->this_hdr.contents = contents;
912               free_contents = NULL;
913
914               symtab_hdr->contents = (bfd_byte *) isymbuf;
915               free_extsyms = NULL;
916
917               m68hc11_relax_group (abfd, sec, contents, offset,
918                                    prev_insn_group->r_offset,
919                                    insn_group_value);
920               irel = prev_insn_group;
921               prev_insn_group = 0;
922               irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
923                                            R_M68HC11_NONE);
924               continue;
925             }
926           
927           /* Get the opcode.  */
928           code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
929           rinfo = find_relaxable_insn (code);
930           if (rinfo == 0)
931             {
932               prev_insn_group = 0;
933               continue;
934             }
935
936           /* Note that we've changed the reldection contents, etc.  */
937           elf_section_data (sec)->relocs = internal_relocs;
938           free_relocs = NULL;
939
940           elf_section_data (sec)->this_hdr.contents = contents;
941           free_contents = NULL;
942
943           symtab_hdr->contents = (bfd_byte *) isymbuf;
944           free_extsyms = NULL;
945
946           /* Fix the opcode.  */
947           /* printf ("A relaxable case : 0x%02x (%s)\n",
948              code, rinfo->name); */
949           bfd_put_8 (abfd, rinfo->direct_code,
950                      contents + irel->r_offset - 1);
951
952           /* Delete one byte of data (upper byte of address).  */
953           m68hc11_elf_relax_delete_bytes (abfd, sec, irel->r_offset, 1);
954
955           /* Fix the relocation's type.  */
956           irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
957                                        R_M68HC11_8);
958
959           /* That will change things, so, we should relax again.
960              Note that this is not required, and it may be slow.  */
961           *again = true;
962         }
963       else if (ELF32_R_TYPE (irel->r_info) == R_M68HC11_16)
964         {
965           unsigned char code;
966           bfd_vma offset;
967
968           prev_insn_branch = 0;
969           code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
970           if (code == 0x7e)
971             {
972               offset = value - (irel->r_offset
973                                 + sec->output_section->vma
974                                 + sec->output_offset + 1);
975               offset += bfd_get_16 (abfd, contents + irel->r_offset);
976
977               /* If the offset is still out of -128..+127 range,
978                  leave that far branch unchanged.  */
979               if ((offset & 0xff80) == 0 || (offset & 0xff80) == 0xff80)
980                 {
981
982                   /* Note that we've changed the reldection contents, etc.  */
983                   elf_section_data (sec)->relocs = internal_relocs;
984                   free_relocs = NULL;
985                   
986                   elf_section_data (sec)->this_hdr.contents = contents;
987                   free_contents = NULL;
988                   
989                   symtab_hdr->contents = (bfd_byte *) isymbuf;
990                   free_extsyms = NULL;
991
992                   /* Shrink the branch.  */
993                   code = 0x20;
994                   bfd_put_8 (abfd, code,
995                              contents + irel->r_offset - 1);
996                   bfd_put_8 (abfd, offset,
997                              contents + irel->r_offset);
998                   irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
999                                                R_M68HC11_NONE);
1000                   m68hc11_elf_relax_delete_bytes (abfd, sec,
1001                                                   irel->r_offset + 1, 1);
1002                 }
1003             }
1004         }
1005       prev_insn_branch = 0;
1006     }
1007
1008   if (free_relocs != NULL)
1009     {
1010       free (free_relocs);
1011       free_relocs = NULL;
1012     }
1013
1014   if (free_contents != NULL)
1015     {
1016       if (! link_info->keep_memory)
1017         free (free_contents);
1018       else
1019         {
1020           /* Cache the section contents for elf_link_input_bfd.  */
1021           elf_section_data (sec)->this_hdr.contents = contents;
1022         }
1023       free_contents = NULL;
1024     }
1025
1026   if (free_extsyms != NULL)
1027     {
1028       if (! link_info->keep_memory)
1029         free (free_extsyms);
1030       else
1031         {
1032           /* Cache the symbols for elf_link_input_bfd.  */
1033           symtab_hdr->contents = (unsigned char *) isymbuf;
1034         }
1035       free_extsyms = NULL;
1036     }
1037
1038   return true;
1039
1040  error_return:
1041   if (free_relocs != NULL)
1042     free (free_relocs);
1043   if (free_contents != NULL)
1044     free (free_contents);
1045   if (free_extsyms != NULL)
1046     free (free_extsyms);
1047   return false;
1048 }
1049
1050 /* Delete some bytes from a section while relaxing.  */
1051
1052 static void
1053 m68hc11_elf_relax_delete_bytes (abfd, sec, addr, count)
1054      bfd *abfd;
1055      asection *sec;
1056      bfd_vma addr;
1057      int count;
1058 {
1059   Elf_Internal_Shdr *symtab_hdr;
1060   unsigned int sec_shndx;
1061   bfd_byte *contents;
1062   Elf_Internal_Rela *irel, *irelend;
1063   bfd_vma toaddr;
1064   Elf_Internal_Sym *isymbuf, *isym, *isymend;
1065   struct elf_link_hash_entry **sym_hashes;
1066   struct elf_link_hash_entry **end_hashes;
1067   unsigned int symcount;
1068
1069   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1070   isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1071
1072   sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1073
1074   contents = elf_section_data (sec)->this_hdr.contents;
1075
1076   toaddr = sec->_cooked_size;
1077
1078   irel = elf_section_data (sec)->relocs;
1079   irelend = irel + sec->reloc_count;
1080
1081   /* Actually delete the bytes.  */
1082   memmove (contents + addr, contents + addr + count,
1083            (size_t) (toaddr - addr - count));
1084
1085   sec->_cooked_size -= count;
1086   
1087   /* Adjust all the relocs.  */
1088   for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1089     {
1090       unsigned char code;
1091       unsigned char offset;
1092       unsigned short raddr;
1093       unsigned long old_offset;
1094       int branch_pos;
1095
1096       old_offset = irel->r_offset;
1097
1098       /* See if this reloc was for the bytes we have deleted, in which
1099          case we no longer care about it.  Don't delete relocs which
1100          represent addresses, though.  */
1101       if (ELF32_R_TYPE (irel->r_info) != R_M68HC11_RL_JUMP
1102           && irel->r_offset >= addr && irel->r_offset < addr + count)
1103         irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1104                                      R_M68HC11_NONE);
1105
1106       if (ELF32_R_TYPE (irel->r_info) == R_M68HC11_NONE)
1107         continue;
1108
1109       /* Get the new reloc address.  */
1110       if ((irel->r_offset > addr
1111            && irel->r_offset < toaddr))
1112         irel->r_offset -= count;
1113
1114       /* If this is a PC relative reloc, see if the range it covers
1115          includes the bytes we have deleted.  */
1116       switch (ELF32_R_TYPE (irel->r_info))
1117         {
1118         default:
1119           break;
1120
1121         case R_M68HC11_RL_JUMP:
1122           code = bfd_get_8 (abfd, contents + irel->r_offset);
1123           switch (code)
1124             {
1125               /* jsr and jmp instruction are also marked with RL_JUMP
1126                  relocs but no adjustment must be made.  */
1127             case 0x7e:
1128             case 0x9d:
1129             case 0xbd:
1130               continue;
1131
1132             case 0x12:
1133             case 0x13:
1134               branch_pos = 3;
1135               raddr = 4;
1136
1137               /* Special case when we translate a brclr N,y into brclr *<addr>
1138                  In this case, the 0x18 page2 prefix is removed.
1139                  The reloc offset is not modified but the instruction
1140                  size is reduced by 1.  */
1141               if (old_offset == addr)
1142                 raddr++;
1143               break;
1144
1145             case 0x1e:
1146             case 0x1f:
1147               branch_pos = 3;
1148               raddr = 4;
1149               break;
1150
1151             case 0x18:
1152               branch_pos = 4;
1153               raddr = 5;
1154               break;
1155
1156             default:
1157               branch_pos = 1;
1158               raddr = 2;
1159               break;
1160             }
1161           offset = bfd_get_8 (abfd, contents + irel->r_offset + branch_pos);
1162           raddr += old_offset;
1163           raddr += ((unsigned short) offset | ((offset & 0x80) ? 0xff00 : 0));
1164           if (irel->r_offset < addr && raddr >= addr)
1165             {
1166               offset -= count;
1167               bfd_put_8 (abfd, offset, contents + irel->r_offset + branch_pos);
1168             }
1169           else if (irel->r_offset >= addr && raddr <= addr)
1170             {
1171               offset += count;
1172               bfd_put_8 (abfd, offset, contents + irel->r_offset + branch_pos);
1173             }
1174           else
1175             {
1176               /*printf ("Not adjusted 0x%04x [0x%4x 0x%4x]\n", raddr,
1177                 irel->r_offset, addr);*/
1178             }
1179           
1180           break;
1181         }
1182     }
1183
1184   /* Adjust the local symbols defined in this section.  */
1185   isymend = isymbuf + symtab_hdr->sh_info;
1186   for (isym = isymbuf; isym < isymend; isym++)
1187     {
1188       if (isym->st_shndx == sec_shndx
1189           && isym->st_value > addr
1190           && isym->st_value < toaddr)
1191         isym->st_value -= count;
1192     }
1193
1194   /* Now adjust the global symbols defined in this section.  */
1195   symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1196               - symtab_hdr->sh_info);
1197   sym_hashes = elf_sym_hashes (abfd);
1198   end_hashes = sym_hashes + symcount;
1199   for (; sym_hashes < end_hashes; sym_hashes++)
1200     {
1201       struct elf_link_hash_entry *sym_hash = *sym_hashes;
1202       if ((sym_hash->root.type == bfd_link_hash_defined
1203            || sym_hash->root.type == bfd_link_hash_defweak)
1204           && sym_hash->root.u.def.section == sec
1205           && sym_hash->root.u.def.value > addr
1206           && sym_hash->root.u.def.value < toaddr)
1207         {
1208           sym_hash->root.u.def.value -= count;
1209         }
1210     }
1211 }
1212
1213 /* Look through the relocs for a section during the first phase.
1214    Since we don't do .gots or .plts, we just need to consider the
1215    virtual table relocs for gc.  */
1216
1217 static boolean
1218 elf32_m68hc11_check_relocs (abfd, info, sec, relocs)
1219      bfd * abfd;
1220      struct bfd_link_info * info;
1221      asection * sec;
1222      const Elf_Internal_Rela * relocs;
1223 {
1224   Elf_Internal_Shdr *           symtab_hdr;
1225   struct elf_link_hash_entry ** sym_hashes;
1226   struct elf_link_hash_entry ** sym_hashes_end;
1227   const Elf_Internal_Rela *     rel;
1228   const Elf_Internal_Rela *     rel_end;
1229
1230   if (info->relocateable)
1231     return true;
1232
1233   symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
1234   sym_hashes = elf_sym_hashes (abfd);
1235   sym_hashes_end = sym_hashes + symtab_hdr->sh_size / sizeof (Elf32_External_Sym);
1236   if (!elf_bad_symtab (abfd))
1237     sym_hashes_end -= symtab_hdr->sh_info;
1238
1239   rel_end = relocs + sec->reloc_count;
1240
1241   for (rel = relocs; rel < rel_end; rel++)
1242     {
1243       struct elf_link_hash_entry * h;
1244       unsigned long r_symndx;
1245
1246       r_symndx = ELF32_R_SYM (rel->r_info);
1247
1248       if (r_symndx < symtab_hdr->sh_info)
1249         h = NULL;
1250       else
1251         h = sym_hashes [r_symndx - symtab_hdr->sh_info];
1252
1253       switch (ELF32_R_TYPE (rel->r_info))
1254         {
1255         /* This relocation describes the C++ object vtable hierarchy.
1256            Reconstruct it for later use during GC.  */
1257         case R_M68HC11_GNU_VTINHERIT:
1258           if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
1259             return false;
1260           break;
1261
1262         /* This relocation describes which C++ vtable entries are actually
1263            used.  Record for later use during GC.  */
1264         case R_M68HC11_GNU_VTENTRY:
1265           if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
1266             return false;
1267           break;
1268         }
1269     }
1270
1271   return true;
1272 }
1273
1274 /* Relocate a 68hc11/68hc12 ELF section.  */
1275 static boolean
1276 elf32_m68hc11_relocate_section (output_bfd, info, input_bfd, input_section,
1277                                 contents, relocs, local_syms, local_sections)
1278      bfd *output_bfd ATTRIBUTE_UNUSED;
1279      struct bfd_link_info *info;
1280      bfd *input_bfd;
1281      asection *input_section;
1282      bfd_byte *contents;
1283      Elf_Internal_Rela *relocs;
1284      Elf_Internal_Sym *local_syms;
1285      asection **local_sections;
1286 {
1287   Elf_Internal_Shdr *symtab_hdr;
1288   struct elf_link_hash_entry **sym_hashes;
1289   Elf_Internal_Rela *rel, *relend;
1290   const char *name;
1291
1292   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1293   sym_hashes = elf_sym_hashes (input_bfd);
1294
1295   rel = relocs;
1296   relend = relocs + input_section->reloc_count;
1297   for (; rel < relend; rel++)
1298     {
1299       int r_type;
1300       reloc_howto_type *howto;
1301       unsigned long r_symndx;
1302       Elf_Internal_Sym *sym;
1303       asection *sec;
1304       struct elf_link_hash_entry *h;
1305       bfd_vma relocation;
1306       bfd_reloc_status_type r;
1307
1308       r_symndx = ELF32_R_SYM (rel->r_info);
1309       r_type = ELF32_R_TYPE (rel->r_info);
1310
1311       if (r_type == R_M68HC11_GNU_VTENTRY
1312           || r_type == R_M68HC11_GNU_VTINHERIT )
1313         continue;
1314
1315       howto = elf_m68hc11_howto_table + r_type;
1316
1317       if (info->relocateable)
1318         {
1319           /* This is a relocateable link.  We don't have to change
1320              anything, unless the reloc is against a section symbol,
1321              in which case we have to adjust according to where the
1322              section symbol winds up in the output section.  */
1323           if (r_symndx < symtab_hdr->sh_info)
1324             {
1325               sym = local_syms + r_symndx;
1326               if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
1327                 {
1328                   sec = local_sections[r_symndx];
1329                   rel->r_addend += sec->output_offset + sym->st_value;
1330                 }
1331             }
1332
1333           continue;
1334         }
1335
1336       /* This is a final link.  */
1337       h = NULL;
1338       sym = NULL;
1339       sec = NULL;
1340       if (r_symndx < symtab_hdr->sh_info)
1341         {
1342           sym = local_syms + r_symndx;
1343           sec = local_sections[r_symndx];
1344           relocation = (sec->output_section->vma
1345                         + sec->output_offset
1346                         + sym->st_value);
1347         }
1348       else
1349         {
1350           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
1351           while (h->root.type == bfd_link_hash_indirect
1352                  || h->root.type == bfd_link_hash_warning)
1353             h = (struct elf_link_hash_entry *) h->root.u.i.link;
1354           if (h->root.type == bfd_link_hash_defined
1355               || h->root.type == bfd_link_hash_defweak)
1356             {
1357               sec = h->root.u.def.section;
1358               relocation = (h->root.u.def.value
1359                             + sec->output_section->vma
1360                             + sec->output_offset);
1361             }
1362           else if (h->root.type == bfd_link_hash_undefweak)
1363             relocation = 0;
1364           else
1365             {
1366               if (!((*info->callbacks->undefined_symbol)
1367                     (info, h->root.root.string, input_bfd,
1368                      input_section, rel->r_offset, true)))
1369                 return false;
1370               relocation = 0;
1371             }
1372         }
1373
1374       if (h != NULL)
1375         name = h->root.root.string;
1376       else
1377         {
1378           name = (bfd_elf_string_from_elf_section
1379                   (input_bfd, symtab_hdr->sh_link, sym->st_name));
1380           if (name == NULL || *name == '\0')
1381             name = bfd_section_name (input_bfd, sec);
1382         }
1383
1384       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
1385                                     contents, rel->r_offset,
1386                                     relocation, rel->r_addend);
1387
1388       if (r != bfd_reloc_ok)
1389         {
1390           const char * msg = (const char *) 0;
1391
1392           switch (r)
1393             {
1394             case bfd_reloc_overflow:
1395               if (!((*info->callbacks->reloc_overflow)
1396                     (info, name, howto->name, (bfd_vma) 0,
1397                      input_bfd, input_section, rel->r_offset)))
1398                 return false;
1399               break;
1400
1401             case bfd_reloc_undefined:
1402               if (!((*info->callbacks->undefined_symbol)
1403                     (info, name, input_bfd, input_section,
1404                      rel->r_offset, true)))
1405                 return false;
1406               break;
1407
1408             case bfd_reloc_outofrange:
1409               msg = _ ("internal error: out of range error");
1410               goto common_error;
1411
1412             case bfd_reloc_notsupported:
1413               msg = _ ("internal error: unsupported relocation error");
1414               goto common_error;
1415
1416             case bfd_reloc_dangerous:
1417               msg = _ ("internal error: dangerous error");
1418               goto common_error;
1419
1420             default:
1421               msg = _ ("internal error: unknown error");
1422               /* fall through */
1423
1424             common_error:
1425               if (!((*info->callbacks->warning)
1426                     (info, msg, name, input_bfd, input_section,
1427                      rel->r_offset)))
1428                 return false;
1429               break;
1430             }
1431         }
1432     }
1433
1434   return true;
1435 }
1436
1437
1438 \f
1439 /* Set and control ELF flags in ELF header.  */
1440
1441 boolean
1442 _bfd_m68hc11_elf_set_private_flags (abfd, flags)
1443      bfd *abfd;
1444      flagword flags;
1445 {
1446   BFD_ASSERT (!elf_flags_init (abfd)
1447               || elf_elfheader (abfd)->e_flags == flags);
1448
1449   elf_elfheader (abfd)->e_flags = flags;
1450   elf_flags_init (abfd) = true;
1451   return true;
1452 }
1453
1454 /* Merge backend specific data from an object file to the output
1455    object file when linking.  */
1456
1457 boolean
1458 _bfd_m68hc11_elf_merge_private_bfd_data (ibfd, obfd)
1459      bfd *ibfd;
1460      bfd *obfd;
1461 {
1462   flagword old_flags;
1463   flagword new_flags;
1464   boolean ok = true;
1465
1466   /* Check if we have the same endianess */
1467   if (_bfd_generic_verify_endian_match (ibfd, obfd) == false)
1468     return false;
1469
1470   if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
1471       || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
1472     return true;
1473
1474   new_flags = elf_elfheader (ibfd)->e_flags;
1475   elf_elfheader (obfd)->e_flags |= new_flags & EF_M68HC11_ABI;
1476   old_flags = elf_elfheader (obfd)->e_flags;
1477
1478   if (! elf_flags_init (obfd))
1479     {
1480       elf_flags_init (obfd) = true;
1481       elf_elfheader (obfd)->e_flags = new_flags;
1482       elf_elfheader (obfd)->e_ident[EI_CLASS]
1483         = elf_elfheader (ibfd)->e_ident[EI_CLASS];
1484
1485       if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
1486           && bfd_get_arch_info (obfd)->the_default)
1487         {
1488           if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
1489                                    bfd_get_mach (ibfd)))
1490             return false;
1491         }
1492
1493       return true;
1494     }
1495
1496   /* Check ABI compatibility.  */
1497   if ((new_flags & E_M68HC11_I32) != (old_flags & E_M68HC11_I32))
1498     {
1499       (*_bfd_error_handler)
1500         (_("%s: linking files compiled for 16-bit integers (-mshort) "
1501            "and others for 32-bit integers"),
1502          bfd_archive_filename (ibfd));
1503       ok = false;
1504     }
1505   if ((new_flags & E_M68HC11_F64) != (old_flags & E_M68HC11_F64))
1506     {
1507       (*_bfd_error_handler)
1508         (_("%s: linking files compiled for 32-bit double (-fshort-double) "
1509            "and others for 64-bit double"),
1510          bfd_archive_filename (ibfd));
1511       ok = false;
1512     }
1513   new_flags &= ~EF_M68HC11_ABI;
1514   old_flags &= ~EF_M68HC11_ABI;
1515
1516   /* Warn about any other mismatches */
1517   if (new_flags != old_flags)
1518     {
1519       (*_bfd_error_handler)
1520         (_("%s: uses different e_flags (0x%lx) fields than previous modules (0x%lx)"),
1521          bfd_archive_filename (ibfd), (unsigned long) new_flags,
1522          (unsigned long) old_flags);
1523       ok = false;
1524     }
1525
1526   if (! ok)
1527     {
1528       bfd_set_error (bfd_error_bad_value);
1529       return false;
1530     }
1531
1532   return true;
1533 }
1534
1535 boolean
1536 _bfd_m68hc11_elf_print_private_bfd_data (abfd, ptr)
1537      bfd *abfd;
1538      PTR ptr;
1539 {
1540   FILE *file = (FILE *) ptr;
1541
1542   BFD_ASSERT (abfd != NULL && ptr != NULL);
1543
1544   /* Print normal ELF private data.  */
1545   _bfd_elf_print_private_bfd_data (abfd, ptr);
1546
1547   /* xgettext:c-format */
1548   fprintf (file, _("private flags = %lx:"), elf_elfheader (abfd)->e_flags);
1549
1550   if (elf_elfheader (abfd)->e_flags & E_M68HC11_I32)
1551     fprintf (file, _("[abi=32-bit int,"));
1552   else
1553     fprintf (file, _("[abi=16-bit int,"));
1554
1555   if (elf_elfheader (abfd)->e_flags & E_M68HC11_F64)
1556     fprintf (file, _(" 64-bit double]"));
1557   else
1558     fprintf (file, _(" 32-bit double]"));
1559
1560   if (elf_elfheader (abfd)->e_flags & E_M68HC12_BANKS)
1561     fprintf (file, _(" [memory=bank-model]"));
1562   else
1563     fprintf (file, _(" [memory=flat]"));
1564
1565   fputc ('\n', file);
1566
1567   return true;
1568 }
1569
1570 /* Below is the only difference between elf32-m68hc12.c and elf32-m68hc11.c.
1571    The Motorola spec says to use a different Elf machine code.  */
1572 #define ELF_ARCH                bfd_arch_m68hc11
1573 #define ELF_MACHINE_CODE        EM_68HC11
1574 #define ELF_MAXPAGESIZE         0x1000
1575
1576 #define TARGET_BIG_SYM          bfd_elf32_m68hc11_vec
1577 #define TARGET_BIG_NAME         "elf32-m68hc11"
1578
1579 #define elf_info_to_howto       0
1580 #define elf_info_to_howto_rel   m68hc11_info_to_howto_rel
1581 #define bfd_elf32_bfd_relax_section  m68hc11_elf_relax_section
1582 #define elf_backend_gc_mark_hook     elf32_m68hc11_gc_mark_hook
1583 #define elf_backend_gc_sweep_hook    elf32_m68hc11_gc_sweep_hook
1584 #define elf_backend_check_relocs     elf32_m68hc11_check_relocs
1585 #define elf_backend_relocate_section elf32_m68hc11_relocate_section
1586 #define elf_backend_object_p    0
1587 #define elf_backend_final_write_processing      0
1588 #define elf_backend_can_gc_sections             1
1589 #define bfd_elf32_bfd_merge_private_bfd_data \
1590                                         _bfd_m68hc11_elf_merge_private_bfd_data
1591 #define bfd_elf32_bfd_set_private_flags _bfd_m68hc11_elf_set_private_flags
1592 #define bfd_elf32_bfd_print_private_bfd_data \
1593                                         _bfd_m68hc11_elf_print_private_bfd_data
1594
1595 #include "elf32-target.h"