]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blob - bfd/elf-m10300.c
* elf-bfd.h (struct elf_reloc_cookie): Remove locsym_shndx,
[thirdparty/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 *, 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 /* 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_Rela *internal_relocs = NULL;
787 Elf_Internal_Rela *irel, *irelend;
788 bfd_byte *contents = NULL;
789 Elf_Internal_Sym *isymbuf = NULL;
790 struct elf32_mn10300_link_hash_table *hash_table;
791 asection *section = sec;
792
793 /* Assume nothing changes. */
794 *again = false;
795
796 /* We need a pointer to the mn10300 specific hash table. */
797 hash_table = elf32_mn10300_hash_table (link_info);
798
799 /* Initialize fields in each hash table entry the first time through. */
800 if ((hash_table->flags & MN10300_HASH_ENTRIES_INITIALIZED) == 0)
801 {
802 bfd *input_bfd;
803
804 /* Iterate over all the input bfds. */
805 for (input_bfd = link_info->input_bfds;
806 input_bfd != NULL;
807 input_bfd = input_bfd->link_next)
808 {
809 /* We're going to need all the symbols for each bfd. */
810 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
811 if (symtab_hdr->sh_info != 0)
812 {
813 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
814 if (isymbuf == NULL)
815 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
816 symtab_hdr->sh_info, 0,
817 NULL, NULL, NULL);
818 if (isymbuf == NULL)
819 goto error_return;
820 }
821
822 /* Iterate over each section in this bfd. */
823 for (section = input_bfd->sections;
824 section != NULL;
825 section = section->next)
826 {
827 struct elf32_mn10300_link_hash_entry *hash;
828 Elf_Internal_Sym *sym;
829 asection *sym_sec = NULL;
830 const char *sym_name;
831 char *new_name;
832
833 /* If there's nothing to do in this section, skip it. */
834 if (! (((section->flags & SEC_RELOC) != 0
835 && section->reloc_count != 0)
836 || (section->flags & SEC_CODE) != 0))
837 continue;
838
839 /* Get cached copy of section contents if it exists. */
840 if (elf_section_data (section)->this_hdr.contents != NULL)
841 contents = elf_section_data (section)->this_hdr.contents;
842 else if (section->_raw_size != 0)
843 {
844 /* Go get them off disk. */
845 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
846 if (contents == NULL)
847 goto error_return;
848
849 if (!bfd_get_section_contents (input_bfd, section,
850 contents, (file_ptr) 0,
851 section->_raw_size))
852 goto error_return;
853 }
854 else
855 contents = NULL;
856
857 /* If there aren't any relocs, then there's nothing to do. */
858 if ((section->flags & SEC_RELOC) != 0
859 && section->reloc_count != 0)
860 {
861
862 /* Get a copy of the native relocations. */
863 internal_relocs = (_bfd_elf32_link_read_relocs
864 (input_bfd, section, (PTR) NULL,
865 (Elf_Internal_Rela *) NULL,
866 link_info->keep_memory));
867 if (internal_relocs == NULL)
868 goto error_return;
869
870 /* Now examine each relocation. */
871 irel = internal_relocs;
872 irelend = irel + section->reloc_count;
873 for (; irel < irelend; irel++)
874 {
875 long r_type;
876 unsigned long r_index;
877 unsigned char code;
878
879 r_type = ELF32_R_TYPE (irel->r_info);
880 r_index = ELF32_R_SYM (irel->r_info);
881
882 if (r_type < 0 || r_type >= (int) R_MN10300_MAX)
883 goto error_return;
884
885 /* We need the name and hash table entry of the target
886 symbol! */
887 hash = NULL;
888 sym = NULL;
889 sym_sec = NULL;
890
891 if (r_index < symtab_hdr->sh_info)
892 {
893 /* A local symbol. */
894 Elf_Internal_Sym *isym;
895 struct elf_link_hash_table *elftab;
896 bfd_size_type amt;
897
898 isym = isymbuf + r_index;
899 if (isym->st_shndx == SHN_UNDEF)
900 sym_sec = bfd_und_section_ptr;
901 else if (isym->st_shndx == SHN_ABS)
902 sym_sec = bfd_abs_section_ptr;
903 else if (isym->st_shndx == SHN_COMMON)
904 sym_sec = bfd_com_section_ptr;
905 else
906 sym_sec
907 = bfd_section_from_elf_index (input_bfd,
908 isym->st_shndx);
909
910 sym_name
911 = bfd_elf_string_from_elf_section (input_bfd,
912 (symtab_hdr
913 ->sh_link),
914 isym->st_name);
915
916 /* If it isn't a function, then we don't care
917 about it. */
918 if (ELF_ST_TYPE (isym->st_info) != STT_FUNC)
919 continue;
920
921 /* Tack on an ID so we can uniquely identify this
922 local symbol in the global hash table. */
923 amt = strlen (sym_name) + 10;
924 new_name = bfd_malloc (amt);
925 if (new_name == 0)
926 goto error_return;
927
928 sprintf (new_name, "%s_%08x",
929 sym_name, (int) sym_sec);
930 sym_name = new_name;
931
932 elftab = &hash_table->static_hash_table->root;
933 hash = ((struct elf32_mn10300_link_hash_entry *)
934 elf_link_hash_lookup (elftab, sym_name,
935 true, true, false));
936 free (new_name);
937 }
938 else
939 {
940 r_index -= symtab_hdr->sh_info;
941 hash = (struct elf32_mn10300_link_hash_entry *)
942 elf_sym_hashes (input_bfd)[r_index];
943 }
944
945 /* If this is not a "call" instruction, then we
946 should convert "call" instructions to "calls"
947 instructions. */
948 code = bfd_get_8 (input_bfd,
949 contents + irel->r_offset - 1);
950 if (code != 0xdd && code != 0xcd)
951 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
952
953 /* If this is a jump/call, then bump the
954 direct_calls counter. Else force "call" to
955 "calls" conversions. */
956 if (r_type == R_MN10300_PCREL32
957 || r_type == R_MN10300_PCREL16)
958 hash->direct_calls++;
959 else
960 hash->flags |= MN10300_CONVERT_CALL_TO_CALLS;
961 }
962 }
963
964 /* Now look at the actual contents to get the stack size,
965 and a list of what registers were saved in the prologue
966 (ie movm_args). */
967 if ((section->flags & SEC_CODE) != 0)
968 {
969 Elf_Internal_Sym *isym, *isymend;
970 unsigned int sec_shndx;
971 struct elf_link_hash_entry **hashes;
972 struct elf_link_hash_entry **end_hashes;
973 unsigned int symcount;
974
975 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
976 section);
977
978 /* Look at each function defined in this section and
979 update info for that function. */
980 isymend = isymbuf + symtab_hdr->sh_info;
981 for (isym = isymbuf; isym < isymend; isym++)
982 {
983 if (isym->st_shndx == sec_shndx
984 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
985 {
986 struct elf_link_hash_table *elftab;
987 bfd_size_type amt;
988
989 if (isym->st_shndx == SHN_UNDEF)
990 sym_sec = bfd_und_section_ptr;
991 else if (isym->st_shndx == SHN_ABS)
992 sym_sec = bfd_abs_section_ptr;
993 else if (isym->st_shndx == SHN_COMMON)
994 sym_sec = bfd_com_section_ptr;
995 else
996 sym_sec
997 = bfd_section_from_elf_index (input_bfd,
998 isym->st_shndx);
999
1000 sym_name = (bfd_elf_string_from_elf_section
1001 (input_bfd, symtab_hdr->sh_link,
1002 isym->st_name));
1003
1004 /* Tack on an ID so we can uniquely identify this
1005 local symbol in the global hash table. */
1006 amt = strlen (sym_name) + 10;
1007 new_name = bfd_malloc (amt);
1008 if (new_name == 0)
1009 goto error_return;
1010
1011 sprintf (new_name, "%s_%08x",
1012 sym_name, (int) sym_sec);
1013 sym_name = new_name;
1014
1015 elftab = &hash_table->static_hash_table->root;
1016 hash = ((struct elf32_mn10300_link_hash_entry *)
1017 elf_link_hash_lookup (elftab, sym_name,
1018 true, true, false));
1019 free (new_name);
1020 compute_function_info (input_bfd, hash,
1021 isym->st_value, contents);
1022 }
1023 }
1024
1025 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1026 - symtab_hdr->sh_info);
1027 hashes = elf_sym_hashes (abfd);
1028 end_hashes = hashes + symcount;
1029 for (; hashes < end_hashes; hashes++)
1030 {
1031 hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
1032 if ((hash->root.root.type == bfd_link_hash_defined
1033 || hash->root.root.type == bfd_link_hash_defweak)
1034 && hash->root.root.u.def.section == section
1035 && ELF_ST_TYPE (isym->st_info) == STT_FUNC)
1036 compute_function_info (input_bfd, hash,
1037 (hash)->root.root.u.def.value,
1038 contents);
1039 }
1040 }
1041
1042 /* Cache or free any memory we allocated for the relocs. */
1043 if (internal_relocs != NULL
1044 && elf_section_data (section)->relocs != internal_relocs)
1045 free (internal_relocs);
1046 internal_relocs = NULL;
1047
1048 /* Cache or free any memory we allocated for the contents. */
1049 if (contents != NULL
1050 && elf_section_data (section)->this_hdr.contents != contents)
1051 {
1052 if (! link_info->keep_memory)
1053 free (contents);
1054 else
1055 {
1056 /* Cache the section contents for elf_link_input_bfd. */
1057 elf_section_data (section)->this_hdr.contents = contents;
1058 }
1059 }
1060 contents = NULL;
1061 }
1062
1063 /* Cache or free any memory we allocated for the symbols. */
1064 if (isymbuf != NULL
1065 && symtab_hdr->contents != (unsigned char *) isymbuf)
1066 {
1067 if (! link_info->keep_memory)
1068 free (isymbuf);
1069 else
1070 {
1071 /* Cache the symbols for elf_link_input_bfd. */
1072 symtab_hdr->contents = (unsigned char *) isymbuf;
1073 }
1074 }
1075 isymbuf = NULL;
1076 }
1077
1078 /* Now iterate on each symbol in the hash table and perform
1079 the final initialization steps on each. */
1080 elf32_mn10300_link_hash_traverse (hash_table,
1081 elf32_mn10300_finish_hash_table_entry,
1082 NULL);
1083 elf32_mn10300_link_hash_traverse (hash_table->static_hash_table,
1084 elf32_mn10300_finish_hash_table_entry,
1085 NULL);
1086
1087 /* All entries in the hash table are fully initialized. */
1088 hash_table->flags |= MN10300_HASH_ENTRIES_INITIALIZED;
1089
1090 /* Now that everything has been initialized, go through each
1091 code section and delete any prologue insns which will be
1092 redundant because their operations will be performed by
1093 a "call" instruction. */
1094 for (input_bfd = link_info->input_bfds;
1095 input_bfd != NULL;
1096 input_bfd = input_bfd->link_next)
1097 {
1098 /* We're going to need all the local symbols for each bfd. */
1099 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1100 if (symtab_hdr->sh_info != 0)
1101 {
1102 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1103 if (isymbuf == NULL)
1104 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
1105 symtab_hdr->sh_info, 0,
1106 NULL, NULL, NULL);
1107 if (isymbuf == NULL)
1108 goto error_return;
1109 }
1110
1111 /* Walk over each section in this bfd. */
1112 for (section = input_bfd->sections;
1113 section != NULL;
1114 section = section->next)
1115 {
1116 unsigned int sec_shndx;
1117 Elf_Internal_Sym *isym, *isymend;
1118 struct elf_link_hash_entry **hashes;
1119 struct elf_link_hash_entry **end_hashes;
1120 unsigned int symcount;
1121
1122 /* Skip non-code sections and empty sections. */
1123 if ((section->flags & SEC_CODE) == 0 || section->_raw_size == 0)
1124 continue;
1125
1126 if (section->reloc_count != 0)
1127 {
1128 /* Get a copy of the native relocations. */
1129 internal_relocs = (_bfd_elf32_link_read_relocs
1130 (input_bfd, section, (PTR) NULL,
1131 (Elf_Internal_Rela *) NULL,
1132 link_info->keep_memory));
1133 if (internal_relocs == NULL)
1134 goto error_return;
1135 }
1136
1137 /* Get cached copy of section contents if it exists. */
1138 if (elf_section_data (section)->this_hdr.contents != NULL)
1139 contents = elf_section_data (section)->this_hdr.contents;
1140 else
1141 {
1142 /* Go get them off disk. */
1143 contents = (bfd_byte *) bfd_malloc (section->_raw_size);
1144 if (contents == NULL)
1145 goto error_return;
1146
1147 if (!bfd_get_section_contents (input_bfd, section,
1148 contents, (file_ptr) 0,
1149 section->_raw_size))
1150 goto error_return;
1151 }
1152
1153 sec_shndx = _bfd_elf_section_from_bfd_section (input_bfd,
1154 section);
1155
1156 /* Now look for any function in this section which needs
1157 insns deleted from its prologue. */
1158 isymend = isymbuf + symtab_hdr->sh_info;
1159 for (isym = isymbuf; isym < isymend; isym++)
1160 {
1161 struct elf32_mn10300_link_hash_entry *sym_hash;
1162 asection *sym_sec = NULL;
1163 const char *sym_name;
1164 char *new_name;
1165 struct elf_link_hash_table *elftab;
1166 bfd_size_type amt;
1167
1168 if (isym->st_shndx != sec_shndx)
1169 continue;
1170
1171 if (isym->st_shndx == SHN_UNDEF)
1172 sym_sec = bfd_und_section_ptr;
1173 else if (isym->st_shndx == SHN_ABS)
1174 sym_sec = bfd_abs_section_ptr;
1175 else if (isym->st_shndx == SHN_COMMON)
1176 sym_sec = bfd_com_section_ptr;
1177 else
1178 sym_sec
1179 = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
1180
1181 sym_name
1182 = bfd_elf_string_from_elf_section (input_bfd,
1183 symtab_hdr->sh_link,
1184 isym->st_name);
1185
1186 /* Tack on an ID so we can uniquely identify this
1187 local symbol in the global hash table. */
1188 amt = strlen (sym_name) + 10;
1189 new_name = bfd_malloc (amt);
1190 if (new_name == 0)
1191 goto error_return;
1192 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1193 sym_name = new_name;
1194
1195 elftab = &hash_table->static_hash_table->root;
1196 sym_hash = ((struct elf32_mn10300_link_hash_entry *)
1197 elf_link_hash_lookup (elftab, sym_name,
1198 false, false, false));
1199
1200 free (new_name);
1201 if (sym_hash == NULL)
1202 continue;
1203
1204 if (! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1205 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1206 {
1207 int bytes = 0;
1208
1209 /* Note that we've changed things. */
1210 elf_section_data (section)->relocs = internal_relocs;
1211 elf_section_data (section)->this_hdr.contents = contents;
1212 symtab_hdr->contents = (unsigned char *) isymbuf;
1213
1214 /* Count how many bytes we're going to delete. */
1215 if (sym_hash->movm_args)
1216 bytes += 2;
1217
1218 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1219 bytes += 3;
1220 else if (sym_hash->stack_size
1221 && sym_hash->stack_size < 256)
1222 bytes += 4;
1223
1224 /* Note that we've deleted prologue bytes for this
1225 function. */
1226 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1227
1228 /* Actually delete the bytes. */
1229 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1230 section,
1231 isym->st_value,
1232 bytes))
1233 goto error_return;
1234
1235 /* Something changed. Not strictly necessary, but
1236 may lead to more relaxing opportunities. */
1237 *again = true;
1238 }
1239 }
1240
1241 /* Look for any global functions in this section which
1242 need insns deleted from their prologues. */
1243 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1244 - symtab_hdr->sh_info);
1245 hashes = elf_sym_hashes (abfd);
1246 end_hashes = hashes + symcount;
1247 for (; hashes < end_hashes; hashes++)
1248 {
1249 struct elf32_mn10300_link_hash_entry *sym_hash;
1250
1251 sym_hash = (struct elf32_mn10300_link_hash_entry *) *hashes;
1252 if ((sym_hash->root.root.type == bfd_link_hash_defined
1253 || sym_hash->root.root.type == bfd_link_hash_defweak)
1254 && sym_hash->root.root.u.def.section == section
1255 && ! (sym_hash->flags & MN10300_CONVERT_CALL_TO_CALLS)
1256 && ! (sym_hash->flags & MN10300_DELETED_PROLOGUE_BYTES))
1257 {
1258 int bytes = 0;
1259 bfd_vma symval;
1260
1261 /* Note that we've changed things. */
1262 elf_section_data (section)->relocs = internal_relocs;
1263 elf_section_data (section)->this_hdr.contents = contents;
1264 symtab_hdr->contents = (unsigned char *) isymbuf;
1265
1266 /* Count how many bytes we're going to delete. */
1267 if (sym_hash->movm_args)
1268 bytes += 2;
1269
1270 if (sym_hash->stack_size && sym_hash->stack_size <= 128)
1271 bytes += 3;
1272 else if (sym_hash->stack_size
1273 && sym_hash->stack_size < 256)
1274 bytes += 4;
1275
1276 /* Note that we've deleted prologue bytes for this
1277 function. */
1278 sym_hash->flags |= MN10300_DELETED_PROLOGUE_BYTES;
1279
1280 /* Actually delete the bytes. */
1281 symval = sym_hash->root.root.u.def.value;
1282 if (!mn10300_elf_relax_delete_bytes (input_bfd,
1283 section,
1284 symval,
1285 bytes))
1286 goto error_return;
1287
1288 /* Something changed. Not strictly necessary, but
1289 may lead to more relaxing opportunities. */
1290 *again = true;
1291 }
1292 }
1293
1294 /* Cache or free any memory we allocated for the relocs. */
1295 if (internal_relocs != NULL
1296 && elf_section_data (section)->relocs != internal_relocs)
1297 free (internal_relocs);
1298 internal_relocs = NULL;
1299
1300 /* Cache or free any memory we allocated for the contents. */
1301 if (contents != NULL
1302 && elf_section_data (section)->this_hdr.contents != contents)
1303 {
1304 if (! link_info->keep_memory)
1305 free (contents);
1306 else
1307 {
1308 /* Cache the section contents for elf_link_input_bfd. */
1309 elf_section_data (section)->this_hdr.contents = contents;
1310 }
1311 }
1312 contents = NULL;
1313 }
1314
1315 /* Cache or free any memory we allocated for the symbols. */
1316 if (isymbuf != NULL
1317 && symtab_hdr->contents != (unsigned char *) isymbuf)
1318 {
1319 if (! link_info->keep_memory)
1320 free (isymbuf);
1321 else
1322 {
1323 /* Cache the symbols for elf_link_input_bfd. */
1324 symtab_hdr->contents = (unsigned char *) isymbuf;
1325 }
1326 }
1327 isymbuf = NULL;
1328 }
1329 }
1330
1331 /* (Re)initialize for the basic instruction shortening/relaxing pass. */
1332 contents = NULL;
1333 internal_relocs = NULL;
1334 isymbuf = NULL;
1335 /* For error_return. */
1336 section = sec;
1337
1338 /* We don't have to do anything for a relocateable link, if
1339 this section does not have relocs, or if this is not a
1340 code section. */
1341 if (link_info->relocateable
1342 || (sec->flags & SEC_RELOC) == 0
1343 || sec->reloc_count == 0
1344 || (sec->flags & SEC_CODE) == 0)
1345 return true;
1346
1347 /* If this is the first time we have been called for this section,
1348 initialize the cooked size. */
1349 if (sec->_cooked_size == 0)
1350 sec->_cooked_size = sec->_raw_size;
1351
1352 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1353
1354 /* Get a copy of the native relocations. */
1355 internal_relocs = (_bfd_elf32_link_read_relocs
1356 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
1357 link_info->keep_memory));
1358 if (internal_relocs == NULL)
1359 goto error_return;
1360
1361 /* Walk through them looking for relaxing opportunities. */
1362 irelend = internal_relocs + sec->reloc_count;
1363 for (irel = internal_relocs; irel < irelend; irel++)
1364 {
1365 bfd_vma symval;
1366 struct elf32_mn10300_link_hash_entry *h = NULL;
1367
1368 /* If this isn't something that can be relaxed, then ignore
1369 this reloc. */
1370 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_NONE
1371 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_8
1372 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_MAX)
1373 continue;
1374
1375 /* Get the section contents if we haven't done so already. */
1376 if (contents == NULL)
1377 {
1378 /* Get cached copy if it exists. */
1379 if (elf_section_data (sec)->this_hdr.contents != NULL)
1380 contents = elf_section_data (sec)->this_hdr.contents;
1381 else
1382 {
1383 /* Go get them off disk. */
1384 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
1385 if (contents == NULL)
1386 goto error_return;
1387
1388 if (! bfd_get_section_contents (abfd, sec, contents,
1389 (file_ptr) 0, sec->_raw_size))
1390 goto error_return;
1391 }
1392 }
1393
1394 /* Read this BFD's symbols if we haven't done so already. */
1395 if (isymbuf == NULL && symtab_hdr->sh_info != 0)
1396 {
1397 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
1398 if (isymbuf == NULL)
1399 isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
1400 symtab_hdr->sh_info, 0,
1401 NULL, NULL, NULL);
1402 if (isymbuf == NULL)
1403 goto error_return;
1404 }
1405
1406 /* Get the value of the symbol referred to by the reloc. */
1407 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
1408 {
1409 Elf_Internal_Sym *isym;
1410 asection *sym_sec = NULL;
1411 const char *sym_name;
1412 char *new_name;
1413
1414 /* A local symbol. */
1415 isym = isymbuf + ELF32_R_SYM (irel->r_info);
1416 if (isym->st_shndx == SHN_UNDEF)
1417 sym_sec = bfd_und_section_ptr;
1418 else if (isym->st_shndx == SHN_ABS)
1419 sym_sec = bfd_abs_section_ptr;
1420 else if (isym->st_shndx == SHN_COMMON)
1421 sym_sec = bfd_com_section_ptr;
1422 else
1423 sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
1424
1425 symval = (isym->st_value
1426 + sym_sec->output_section->vma
1427 + sym_sec->output_offset);
1428 sym_name = bfd_elf_string_from_elf_section (abfd,
1429 symtab_hdr->sh_link,
1430 isym->st_name);
1431
1432 /* Tack on an ID so we can uniquely identify this
1433 local symbol in the global hash table. */
1434 new_name = bfd_malloc ((bfd_size_type) strlen (sym_name) + 10);
1435 if (new_name == 0)
1436 goto error_return;
1437 sprintf (new_name, "%s_%08x", sym_name, (int) sym_sec);
1438 sym_name = new_name;
1439
1440 h = (struct elf32_mn10300_link_hash_entry *)
1441 elf_link_hash_lookup (&hash_table->static_hash_table->root,
1442 sym_name, false, false, false);
1443 free (new_name);
1444 }
1445 else
1446 {
1447 unsigned long indx;
1448
1449 /* An external symbol. */
1450 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
1451 h = (struct elf32_mn10300_link_hash_entry *)
1452 (elf_sym_hashes (abfd)[indx]);
1453 BFD_ASSERT (h != NULL);
1454 if (h->root.root.type != bfd_link_hash_defined
1455 && h->root.root.type != bfd_link_hash_defweak)
1456 {
1457 /* This appears to be a reference to an undefined
1458 symbol. Just ignore it--it will be caught by the
1459 regular reloc processing. */
1460 continue;
1461 }
1462
1463 symval = (h->root.root.u.def.value
1464 + h->root.root.u.def.section->output_section->vma
1465 + h->root.root.u.def.section->output_offset);
1466 }
1467
1468 /* For simplicity of coding, we are going to modify the section
1469 contents, the section relocs, and the BFD symbol table. We
1470 must tell the rest of the code not to free up this
1471 information. It would be possible to instead create a table
1472 of changes which have to be made, as is done in coff-mips.c;
1473 that would be more work, but would require less memory when
1474 the linker is run. */
1475
1476 /* Try to turn a 32bit pc-relative branch/call into a 16bit pc-relative
1477 branch/call, also deal with "call" -> "calls" conversions and
1478 insertion of prologue data into "call" instructions. */
1479 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL32)
1480 {
1481 bfd_vma value = symval;
1482
1483 /* If we've got a "call" instruction that needs to be turned
1484 into a "calls" instruction, do so now. It saves a byte. */
1485 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1486 {
1487 unsigned char code;
1488
1489 /* Get the opcode. */
1490 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1491
1492 /* Make sure we're working with a "call" instruction! */
1493 if (code == 0xdd)
1494 {
1495 /* Note that we've changed the relocs, section contents,
1496 etc. */
1497 elf_section_data (sec)->relocs = internal_relocs;
1498 elf_section_data (sec)->this_hdr.contents = contents;
1499 symtab_hdr->contents = (unsigned char *) isymbuf;
1500
1501 /* Fix the opcode. */
1502 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 1);
1503 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1504
1505 /* Fix irel->r_offset and irel->r_addend. */
1506 irel->r_offset += 1;
1507 irel->r_addend += 1;
1508
1509 /* Delete one byte of data. */
1510 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1511 irel->r_offset + 3, 1))
1512 goto error_return;
1513
1514 /* That will change things, so, we should relax again.
1515 Note that this is not required, and it may be slow. */
1516 *again = true;
1517 }
1518 }
1519 else if (h)
1520 {
1521 /* We've got a "call" instruction which needs some data
1522 from target function filled in. */
1523 unsigned char code;
1524
1525 /* Get the opcode. */
1526 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1527
1528 /* Insert data from the target function into the "call"
1529 instruction if needed. */
1530 if (code == 0xdd)
1531 {
1532 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 4);
1533 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1534 contents + irel->r_offset + 5);
1535 }
1536 }
1537
1538 /* Deal with pc-relative gunk. */
1539 value -= (sec->output_section->vma + sec->output_offset);
1540 value -= irel->r_offset;
1541 value += irel->r_addend;
1542
1543 /* See if the value will fit in 16 bits, note the high value is
1544 0x7fff + 2 as the target will be two bytes closer if we are
1545 able to relax. */
1546 if ((long) value < 0x8001 && (long) value > -0x8000)
1547 {
1548 unsigned char code;
1549
1550 /* Get the opcode. */
1551 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1552
1553 if (code != 0xdc && code != 0xdd && code != 0xff)
1554 continue;
1555
1556 /* Note that we've changed the relocs, section contents, etc. */
1557 elf_section_data (sec)->relocs = internal_relocs;
1558 elf_section_data (sec)->this_hdr.contents = contents;
1559 symtab_hdr->contents = (unsigned char *) isymbuf;
1560
1561 /* Fix the opcode. */
1562 if (code == 0xdc)
1563 bfd_put_8 (abfd, 0xcc, contents + irel->r_offset - 1);
1564 else if (code == 0xdd)
1565 bfd_put_8 (abfd, 0xcd, contents + irel->r_offset - 1);
1566 else if (code == 0xff)
1567 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
1568
1569 /* Fix the relocation's type. */
1570 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1571 R_MN10300_PCREL16);
1572
1573 /* Delete two bytes of data. */
1574 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1575 irel->r_offset + 1, 2))
1576 goto error_return;
1577
1578 /* That will change things, so, we should relax again.
1579 Note that this is not required, and it may be slow. */
1580 *again = true;
1581 }
1582 }
1583
1584 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
1585 branch. */
1586 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL16)
1587 {
1588 bfd_vma value = symval;
1589
1590 /* If we've got a "call" instruction that needs to be turned
1591 into a "calls" instruction, do so now. It saves a byte. */
1592 if (h && (h->flags & MN10300_CONVERT_CALL_TO_CALLS))
1593 {
1594 unsigned char code;
1595
1596 /* Get the opcode. */
1597 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1598
1599 /* Make sure we're working with a "call" instruction! */
1600 if (code == 0xcd)
1601 {
1602 /* Note that we've changed the relocs, section contents,
1603 etc. */
1604 elf_section_data (sec)->relocs = internal_relocs;
1605 elf_section_data (sec)->this_hdr.contents = contents;
1606 symtab_hdr->contents = (unsigned char *) isymbuf;
1607
1608 /* Fix the opcode. */
1609 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 1);
1610 bfd_put_8 (abfd, 0xff, contents + irel->r_offset);
1611
1612 /* Fix irel->r_offset and irel->r_addend. */
1613 irel->r_offset += 1;
1614 irel->r_addend += 1;
1615
1616 /* Delete one byte of data. */
1617 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1618 irel->r_offset + 1, 1))
1619 goto error_return;
1620
1621 /* That will change things, so, we should relax again.
1622 Note that this is not required, and it may be slow. */
1623 *again = true;
1624 }
1625 }
1626 else if (h)
1627 {
1628 unsigned char code;
1629
1630 /* Get the opcode. */
1631 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1632
1633 /* Insert data from the target function into the "call"
1634 instruction if needed. */
1635 if (code == 0xcd)
1636 {
1637 bfd_put_8 (abfd, h->movm_args, contents + irel->r_offset + 2);
1638 bfd_put_8 (abfd, h->stack_size + h->movm_stack_size,
1639 contents + irel->r_offset + 3);
1640 }
1641 }
1642
1643 /* Deal with pc-relative gunk. */
1644 value -= (sec->output_section->vma + sec->output_offset);
1645 value -= irel->r_offset;
1646 value += irel->r_addend;
1647
1648 /* See if the value will fit in 8 bits, note the high value is
1649 0x7f + 1 as the target will be one bytes closer if we are
1650 able to relax. */
1651 if ((long) value < 0x80 && (long) value > -0x80)
1652 {
1653 unsigned char code;
1654
1655 /* Get the opcode. */
1656 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1657
1658 if (code != 0xcc)
1659 continue;
1660
1661 /* Note that we've changed the relocs, section contents, etc. */
1662 elf_section_data (sec)->relocs = internal_relocs;
1663 elf_section_data (sec)->this_hdr.contents = contents;
1664 symtab_hdr->contents = (unsigned char *) isymbuf;
1665
1666 /* Fix the opcode. */
1667 bfd_put_8 (abfd, 0xca, contents + irel->r_offset - 1);
1668
1669 /* Fix the relocation's type. */
1670 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1671 R_MN10300_PCREL8);
1672
1673 /* Delete one byte of data. */
1674 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1675 irel->r_offset + 1, 1))
1676 goto error_return;
1677
1678 /* That will change things, so, we should relax again.
1679 Note that this is not required, and it may be slow. */
1680 *again = true;
1681 }
1682 }
1683
1684 /* Try to eliminate an unconditional 8 bit pc-relative branch
1685 which immediately follows a conditional 8 bit pc-relative
1686 branch around the unconditional branch.
1687
1688 original: new:
1689 bCC lab1 bCC' lab2
1690 bra lab2
1691 lab1: lab1:
1692
1693 This happens when the bCC can't reach lab2 at assembly time,
1694 but due to other relaxations it can reach at link time. */
1695 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_PCREL8)
1696 {
1697 Elf_Internal_Rela *nrel;
1698 bfd_vma value = symval;
1699 unsigned char code;
1700
1701 /* Deal with pc-relative gunk. */
1702 value -= (sec->output_section->vma + sec->output_offset);
1703 value -= irel->r_offset;
1704 value += irel->r_addend;
1705
1706 /* Do nothing if this reloc is the last byte in the section. */
1707 if (irel->r_offset == sec->_cooked_size)
1708 continue;
1709
1710 /* See if the next instruction is an unconditional pc-relative
1711 branch, more often than not this test will fail, so we
1712 test it first to speed things up. */
1713 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
1714 if (code != 0xca)
1715 continue;
1716
1717 /* Also make sure the next relocation applies to the next
1718 instruction and that it's a pc-relative 8 bit branch. */
1719 nrel = irel + 1;
1720 if (nrel == irelend
1721 || irel->r_offset + 2 != nrel->r_offset
1722 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10300_PCREL8)
1723 continue;
1724
1725 /* Make sure our destination immediately follows the
1726 unconditional branch. */
1727 if (symval != (sec->output_section->vma + sec->output_offset
1728 + irel->r_offset + 3))
1729 continue;
1730
1731 /* Now make sure we are a conditional branch. This may not
1732 be necessary, but why take the chance.
1733
1734 Note these checks assume that R_MN10300_PCREL8 relocs
1735 only occur on bCC and bCCx insns. If they occured
1736 elsewhere, we'd need to know the start of this insn
1737 for this check to be accurate. */
1738 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1739 if (code != 0xc0 && code != 0xc1 && code != 0xc2
1740 && code != 0xc3 && code != 0xc4 && code != 0xc5
1741 && code != 0xc6 && code != 0xc7 && code != 0xc8
1742 && code != 0xc9 && code != 0xe8 && code != 0xe9
1743 && code != 0xea && code != 0xeb)
1744 continue;
1745
1746 /* We also have to be sure there is no symbol/label
1747 at the unconditional branch. */
1748 if (mn10300_elf_symbol_address_p (abfd, sec, isymbuf,
1749 irel->r_offset + 1))
1750 continue;
1751
1752 /* Note that we've changed the relocs, section contents, etc. */
1753 elf_section_data (sec)->relocs = internal_relocs;
1754 elf_section_data (sec)->this_hdr.contents = contents;
1755 symtab_hdr->contents = (unsigned char *) isymbuf;
1756
1757 /* Reverse the condition of the first branch. */
1758 switch (code)
1759 {
1760 case 0xc8:
1761 code = 0xc9;
1762 break;
1763 case 0xc9:
1764 code = 0xc8;
1765 break;
1766 case 0xc0:
1767 code = 0xc2;
1768 break;
1769 case 0xc2:
1770 code = 0xc0;
1771 break;
1772 case 0xc3:
1773 code = 0xc1;
1774 break;
1775 case 0xc1:
1776 code = 0xc3;
1777 break;
1778 case 0xc4:
1779 code = 0xc6;
1780 break;
1781 case 0xc6:
1782 code = 0xc4;
1783 break;
1784 case 0xc7:
1785 code = 0xc5;
1786 break;
1787 case 0xc5:
1788 code = 0xc7;
1789 break;
1790 case 0xe8:
1791 code = 0xe9;
1792 break;
1793 case 0x9d:
1794 code = 0xe8;
1795 break;
1796 case 0xea:
1797 code = 0xeb;
1798 break;
1799 case 0xeb:
1800 code = 0xea;
1801 break;
1802 }
1803 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1804
1805 /* Set the reloc type and symbol for the first branch
1806 from the second branch. */
1807 irel->r_info = nrel->r_info;
1808
1809 /* Make the reloc for the second branch a null reloc. */
1810 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
1811 R_MN10300_NONE);
1812
1813 /* Delete two bytes of data. */
1814 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1815 irel->r_offset + 1, 2))
1816 goto error_return;
1817
1818 /* That will change things, so, we should relax again.
1819 Note that this is not required, and it may be slow. */
1820 *again = true;
1821 }
1822
1823 /* Try to turn a 24 immediate, displacement or absolute address
1824 into a 8 immediate, displacement or absolute address. */
1825 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_24)
1826 {
1827 bfd_vma value = symval;
1828 value += irel->r_addend;
1829
1830 /* See if the value will fit in 8 bits. */
1831 if ((long) value < 0x7f && (long) value > -0x80)
1832 {
1833 unsigned char code;
1834
1835 /* AM33 insns which have 24 operands are 6 bytes long and
1836 will have 0xfd as the first byte. */
1837
1838 /* Get the first opcode. */
1839 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1840
1841 if (code == 0xfd)
1842 {
1843 /* Get the second opcode. */
1844 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1845
1846 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
1847 equivalent instructions exists. */
1848 if (code != 0x6b && code != 0x7b
1849 && code != 0x8b && code != 0x9b
1850 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
1851 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
1852 || (code & 0x0f) == 0x0e))
1853 {
1854 /* Not safe if the high bit is on as relaxing may
1855 move the value out of high mem and thus not fit
1856 in a signed 8bit value. This is currently over
1857 conservative. */
1858 if ((value & 0x80) == 0)
1859 {
1860 /* Note that we've changed the relocation contents,
1861 etc. */
1862 elf_section_data (sec)->relocs = internal_relocs;
1863 elf_section_data (sec)->this_hdr.contents = contents;
1864 symtab_hdr->contents = (unsigned char *) isymbuf;
1865
1866 /* Fix the opcode. */
1867 bfd_put_8 (abfd, 0xfb, contents + irel->r_offset - 3);
1868 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1869
1870 /* Fix the relocation's type. */
1871 irel->r_info =
1872 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1873 R_MN10300_8);
1874
1875 /* Delete two bytes of data. */
1876 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1877 irel->r_offset + 1, 2))
1878 goto error_return;
1879
1880 /* That will change things, so, we should relax
1881 again. Note that this is not required, and it
1882 may be slow. */
1883 *again = true;
1884 break;
1885 }
1886 }
1887 }
1888 }
1889 }
1890
1891 /* Try to turn a 32bit immediate, displacement or absolute address
1892 into a 16bit immediate, displacement or absolute address. */
1893 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10300_32)
1894 {
1895 bfd_vma value = symval;
1896 value += irel->r_addend;
1897
1898 /* See if the value will fit in 24 bits.
1899 We allow any 16bit match here. We prune those we can't
1900 handle below. */
1901 if ((long) value < 0x7fffff && (long) value > -0x800000)
1902 {
1903 unsigned char code;
1904
1905 /* AM33 insns which have 32bit operands are 7 bytes long and
1906 will have 0xfe as the first byte. */
1907
1908 /* Get the first opcode. */
1909 code = bfd_get_8 (abfd, contents + irel->r_offset - 3);
1910
1911 if (code == 0xfe)
1912 {
1913 /* Get the second opcode. */
1914 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1915
1916 /* All the am33 32 -> 24 relaxing possibilities. */
1917 /* We can not relax 0x6b, 0x7b, 0x8b, 0x9b as no 24bit
1918 equivalent instructions exists. */
1919 if (code != 0x6b && code != 0x7b
1920 && code != 0x8b && code != 0x9b
1921 && ((code & 0x0f) == 0x09 || (code & 0x0f) == 0x08
1922 || (code & 0x0f) == 0x0a || (code & 0x0f) == 0x0b
1923 || (code & 0x0f) == 0x0e))
1924 {
1925 /* Not safe if the high bit is on as relaxing may
1926 move the value out of high mem and thus not fit
1927 in a signed 16bit value. This is currently over
1928 conservative. */
1929 if ((value & 0x8000) == 0)
1930 {
1931 /* Note that we've changed the relocation contents,
1932 etc. */
1933 elf_section_data (sec)->relocs = internal_relocs;
1934 elf_section_data (sec)->this_hdr.contents = contents;
1935 symtab_hdr->contents = (unsigned char *) isymbuf;
1936
1937 /* Fix the opcode. */
1938 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 3);
1939 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1940
1941 /* Fix the relocation's type. */
1942 irel->r_info =
1943 ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1944 R_MN10300_24);
1945
1946 /* Delete one byte of data. */
1947 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
1948 irel->r_offset + 3, 1))
1949 goto error_return;
1950
1951 /* That will change things, so, we should relax
1952 again. Note that this is not required, and it
1953 may be slow. */
1954 *again = true;
1955 break;
1956 }
1957 }
1958 }
1959 }
1960
1961 /* See if the value will fit in 16 bits.
1962 We allow any 16bit match here. We prune those we can't
1963 handle below. */
1964 if ((long) value < 0x7fff && (long) value > -0x8000)
1965 {
1966 unsigned char code;
1967
1968 /* Most insns which have 32bit operands are 6 bytes long;
1969 exceptions are pcrel insns and bit insns.
1970
1971 We handle pcrel insns above. We don't bother trying
1972 to handle the bit insns here.
1973
1974 The first byte of the remaining insns will be 0xfc. */
1975
1976 /* Get the first opcode. */
1977 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
1978
1979 if (code != 0xfc)
1980 continue;
1981
1982 /* Get the second opcode. */
1983 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
1984
1985 if ((code & 0xf0) < 0x80)
1986 switch (code & 0xf0)
1987 {
1988 /* mov (d32,am),dn -> mov (d32,am),dn
1989 mov dm,(d32,am) -> mov dn,(d32,am)
1990 mov (d32,am),an -> mov (d32,am),an
1991 mov dm,(d32,am) -> mov dn,(d32,am)
1992 movbu (d32,am),dn -> movbu (d32,am),dn
1993 movbu dm,(d32,am) -> movbu dn,(d32,am)
1994 movhu (d32,am),dn -> movhu (d32,am),dn
1995 movhu dm,(d32,am) -> movhu dn,(d32,am) */
1996 case 0x00:
1997 case 0x10:
1998 case 0x20:
1999 case 0x30:
2000 case 0x40:
2001 case 0x50:
2002 case 0x60:
2003 case 0x70:
2004 /* Not safe if the high bit is on as relaxing may
2005 move the value out of high mem and thus not fit
2006 in a signed 16bit value. */
2007 if (code == 0xcc
2008 && (value & 0x8000))
2009 continue;
2010
2011 /* Note that we've changed the relocation contents, etc. */
2012 elf_section_data (sec)->relocs = internal_relocs;
2013 elf_section_data (sec)->this_hdr.contents = contents;
2014 symtab_hdr->contents = (unsigned char *) isymbuf;
2015
2016 /* Fix the opcode. */
2017 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2018 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2019
2020 /* Fix the relocation's type. */
2021 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2022 R_MN10300_16);
2023
2024 /* Delete two bytes of data. */
2025 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2026 irel->r_offset + 2, 2))
2027 goto error_return;
2028
2029 /* That will change things, so, we should relax again.
2030 Note that this is not required, and it may be slow. */
2031 *again = true;
2032 break;
2033 }
2034 else if ((code & 0xf0) == 0x80
2035 || (code & 0xf0) == 0x90)
2036 switch (code & 0xf3)
2037 {
2038 /* mov dn,(abs32) -> mov dn,(abs16)
2039 movbu dn,(abs32) -> movbu dn,(abs16)
2040 movhu dn,(abs32) -> movhu dn,(abs16) */
2041 case 0x81:
2042 case 0x82:
2043 case 0x83:
2044 /* Note that we've changed the relocation contents, etc. */
2045 elf_section_data (sec)->relocs = internal_relocs;
2046 elf_section_data (sec)->this_hdr.contents = contents;
2047 symtab_hdr->contents = (unsigned char *) isymbuf;
2048
2049 if ((code & 0xf3) == 0x81)
2050 code = 0x01 + (code & 0x0c);
2051 else if ((code & 0xf3) == 0x82)
2052 code = 0x02 + (code & 0x0c);
2053 else if ((code & 0xf3) == 0x83)
2054 code = 0x03 + (code & 0x0c);
2055 else
2056 abort ();
2057
2058 /* Fix the opcode. */
2059 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2060
2061 /* Fix the relocation's type. */
2062 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2063 R_MN10300_16);
2064
2065 /* The opcode got shorter too, so we have to fix the
2066 addend and offset too! */
2067 irel->r_offset -= 1;
2068
2069 /* Delete three bytes of data. */
2070 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2071 irel->r_offset + 1, 3))
2072 goto error_return;
2073
2074 /* That will change things, so, we should relax again.
2075 Note that this is not required, and it may be slow. */
2076 *again = true;
2077 break;
2078
2079 /* mov am,(abs32) -> mov am,(abs16)
2080 mov am,(d32,sp) -> mov am,(d16,sp)
2081 mov dm,(d32,sp) -> mov dm,(d32,sp)
2082 movbu dm,(d32,sp) -> movbu dm,(d32,sp)
2083 movhu dm,(d32,sp) -> movhu dm,(d32,sp) */
2084 case 0x80:
2085 case 0x90:
2086 case 0x91:
2087 case 0x92:
2088 case 0x93:
2089 /* sp-based offsets are zero-extended. */
2090 if (code >= 0x90 && code <= 0x93
2091 && (long)value < 0)
2092 continue;
2093
2094 /* Note that we've changed the relocation contents, etc. */
2095 elf_section_data (sec)->relocs = internal_relocs;
2096 elf_section_data (sec)->this_hdr.contents = contents;
2097 symtab_hdr->contents = (unsigned char *) isymbuf;
2098
2099 /* Fix the opcode. */
2100 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2101 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2102
2103 /* Fix the relocation's type. */
2104 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2105 R_MN10300_16);
2106
2107 /* Delete two bytes of data. */
2108 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2109 irel->r_offset + 2, 2))
2110 goto error_return;
2111
2112 /* That will change things, so, we should relax again.
2113 Note that this is not required, and it may be slow. */
2114 *again = true;
2115 break;
2116 }
2117 else if ((code & 0xf0) < 0xf0)
2118 switch (code & 0xfc)
2119 {
2120 /* mov imm32,dn -> mov imm16,dn
2121 mov imm32,an -> mov imm16,an
2122 mov (abs32),dn -> mov (abs16),dn
2123 movbu (abs32),dn -> movbu (abs16),dn
2124 movhu (abs32),dn -> movhu (abs16),dn */
2125 case 0xcc:
2126 case 0xdc:
2127 case 0xa4:
2128 case 0xa8:
2129 case 0xac:
2130 /* Not safe if the high bit is on as relaxing may
2131 move the value out of high mem and thus not fit
2132 in a signed 16bit value. */
2133 if (code == 0xcc
2134 && (value & 0x8000))
2135 continue;
2136
2137 /* mov imm16, an zero-extends the immediate. */
2138 if (code == 0xdc
2139 && (long)value < 0)
2140 continue;
2141
2142 /* Note that we've changed the relocation contents, etc. */
2143 elf_section_data (sec)->relocs = internal_relocs;
2144 elf_section_data (sec)->this_hdr.contents = contents;
2145 symtab_hdr->contents = (unsigned char *) isymbuf;
2146
2147 if ((code & 0xfc) == 0xcc)
2148 code = 0x2c + (code & 0x03);
2149 else if ((code & 0xfc) == 0xdc)
2150 code = 0x24 + (code & 0x03);
2151 else if ((code & 0xfc) == 0xa4)
2152 code = 0x30 + (code & 0x03);
2153 else if ((code & 0xfc) == 0xa8)
2154 code = 0x34 + (code & 0x03);
2155 else if ((code & 0xfc) == 0xac)
2156 code = 0x38 + (code & 0x03);
2157 else
2158 abort ();
2159
2160 /* Fix the opcode. */
2161 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
2162
2163 /* Fix the relocation's type. */
2164 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2165 R_MN10300_16);
2166
2167 /* The opcode got shorter too, so we have to fix the
2168 addend and offset too! */
2169 irel->r_offset -= 1;
2170
2171 /* Delete three bytes of data. */
2172 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2173 irel->r_offset + 1, 3))
2174 goto error_return;
2175
2176 /* That will change things, so, we should relax again.
2177 Note that this is not required, and it may be slow. */
2178 *again = true;
2179 break;
2180
2181 /* mov (abs32),an -> mov (abs16),an
2182 mov (d32,sp),an -> mov (d16,sp),an
2183 mov (d32,sp),dn -> mov (d16,sp),dn
2184 movbu (d32,sp),dn -> movbu (d16,sp),dn
2185 movhu (d32,sp),dn -> movhu (d16,sp),dn
2186 add imm32,dn -> add imm16,dn
2187 cmp imm32,dn -> cmp imm16,dn
2188 add imm32,an -> add imm16,an
2189 cmp imm32,an -> cmp imm16,an
2190 and imm32,dn -> and imm16,dn
2191 or imm32,dn -> or imm16,dn
2192 xor imm32,dn -> xor imm16,dn
2193 btst imm32,dn -> btst imm16,dn */
2194
2195 case 0xa0:
2196 case 0xb0:
2197 case 0xb1:
2198 case 0xb2:
2199 case 0xb3:
2200 case 0xc0:
2201 case 0xc8:
2202
2203 case 0xd0:
2204 case 0xd8:
2205 case 0xe0:
2206 case 0xe1:
2207 case 0xe2:
2208 case 0xe3:
2209 /* cmp imm16, an zero-extends the immediate. */
2210 if (code == 0xdc
2211 && (long)value < 0)
2212 continue;
2213
2214 /* So do sp-based offsets. */
2215 if (code >= 0xb0 && code <= 0xb3
2216 && (long)value < 0)
2217 continue;
2218
2219 /* Note that we've changed the relocation contents, etc. */
2220 elf_section_data (sec)->relocs = internal_relocs;
2221 elf_section_data (sec)->this_hdr.contents = contents;
2222 symtab_hdr->contents = (unsigned char *) isymbuf;
2223
2224 /* Fix the opcode. */
2225 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2226 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
2227
2228 /* Fix the relocation's type. */
2229 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2230 R_MN10300_16);
2231
2232 /* Delete two bytes of data. */
2233 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2234 irel->r_offset + 2, 2))
2235 goto error_return;
2236
2237 /* That will change things, so, we should relax again.
2238 Note that this is not required, and it may be slow. */
2239 *again = true;
2240 break;
2241 }
2242 else if (code == 0xfe)
2243 {
2244 /* add imm32,sp -> add imm16,sp */
2245
2246 /* Note that we've changed the relocation contents, etc. */
2247 elf_section_data (sec)->relocs = internal_relocs;
2248 elf_section_data (sec)->this_hdr.contents = contents;
2249 symtab_hdr->contents = (unsigned char *) isymbuf;
2250
2251 /* Fix the opcode. */
2252 bfd_put_8 (abfd, 0xfa, contents + irel->r_offset - 2);
2253 bfd_put_8 (abfd, 0xfe, contents + irel->r_offset - 1);
2254
2255 /* Fix the relocation's type. */
2256 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
2257 R_MN10300_16);
2258
2259 /* Delete two bytes of data. */
2260 if (!mn10300_elf_relax_delete_bytes (abfd, sec,
2261 irel->r_offset + 2, 2))
2262 goto error_return;
2263
2264 /* That will change things, so, we should relax again.
2265 Note that this is not required, and it may be slow. */
2266 *again = true;
2267 break;
2268 }
2269 }
2270 }
2271 }
2272
2273 if (isymbuf != NULL
2274 && symtab_hdr->contents != (unsigned char *) isymbuf)
2275 {
2276 if (! link_info->keep_memory)
2277 free (isymbuf);
2278 else
2279 {
2280 /* Cache the symbols for elf_link_input_bfd. */
2281 symtab_hdr->contents = (unsigned char *) isymbuf;
2282 }
2283 }
2284
2285 if (contents != NULL
2286 && elf_section_data (sec)->this_hdr.contents != contents)
2287 {
2288 if (! link_info->keep_memory)
2289 free (contents);
2290 else
2291 {
2292 /* Cache the section contents for elf_link_input_bfd. */
2293 elf_section_data (sec)->this_hdr.contents = contents;
2294 }
2295 }
2296
2297 if (internal_relocs != NULL
2298 && elf_section_data (sec)->relocs != internal_relocs)
2299 free (internal_relocs);
2300
2301 return true;
2302
2303 error_return:
2304 if (isymbuf != NULL
2305 && symtab_hdr->contents != (unsigned char *) isymbuf)
2306 free (isymbuf);
2307 if (contents != NULL
2308 && elf_section_data (section)->this_hdr.contents != contents)
2309 free (contents);
2310 if (internal_relocs != NULL
2311 && elf_section_data (section)->relocs != internal_relocs)
2312 free (internal_relocs);
2313
2314 return false;
2315 }
2316
2317 /* Compute the stack size and movm arguments for the function
2318 referred to by HASH at address ADDR in section with
2319 contents CONTENTS, store the information in the hash table. */
2320 static void
2321 compute_function_info (abfd, hash, addr, contents)
2322 bfd *abfd;
2323 struct elf32_mn10300_link_hash_entry *hash;
2324 bfd_vma addr;
2325 unsigned char *contents;
2326 {
2327 unsigned char byte1, byte2;
2328 /* We only care about a very small subset of the possible prologue
2329 sequences here. Basically we look for:
2330
2331 movm [d2,d3,a2,a3],sp (optional)
2332 add <size>,sp (optional, and only for sizes which fit in an unsigned
2333 8 bit number)
2334
2335 If we find anything else, we quit. */
2336
2337 /* Look for movm [regs],sp */
2338 byte1 = bfd_get_8 (abfd, contents + addr);
2339 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2340
2341 if (byte1 == 0xcf)
2342 {
2343 hash->movm_args = byte2;
2344 addr += 2;
2345 byte1 = bfd_get_8 (abfd, contents + addr);
2346 byte2 = bfd_get_8 (abfd, contents + addr + 1);
2347 }
2348
2349 /* Now figure out how much stack space will be allocated by the movm
2350 instruction. We need this kept separate from the funtion's normal
2351 stack space. */
2352 if (hash->movm_args)
2353 {
2354 /* Space for d2. */
2355 if (hash->movm_args & 0x80)
2356 hash->movm_stack_size += 4;
2357
2358 /* Space for d3. */
2359 if (hash->movm_args & 0x40)
2360 hash->movm_stack_size += 4;
2361
2362 /* Space for a2. */
2363 if (hash->movm_args & 0x20)
2364 hash->movm_stack_size += 4;
2365
2366 /* Space for a3. */
2367 if (hash->movm_args & 0x10)
2368 hash->movm_stack_size += 4;
2369
2370 /* "other" space. d0, d1, a0, a1, mdr, lir, lar, 4 byte pad. */
2371 if (hash->movm_args & 0x08)
2372 hash->movm_stack_size += 8 * 4;
2373
2374 if (bfd_get_mach (abfd) == bfd_mach_am33)
2375 {
2376 /* "exother" space. e0, e1, mdrq, mcrh, mcrl, mcvf */
2377 if (hash->movm_args & 0x1)
2378 hash->movm_stack_size += 6 * 4;
2379
2380 /* exreg1 space. e4, e5, e6, e7 */
2381 if (hash->movm_args & 0x2)
2382 hash->movm_stack_size += 4 * 4;
2383
2384 /* exreg0 space. e2, e3 */
2385 if (hash->movm_args & 0x4)
2386 hash->movm_stack_size += 2 * 4;
2387 }
2388 }
2389
2390 /* Now look for the two stack adjustment variants. */
2391 if (byte1 == 0xf8 && byte2 == 0xfe)
2392 {
2393 int temp = bfd_get_8 (abfd, contents + addr + 2);
2394 temp = ((temp & 0xff) ^ (~0x7f)) + 0x80;
2395
2396 hash->stack_size = -temp;
2397 }
2398 else if (byte1 == 0xfa && byte2 == 0xfe)
2399 {
2400 int temp = bfd_get_16 (abfd, contents + addr + 2);
2401 temp = ((temp & 0xffff) ^ (~0x7fff)) + 0x8000;
2402 temp = -temp;
2403
2404 if (temp < 255)
2405 hash->stack_size = temp;
2406 }
2407
2408 /* If the total stack to be allocated by the call instruction is more
2409 than 255 bytes, then we can't remove the stack adjustment by using
2410 "call" (we might still be able to remove the "movm" instruction. */
2411 if (hash->stack_size + hash->movm_stack_size > 255)
2412 hash->stack_size = 0;
2413
2414 return;
2415 }
2416
2417 /* Delete some bytes from a section while relaxing. */
2418
2419 static boolean
2420 mn10300_elf_relax_delete_bytes (abfd, sec, addr, count)
2421 bfd *abfd;
2422 asection *sec;
2423 bfd_vma addr;
2424 int count;
2425 {
2426 Elf_Internal_Shdr *symtab_hdr;
2427 unsigned int sec_shndx;
2428 bfd_byte *contents;
2429 Elf_Internal_Rela *irel, *irelend;
2430 Elf_Internal_Rela *irelalign;
2431 bfd_vma toaddr;
2432 Elf_Internal_Sym *isym, *isymend;
2433 struct elf_link_hash_entry **sym_hashes;
2434 struct elf_link_hash_entry **end_hashes;
2435 unsigned int symcount;
2436
2437 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2438
2439 contents = elf_section_data (sec)->this_hdr.contents;
2440
2441 /* The deletion must stop at the next ALIGN reloc for an aligment
2442 power larger than the number of bytes we are deleting. */
2443
2444 irelalign = NULL;
2445 toaddr = sec->_cooked_size;
2446
2447 irel = elf_section_data (sec)->relocs;
2448 irelend = irel + sec->reloc_count;
2449
2450 /* Actually delete the bytes. */
2451 memmove (contents + addr, contents + addr + count,
2452 (size_t) (toaddr - addr - count));
2453 sec->_cooked_size -= count;
2454
2455 /* Adjust all the relocs. */
2456 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
2457 {
2458 /* Get the new reloc address. */
2459 if ((irel->r_offset > addr
2460 && irel->r_offset < toaddr))
2461 irel->r_offset -= count;
2462 }
2463
2464 /* Adjust the local symbols defined in this section. */
2465 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2466 isym = (Elf_Internal_Sym *) symtab_hdr->contents;
2467 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2468 {
2469 if (isym->st_shndx == sec_shndx
2470 && isym->st_value > addr
2471 && isym->st_value < toaddr)
2472 isym->st_value -= count;
2473 }
2474
2475 /* Now adjust the global symbols defined in this section. */
2476 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2477 - symtab_hdr->sh_info);
2478 sym_hashes = elf_sym_hashes (abfd);
2479 end_hashes = sym_hashes + symcount;
2480 for (; sym_hashes < end_hashes; sym_hashes++)
2481 {
2482 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2483 if ((sym_hash->root.type == bfd_link_hash_defined
2484 || sym_hash->root.type == bfd_link_hash_defweak)
2485 && sym_hash->root.u.def.section == sec
2486 && sym_hash->root.u.def.value > addr
2487 && sym_hash->root.u.def.value < toaddr)
2488 {
2489 sym_hash->root.u.def.value -= count;
2490 }
2491 }
2492
2493 return true;
2494 }
2495
2496 /* Return true if a symbol exists at the given address, else return
2497 false. */
2498 static boolean
2499 mn10300_elf_symbol_address_p (abfd, sec, isym, addr)
2500 bfd *abfd;
2501 asection *sec;
2502 Elf_Internal_Sym *isym;
2503 bfd_vma addr;
2504 {
2505 Elf_Internal_Shdr *symtab_hdr;
2506 unsigned int sec_shndx;
2507 Elf_Internal_Sym *isymend;
2508 struct elf_link_hash_entry **sym_hashes;
2509 struct elf_link_hash_entry **end_hashes;
2510 unsigned int symcount;
2511
2512 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
2513
2514 /* Examine all the symbols. */
2515 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
2516 for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
2517 {
2518 if (isym->st_shndx == sec_shndx
2519 && isym->st_value == addr)
2520 return true;
2521 }
2522
2523 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
2524 - symtab_hdr->sh_info);
2525 sym_hashes = elf_sym_hashes (abfd);
2526 end_hashes = sym_hashes + symcount;
2527 for (; sym_hashes < end_hashes; sym_hashes++)
2528 {
2529 struct elf_link_hash_entry *sym_hash = *sym_hashes;
2530 if ((sym_hash->root.type == bfd_link_hash_defined
2531 || sym_hash->root.type == bfd_link_hash_defweak)
2532 && sym_hash->root.u.def.section == sec
2533 && sym_hash->root.u.def.value == addr)
2534 return true;
2535 }
2536
2537 return false;
2538 }
2539
2540 /* This is a version of bfd_generic_get_relocated_section_contents
2541 which uses mn10300_elf_relocate_section. */
2542
2543 static bfd_byte *
2544 mn10300_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
2545 data, relocateable, symbols)
2546 bfd *output_bfd;
2547 struct bfd_link_info *link_info;
2548 struct bfd_link_order *link_order;
2549 bfd_byte *data;
2550 boolean relocateable;
2551 asymbol **symbols;
2552 {
2553 Elf_Internal_Shdr *symtab_hdr;
2554 asection *input_section = link_order->u.indirect.section;
2555 bfd *input_bfd = input_section->owner;
2556 asection **sections = NULL;
2557 Elf_Internal_Rela *internal_relocs = NULL;
2558 Elf_Internal_Sym *isymbuf = NULL;
2559
2560 /* We only need to handle the case of relaxing, or of having a
2561 particular set of section contents, specially. */
2562 if (relocateable
2563 || elf_section_data (input_section)->this_hdr.contents == NULL)
2564 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
2565 link_order, data,
2566 relocateable,
2567 symbols);
2568
2569 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
2570
2571 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
2572 (size_t) input_section->_raw_size);
2573
2574 if ((input_section->flags & SEC_RELOC) != 0
2575 && input_section->reloc_count > 0)
2576 {
2577 asection **secpp;
2578 Elf_Internal_Sym *isym, *isymend;
2579 bfd_size_type amt;
2580
2581 internal_relocs = (_bfd_elf32_link_read_relocs
2582 (input_bfd, input_section, (PTR) NULL,
2583 (Elf_Internal_Rela *) NULL, false));
2584 if (internal_relocs == NULL)
2585 goto error_return;
2586
2587 if (symtab_hdr->sh_info != 0)
2588 {
2589 isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
2590 if (isymbuf == NULL)
2591 isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
2592 symtab_hdr->sh_info, 0,
2593 NULL, NULL, NULL);
2594 if (isymbuf == NULL)
2595 goto error_return;
2596 }
2597
2598 amt = symtab_hdr->sh_info;
2599 amt *= sizeof (asection *);
2600 sections = (asection **) bfd_malloc (amt);
2601 if (sections == NULL && amt != 0)
2602 goto error_return;
2603
2604 isymend = isymbuf + symtab_hdr->sh_info;
2605 for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
2606 {
2607 asection *isec;
2608
2609 if (isym->st_shndx == SHN_UNDEF)
2610 isec = bfd_und_section_ptr;
2611 else if (isym->st_shndx == SHN_ABS)
2612 isec = bfd_abs_section_ptr;
2613 else if (isym->st_shndx == SHN_COMMON)
2614 isec = bfd_com_section_ptr;
2615 else
2616 isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
2617
2618 *secpp = isec;
2619 }
2620
2621 if (! mn10300_elf_relocate_section (output_bfd, link_info, input_bfd,
2622 input_section, data, internal_relocs,
2623 isymbuf, sections))
2624 goto error_return;
2625
2626 if (sections != NULL)
2627 free (sections);
2628 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2629 free (isymbuf);
2630 if (internal_relocs != elf_section_data (input_section)->relocs)
2631 free (internal_relocs);
2632 }
2633
2634 return data;
2635
2636 error_return:
2637 if (sections != NULL)
2638 free (sections);
2639 if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
2640 free (isymbuf);
2641 if (internal_relocs != NULL
2642 && internal_relocs != elf_section_data (input_section)->relocs)
2643 free (internal_relocs);
2644 return NULL;
2645 }
2646
2647 /* Assorted hash table functions. */
2648
2649 /* Initialize an entry in the link hash table. */
2650
2651 /* Create an entry in an MN10300 ELF linker hash table. */
2652
2653 static struct bfd_hash_entry *
2654 elf32_mn10300_link_hash_newfunc (entry, table, string)
2655 struct bfd_hash_entry *entry;
2656 struct bfd_hash_table *table;
2657 const char *string;
2658 {
2659 struct elf32_mn10300_link_hash_entry *ret =
2660 (struct elf32_mn10300_link_hash_entry *) entry;
2661
2662 /* Allocate the structure if it has not already been allocated by a
2663 subclass. */
2664 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2665 ret = ((struct elf32_mn10300_link_hash_entry *)
2666 bfd_hash_allocate (table,
2667 sizeof (struct elf32_mn10300_link_hash_entry)));
2668 if (ret == (struct elf32_mn10300_link_hash_entry *) NULL)
2669 return (struct bfd_hash_entry *) ret;
2670
2671 /* Call the allocation method of the superclass. */
2672 ret = ((struct elf32_mn10300_link_hash_entry *)
2673 _bfd_elf_link_hash_newfunc ((struct bfd_hash_entry *) ret,
2674 table, string));
2675 if (ret != (struct elf32_mn10300_link_hash_entry *) NULL)
2676 {
2677 ret->direct_calls = 0;
2678 ret->stack_size = 0;
2679 ret->movm_stack_size = 0;
2680 ret->flags = 0;
2681 ret->movm_args = 0;
2682 }
2683
2684 return (struct bfd_hash_entry *) ret;
2685 }
2686
2687 /* Create an mn10300 ELF linker hash table. */
2688
2689 static struct bfd_link_hash_table *
2690 elf32_mn10300_link_hash_table_create (abfd)
2691 bfd *abfd;
2692 {
2693 struct elf32_mn10300_link_hash_table *ret;
2694 bfd_size_type amt = sizeof (struct elf32_mn10300_link_hash_table);
2695
2696 ret = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
2697 if (ret == (struct elf32_mn10300_link_hash_table *) NULL)
2698 return NULL;
2699
2700 if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
2701 elf32_mn10300_link_hash_newfunc))
2702 {
2703 free (ret);
2704 return NULL;
2705 }
2706
2707 ret->flags = 0;
2708 amt = sizeof (struct elf_link_hash_table);
2709 ret->static_hash_table
2710 = (struct elf32_mn10300_link_hash_table *) bfd_malloc (amt);
2711 if (ret->static_hash_table == NULL)
2712 {
2713 free (ret);
2714 return NULL;
2715 }
2716
2717 if (! _bfd_elf_link_hash_table_init (&ret->static_hash_table->root, abfd,
2718 elf32_mn10300_link_hash_newfunc))
2719 {
2720 free (ret->static_hash_table);
2721 free (ret);
2722 return NULL;
2723 }
2724 return &ret->root.root;
2725 }
2726
2727 /* Free an mn10300 ELF linker hash table. */
2728
2729 static void
2730 elf32_mn10300_link_hash_table_free (hash)
2731 struct bfd_link_hash_table *hash;
2732 {
2733 struct elf32_mn10300_link_hash_table *ret
2734 = (struct elf32_mn10300_link_hash_table *) hash;
2735
2736 _bfd_generic_link_hash_table_free
2737 ((struct bfd_link_hash_table *) ret->static_hash_table);
2738 _bfd_generic_link_hash_table_free
2739 ((struct bfd_link_hash_table *) ret);
2740 }
2741
2742 static unsigned long
2743 elf_mn10300_mach (flags)
2744 flagword flags;
2745 {
2746 switch (flags & EF_MN10300_MACH)
2747 {
2748 case E_MN10300_MACH_MN10300:
2749 default:
2750 return bfd_mach_mn10300;
2751
2752 case E_MN10300_MACH_AM33:
2753 return bfd_mach_am33;
2754 }
2755 }
2756
2757 /* The final processing done just before writing out a MN10300 ELF object
2758 file. This gets the MN10300 architecture right based on the machine
2759 number. */
2760
2761 void
2762 _bfd_mn10300_elf_final_write_processing (abfd, linker)
2763 bfd *abfd;
2764 boolean linker ATTRIBUTE_UNUSED;
2765 {
2766 unsigned long val;
2767
2768 switch (bfd_get_mach (abfd))
2769 {
2770 default:
2771 case bfd_mach_mn10300:
2772 val = E_MN10300_MACH_MN10300;
2773 break;
2774
2775 case bfd_mach_am33:
2776 val = E_MN10300_MACH_AM33;
2777 break;
2778 }
2779
2780 elf_elfheader (abfd)->e_flags &= ~ (EF_MN10300_MACH);
2781 elf_elfheader (abfd)->e_flags |= val;
2782 }
2783
2784 boolean
2785 _bfd_mn10300_elf_object_p (abfd)
2786 bfd *abfd;
2787 {
2788 bfd_default_set_arch_mach (abfd, bfd_arch_mn10300,
2789 elf_mn10300_mach (elf_elfheader (abfd)->e_flags));
2790 return true;
2791 }
2792
2793 /* Merge backend specific data from an object file to the output
2794 object file when linking. */
2795
2796 boolean
2797 _bfd_mn10300_elf_merge_private_bfd_data (ibfd, obfd)
2798 bfd *ibfd;
2799 bfd *obfd;
2800 {
2801 if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour
2802 || bfd_get_flavour (obfd) != bfd_target_elf_flavour)
2803 return true;
2804
2805 if (bfd_get_arch (obfd) == bfd_get_arch (ibfd)
2806 && bfd_get_mach (obfd) < bfd_get_mach (ibfd))
2807 {
2808 if (! bfd_set_arch_mach (obfd, bfd_get_arch (ibfd),
2809 bfd_get_mach (ibfd)))
2810 return false;
2811 }
2812
2813 return true;
2814 }
2815
2816 #define TARGET_LITTLE_SYM bfd_elf32_mn10300_vec
2817 #define TARGET_LITTLE_NAME "elf32-mn10300"
2818 #define ELF_ARCH bfd_arch_mn10300
2819 #define ELF_MACHINE_CODE EM_MN10300
2820 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10300
2821 #define ELF_MAXPAGESIZE 0x1000
2822
2823 #define elf_info_to_howto mn10300_info_to_howto
2824 #define elf_info_to_howto_rel 0
2825 #define elf_backend_can_gc_sections 1
2826 #define elf_backend_rela_normal 1
2827 #define elf_backend_check_relocs mn10300_elf_check_relocs
2828 #define elf_backend_gc_mark_hook mn10300_elf_gc_mark_hook
2829 #define elf_backend_relocate_section mn10300_elf_relocate_section
2830 #define bfd_elf32_bfd_relax_section mn10300_elf_relax_section
2831 #define bfd_elf32_bfd_get_relocated_section_contents \
2832 mn10300_elf_get_relocated_section_contents
2833 #define bfd_elf32_bfd_link_hash_table_create \
2834 elf32_mn10300_link_hash_table_create
2835 #define bfd_elf32_bfd_link_hash_table_free \
2836 elf32_mn10300_link_hash_table_free
2837
2838 #define elf_symbol_leading_char '_'
2839
2840 /* So we can set bits in e_flags. */
2841 #define elf_backend_final_write_processing \
2842 _bfd_mn10300_elf_final_write_processing
2843 #define elf_backend_object_p _bfd_mn10300_elf_object_p
2844
2845 #define bfd_elf32_bfd_merge_private_bfd_data \
2846 _bfd_mn10300_elf_merge_private_bfd_data
2847
2848 #include "elf32-target.h"