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