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