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