source: trunk/src/binutils/bfd/elf-m10300.c@ 536

Last change on this file since 536 was 10, checked in by bird, 22 years ago

Initial revision

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