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