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