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