920885709024bdd45f1724fef9d8125baf93e67f
[deliverable/binutils-gdb.git] / bfd / elf-m10300.c
1 /* Matsushita 10300 specific support for 32-bit ELF
2 Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 This file is part of BFD, the Binary File Descriptor library.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
19
20 #include "bfd.h"
21 #include "sysdep.h"
22 #include "libbfd.h"
23 #include "elf-bfd.h"
24 #include "elf/mn10300.h"
25
26 struct elf32_mn10300_link_hash_entry
27 {
28 /* The basic elf link hash table entry. */
29 struct elf_link_hash_entry root;
30
31 /* For function symbols, the number of times this function is
32 called directly (ie by name). */
33 unsigned int direct_calls;
34
35 /* For function symbols, the size of this function's stack
36 (if <= 255 bytes). We stuff this into "call" instructions
37 to this target when it's valid and profitable to do so.
38
39 This does not include stack allocated by movm! */
40 unsigned char stack_size;
41
42 /* For function symbols, arguments (if any) for movm instruction
43 in the prologue. We stuff this value into "call" instructions
44 to the target when it's valid and profitable to do so. */
45 unsigned char movm_args;
46
47 /* For funtion symbols, the amount of stack space that would be allocated
48 by the movm instruction. This is redundant with movm_args, but we
49 add it to the hash table to avoid computing it over and over. */
50 unsigned char movm_stack_size;
51
52 /* When set, convert all "call" instructions to this target into "calls"
53 instructions. */
54 #define MN10300_CONVERT_CALL_TO_CALLS 0x1
55
56 /* Used to mark functions which have had redundant parts of their
57 prologue deleted. */
58 #define MN10300_DELETED_PROLOGUE_BYTES 0x2
59 unsigned char flags;
60 };
61
62 /* We derive a hash table from the main elf linker hash table so
63 we can store state variables and a secondary hash table without
64 resorting to global variables. */
65 struct elf32_mn10300_link_hash_table
66 {
67 /* The main hash table. */
68 struct elf_link_hash_table root;
69
70 /* A hash table for static functions. We could derive a new hash table
71 instead of using the full elf32_mn10300_link_hash_table if we wanted
72 to save some memory. */
73 struct elf32_mn10300_link_hash_table *static_hash_table;
74
75 /* Random linker state flags. */
76 #define MN10300_HASH_ENTRIES_INITIALIZED 0x1
77 char flags;
78 };
79
80 /* For MN10300 linker hash table. */
81
82 /* Get the MN10300 ELF linker hash table from a link_info structure. */
83
84 #define elf32_mn10300_hash_table(p) \
85 ((struct elf32_mn10300_link_hash_table *) ((p)->hash))
86
87 #define elf32_mn10300_link_hash_traverse(table, func, info) \
88 (elf_link_hash_traverse \
89 (&(table)->root, \
90 (boolean (*) PARAMS ((struct elf_link_hash_entry *, PTR))) (func), \
91 (info)))
92
93 static struct bfd_hash_entry *elf32_mn10300_link_hash_newfunc
94 PARAMS ((struct bfd_hash_entry *, struct bfd_hash_table *, const char *));
95 static struct bfd_link_hash_table *elf32_mn10300_link_hash_table_create
96 PARAMS ((bfd *));
97
98 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
99 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
100 static void mn10300_info_to_howto
101 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
102 static boolean mn10300_elf_check_relocs
103 PARAMS ((bfd *, struct bfd_link_info *, asection *,
104 const Elf_Internal_Rela *));
105 static asection *mn10300_elf_gc_mark_hook
106 PARAMS ((bfd *, struct bfd_link_info *info, Elf_Internal_Rela *,
107 struct elf_link_hash_entry *, Elf_Internal_Sym *));
108 static boolean mn10300_elf_relax_delete_bytes
109 PARAMS ((bfd *, asection *, bfd_vma, int));
110 static boolean mn10300_elf_symbol_address_p
111 PARAMS ((bfd *, asection *, Elf32_External_Sym *, bfd_vma));
112 static boolean elf32_mn10300_finish_hash_table_entry
113 PARAMS ((struct bfd_hash_entry *, PTR));
114 static void compute_function_info
115 PARAMS ((bfd *, struct elf32_mn10300_link_hash_entry *,
116 bfd_vma, unsigned char *));
117
118 /* We have to use RELA instructions since md_apply_fix3 in the assembler
119 does absolutely nothing. */
120 #define USE_RELA
121
122 static reloc_howto_type elf_mn10300_howto_table[] =
123 {
124 /* Dummy relocation. Does nothing. */
125 HOWTO (R_MN10300_NONE,
126 0,
127 2,
128 16,
129 false,
130 0,
131 complain_overflow_bitfield,
132 bfd_elf_generic_reloc,
133 "R_MN10300_NONE",
134 false,
135 0,
136 0,
137 false),
138 /* Standard 32 bit reloc. */
139 HOWTO (R_MN10300_32,
140 0,
141 2,
142 32,
143 false,
144 0,
145 complain_overflow_bitfield,
146 bfd_elf_generic_reloc,
147 "R_MN10300_32",
148 false,
149 0xffffffff,
150 0xffffffff,
151 false),
152 /* Standard 16 bit reloc. */
153 HOWTO (R_MN10300_16,
154 0,
155 1,
156 16,
157 false,
158 0,
159 complain_overflow_bitfield,
160 bfd_elf_generic_reloc,
161 "R_MN10300_16",
162 false,
163 0xffff,
164 0xffff,
165 false),
166 /* Standard 8 bit reloc. */
167 HOWTO (R_MN10300_8,
168 0,
169 0,
170 8,
171 false,
172 0,
173 complain_overflow_bitfield,
174 bfd_elf_generic_reloc,
175 "R_MN10300_8",
176 false,
177 0xff,
178 0xff,
179 false),
180 /* Standard 32bit pc-relative reloc. */
181 HOWTO (R_MN10300_PCREL32,
182 0,
183 2,
184 32,
185 true,
186 0,
187 complain_overflow_bitfield,
188 bfd_elf_generic_reloc,
189 "R_MN10300_PCREL32",
190 false,
191 0xffffffff,
192 0xffffffff,
193 true),
194 /* Standard 16bit pc-relative reloc. */
195 HOWTO (R_MN10300_PCREL16,
196 0,
197 1,
198 16,
199 true,
200 0,
201 complain_overflow_bitfield,
202 bfd_elf_generic_reloc,
203 "R_MN10300_PCREL16",
204 false,
205 0xffff,
206 0xffff,
207 true),
208 /* Standard 8 pc-relative reloc. */
209 HOWTO (R_MN10300_PCREL8,
210 0,
211 0,
212 8,
213 true,
214 0,
215 complain_overflow_bitfield,
216 bfd_elf_generic_reloc,
217 "R_MN10300_PCREL8",
218 false,
219 0xff,
220 0xff,
221 true),
222
223 /* GNU extension to record C++ vtable hierarchy */
224 HOWTO (R_MN10300_GNU_VTINHERIT, /* type */
225 0, /* rightshift */
226 0, /* size (0 = byte, 1 = short, 2 = long) */
227 0, /* bitsize */
228 false, /* pc_relative */
229 0, /* bitpos */
230 complain_overflow_dont, /* complain_on_overflow */
231 NULL, /* special_function */
232 "R_MN10300_GNU_VTINHERIT", /* name */
233 false, /* partial_inplace */
234 0, /* src_mask */
235 0, /* dst_mask */
236 false), /* pcrel_offset */
237
238 /* GNU extension to record C++ vtable member usage */
239 HOWTO (R_MN10300_GNU_VTENTRY, /* type */
240 0, /* rightshift */
241 0, /* size (0 = byte, 1 = short, 2 = long) */
242 0, /* bitsize */
243 false, /* pc_relative */
244 0, /* bitpos */
245 complain_overflow_dont, /* complain_on_overflow */
246 NULL, /* special_function */
247 "R_MN10300_GNU_VTENTRY", /* name */
248 false, /* partial_inplace */
249 0, /* src_mask */
250 0, /* dst_mask */
251 false), /* pcrel_offset */
252
253 /* Standard 24 bit reloc. */
254 HOWTO (R_MN10300_24,
255 0,
256 2,
257 24,
258 false,
259 0,
260 complain_overflow_bitfield,
261 bfd_elf_generic_reloc,
262 "R_MN10300_24",
263 false,
264 0xffffff,
265 0xffffff,
266 false),
267
268 };
269
270 struct mn10300_reloc_map
271 {
272 bfd_reloc_code_real_type bfd_reloc_val;
273 unsigned char elf_reloc_val;
274 };
275
276 static const struct mn10300_reloc_map mn10300_reloc_map[] =
277 {
278 { BFD_RELOC_NONE, R_MN10300_NONE, },
279 { BFD_RELOC_32, R_MN10300_32, },
280 { BFD_RELOC_16, R_MN10300_16, },
281 { BFD_RELOC_8, R_MN10300_8, },
282 { BFD_RELOC_32_PCREL, R_MN10300_PCREL32, },
283 { BFD_RELOC_16_PCREL, R_MN10300_PCREL16, },
284 { BFD_RELOC_8_PCREL, R_MN10300_PCREL8, },
285 { BFD_RELOC_24, R_MN10300_24, },
286 { BFD_RELOC_VTABLE_INHERIT, R_MN10300_GNU_VTINHERIT },
287 { BFD_RELOC_VTABLE_ENTRY, R_MN10300_GNU_VTENTRY },
288 };
289
290 static reloc_howto_type *
291 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
292 bfd *abfd ATTRIBUTE_UNUSED;
293 bfd_reloc_code_real_type code;
294 {
295 unsigned int i;
296
297 for (i = 0;
298 i < sizeof (mn10300_reloc_map) / sizeof (struct mn10300_reloc_map);
299 i++)
300 {
301 if (mn10300_reloc_map[i].bfd_reloc_val == code)
302 return &elf_mn10300_howto_table[mn10300_reloc_map[i].elf_reloc_val];
303 }
304
305 return NULL;
306 }
307
308 /* Set the howto pointer for an MN10300 ELF reloc. */
309
310 static void
311 mn10300_info_to_howto (abfd, cache_ptr, dst)
312 bfd *abfd ATTRIBUTE_UNUSED;
313 arelent *cache_ptr;
314 Elf32_Internal_Rela *dst;
315 {
316 unsigned int r_type;
317
318 r_type = ELF32_R_TYPE (dst->r_info);
319 BFD_ASSERT (r_type < (unsigned int) R_MN10300_MAX);
320 cache_ptr->howto = &elf_mn10300_howto_table[r_type];
321 }
322
323 /* Look through the relocs for a section during the first phase.
324 Since we don't do .gots or .plts, we just need to consider the
325 virtual table relocs for gc. */
326
327 static boolean
328 mn10300_elf_check_relocs (abfd, info, sec, relocs)
329 bfd *abfd;
330 struct bfd_link_info *info;
331 asection *sec;
332 const Elf_Internal_Rela *relocs;
333 {
334 Elf_Internal_Shdr *symtab_hdr;
335 struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
336 const Elf_Internal_Rela *rel;
337 const Elf_Internal_Rela *rel_end;
338
339 if (info->relocateable)
340 return true;
341
342 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
343 sym_hashes = elf_sym_hashes (abfd);
344 sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
345 if (!elf_bad_symtab (abfd))
346 sym_hashes_end -= symtab_hdr->sh_info;
347
348 rel_end = relocs + sec->reloc_count;
349 for (rel = relocs; rel < rel_end; rel++)
350 {
351 struct elf_link_hash_entry *h;
352 unsigned long r_symndx;
353
354 r_symndx = ELF32_R_SYM (rel->r_info);
355 if (r_symndx < symtab_hdr->sh_info)
356 h = NULL;
357 else
358 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
359
360 switch (ELF32_R_TYPE (rel->r_info))
361 {
362 /* This relocation describes the C++ object vtable hierarchy.
363 Reconstruct it for later use during GC. */
364 case R_MN10300_GNU_VTINHERIT:
365 if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
366 return false;
367 break;
368
369 /* This relocation describes which C++ vtable entries are actually
370 used. Record for later use during GC. */
371 case R_MN10300_GNU_VTENTRY:
372 if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
373 return false;
374 break;
375 }
376 }
377
378 return true;
379 }
380
381 /* Return the section that should be marked against GC for a given
382 relocation. */
383
384 static asection *
385 mn10300_elf_gc_mark_hook (abfd, info, rel, h, sym)
386 bfd *abfd;
387 struct bfd_link_info *info ATTRIBUTE_UNUSED;
388 Elf_Internal_Rela *rel;
389 struct elf_link_hash_entry *h;
390 Elf_Internal_Sym *sym;
391 {
392 if (h != NULL)
393 {
394 switch (ELF32_R_TYPE (rel->r_info))
395 {
396 case R_MN10300_GNU_VTINHERIT:
397 case R_MN10300_GNU_VTENTRY:
398 break;
399
400 default:
401 switch (h->root.type)
402 {
403 case bfd_link_hash_defined:
404 case bfd_link_hash_defweak:
405 return h->root.u.def.section;
406
407 case bfd_link_hash_common:
408 return h->root.u.c.p->section;
409
410 default:
411 break;
412 }
413 }
414 }
415 else
416 {
417 if (!(elf_bad_symtab (abfd)
418 && ELF_ST_BIND (sym->st_info) != STB_LOCAL)
419 && ! ((sym->st_shndx <= 0 || sym->st_shndx >= SHN_LORESERVE)
420 && sym->st_shndx != SHN_COMMON))
421 {
422 return bfd_section_from_elf_index (abfd, sym->st_shndx);
423 }
424 }
425
426 return NULL;
427 }
428
429 /* Perform a relocation as part of a final link. */
430 static bfd_reloc_status_type
431 mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
432 input_section, contents, offset, value,
433 addend, info, sym_sec, is_local)
434 reloc_howto_type *howto;
435 bfd *input_bfd;
436 bfd *output_bfd ATTRIBUTE_UNUSED;
437 asection *input_section;
438 bfd_byte *contents;
439 bfd_vma offset;
440 bfd_vma value;
441 bfd_vma addend;
442 struct bfd_link_info *info ATTRIBUTE_UNUSED;
443 asection *sym_sec ATTRIBUTE_UNUSED;
444 int is_local ATTRIBUTE_UNUSED;
445 {
446 unsigned long r_type = howto->type;
447 bfd_byte *hit_data = contents + offset;
448
449 switch (r_type)
450 {
451 case R_MN10300_NONE:
452 return bfd_reloc_ok;
453
454 case R_MN10300_32:
455 value += addend;
456 bfd_put_32 (input_bfd, value, hit_data);
457 return bfd_reloc_ok;
458
459 case R_MN10300_24:
460 value += addend;
461
462 if ((long)value > 0x7fffff || (long)value < -0x800000)
463 return bfd_reloc_overflow;
464
465 bfd_put_8 (input_bfd, value & 0xff, hit_data);
466 bfd_put_8 (input_bfd, (value >> 8) & 0xff, hit_data + 1);
467 bfd_put_8 (input_bfd, (value >> 16) & 0xff, hit_data + 2);
468 return bfd_reloc_ok;
469
470 case R_MN10300_16:
471 value += addend;
472
473 if ((long)value > 0x7fff || (long)value < -0x8000)
474 return bfd_reloc_overflow;
475
476 bfd_put_16 (input_bfd, value, hit_data);
477 return bfd_reloc_ok;
478
479 case R_MN10300_8:
480 value += addend;
481
482 if ((long)value > 0x7f || (long)value < -0x80)
483 return bfd_reloc_overflow;
484
485 bfd_put_8 (input_bfd, value, hit_data);
486 return bfd_reloc_ok;
487
488 case R_MN10300_PCREL8:
489 value -= (input_section->output_section->vma
490 + input_section->output_offset);
491 value -= offset;
492 value += addend;
493
494 if ((long)value > 0xff || (long)value < -0x100)
495 return bfd_reloc_overflow;
496
497 bfd_put_8 (input_bfd, value, hit_data);
498 return bfd_reloc_ok;
499
500 case R_MN10300_PCREL16:
501 value -= (input_section->output_section->vma
502 + input_section->output_offset);
503 value -= offset;
504 value += addend;
505
506 if ((long)value > 0xffff || (long)value < -0x10000)
507 return bfd_reloc_overflow;
508
509 bfd_put_16 (input_bfd, value, hit_data);
510 return bfd_reloc_ok;
511
512 case R_MN10300_PCREL32:
513 value -= (input_section->output_section->vma
514 + input_section->output_offset);
515 value -= offset;
516 value += addend;
517
518 bfd_put_32 (input_bfd, value, hit_data);
519 return bfd_reloc_ok;
520
521 case R_MN10300_GNU_VTINHERIT:
522 case R_MN10300_GNU_VTENTRY:
523 return bfd_reloc_ok;
524
525 default:
526 return bfd_reloc_notsupported;
527 }
528 }
529 \f
530 /* Relocate an MN10300 ELF section. */
531 static boolean
532 mn10300_elf_relocate_section (output_bfd, info, input_bfd, input_section,
533 contents, relocs, local_syms, local_sections)
534 bfd *output_bfd;
535 struct bfd_link_info *info;
536 bfd *input_bfd;
537 asection *input_section;
538 bfd_byte *contents;
539 Elf_Internal_Rela *relocs;
540 Elf_Internal_Sym *local_syms;
541 asection **local_sections;
542 {
543 Elf_Internal_Shdr *symtab_hdr;
544 struct elf32_mn10300_link_hash_entry **sym_hashes;
545 Elf_Internal_Rela *rel, *relend;
546
547 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
548 sym_hashes = (struct elf32_mn10300_link_hash_entry **)
549 (elf_sym_hashes (input_bfd));
550
551 rel = relocs;
552 relend = relocs + input_section->reloc_count;
553 for (; rel < relend; rel++)
554 {
555 int r_type;
556 reloc_howto_type *howto;
557 unsigned long r_symndx;
558 Elf_Internal_Sym *sym;
559 asection *sec;
560 struct elf32_mn10300_link_hash_entry *h;
561 bfd_vma relocation;
562 bfd_reloc_status_type r;
563
564 r_symndx = ELF32_R_SYM (rel->r_info);
565 r_type = ELF32_R_TYPE (rel->r_info);
566 howto = elf_mn10300_howto_table + r_type;
567
568 /* Just skip the vtable gc relocs. */
569 if (r_type == R_MN10300_GNU_VTINHERIT
570 || r_type == R_MN10300_GNU_VTENTRY)
571 continue;
572
573 if (info->relocateable)
574 {
575 /* This is a relocateable link. We don't have to change
576 anything, unless the reloc is against a section symbol,
577 in which case we have to adjust according to where the
578 section symbol winds up in the output section. */
579 if (r_symndx < symtab_hdr->sh_info)
580 {
581 sym = local_syms + r_symndx;
582 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
583 {
584 sec = local_sections[r_symndx];
585 rel->r_addend += sec->output_offset + sym->st_value;
586 }
587 }
588
589 continue;
590 }
591
592 /* This is a final link. */
593 h = NULL;
594 sym = NULL;
595 sec = NULL;
596 if (r_symndx < symtab_hdr->sh_info)
597 {
598 sym = local_syms + r_symndx;
599 sec = local_sections[r_symndx];
600 relocation = (sec->output_section->vma
601 + sec->output_offset
602 + sym->st_value);
603 }
604 else
605 {
606 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
607 while (h->root.type == bfd_link_hash_indirect
608 || h->root.type == bfd_link_hash_warning)
609 h = (struct elf32_mn10300_link_hash_entry *) h->root.root.u.i.link;
610 if (h->root.root.type == bfd_link_hash_defined
611 || h->root.root.type == bfd_link_hash_defweak)
612 {
613 sec = h->root.root.u.def.section;
614 relocation = (h->root.root.u.def.value
615 + sec->output_section->vma
616 + sec->output_offset);
617 }
618 else if (h->root.root.type == bfd_link_hash_undefweak)
619 relocation = 0;
620 else
621 {
622 if (! ((*info->callbacks->undefined_symbol)
623 (info, h->root.root.root.string, input_bfd,
624 input_section, rel->r_offset, true)))
625 return false;
626 relocation = 0;
627 }
628 }
629
630 r = mn10300_elf_final_link_relocate (howto, input_bfd, output_bfd,
631 input_section,
632 contents, rel->r_offset,
633 relocation, rel->r_addend,
634 info, sec, h == NULL);
635
636 if (r != bfd_reloc_ok)
637 {
638 const char *name;
639 const char *msg = (const char *)0;
640
641 if (h != NULL)
642 name = h->root.root.root.string;
643 else
644 {
645 name = (bfd_elf_string_from_elf_section
646 (input_bfd, symtab_hdr->sh_link, sym->st_name));
647 if (name == NULL || *name == '\0')
648 name = bfd_section_name (input_bfd, sec);
649 }
650
651 switch (r)
652 {
653 case bfd_reloc_overflow:
654 if (! ((*info->callbacks->reloc_overflow)
655 (info, name, howto->name, (bfd_vma) 0,
656 input_bfd, input_section, rel->r_offset)))
657 return false;
658 break;
659
660 case bfd_reloc_undefined:
661 if (! ((*info->callbacks->undefined_symbol)
662 (info, name, input_bfd, input_section,
663 rel->r_offset, true)))
664 return false;
665 break;
666
667 case bfd_reloc_outofrange:
668 msg = _("internal error: out of range error");
669 goto common_error;
670
671 case bfd_reloc_notsupported:
672 msg = _("internal error: unsupported relocation error");
673 goto common_error;
674
675 case bfd_reloc_dangerous:
676 msg = _("internal error: dangerous error");
677 goto common_error;
678
679 default:
680 msg = _("internal error: unknown error");
681 /* fall through */
682
683 common_error:
684 if (!((*info->callbacks->warning)
685 (info, msg, name, input_bfd, input_section,
686 rel->r_offset)))
687 return false;
688 break;
689 }
690 }
691 }
692
693 return true;
694 }
695
696 /* Finish initializing one hash table entry. */
697 static boolean
698 elf32_mn10300_finish_hash_table_entry (gen_entry, in_args)
699 struct bfd_hash_entry *gen_entry;
700 PTR in_args ATTRIBUTE_UNUSED;
701 {
702 struct elf32_mn10300_link_hash_entry *entry;
703 unsigned int byte_count = 0;
704
705 entry = (struct elf32_mn10300_link_hash_entry *)gen_entry;
706
707 /* If we already know we want to convert "call" to "calls" for calls
708 to this symbol, then return now. */
709 if (entry->flags == MN10300_CONVERT_CALL_TO_CALLS)
710 return true;
711
712 /* If there are no named calls to this symbol, or there's nothing we
713 can move from the function itself into the "call" instruction, then
714 note that all "call" instructions should be converted into "calls"
715 instructions and return. */
716 if (entry->direct_calls == 0
717 || (entry->stack_size == 0 && entry->movm_args == 0))
718 {
719 /* Make a note that we should convert "call" instructions to "calls"
720 instructions for calls to this symbol. */
721 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
722 return true;
723 }
724
725 /* We may be able to move some instructions from the function itself into
726 the "call" instruction. Count how many bytes we might be able to
727 eliminate in the function itself. */
728
729 /* A movm instruction is two bytes. */
730 if (entry->movm_args)
731 byte_count += 2;
732
733 /* Count the insn to allocate stack space too. */
734 if (entry->stack_size > 0 && entry->stack_size <= 128)
735 byte_count += 3;
736 else if (entry->stack_size > 0 && entry->stack_size < 256)
737 byte_count += 4;
738
739 /* If using "call" will result in larger code, then turn all
740 the associated "call" instructions into "calls" instrutions. */
741 if (byte_count < entry->direct_calls)
742 entry->flags |= MN10300_CONVERT_CALL_TO_CALLS;
743
744 /* This routine never fails. */
745 return true;
746 }
747
748 /* This function handles relaxing for the mn10300.
749
750 There's quite a few relaxing opportunites available on the mn10300:
751
752 * calls:32 -> calls:16 2 bytes
753 * call:32 -> call:16 2 bytes
754
755 * call:32 -> calls:32 1 byte
756 * call:16 -> calls:16 1 byte
757 * These are done anytime using "calls" would result
758 in smaller code, or when necessary to preserve the
759 meaning of the program.
760
761 * call:32 varies
762 * call:16
763 * In some circumstances we can move instructions
764 from a function prologue into a "call" instruction.
765 This is only done if the resulting code is no larger
766 than the original code.
767
768 * jmp:32 -> jmp:16 2 bytes
769 * jmp:16 -> bra:8 1 byte
770
771 * If the previous instruction is a conditional branch
772 around the jump/bra, we may be able to reverse its condition
773 and change its target to the jump's target. The jump/bra
774 can then be deleted. 2 bytes
775
776 * mov abs32 -> mov abs16 1 or 2 bytes
777
778 * Most instructions which accept imm32 can relax to imm16 1 or 2 bytes
779 - Most instructions which accept imm16 can relax to imm8 1 or 2 bytes
780
781 * Most instructions which accept d32 can relax to d16 1 or 2 bytes
782 - Most instructions which accept d16 can relax to d8 1 or 2 bytes
783
784 We don't handle imm16->imm8 or d16->d8 as they're very rare
785 and somewhat more difficult to support. */
786
787 static boolean
788 mn10300_elf_relax_section (abfd, sec, link_info, again)
789 bfd *abfd;
790 asection *sec;
791 struct bfd_link_info *link_info;
792 boolean *again;
793 {
794 Elf_Internal_Shdr *symtab_hdr;
795 Elf_Internal_Rela *internal_relocs = NULL;
796 Elf_Internal_Rela *free_relocs = NULL;
797 Elf_Internal_Rela *irel, *irelend;
798 bfd_byte *contents = NULL;
799 bfd_byte *free_contents = NULL;
800 Elf32_External_Sym *extsyms = NULL;
801 Elf32_External_Sym *free_extsyms = NULL;
802 struct elf32_mn10300_link_hash_table *hash_table;
803
804 /* Assume nothing changes. */
805 *again = false;
806
807 /* We need a pointer to the mn10300 specific hash table. */
808 hash_table = elf32_mn10300_hash_table (link_info);
809
810 /* Initialize fields in each hash table entry the first time through. */
811 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
812 {
813 bfd *input_bfd;
814
815 /* Iterate over all the input bfds. */
816 for (input_bfd = link_info->input_bfds;
817 input_bfd != NULL;
818 input_bfd = input_bfd->link_next)
819 {
820 asection *section;
821
822 /* We're going to need all the symbols for each bfd. */
823 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
824
825 /* Get cached copy if it exists. */
826 if (symtab_hdr->contents != NULL)
827 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
828 else
829 {
830 /* Go get them off disk. */
831 extsyms = ((Elf32_External_Sym *)
832 bfd_malloc (symtab_hdr->sh_size));
833 if (extsyms == NULL)
834 goto error_return;
835 free_extsyms = extsyms;
836 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
837 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
838 != symtab_hdr->sh_size))
839 goto error_return;
840 }
841
842 /* Iterate over each section in this bfd. */
843 for (section = input_bfd->sections;
844 section != NULL;
845 section = section->next)
846 {
847 struct elf32_mn10300_link_hash_entry *hash;
848 Elf_Internal_Sym *sym;
849 asection *sym_sec = NULL;
850 const char *sym_name;
851 char *new_name;
852
853 /* Get cached copy of section contents if it exists. */
854 if (elf_section_data (section)->this_hdr.contents != NULL)
855 contents = elf_section_data (section)->this_hdr.contents;
856 else if (section->_raw_size != 0)
857 {
858 /* Go get them off disk. */
859 contents = (bfd_byte *)bfd_malloc (section->_raw_size);
860 if (contents == NULL)
861 goto error_return;
862 free_contents = contents;
863
864 if (!bfd_get_section_contents (input_bfd, section,
865 contents, (file_ptr) 0,
866 section->_raw_size))
867 goto error_return;
868 }
869 else
870 {
871 contents = NULL;
872 free_contents = NULL;
873 }
874
875 /* If there aren't any relocs, then there's nothing to do. */
876 if ((section->flags & SEC_RELOC) != 0
877 && section->reloc_count != 0)
878 {
879
880 /* Get a copy of the native relocations. */
881 internal_relocs = (_bfd_elf32_link_read_relocs
882 (input_bfd, section, (PTR) NULL,
883 (Elf_Internal_Rela *) NULL,
884 link_info->keep_memory));
885 if (internal_relocs == NULL)
886 goto error_return;
887 if (! link_info->keep_memory)
888 free_relocs = internal_relocs;
889
890 /* Now examine each relocation. */
891 irel = internal_relocs;
892 irelend = irel + section->reloc_count;
893 for (; irel < irelend; irel++)
894 {
895 long r_type;
896 unsigned long r_index;
897 unsigned char code;
898
899 r_type = ELF32_R_TYPE (irel->r_info);
900 r_index = ELF32_R_SYM (irel->r_info);
901
902 if (r_type < 0 || r_type >= (int)R_MN10300_MAX)
903 goto error_return;
904
905 /* We need the name and hash table entry of the target
906 symbol! */
907 hash = NULL;
908 sym = NULL;
909 sym_sec = NULL;
910
911 if (r_index < symtab_hdr->sh_info)
912 {
913 /* A local symbol. */
914 Elf_Internal_Sym isym;
915
916 bfd_elf32_swap_symbol_in (input_bfd,
917 extsyms + r_index, &isym);
918
919 if (isym.st_shndx == SHN_UNDEF)
920 sym_sec = bfd_und_section_ptr;
921 else if (isym.st_shndx > 0
922 && isym.st_shndx < SHN_LORESERVE)
923 sym_sec
924 = bfd_section_from_elf_index (input_bfd,
925 isym.st_shndx);
926 else if (isym.st_shndx == SHN_ABS)
927 sym_sec = bfd_abs_section_ptr;
928 else if (isym.st_shndx == SHN_COMMON)
929 sym_sec = bfd_com_section_ptr;
930
931 sym_name = bfd_elf_string_from_elf_section (input_bfd,
932 symtab_hdr->sh_link,
933 isym.st_name);
934
935 /* If it isn't a function, then we don't care
936 about it. */
937 if (r_index < symtab_hdr->sh_info
938 && ELF_ST_TYPE (isym.st_info) != STT_FUNC)
939 continue;
940
941 /* Tack on an ID so we can uniquely identify this
942 local symbol in the global hash table. */
943 new_name = bfd_malloc (strlen (sym_name) + 10);
944 if (new_name == 0)
945 goto error_return;
946
947 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
948 sym_name = new_name;
949
950 hash = (struct elf32_mn10300_link_hash_entry *)
951 elf_link_hash_lookup (&hash_table->static_hash_table->root,
952 sym_name, true,
953 true, false);
954 free (new_name);
955 }
956 else
957 {
958 r_index -= symtab_hdr->sh_info;
959 hash = (struct elf32_mn10300_link_hash_entry *)
960 elf_sym_hashes (input_bfd)[r_index];
961 }
962
963 /* If this is not a "call" instruction, then we
964 should convert "call" instructions to "calls"
965 instructions. */
966 code = bfd_get_8 (input_bfd,
967 contents + irel->r_offset - 1);
968 if (code != 0xdd && code != 0xcd)
969 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
970
971 /* If this is a jump/call, then bump the direct_calls
972 counter. Else force "call" to "calls" conversions. */
973 if (r_type == R_MN10300_PCREL32
974 || r_type == R_MN10300_PCREL16)
975 hash->direct_calls++;
976 else
977 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
978 }
979 }
980
981 /* Now look at the actual contents to get the stack size,
982 and a list of what registers were saved in the prologue
983 (ie movm_args). */
984 if ((section->flags & SEC_CODE) != 0)
985 {
986
987 Elf32_External_Sym *esym, *esymend;
988 int idx, shndx;
989
990 shndx = _bfd_elf_section_from_bfd_section (input_bfd,
991 section);
992
993 /* Look at each function defined in this section and
994 update info for that function. */
995 esym = extsyms;
996 esymend = esym + symtab_hdr->sh_info;
997 for (; esym < esymend; esym++)
998 {
999 Elf_Internal_Sym isym;
1000
1001 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1002 if (isym.st_shndx == shndx
1003 && ELF_ST_TYPE (isym.st_info) == STT_FUNC)
1004 {
1005 if (isym.st_shndx == SHN_UNDEF)
1006 sym_sec = bfd_und_section_ptr;
1007 else if (isym.st_shndx > 0
1008 && isym.st_shndx < SHN_LORESERVE)
1009 sym_sec
1010 = bfd_section_from_elf_index (input_bfd,
1011 isym.st_shndx);
1012 else if (isym.st_shndx == SHN_ABS)
1013 sym_sec = bfd_abs_section_ptr;
1014 else if (isym.st_shndx == SHN_COMMON)
1015 sym_sec = bfd_com_section_ptr;
1016
1017 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1018 symtab_hdr->sh_link,
1019 isym.st_name);
1020
1021 /* Tack on an ID so we can uniquely identify this
1022 local symbol in the global hash table. */
1023 new_name = bfd_malloc (strlen (sym_name) + 10);
1024 if (new_name == 0)
1025 goto error_return;
1026
1027 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1028 sym_name = new_name;
1029
1030 hash = (struct elf32_mn10300_link_hash_entry *)
1031 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1032 sym_name, true,
1033 true, false);
1034 free (new_name);
1035 compute_function_info (input_bfd, hash,
1036 isym.st_value, contents);
1037 }
1038 }
1039
1040 esym = extsyms + symtab_hdr->sh_info;
1041 esymend = extsyms + (symtab_hdr->sh_size
1042 / sizeof (Elf32_External_Sym));
1043 for (idx = 0; esym < esymend; esym++, idx++)
1044 {
1045 Elf_Internal_Sym isym;
1046
1047 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1048 hash = (struct elf32_mn10300_link_hash_entry *)
1049 elf_sym_hashes (input_bfd)[idx];
1050 if (isym.st_shndx == shndx
1051 && ELF_ST_TYPE (isym.st_info) == STT_FUNC
1052 && (hash)->root.root.u.def.section == section
1053 && ((hash)->root.root.type == bfd_link_hash_defined
1054 || (hash)->root.root.type == bfd_link_hash_defweak))
1055 compute_function_info (input_bfd, hash,
1056 (hash)->root.root.u.def.value,
1057 contents);
1058 }
1059 }
1060
1061 /* Cache or free any memory we allocated for the relocs. */
1062 if (free_relocs != NULL)
1063 {
1064 free (free_relocs);
1065 free_relocs = NULL;
1066 }
1067
1068 /* Cache or free any memory we allocated for the contents. */
1069 if (free_contents != NULL)
1070 {
1071 if (! link_info->keep_memory)
1072 free (free_contents);
1073 else
1074 {
1075 /* Cache the section contents for elf_link_input_bfd. */
1076 elf_section_data (section)->this_hdr.contents = contents;
1077 }
1078 free_contents = NULL;
1079 }
1080 }
1081
1082 /* Cache or free any memory we allocated for the symbols. */
1083 if (free_extsyms != NULL)
1084 {
1085 if (! link_info->keep_memory)
1086 free (free_extsyms);
1087 else
1088 {
1089 /* Cache the symbols for elf_link_input_bfd. */
1090 symtab_hdr->contents = extsyms;
1091 }
1092 free_extsyms = NULL;
1093 }
1094 }
1095
1096 /* Now iterate on each symbol in the hash table and perform
1097 the final initialization steps on each. */
1098 elf32_mn10300_link_hash_traverse (hash_table,
1099 elf32_mn10300_finish_hash_table_entry,
1100 NULL);
1101 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1102 elf32_mn10300_finish_hash_table_entry,
1103 NULL);
1104
1105 /* All entries in the hash table are fully initialized. */
1106 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1107
1108 /* Now that everything has been initialized, go through each
1109 code section and delete any prologue insns which will be
1110 redundant because their operations will be performed by
1111 a "call" instruction. */
1112 for (input_bfd = link_info->input_bfds;
1113 input_bfd != NULL;
1114 input_bfd = input_bfd->link_next)
1115 {
1116 asection *section;
1117
1118 /* We're going to need all the symbols for each bfd. */
1119 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1120
1121 /* Get cached copy if it exists. */
1122 if (symtab_hdr->contents != NULL)
1123 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1124 else
1125 {
1126 /* Go get them off disk. */
1127 extsyms = ((Elf32_External_Sym *)
1128 bfd_malloc (symtab_hdr->sh_size));
1129 if (extsyms == NULL)
1130 goto error_return;
1131 free_extsyms = extsyms;
1132 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1133 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, input_bfd)
1134 != symtab_hdr->sh_size))
1135 goto error_return;
1136 }
1137
1138 /* Walk over each section in this bfd. */
1139 for (section = input_bfd->sections;
1140 section != NULL;
1141 section = section->next)
1142 {
1143 int shndx;
1144 Elf32_External_Sym *esym, *esymend;
1145 int idx;
1146
1147 /* Skip non-code sections and empty sections. */
1148 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1149 continue;
1150
1151 if (section->reloc_count != 0)
1152 {
1153 /* Get a copy of the native relocations. */
1154 internal_relocs = (_bfd_elf32_link_read_relocs
1155 (input_bfd, section, (PTR) NULL,
1156 (Elf_Internal_Rela *) NULL,
1157 link_info->keep_memory));
1158 if (internal_relocs == NULL)
1159 goto error_return;
1160 if (! link_info->keep_memory)
1161 free_relocs = internal_relocs;
1162 }
1163
1164 /* Get cached copy of section contents if it exists. */
1165 if (elf_section_data (section)->this_hdr.contents != NULL)
1166 contents = elf_section_data (section)->this_hdr.contents;
1167 else
1168 {
1169 /* Go get them off disk. */
1170 contents = (bfd_byte *)bfd_malloc (section->_raw_size);
1171 if (contents == NULL)
1172 goto error_return;
1173 free_contents = contents;
1174
1175 if (!bfd_get_section_contents (input_bfd, section,
1176 contents, (file_ptr) 0,
1177 section->_raw_size))
1178 goto error_return;
1179 }
1180
1181 shndx = _bfd_elf_section_from_bfd_section (input_bfd, section);
1182
1183 /* Now look for any function in this section which needs
1184 insns deleted from its prologue. */
1185 esym = extsyms;
1186 esymend = esym + symtab_hdr->sh_info;
1187 for (; esym < esymend; esym++)
1188 {
1189 Elf_Internal_Sym isym;
1190 struct elf32_mn10300_link_hash_entry *sym_hash;
1191 asection *sym_sec = NULL;
1192 const char *sym_name;
1193 char *new_name;
1194
1195 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1196
1197 if (isym.st_shndx != shndx)
1198 continue;
1199
1200 if (isym.st_shndx == SHN_UNDEF)
1201 sym_sec = bfd_und_section_ptr;
1202 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1203 sym_sec
1204 = bfd_section_from_elf_index (input_bfd, isym.st_shndx);
1205 else if (isym.st_shndx == SHN_ABS)
1206 sym_sec = bfd_abs_section_ptr;
1207 else if (isym.st_shndx == SHN_COMMON)
1208 sym_sec = bfd_com_section_ptr;
1209 else
1210 abort ();
1211
1212 sym_name = bfd_elf_string_from_elf_section (input_bfd,
1213 symtab_hdr->sh_link,
1214 isym.st_name);
1215
1216 /* Tack on an ID so we can uniquely identify this
1217 local symbol in the global hash table. */
1218 new_name = bfd_malloc (strlen (sym_name) + 10);
1219 if (new_name == 0)
1220 goto error_return;
1221 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1222 sym_name = new_name;
1223
1224 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1225 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1226 sym_name, false,
1227 false, false);
1228
1229 free (new_name);
1230 if (sym_hash == NULL)
1231 continue;
1232
1233 if (! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1234 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1235 {
1236 int bytes = 0;
1237
1238 /* Note that we've changed things. */
1239 elf_section_data (section)->relocs = internal_relocs;
1240 free_relocs = NULL;
1241
1242 elf_section_data (section)->this_hdr.contents = contents;
1243 free_contents = NULL;
1244
1245 symtab_hdr->contents = (bfd_byte *)extsyms;
1246 free_extsyms = NULL;
1247
1248 /* Count how many bytes we're going to delete. */
1249 if (sym_hash->movm_args)
1250 bytes += 2;
1251
1252 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1253 bytes += 3;
1254 else if (sym_hash->stack_size
1255 && sym_hash->stack_size < 256)
1256 bytes += 4;
1257
1258 /* Note that we've deleted prologue bytes for this
1259 function. */
1260 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1261
1262 /* Actually delete the bytes. */
1263 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1264 section,
1265 isym.st_value,
1266 bytes))
1267 goto error_return;
1268
1269 /* Something changed. Not strictly necessary, but
1270 may lead to more relaxing opportunities. */
1271 *again = true;
1272 }
1273 }
1274
1275 /* Look for any global functions in this section which
1276 need insns deleted from their prologues. */
1277 esym = extsyms + symtab_hdr->sh_info;
1278 esymend = extsyms + (symtab_hdr->sh_size
1279 / sizeof (Elf32_External_Sym));
1280 for (idx = 0; esym < esymend; esym++, idx++)
1281 {
1282 Elf_Internal_Sym isym;
1283 struct elf32_mn10300_link_hash_entry *sym_hash;
1284
1285 bfd_elf32_swap_symbol_in (input_bfd, esym, &isym);
1286 sym_hash = (struct elf32_mn10300_link_hash_entry *)
1287 (elf_sym_hashes (input_bfd)[idx]);
1288 if (isym.st_shndx == shndx
1289 && (sym_hash)->root.root.u.def.section == section
1290 && ! ((sym_hash)->flags & MN10300_CONVERT_CALL_TO_CALLS)
1291 && ! ((sym_hash)->flags & MN10300_DELETED_PROLOGUE_BYTES))
1292 {
1293 int bytes = 0;
1294
1295 /* Note that we've changed things. */
1296 elf_section_data (section)->relocs = internal_relocs;
1297 free_relocs = NULL;
1298
1299 elf_section_data (section)->this_hdr.contents = contents;
1300 free_contents = NULL;
1301
1302 symtab_hdr->contents = (bfd_byte *)extsyms;
1303 free_extsyms = NULL;
1304
1305 /* Count how many bytes we're going to delete. */
1306 if (sym_hash->movm_args)
1307 bytes += 2;
1308
1309 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1310 bytes += 3;
1311 else if (sym_hash->stack_size
1312 && sym_hash->stack_size < 256)
1313 bytes += 4;
1314
1315 /* Note that we've deleted prologue bytes for this
1316 function. */
1317 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1318
1319 /* Actually delete the bytes. */
1320 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1321 section,
1322 (sym_hash)->root.root.u.def.value,
1323 bytes))
1324 goto error_return;
1325
1326 /* Something changed. Not strictly necessary, but
1327 may lead to more relaxing opportunities. */
1328 *again = true;
1329 }
1330 }
1331
1332 /* Cache or free any memory we allocated for the relocs. */
1333 if (free_relocs != NULL)
1334 {
1335 free (free_relocs);
1336 free_relocs = NULL;
1337 }
1338
1339 /* Cache or free any memory we allocated for the contents. */
1340 if (free_contents != NULL)
1341 {
1342 if (! link_info->keep_memory)
1343 free (free_contents);
1344 else
1345 {
1346 /* Cache the section contents for elf_link_input_bfd. */
1347 elf_section_data (section)->this_hdr.contents = contents;
1348 }
1349 free_contents = NULL;
1350 }
1351 }
1352
1353 /* Cache or free any memory we allocated for the symbols. */
1354 if (free_extsyms != NULL)
1355 {
1356 if (! link_info->keep_memory)
1357 free (free_extsyms);
1358 else
1359 {
1360 /* Cache the symbols for elf_link_input_bfd. */
1361 symtab_hdr->contents = extsyms;
1362 }
1363 free_extsyms = NULL;
1364 }
1365 }
1366 }
1367
1368 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1369 contents = NULL;
1370 extsyms = NULL;
1371 internal_relocs = NULL;
1372 free_relocs = NULL;
1373 free_contents = NULL;
1374 free_extsyms = NULL;
1375
1376 /* We don't have to do anything for a relocateable link, if
1377 this section does not have relocs, or if this is not a
1378 code section. */
1379 if (link_info->relocateable
1380 || (sec->flags & SEC_RELOC) == 0
1381 || sec->reloc_count == 0
1382 || (sec->flags & SEC_CODE) == 0)
1383 return true;
1384
1385 /* If this is the first time we have been called for this section,
1386 initialize the cooked size. */
1387 if (sec->_cooked_size == 0)
1388 sec->_cooked_size = sec->_raw_size;
1389
1390 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1391
1392 /* Get a copy of the native relocations. */
1393 internal_relocs = (_bfd_elf32_link_read_relocs
1394 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1395 link_info->keep_memory));
1396 if (internal_relocs == NULL)
1397 goto error_return;
1398 if (! link_info->keep_memory)
1399 free_relocs = internal_relocs;
1400
1401 /* Walk through them looking for relaxing opportunities. */
1402 irelend = internal_relocs + sec->reloc_count;
1403 for (irel = internal_relocs; irel < irelend; irel++)
1404 {
1405 bfd_vma symval;
1406 struct elf32_mn10300_link_hash_entry *h = NULL;
1407
1408 /* If this isn't something that can be relaxed, then ignore
1409 this reloc. */
1410 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1411 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1412 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1413 continue;
1414
1415 /* Get the section contents if we haven't done so already. */
1416 if (contents == NULL)
1417 {
1418 /* Get cached copy if it exists. */
1419 if (elf_section_data (sec)->this_hdr.contents != NULL)
1420 contents = elf_section_data (sec)->this_hdr.contents;
1421 else
1422 {
1423 /* Go get them off disk. */
1424 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1425 if (contents == NULL)
1426 goto error_return;
1427 free_contents = contents;
1428
1429 if (! bfd_get_section_contents (abfd, sec, contents,
1430 (file_ptr) 0, sec->_raw_size))
1431 goto error_return;
1432 }
1433 }
1434
1435 /* Read this BFD's symbols if we haven't done so already. */
1436 if (extsyms == NULL)
1437 {
1438 /* Get cached copy if it exists. */
1439 if (symtab_hdr->contents != NULL)
1440 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1441 else
1442 {
1443 /* Go get them off disk. */
1444 extsyms = ((Elf32_External_Sym *)
1445 bfd_malloc (symtab_hdr->sh_size));
1446 if (extsyms == NULL)
1447 goto error_return;
1448 free_extsyms = extsyms;
1449 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1450 || (bfd_read (extsyms, 1, symtab_hdr->sh_size, abfd)
1451 != symtab_hdr->sh_size))
1452 goto error_return;
1453 }
1454 }
1455
1456 /* Get the value of the symbol referred to by the reloc. */
1457 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1458 {
1459 Elf_Internal_Sym isym;
1460 asection *sym_sec = NULL;
1461 const char *sym_name;
1462 char *new_name;
1463
1464 /* A local symbol. */
1465 bfd_elf32_swap_symbol_in (abfd,
1466 extsyms + ELF32_R_SYM (irel->r_info),
1467 &isym);
1468
1469 if (isym.st_shndx == SHN_UNDEF)
1470 sym_sec = bfd_und_section_ptr;
1471 else if (isym.st_shndx > 0 && isym.st_shndx < SHN_LORESERVE)
1472 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
1473 else if (isym.st_shndx == SHN_ABS)
1474 sym_sec = bfd_abs_section_ptr;
1475 else if (isym.st_shndx == SHN_COMMON)
1476 sym_sec = bfd_com_section_ptr;
1477 else
1478 abort ();
1479
1480 symval = (isym.st_value
1481 + sym_sec->output_section->vma
1482 + sym_sec->output_offset);
1483 sym_name = bfd_elf_string_from_elf_section (abfd,
1484 symtab_hdr->sh_link,
1485 isym.st_name);
1486
1487 /* Tack on an ID so we can uniquely identify this
1488 local symbol in the global hash table. */
1489 new_name = bfd_malloc (strlen (sym_name) + 10);
1490 if (new_name == 0)
1491 goto error_return;
1492 sprintf (new_name, "%s_%08x", sym_name, (int)sym_sec);
1493 sym_name = new_name;
1494
1495 h = (struct elf32_mn10300_link_hash_entry *)
1496 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1497 sym_name, false, false, false);
1498 free (new_name);
1499 }
1500 else
1501 {
1502 unsigned long indx;
1503
1504 /* An external symbol. */
1505 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1506 h = (struct elf32_mn10300_link_hash_entry *)
1507 (elf_sym_hashes (abfd)[indx]);
1508 BFD_ASSERT (h != NULL);
1509 if (h->root.root.type != bfd_link_hash_defined
1510 && h->root.root.type != bfd_link_hash_defweak)
1511 {
1512 /* This appears to be a reference to an undefined
1513 symbol. Just ignore it--it will be caught by the
1514 regular reloc processing. */
1515 continue;
1516 }
1517
1518 symval = (h->root.root.u.def.value
1519 + h->root.root.u.def.section->output_section->vma
1520 + h->root.root.u.def.section->output_offset);
1521 }
1522
1523 /* For simplicity of coding, we are going to modify the section
1524 contents, the section relocs, and the BFD symbol table. We
1525 must tell the rest of the code not to free up this
1526 information. It would be possible to instead create a table
1527 of changes which have to be made, as is done in coff-mips.c;
1528 that would be more work, but would require less memory when
1529 the linker is run. */
1530
1531 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1532 branch/call, also deal with "call" -> "calls" conversions and
1533 insertion of prologue data into "call" instructions. */
1534 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1535 {
1536 bfd_vma value = symval;
1537
1538 /* If we've got a "call" instruction that needs to be turned
1539 into a "calls" instruction, do so now. It saves a byte. */
1540 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1541 {
1542 unsigned char code;
1543
1544 /* Get the opcode. */
1545 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1546
1547 /* Make sure we're working with a "call" instruction! */
1548 if (code == 0xdd)
1549 {
1550 /* Note that we've changed the relocs, section contents,
1551 etc. */
1552 elf_section_data (sec)->relocs = internal_relocs;
1553 free_relocs = NULL;
1554
1555 elf_section_data (sec)->this_hdr.contents = contents;
1556 free_contents = NULL;
1557
1558 symtab_hdr->contents = (bfd_byte *) extsyms;
1559 free_extsyms = NULL;
1560
1561 /* Fix the opcode. */
1562 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1563 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1564
1565 /* Fix irel->r_offset and irel->r_addend. */
1566 irel->r_offset += 1;
1567 irel->r_addend += 1;
1568
1569 /* Delete one byte of data. */
1570 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1571 irel->r_offset + 3, 1))
1572 goto error_return;
1573
1574 /* That will change things, so, we should relax again.
1575 Note that this is not required, and it may be slow. */
1576 *again = true;
1577 }
1578 }
1579 else if (h)
1580 {
1581 /* We've got a "call" instruction which needs some data
1582 from target function filled in. */
1583 unsigned char code;
1584
1585 /* Get the opcode. */
1586 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1587
1588 /* Insert data from the target function into the "call"
1589 instruction if needed. */
1590 if (code == 0xdd)
1591 {
1592 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1593 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1594 contents + irel->r_offset + 5);
1595 }
1596 }
1597
1598 /* Deal with pc-relative gunk. */
1599 value -= (sec->output_section->vma + sec->output_offset);
1600 value -= irel->r_offset;
1601 value += irel->r_addend;
1602
1603 /* See if the value will fit in 16 bits, note the high value is
1604 0x7fff + 2 as the target will be two bytes closer if we are
1605 able to relax. */
1606 if ((long)value < 0x8001 && (long)value > -0x8000)
1607 {
1608 unsigned char code;
1609
1610 /* Get the opcode. */
1611 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1612
1613 if (code != 0xdc && code != 0xdd && code != 0xff)
1614 continue;
1615
1616 /* Note that we've changed the relocs, section contents, etc. */
1617 elf_section_data (sec)->relocs = internal_relocs;
1618 free_relocs = NULL;
1619
1620 elf_section_data (sec)->this_hdr.contents = contents;
1621 free_contents = NULL;
1622
1623 symtab_hdr->contents = (bfd_byte *) extsyms;
1624 free_extsyms = NULL;
1625
1626 /* Fix the opcode. */
1627 if (code == 0xdc)
1628 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1629 else if (code == 0xdd)
1630 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1631 else if (code == 0xff)
1632 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1633
1634 /* Fix the relocation's type. */
1635 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1636 R_MN10300_PCREL16);
1637
1638 /* Delete two bytes of data. */
1639 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1640 irel->r_offset + 1, 2))
1641 goto error_return;
1642
1643 /* That will change things, so, we should relax again.
1644 Note that this is not required, and it may be slow. */
1645 *again = true;
1646 }
1647 }
1648
1649 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1650 branch. */
1651 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1652 {
1653 bfd_vma value = symval;
1654
1655 /* If we've got a "call" instruction that needs to be turned
1656 into a "calls" instruction, do so now. It saves a byte. */
1657 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1658 {
1659 unsigned char code;
1660
1661 /* Get the opcode. */
1662 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1663
1664 /* Make sure we're working with a "call" instruction! */
1665 if (code == 0xcd)
1666 {
1667 /* Note that we've changed the relocs, section contents,
1668 etc. */
1669 elf_section_data (sec)->relocs = internal_relocs;
1670 free_relocs = NULL;
1671
1672 elf_section_data (sec)->this_hdr.contents = contents;
1673 free_contents = NULL;
1674
1675 symtab_hdr->contents = (bfd_byte *) extsyms;
1676 free_extsyms = NULL;
1677
1678 /* Fix the opcode. */
1679 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1680 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1681
1682 /* Fix irel->r_offset and irel->r_addend. */
1683 irel->r_offset += 1;
1684 irel->r_addend += 1;
1685
1686 /* Delete one byte of data. */
1687 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1688 irel->r_offset + 1, 1))
1689 goto error_return;
1690
1691 /* That will change things, so, we should relax again.
1692 Note that this is not required, and it may be slow. */
1693 *again = true;
1694 }
1695 }
1696 else if (h)
1697 {
1698 unsigned char code;
1699
1700 /* Get the opcode. */
1701 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1702
1703 /* Insert data from the target function into the "call"
1704 instruction if needed. */
1705 if (code == 0xcd)
1706 {
1707 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1708 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1709 contents + irel->r_offset + 3);
1710 }
1711 }
1712
1713 /* Deal with pc-relative gunk. */
1714 value -= (sec->output_section->vma + sec->output_offset);
1715 value -= irel->r_offset;
1716 value += irel->r_addend;
1717
1718 /* See if the value will fit in 8 bits, note the high value is
1719 0x7f + 1 as the target will be one bytes closer if we are
1720 able to relax. */
1721 if ((long)value < 0x80 && (long)value > -0x80)
1722 {
1723 unsigned char code;
1724
1725 /* Get the opcode. */
1726 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1727
1728 if (code != 0xcc)
1729 continue;
1730
1731 /* Note that we've changed the relocs, section contents, etc. */
1732 elf_section_data (sec)->relocs = internal_relocs;
1733 free_relocs = NULL;
1734
1735 elf_section_data (sec)->this_hdr.contents = contents;
1736 free_contents = NULL;
1737
1738 symtab_hdr->contents = (bfd_byte *) extsyms;
1739 free_extsyms = NULL;
1740
1741 /* Fix the opcode. */
1742 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1743
1744 /* Fix the relocation's type. */
1745 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1746 R_MN10300_PCREL8);
1747
1748 /* Delete one byte of data. */
1749 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1750 irel->r_offset + 1, 1))
1751 goto error_return;
1752
1753 /* That will change things, so, we should relax again.
1754 Note that this is not required, and it may be slow. */
1755 *again = true;
1756 }
1757 }
1758
1759 /* Try to eliminate an unconditional 8 bit pc-relative branch
1760 which immediately follows a conditional 8 bit pc-relative
1761 branch around the unconditional branch.
1762
1763 original: new:
1764 bCC lab1 bCC' lab2
1765 bra lab2
1766 lab1: lab1:
1767
1768 This happens when the bCC can't reach lab2 at assembly time,
1769 but due to other relaxations it can reach at link time. */
1770 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1771 {
1772 Elf_Internal_Rela *nrel;
1773 bfd_vma value = symval;
1774 unsigned char code;
1775
1776 /* Deal with pc-relative gunk. */
1777 value -= (sec->output_section->vma + sec->output_offset);
1778 value -= irel->r_offset;
1779 value += irel->r_addend;
1780
1781 /* Do nothing if this reloc is the last byte in the section. */
1782 if (irel->r_offset == sec->_cooked_size)
1783 continue;
1784
1785 /* See if the next instruction is an unconditional pc-relative
1786 branch, more often than not this test will fail, so we
1787 test it first to speed things up. */
1788 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1789 if (code != 0xca)
1790 continue;
1791
1792 /* Also make sure the next relocation applies to the next
1793 instruction and that it's a pc-relative 8 bit branch. */
1794 nrel = irel + 1;
1795 if (nrel == irelend
1796 || irel->r_offset + 2 != nrel->r_offset
1797 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1798 continue;
1799
1800 /* Make sure our destination immediately follows the
1801 unconditional branch. */
1802 if (symval != (sec->output_section->vma + sec->output_offset
1803 + irel->r_offset + 3))
1804 continue;
1805
1806 /* Now make sure we are a conditional branch. This may not
1807 be necessary, but why take the chance.
1808
1809 Note these checks assume that R_MN10300_PCREL8 relocs
1810 only occur on bCC and bCCx insns. If they occured
1811 elsewhere, we'd need to know the start of this insn
1812 for this check to be accurate. */
1813 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1814 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1815 && code != 0xc3 && code != 0xc4 && code != 0xc5
1816 && code != 0xc6 && code != 0xc7 && code != 0xc8
1817 && code != 0xc9 && code != 0xe8 && code != 0xe9
1818 && code != 0xea && code != 0xeb)
1819 continue;
1820
1821 /* We also have to be sure there is no symbol/label
1822 at the unconditional branch. */
1823 if (mn10300_elf_symbol_address_p (abfd, sec, extsyms,
1824 irel->r_offset + 1))
1825 continue;
1826
1827 /* Note that we've changed the relocs, section contents, etc. */
1828 elf_section_data (sec)->relocs = internal_relocs;
1829 free_relocs = NULL;
1830
1831 elf_section_data (sec)->this_hdr.contents = contents;
1832 free_contents = NULL;
1833
1834 symtab_hdr->contents = (bfd_byte *) extsyms;
1835 free_extsyms = NULL;
1836
1837 /* Reverse the condition of the first branch. */
1838 switch (code)
1839 {
1840 case 0xc8:
1841 code = 0xc9;
1842 break;
1843 case 0xc9:
1844 code = 0xc8;
1845 break;
1846 case 0xc0:
1847 code = 0xc2;
1848 break;
1849 case 0xc2:
1850 code = 0xc0;
1851 break;
1852 case 0xc3:
1853 code = 0xc1;
1854 break;
1855 case 0xc1:
1856 code = 0xc3;
1857 break;
1858 case 0xc4:
1859 code = 0xc6;
1860 break;
1861 case 0xc6:
1862 code = 0xc4;
1863 break;
1864 case 0xc7:
1865 code = 0xc5;
1866 break;
1867 case 0xc5:
1868 code = 0xc7;
1869 break;
1870 case 0xe8:
1871 code = 0xe9;
1872 break;
1873 case 0x9d:
1874 code = 0xe8;
1875 break;
1876 case 0xea:
1877 code = 0xeb;
1878 break;
1879 case 0xeb:
1880 code = 0xea;
1881 break;
1882 }
1883 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1884
1885 /* Set the reloc type and symbol for the first branch
1886 from the second branch. */
1887 irel->r_info = nrel->r_info;
1888
1889 /* Make the reloc for the second branch a null reloc. */
1890 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1891 R_MN10300_NONE);
1892
1893 /* Delete two bytes of data. */
1894 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1895 irel->r_offset + 1, 2))
1896 goto error_return;
1897
1898 /* That will change things, so, we should relax again.
1899 Note that this is not required, and it may be slow. */
1900 *again = true;
1901 }
1902
1903 /* Try to turn a 24 immediate, displacement or absolute address
1904 into a 8 immediate, displacement or absolute address. */
1905 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
1906 {
1907 bfd_vma value = symval;
1908 value += irel->r_addend;
1909
1910 /* See if the value will fit in 8 bits. */
1911 if ((long)value < 0x7f && (long)value > -0x80)
1912 {
1913 unsigned char code;
1914
1915 /* AM33 insns which have 24 operands are 6 bytes long and
1916 will have 0xfd as the first byte. */
1917
1918 /* Get the first opcode. */
1919 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1920
1921 if (code == 0xfd)
1922 {
1923 /* Get the second opcode. */
1924 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1925
1926 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
1927 equivalent instructions exists. */
1928 if (code != 0x6b && code != 0x7b
1929 && code != 0x8b && code != 0x9b
1930 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
1931 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
1932 || (code & 0x0f) == 0x0e))
1933 {
1934 /* Not safe if the high bit is on as relaxing may
1935 move the value out of high mem and thus not fit
1936 in a signed 8bit value. This is currently over
1937 conservative. */
1938 if ((value & 0x80) == 0)
1939 {
1940 /* Note that we've changed the relocation contents,
1941 etc. */
1942 elf_section_data (sec)->relocs = internal_relocs;
1943 free_relocs = NULL;
1944
1945 elf_section_data (sec)->this_hdr.contents = contents;
1946 free_contents = NULL;
1947
1948 symtab_hdr->contents = (bfd_byte *) extsyms;
1949 free_extsyms = NULL;
1950
1951 /* Fix the opcode. */
1952 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
1953 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1954
1955 /* Fix the relocation's type. */
1956 irel->r_info
1957 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1958 R_MN10300_8);
1959
1960 /* Delete two bytes of data. */
1961 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1962 irel->r_offset + 1, 2))
1963 goto error_return;
1964
1965 /* That will change things, so, we should relax
1966 again. Note that this is not required, and it
1967 may be slow. */
1968 *again = true;
1969 break;
1970 }
1971 }
1972
1973 }
1974 }
1975 }
1976
1977 /* Try to turn a 32bit immediate, displacement or absolute address
1978 into a 16bit immediate, displacement or absolute address. */
1979 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
1980 {
1981 bfd_vma value = symval;
1982 value += irel->r_addend;
1983
1984 /* See if the value will fit in 24 bits.
1985 We allow any 16bit match here. We prune those we can't
1986 handle below. */
1987 if ((long)value < 0x7fffff && (long)value > -0x800000)
1988 {
1989 unsigned char code;
1990
1991 /* AM33 insns which have 32bit operands are 7 bytes long and
1992 will have 0xfe as the first byte. */
1993
1994 /* Get the first opcode. */
1995 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1996
1997 if (code == 0xfe)
1998 {
1999 /* Get the second opcode. */
2000 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2001
2002 /* All the am33 32 -> 24 relaxing possibilities. */
2003 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
2004 equivalent instructions exists. */
2005 if (code != 0x6b && code != 0x7b
2006 && code != 0x8b && code != 0x9b
2007 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
2008 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
2009 || (code & 0x0f) == 0x0e))
2010 {
2011 /* Not safe if the high bit is on as relaxing may
2012 move the value out of high mem and thus not fit
2013 in a signed 16bit value. This is currently over
2014 conservative. */
2015 if ((value & 0x8000) == 0)
2016 {
2017 /* Note that we've changed the relocation contents,
2018 etc. */
2019 elf_section_data (sec)->relocs = internal_relocs;
2020 free_relocs = NULL;
2021
2022 elf_section_data (sec)->this_hdr.contents = contents;
2023 free_contents = NULL;
2024
2025 symtab_hdr->contents = (bfd_byte *) extsyms;
2026 free_extsyms = NULL;
2027
2028 /* Fix the opcode. */
2029 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
2030 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2031
2032 /* Fix the relocation's type. */
2033 irel->r_info
2034 = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2035 R_MN10300_24);
2036
2037 /* Delete one byte of data. */
2038 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2039 irel->r_offset + 3, 1))
2040 goto error_return;
2041
2042 /* That will change things, so, we should relax
2043 again. Note that this is not required, and it
2044 may be slow. */
2045 *again = true;
2046 break;
2047 }
2048 }
2049
2050 }
2051 }
2052
2053 /* See if the value will fit in 16 bits.
2054 We allow any 16bit match here. We prune those we can't
2055 handle below. */
2056 if ((long)value < 0x7fff && (long)value > -0x8000)
2057 {
2058 unsigned char code;
2059
2060 /* Most insns which have 32bit operands are 6 bytes long;
2061 exceptions are pcrel insns and bit insns.
2062
2063 We handle pcrel insns above. We don't bother trying
2064 to handle the bit insns here.
2065
2066 The first byte of the remaining insns will be 0xfc. */
2067
2068 /* Get the first opcode. */
2069 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
2070
2071 if (code != 0xfc)
2072 continue;
2073
2074 /* Get the second opcode. */
2075 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
2076
2077 if ((code & 0xf0) < 0x80)
2078 switch (code & 0xf0)
2079 {
2080 /* mov (d32,am),dn -> mov (d32,am),dn
2081 mov dm,(d32,am) -> mov dn,(d32,am)
2082 mov (d32,am),an -> mov (d32,am),an
2083 mov dm,(d32,am) -> mov dn,(d32,am)
2084 movbu (d32,am),dn -> movbu (d32,am),dn
2085 movbu dm,(d32,am) -> movbu dn,(d32,am)
2086 movhu (d32,am),dn -> movhu (d32,am),dn
2087 movhu dm,(d32,am) -> movhu dn,(d32,am) */
2088 case 0x00:
2089 case 0x10:
2090 case 0x20:
2091 case 0x30:
2092 case 0x40:
2093 case 0x50:
2094 case 0x60:
2095 case 0x70:
2096 /* Not safe if the high bit is on as relaxing may
2097 move the value out of high mem and thus not fit
2098 in a signed 16bit value. */
2099 if (code == 0xcc
2100 && (value & 0x8000))
2101 continue;
2102
2103 /* Note that we've changed the relocation contents, etc. */
2104 elf_section_data (sec)->relocs = internal_relocs;
2105 free_relocs = NULL;
2106
2107 elf_section_data (sec)->this_hdr.contents = contents;
2108 free_contents = NULL;
2109
2110 symtab_hdr->contents = (bfd_byte *) extsyms;
2111 free_extsyms = NULL;
2112
2113 /* Fix the opcode. */
2114 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2115 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2116
2117 /* Fix the relocation's type. */
2118 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2119 R_MN10300_16);
2120
2121 /* Delete two bytes of data. */
2122 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2123 irel->r_offset + 2, 2))
2124 goto error_return;
2125
2126 /* That will change things, so, we should relax again.
2127 Note that this is not required, and it may be slow. */
2128 *again = true;
2129 break;
2130 }
2131 else if ((code & 0xf0) == 0x80
2132 || (code & 0xf0) == 0x90)
2133 switch (code & 0xf3)
2134 {
2135 /* mov dn,(abs32) -> mov dn,(abs16)
2136 movbu dn,(abs32) -> movbu dn,(abs16)
2137 movhu dn,(abs32) -> movhu dn,(abs16) */
2138 case 0x81:
2139 case 0x82:
2140 case 0x83:
2141 /* Note that we've changed the relocation contents, etc. */
2142 elf_section_data (sec)->relocs = internal_relocs;
2143 free_relocs = NULL;
2144
2145 elf_section_data (sec)->this_hdr.contents = contents;
2146 free_contents = NULL;
2147
2148 symtab_hdr->contents = (bfd_byte *) extsyms;
2149 free_extsyms = NULL;
2150
2151 if ((code & 0xf3) == 0x81)
2152 code = 0x01 + (code & 0x0c);
2153 else if ((code & 0xf3) == 0x82)
2154 code = 0x02 + (code & 0x0c);
2155 else if ((code & 0xf3) == 0x83)
2156 code = 0x03 + (code & 0x0c);
2157 else
2158 abort ();
2159
2160 /* Fix the opcode. */
2161 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2162
2163 /* Fix the relocation's type. */
2164 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2165 R_MN10300_16);
2166
2167 /* The opcode got shorter too, so we have to fix the
2168 addend and offset too! */
2169 irel->r_offset -= 1;
2170
2171 /* Delete three bytes of data. */
2172 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2173 irel->r_offset + 1, 3))
2174 goto error_return;
2175
2176 /* That will change things, so, we should relax again.
2177 Note that this is not required, and it may be slow. */
2178 *again = true;
2179 break;
2180
2181 /* mov am,(abs32) -> mov am,(abs16)
2182 mov am,(d32,sp) -> mov am,(d16,sp)
2183 mov dm,(d32,sp) -> mov dm,(d32,sp)
2184 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2185 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2186 case 0x80:
2187 case 0x90:
2188 case 0x91:
2189 case 0x92:
2190 case 0x93:
2191 /* Note that we've changed the relocation contents, etc. */
2192 elf_section_data (sec)->relocs = internal_relocs;
2193 free_relocs = NULL;
2194
2195 elf_section_data (sec)->this_hdr.contents = contents;
2196 free_contents = NULL;
2197
2198 symtab_hdr->contents = (bfd_byte *) extsyms;
2199 free_extsyms = NULL;
2200
2201 /* Fix the opcode. */
2202 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2203 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2204
2205 /* Fix the relocation's type. */
2206 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2207 R_MN10300_16);
2208
2209 /* Delete two bytes of data. */
2210 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2211 irel->r_offset + 2, 2))
2212 goto error_return;
2213
2214 /* That will change things, so, we should relax again.
2215 Note that this is not required, and it may be slow. */
2216 *again = true;
2217 break;
2218 }
2219 else if ((code & 0xf0) < 0xf0)
2220 switch (code & 0xfc)
2221 {
2222 /* mov imm32,dn -> mov imm16,dn
2223 mov imm32,an -> mov imm16,an
2224 mov (abs32),dn -> mov (abs16),dn
2225 movbu (abs32),dn -> movbu (abs16),dn
2226 movhu (abs32),dn -> movhu (abs16),dn */
2227 case 0xcc:
2228 case 0xdc:
2229 case 0xa4:
2230 case 0xa8:
2231 case 0xac:
2232 /* Not safe if the high bit is on as relaxing may
2233 move the value out of high mem and thus not fit
2234 in a signed 16bit value. */
2235 if (code == 0xcc
2236 && (value & 0x8000))
2237 continue;
2238
2239 /* Note that we've changed the relocation contents, etc. */
2240 elf_section_data (sec)->relocs = internal_relocs;
2241 free_relocs = NULL;
2242
2243 elf_section_data (sec)->this_hdr.contents = contents;
2244 free_contents = NULL;
2245
2246 symtab_hdr->contents = (bfd_byte *) extsyms;
2247 free_extsyms = NULL;
2248
2249 if ((code & 0xfc) == 0xcc)
2250 code = 0x2c + (code & 0x03);
2251 else if ((code & 0xfc) == 0xdc)
2252 code = 0x24 + (code & 0x03);
2253 else if ((code & 0xfc) == 0xa4)
2254 code = 0x30 + (code & 0x03);
2255 else if ((code & 0xfc) == 0xa8)
2256 code = 0x34 + (code & 0x03);
2257 else if ((code & 0xfc) == 0xac)
2258 code = 0x38 + (code & 0x03);
2259 else
2260 abort ();
2261
2262 /* Fix the opcode. */
2263 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2264
2265 /* Fix the relocation's type. */
2266 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2267 R_MN10300_16);
2268
2269 /* The opcode got shorter too, so we have to fix the
2270 addend and offset too! */
2271 irel->r_offset -= 1;
2272
2273 /* Delete three bytes of data. */
2274 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2275 irel->r_offset + 1, 3))
2276 goto error_return;
2277
2278 /* That will change things, so, we should relax again.
2279 Note that this is not required, and it may be slow. */
2280 *again = true;
2281 break;
2282
2283 /* mov (abs32),an -> mov (abs16),an
2284 mov (d32,sp),an -> mov (d32,sp),an
2285 mov (d32,sp),dn -> mov (d32,sp),dn
2286 movbu (d32,sp),dn -> movbu (d32,sp),dn
2287 movhu (d32,sp),dn -> movhu (d32,sp),dn
2288 add imm32,dn -> add imm16,dn
2289 cmp imm32,dn -> cmp imm16,dn
2290 add imm32,an -> add imm16,an
2291 cmp imm32,an -> cmp imm16,an
2292 and imm32,dn -> and imm32,dn
2293 or imm32,dn -> or imm32,dn
2294 xor imm32,dn -> xor imm32,dn
2295 btst imm32,dn -> btst imm32,dn */
2296
2297 case 0xa0:
2298 case 0xb0:
2299 case 0xb1:
2300 case 0xb2:
2301 case 0xb3:
2302 case 0xc0:
2303 case 0xc8:
2304
2305 case 0xd0:
2306 case 0xd8:
2307 case 0xe0:
2308 case 0xe1:
2309 case 0xe2:
2310 case 0xe3:
2311 /* Note that we've changed the relocation contents, etc. */
2312 elf_section_data (sec)->relocs = internal_relocs;
2313 free_relocs = NULL;
2314
2315 elf_section_data (sec)->this_hdr.contents = contents;
2316 free_contents = NULL;
2317
2318 symtab_hdr->contents = (bfd_byte *) extsyms;
2319 free_extsyms = NULL;
2320
2321 /* Fix the opcode. */
2322 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2323 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2324
2325 /* Fix the relocation's type. */
2326 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2327 R_MN10300_16);
2328
2329 /* Delete two bytes of data. */
2330 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2331 irel->r_offset + 2, 2))
2332 goto error_return;
2333
2334 /* That will change things, so, we should relax again.
2335 Note that this is not required, and it may be slow. */
2336 *again = true;
2337 break;
2338 }
2339 else if (code == 0xfe)
2340 {
2341 /* add imm32,sp -> add imm16,sp */
2342
2343 /* Note that we've changed the relocation contents, etc. */
2344 elf_section_data (sec)->relocs = internal_relocs;
2345 free_relocs = NULL;
2346
2347 elf_section_data (sec)->this_hdr.contents = contents;
2348 free_contents = NULL;
2349
2350 symtab_hdr->contents = (bfd_byte *) extsyms;
2351 free_extsyms = NULL;
2352
2353 /* Fix the opcode. */
2354 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2355 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2356
2357 /* Fix the relocation's type. */
2358 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2359 R_MN10300_16);
2360
2361 /* Delete two bytes of data. */
2362 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2363 irel->r_offset + 2, 2))
2364 goto error_return;
2365
2366 /* That will change things, so, we should relax again.
2367 Note that this is not required, and it may be slow. */
2368 *again = true;
2369 break;
2370 }
2371 }
2372 }
2373 }
2374
2375 if (free_relocs != NULL)
2376 {
2377 free (free_relocs);
2378 free_relocs = NULL;
2379 }
2380
2381 if (free_contents != NULL)
2382 {
2383 if (! link_info->keep_memory)
2384 free (free_contents);
2385 else
2386 {
2387 /* Cache the section contents for elf_link_input_bfd. */
2388 elf_section_data (sec)->this_hdr.contents = contents;
2389 }
2390 free_contents = NULL;
2391 }
2392
2393 if (free_extsyms != NULL)
2394 {
2395 if (! link_info->keep_memory)
2396 free (free_extsyms);
2397 else
2398 {
2399 /* Cache the symbols for elf_link_input_bfd. */
2400 symtab_hdr->contents = extsyms;
2401 }
2402 free_extsyms = NULL;
2403 }
2404
2405 return true;
2406
2407 error_return:
2408 if (free_relocs != NULL)
2409 free (free_relocs);
2410 if (free_contents != NULL)
2411 free (free_contents);
2412 if (free_extsyms != NULL)
2413 free (free_extsyms);
2414 return false;
2415 }
2416
2417 /* Compute the stack size and movm arguments for the function
2418 referred to by HASH at address ADDR in section with
2419 contents CONTENTS, store the information in the hash table. */
2420 static void
2421 compute_function_info (abfd, hash, addr, contents)
2422 bfd *abfd;
2423 struct elf32_mn10300_link_hash_entry *hash;
2424 bfd_vma addr;
2425 unsigned char *contents;
2426 {
2427 unsigned char byte1, byte2;
2428 /* We only care about a very small subset of the possible prologue
2429 sequences here. Basically we look for:
2430
2431 movm [d2,d3,a2,a3],sp (optional)
2432 add <size>,sp (optional, and only for sizes which fit in an unsigned
2433 8 bit number)
2434
2435 If we find anything else, we quit. */
2436
2437 /* Look for movm [regs],sp */
2438 byte1 = bfd_get_8 (abfd, contents + addr);
2439 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2440
2441 if (byte1 == 0xcf)
2442 {
2443 hash->movm_args = byte2;
2444 addr += 2;
2445 byte1 = bfd_get_8 (abfd, contents + addr);
2446 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2447 }
2448
2449 /* Now figure out how much stack space will be allocated by the movm
2450 instruction. We need this kept separate from the funtion's normal
2451 stack space. */
2452 if (hash->movm_args)
2453 {
2454 /* Space for d2. */
2455 if (hash->movm_args & 0x80)
2456 hash->movm_stack_size += 4;
2457
2458 /* Space for d3. */
2459 if (hash->movm_args & 0x40)
2460 hash->movm_stack_size += 4;
2461
2462 /* Space for a2. */
2463 if (hash->movm_args & 0x20)
2464 hash->movm_stack_size += 4;
2465
2466 /* Space for a3. */
2467 if (hash->movm_args & 0x10)
2468 hash->movm_stack_size += 4;
2469
2470 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2471 if (hash->movm_args & 0x08)
2472 hash->movm_stack_size += 8 * 4;
2473
2474 if (bfd_get_mach (abfd) == bfd_mach_am33)
2475 {
2476 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2477 if (hash->movm_args & 0x1)
2478 hash->movm_stack_size += 6 * 4;
2479
2480 /* exreg1 space. e4, e5, e6, e7 */
2481 if (hash->movm_args & 0x2)
2482 hash->movm_stack_size += 4 * 4;
2483
2484 /* exreg0 space. e2, e3 */
2485 if (hash->movm_args & 0x4)
2486 hash->movm_stack_size += 2 * 4;
2487 }
2488 }
2489
2490 /* Now look for the two stack adjustment variants. */
2491 if (byte1 == 0xf8 && byte2 == 0xfe)
2492 {
2493 int temp = bfd_get_8 (abfd, contents + addr + 2);
2494 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2495
2496 hash->stack_size = -temp;
2497 }
2498 else if (byte1 == 0xfa && byte2 == 0xfe)
2499 {
2500 int temp = bfd_get_16 (abfd, contents + addr + 2);
2501 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2502 temp = -temp;
2503
2504 if (temp < 255)
2505 hash->stack_size = temp;
2506 }
2507
2508 /* If the total stack to be allocated by the call instruction is more
2509 than 255 bytes, then we can't remove the stack adjustment by using
2510 "call" (we might still be able to remove the "movm" instruction. */
2511 if (hash->stack_size + hash->movm_stack_size > 255)
2512 hash->stack_size = 0;
2513
2514 return;
2515 }
2516
2517 /* Delete some bytes from a section while relaxing. */
2518
2519 static boolean
2520 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2521 bfd *abfd;
2522 asection *sec;
2523 bfd_vma addr;
2524 int count;
2525 {
2526 Elf_Internal_Shdr *symtab_hdr;
2527 Elf32_External_Sym *extsyms;
2528 int shndx, index;
2529 bfd_byte *contents;
2530 Elf_Internal_Rela *irel, *irelend;
2531 Elf_Internal_Rela *irelalign;
2532 bfd_vma toaddr;
2533 Elf32_External_Sym *esym, *esymend;
2534 struct elf32_mn10300_link_hash_entry *sym_hash;
2535
2536 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2537 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
2538
2539 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2540
2541 contents = elf_section_data (sec)->this_hdr.contents;
2542
2543 /* The deletion must stop at the next ALIGN reloc for an aligment
2544 power larger than the number of bytes we are deleting. */
2545
2546 irelalign = NULL;
2547 toaddr = sec->_cooked_size;
2548
2549 irel = elf_section_data (sec)->relocs;
2550 irelend = irel + sec->reloc_count;
2551
2552 /* Actually delete the bytes. */
2553 memmove (contents + addr, contents + addr + count, toaddr - addr - count);
2554 sec->_cooked_size -= count;
2555
2556 /* Adjust all the relocs. */
2557 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2558 {
2559 /* Get the new reloc address. */
2560 if ((irel->r_offset > addr
2561 && irel->r_offset < toaddr))
2562 irel->r_offset -= count;
2563 }
2564
2565 /* Adjust the local symbols defined in this section. */
2566 esym = extsyms;
2567 esymend = esym + symtab_hdr->sh_info;
2568 for (; esym < esymend; esym++)
2569 {
2570 Elf_Internal_Sym isym;
2571
2572 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2573
2574 if (isym.st_shndx == shndx
2575 && isym.st_value > addr
2576 && isym.st_value < toaddr)
2577 {
2578 isym.st_value -= count;
2579 bfd_elf32_swap_symbol_out (abfd, &isym, esym);
2580 }
2581 }
2582
2583 /* Now adjust the global symbols defined in this section. */
2584 esym = extsyms + symtab_hdr->sh_info;
2585 esymend = extsyms + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym));
2586 for (index = 0; esym < esymend; esym++, index++)
2587 {
2588 Elf_Internal_Sym isym;
2589
2590 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2591 sym_hash = (struct elf32_mn10300_link_hash_entry *)
2592 (elf_sym_hashes (abfd)[index]);
2593 if (isym.st_shndx == shndx
2594 && ((sym_hash)->root.root.type == bfd_link_hash_defined
2595 || (sym_hash)->root.root.type == bfd_link_hash_defweak)
2596 && (sym_hash)->root.root.u.def.section == sec
2597 && (sym_hash)->root.root.u.def.value > addr
2598 && (sym_hash)->root.root.u.def.value < toaddr)
2599 {
2600 (sym_hash)->root.root.u.def.value -= count;
2601 }
2602 }
2603
2604 return true;
2605 }
2606
2607 /* Return true if a symbol exists at the given address, else return
2608 false. */
2609 static boolean
2610 mn10300_elf_symbol_address_p (abfd, sec, extsyms, addr)
2611 bfd *abfd;
2612 asection *sec;
2613 Elf32_External_Sym *extsyms;
2614 bfd_vma addr;
2615 {
2616 Elf_Internal_Shdr *symtab_hdr;
2617 int shndx;
2618 Elf32_External_Sym *esym, *esymend;
2619 struct elf32_mn10300_link_hash_entry **sym_hash, **sym_hash_end;
2620
2621 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2622 shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2623
2624 /* Examine all the symbols. */
2625 esym = extsyms;
2626 esymend = esym + symtab_hdr->sh_info;
2627 for (; esym < esymend; esym++)
2628 {
2629 Elf_Internal_Sym isym;
2630
2631 bfd_elf32_swap_symbol_in (abfd, esym, &isym);
2632
2633 if (isym.st_shndx == shndx
2634 && isym.st_value == addr)
2635 return true;
2636 }
2637
2638 sym_hash = (struct elf32_mn10300_link_hash_entry **) (elf_sym_hashes (abfd));
2639 sym_hash_end = (sym_hash
2640 + (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2641 - symtab_hdr->sh_info));
2642 for (; sym_hash < sym_hash_end; sym_hash++)
2643 {
2644 if (((*sym_hash)->root.root.type == bfd_link_hash_defined
2645 || (*sym_hash)->root.root.type == bfd_link_hash_defweak)
2646 && (*sym_hash)->root.root.u.def.section == sec
2647 && (*sym_hash)->root.root.u.def.value == addr)
2648 return true;
2649 }
2650 return false;
2651 }
2652
2653 /* This is a version of bfd_generic_get_relocated_section_contents
2654 which uses mn10300_elf_relocate_section. */
2655
2656 static bfd_byte *
2657 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2658 data, relocateable, symbols)
2659 bfd *output_bfd;
2660 struct bfd_link_info *link_info;
2661 struct bfd_link_order *link_order;
2662 bfd_byte *data;
2663 boolean relocateable;
2664 asymbol **symbols;
2665 {
2666 Elf_Internal_Shdr *symtab_hdr;
2667 asection *input_section = link_order->u.indirect.section;
2668 bfd *input_bfd = input_section->owner;
2669 asection **sections = NULL;
2670 Elf_Internal_Rela *internal_relocs = NULL;
2671 Elf32_External_Sym *external_syms = NULL;
2672 Elf_Internal_Sym *internal_syms = NULL;
2673
2674 /* We only need to handle the case of relaxing, or of having a
2675 particular set of section contents, specially. */
2676 if (relocateable
2677 || elf_section_data (input_section)->this_hdr.contents == NULL)
2678 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2679 link_order, data,
2680 relocateable,
2681 symbols);
2682
2683 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2684
2685 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2686 input_section->_raw_size);
2687
2688 if ((input_section->flags & SEC_RELOC) != 0
2689 && input_section->reloc_count > 0)
2690 {
2691 Elf_Internal_Sym *isymp;
2692 asection **secpp;
2693 Elf32_External_Sym *esym, *esymend;
2694
2695 if (symtab_hdr->contents != NULL)
2696 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
2697 else
2698 {
2699 external_syms = ((Elf32_External_Sym *)
2700 bfd_malloc (symtab_hdr->sh_info
2701 * sizeof (Elf32_External_Sym)));
2702 if (external_syms == NULL && symtab_hdr->sh_info > 0)
2703 goto error_return;
2704 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
2705 || (bfd_read (external_syms, sizeof (Elf32_External_Sym),
2706 symtab_hdr->sh_info, input_bfd)
2707 != (symtab_hdr->sh_info * sizeof (Elf32_External_Sym))))
2708 goto error_return;
2709 }
2710
2711 internal_relocs = (_bfd_elf32_link_read_relocs
2712 (input_bfd, input_section, (PTR) NULL,
2713 (Elf_Internal_Rela *) NULL, false));
2714 if (internal_relocs == NULL)
2715 goto error_return;
2716
2717 internal_syms = ((Elf_Internal_Sym *)
2718 bfd_malloc (symtab_hdr->sh_info
2719 * sizeof (Elf_Internal_Sym)));
2720 if (internal_syms == NULL && symtab_hdr->sh_info > 0)
2721 goto error_return;
2722
2723 sections = (asection **) bfd_malloc (symtab_hdr->sh_info
2724 * sizeof (asection *));
2725 if (sections == NULL && symtab_hdr->sh_info > 0)
2726 goto error_return;
2727
2728 isymp = internal_syms;
2729 secpp = sections;
2730 esym = external_syms;
2731 esymend = esym + symtab_hdr->sh_info;
2732 for (; esym < esymend; ++esym, ++isymp, ++secpp)
2733 {
2734 asection *isec;
2735
2736 bfd_elf32_swap_symbol_in (input_bfd, esym, isymp);
2737
2738 if (isymp->st_shndx == SHN_UNDEF)
2739 isec = bfd_und_section_ptr;
2740 else if (isymp->st_shndx > 0 && isymp->st_shndx < SHN_LORESERVE)
2741 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
2742 else if (isymp->st_shndx == SHN_ABS)
2743 isec = bfd_abs_section_ptr;
2744 else if (isymp->st_shndx == SHN_COMMON)
2745 isec = bfd_com_section_ptr;
2746 else
2747 {
2748 /* Who knows? */
2749 isec = NULL;
2750 }
2751
2752 *secpp = isec;
2753 }
2754
2755 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2756 input_section, data, internal_relocs,
2757 internal_syms, sections))
2758 goto error_return;
2759
2760 if (sections != NULL)
2761 free (sections);
2762 sections = NULL;
2763 if (internal_syms != NULL)
2764 free (internal_syms);
2765 internal_syms = NULL;
2766 if (external_syms != NULL && symtab_hdr->contents == NULL)
2767 free (external_syms);
2768 external_syms = NULL;
2769 if (internal_relocs != elf_section_data (input_section)->relocs)
2770 free (internal_relocs);
2771 internal_relocs = NULL;
2772 }
2773
2774 return data;
2775
2776 error_return:
2777 if (internal_relocs != NULL
2778 && internal_relocs != elf_section_data (input_section)->relocs)
2779 free (internal_relocs);
2780 if (external_syms != NULL && symtab_hdr->contents == NULL)
2781 free (external_syms);
2782 if (internal_syms != NULL)
2783 free (internal_syms);
2784 if (sections != NULL)
2785 free (sections);
2786 return NULL;
2787 }
2788
2789 /* Assorted hash table functions. */
2790
2791 /* Initialize an entry in the link hash table. */
2792
2793 /* Create an entry in an MN10300 ELF linker hash table. */
2794
2795 static struct bfd_hash_entry *
2796 elf32_mn10300_link_hash_newfunc (entry, table, string)
2797 struct bfd_hash_entry *entry;
2798 struct bfd_hash_table *table;
2799 const char *string;
2800 {
2801 struct elf32_mn10300_link_hash_entry *ret =
2802 (struct elf32_mn10300_link_hash_entry *) entry;
2803
2804 /* Allocate the structure if it has not already been allocated by a
2805 subclass. */
2806 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2807 ret = ((struct elf32_mn10300_link_hash_entry *)
2808 bfd_hash_allocate (table,
2809 sizeof (struct elf32_mn10300_link_hash_entry)));
2810 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2811 return (struct bfd_hash_entry *) ret;
2812
2813 /* Call the allocation method of the superclass. */
2814 ret = ((struct elf32_mn10300_link_hash_entry *)
2815 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2816 table, string));
2817 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2818 {
2819 ret->direct_calls = 0;
2820 ret->stack_size = 0;
2821 ret->movm_stack_size = 0;
2822 ret->flags = 0;
2823 ret->movm_args = 0;
2824 }
2825
2826 return (struct bfd_hash_entry *) ret;
2827 }
2828
2829 /* Create an mn10300 ELF linker hash table. */
2830
2831 static struct bfd_link_hash_table *
2832 elf32_mn10300_link_hash_table_create (abfd)
2833 bfd *abfd;
2834 {
2835 struct elf32_mn10300_link_hash_table *ret;
2836
2837 ret = ((struct elf32_mn10300_link_hash_table *)
2838 bfd_alloc (abfd, sizeof (struct elf32_mn10300_link_hash_table)));
2839 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2840 return NULL;
2841
2842 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2843 elf32_mn10300_link_hash_newfunc))
2844 {
2845 bfd_release (abfd, ret);
2846 return NULL;
2847 }
2848
2849 ret->flags = 0;
2850 ret->static_hash_table
2851 = ((struct elf32_mn10300_link_hash_table *)
2852 bfd_alloc (abfd, sizeof (struct elf_link_hash_table)));
2853 if (ret->static_hash_table == NULL)
2854 {
2855 bfd_release (abfd, ret);
2856 return NULL;
2857 }
2858
2859 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2860 elf32_mn10300_link_hash_newfunc))
2861 {
2862 bfd_release (abfd, ret->static_hash_table);
2863 bfd_release (abfd, ret);
2864 return NULL;
2865 }
2866 return &ret->root.root;
2867 }
2868
2869 static int
2870 elf_mn10300_mach (flags)
2871 flagword flags;
2872 {
2873 switch (flags & EF_MN10300_MACH)
2874 {
2875 case E_MN10300_MACH_MN10300:
2876 default:
2877 return bfd_mach_mn10300;
2878
2879 case E_MN10300_MACH_AM33:
2880 return bfd_mach_am33;
2881 }
2882 }
2883
2884 /* The final processing done just before writing out a MN10300 ELF object
2885 file. This gets the MN10300 architecture right based on the machine
2886 number. */
2887
2888 void
2889 _bfd_mn10300_elf_final_write_processing (abfd, linker)
2890 bfd *abfd;
2891 boolean linker ATTRIBUTE_UNUSED;
2892 {
2893 unsigned long val;
2894
2895 switch (bfd_get_mach (abfd))
2896 {
2897 default:
2898 case bfd_mach_mn10300:
2899 val = E_MN10300_MACH_MN10300;
2900 break;
2901
2902 case bfd_mach_am33:
2903 val = E_MN10300_MACH_AM33;
2904 break;
2905 }
2906
2907 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
2908 elf_elfheader (abfd)->e_flags |= val;
2909 }
2910
2911 boolean
2912 _bfd_mn10300_elf_object_p (abfd)
2913 bfd *abfd;
2914 {
2915 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
2916 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
2917 return true;
2918 }
2919
2920 /* Merge backend specific data from an object file to the output
2921 object file when linking. */
2922
2923 boolean
2924 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
2925 bfd *ibfd;
2926 bfd *obfd;
2927 {
2928 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2929 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2930 return true;
2931
2932 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2933 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
2934 {
2935 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2936 bfd_get_mach (ibfd)))
2937 return false;
2938 }
2939
2940 return true;
2941 }
2942
2943 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2944 #define TARGET_LITTLE_NAME "elf32-mn10300"
2945 #define ELF_ARCH bfd_arch_mn10300
2946 #define ELF_MACHINE_CODE EM_CYGNUS_MN10300
2947 #define ELF_MAXPAGESIZE 0x1000
2948
2949 #define elf_info_to_howto mn10300_info_to_howto
2950 #define elf_info_to_howto_rel 0
2951 #define elf_backend_can_gc_sections 1
2952 #define elf_backend_check_relocs mn10300_elf_check_relocs
2953 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2954 #define elf_backend_relocate_section mn10300_elf_relocate_section
2955 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2956 #define bfd_elf32_bfd_get_relocated_section_contents \
2957 mn10300_elf_get_relocated_section_contents
2958 #define bfd_elf32_bfd_link_hash_table_create \
2959 elf32_mn10300_link_hash_table_create
2960
2961 #define elf_symbol_leading_char '_'
2962
2963 /* So we can set bits in e_flags. */
2964 #define elf_backend_final_write_processing \
2965 _bfd_mn10300_elf_final_write_processing
2966 #define elf_backend_object_p _bfd_mn10300_elf_object_p
2967
2968 #define bfd_elf32_bfd_merge_private_bfd_data \
2969 _bfd_mn10300_elf_merge_private_bfd_data
2970
2971 #include "elf32-target.h"
This page took 0.092849 seconds and 4 git commands to generate.