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