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