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