* Contribute Hitachi SH5 simulator.
[deliverable/binutils-gdb.git] / bfd / elf-m10200.c
1 /* Matsushita 10200 specific support for 32-bit ELF
2 Copyright 1996, 1997, 1998, 1999, 2000, 2001
3 Free Software Foundation, Inc.
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
20
21 #include "bfd.h"
22 #include "sysdep.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25
26 static reloc_howto_type *bfd_elf32_bfd_reloc_type_lookup
27 PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
28 static void mn10200_info_to_howto
29 PARAMS ((bfd *, arelent *, Elf32_Internal_Rela *));
30 static boolean mn10200_elf_relax_delete_bytes
31 PARAMS ((bfd *, asection *, bfd_vma, int));
32 static boolean mn10200_elf_symbol_address_p
33 PARAMS ((bfd *, asection *, bfd_vma));
34 static bfd_reloc_status_type mn10200_elf_final_link_relocate
35 PARAMS ((reloc_howto_type *, bfd *, bfd *, asection *,
36 bfd_byte *, bfd_vma, bfd_vma, bfd_vma,
37 struct bfd_link_info *, asection *, int));
38 static boolean mn10200_elf_relocate_section
39 PARAMS (( bfd *, struct bfd_link_info *, bfd *, asection *,
40 bfd_byte *, Elf_Internal_Rela *, Elf_Internal_Sym *,
41 asection **));
42 static boolean mn10200_elf_relax_section
43 PARAMS ((bfd *, asection *, struct bfd_link_info *, boolean *));
44 static bfd_byte * mn10200_elf_get_relocated_section_contents
45 PARAMS ((bfd *, struct bfd_link_info *, struct bfd_link_order *,
46 bfd_byte *, boolean, asymbol **));
47
48 /* We have to use RELA instructions since md_apply_fix3 in the assembler
49 does absolutely nothing. */
50 #define USE_RELA
51
52 enum reloc_type {
53 R_MN10200_NONE = 0,
54 R_MN10200_32,
55 R_MN10200_16,
56 R_MN10200_8,
57 R_MN10200_24,
58 R_MN10200_PCREL8,
59 R_MN10200_PCREL16,
60 R_MN10200_PCREL24,
61 R_MN10200_MAX
62 };
63
64 static reloc_howto_type elf_mn10200_howto_table[] = {
65 /* Dummy relocation. Does nothing. */
66 HOWTO (R_MN10200_NONE,
67 0,
68 2,
69 16,
70 false,
71 0,
72 complain_overflow_bitfield,
73 bfd_elf_generic_reloc,
74 "R_MN10200_NONE",
75 false,
76 0,
77 0,
78 false),
79 /* Standard 32 bit reloc. */
80 HOWTO (R_MN10200_32,
81 0,
82 2,
83 32,
84 false,
85 0,
86 complain_overflow_bitfield,
87 bfd_elf_generic_reloc,
88 "R_MN10200_32",
89 false,
90 0xffffffff,
91 0xffffffff,
92 false),
93 /* Standard 16 bit reloc. */
94 HOWTO (R_MN10200_16,
95 0,
96 1,
97 16,
98 false,
99 0,
100 complain_overflow_bitfield,
101 bfd_elf_generic_reloc,
102 "R_MN10200_16",
103 false,
104 0xffff,
105 0xffff,
106 false),
107 /* Standard 8 bit reloc. */
108 HOWTO (R_MN10200_8,
109 0,
110 0,
111 8,
112 false,
113 0,
114 complain_overflow_bitfield,
115 bfd_elf_generic_reloc,
116 "R_MN10200_8",
117 false,
118 0xff,
119 0xff,
120 false),
121 /* Standard 24 bit reloc. */
122 HOWTO (R_MN10200_24,
123 0,
124 2,
125 24,
126 false,
127 0,
128 complain_overflow_bitfield,
129 bfd_elf_generic_reloc,
130 "R_MN10200_24",
131 false,
132 0xffffff,
133 0xffffff,
134 false),
135 /* Simple 8 pc-relative reloc. */
136 HOWTO (R_MN10200_PCREL8,
137 0,
138 0,
139 8,
140 true,
141 0,
142 complain_overflow_bitfield,
143 bfd_elf_generic_reloc,
144 "R_MN10200_PCREL8",
145 false,
146 0xff,
147 0xff,
148 true),
149 /* Simple 16 pc-relative reloc. */
150 HOWTO (R_MN10200_PCREL16,
151 0,
152 1,
153 16,
154 true,
155 0,
156 complain_overflow_bitfield,
157 bfd_elf_generic_reloc,
158 "R_MN10200_PCREL16",
159 false,
160 0xffff,
161 0xffff,
162 true),
163 /* Simple 32bit pc-relative reloc with a 1 byte adjustment
164 to get the pc-relative offset correct. */
165 HOWTO (R_MN10200_PCREL24,
166 0,
167 2,
168 24,
169 true,
170 0,
171 complain_overflow_bitfield,
172 bfd_elf_generic_reloc,
173 "R_MN10200_PCREL24",
174 false,
175 0xffffff,
176 0xffffff,
177 true),
178 };
179
180 struct mn10200_reloc_map {
181 bfd_reloc_code_real_type bfd_reloc_val;
182 unsigned char elf_reloc_val;
183 };
184
185 static const struct mn10200_reloc_map mn10200_reloc_map[] = {
186 { BFD_RELOC_NONE , R_MN10200_NONE , },
187 { BFD_RELOC_32 , R_MN10200_32 , },
188 { BFD_RELOC_16 , R_MN10200_16 , },
189 { BFD_RELOC_8 , R_MN10200_8 , },
190 { BFD_RELOC_24 , R_MN10200_24 , },
191 { BFD_RELOC_8_PCREL , R_MN10200_PCREL8 , },
192 { BFD_RELOC_16_PCREL, R_MN10200_PCREL16, },
193 { BFD_RELOC_24_PCREL, R_MN10200_PCREL24, },
194 };
195
196 static reloc_howto_type *
197 bfd_elf32_bfd_reloc_type_lookup (abfd, code)
198 bfd *abfd ATTRIBUTE_UNUSED;
199 bfd_reloc_code_real_type code;
200 {
201 unsigned int i;
202
203 for (i = 0;
204 i < sizeof (mn10200_reloc_map) / sizeof (struct mn10200_reloc_map);
205 i++)
206 {
207 if (mn10200_reloc_map[i].bfd_reloc_val == code)
208 return &elf_mn10200_howto_table[mn10200_reloc_map[i].elf_reloc_val];
209 }
210
211 return NULL;
212 }
213
214 /* Set the howto pointer for an MN10200 ELF reloc. */
215
216 static void
217 mn10200_info_to_howto (abfd, cache_ptr, dst)
218 bfd *abfd ATTRIBUTE_UNUSED;
219 arelent *cache_ptr;
220 Elf32_Internal_Rela *dst;
221 {
222 unsigned int r_type;
223
224 r_type = ELF32_R_TYPE (dst->r_info);
225 BFD_ASSERT (r_type < (unsigned int) R_MN10200_MAX);
226 cache_ptr->howto = &elf_mn10200_howto_table[r_type];
227 }
228
229 /* Perform a relocation as part of a final link. */
230
231 static bfd_reloc_status_type
232 mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
233 input_section, contents, offset, value,
234 addend, info, sym_sec, is_local)
235 reloc_howto_type *howto;
236 bfd *input_bfd;
237 bfd *output_bfd ATTRIBUTE_UNUSED;
238 asection *input_section;
239 bfd_byte *contents;
240 bfd_vma offset;
241 bfd_vma value;
242 bfd_vma addend;
243 struct bfd_link_info *info ATTRIBUTE_UNUSED;
244 asection *sym_sec ATTRIBUTE_UNUSED;
245 int is_local ATTRIBUTE_UNUSED;
246 {
247 unsigned long r_type = howto->type;
248 bfd_byte *hit_data = contents + offset;
249
250 switch (r_type)
251 {
252
253 case R_MN10200_NONE:
254 return bfd_reloc_ok;
255
256 case R_MN10200_32:
257 value += addend;
258 bfd_put_32 (input_bfd, value, hit_data);
259 return bfd_reloc_ok;
260
261 case R_MN10200_16:
262 value += addend;
263
264 if ((long) value > 0x7fff || (long) value < -0x8000)
265 return bfd_reloc_overflow;
266
267 bfd_put_16 (input_bfd, value, hit_data);
268 return bfd_reloc_ok;
269
270 case R_MN10200_8:
271 value += addend;
272
273 if ((long) value > 0x7f || (long) value < -0x80)
274 return bfd_reloc_overflow;
275
276 bfd_put_8 (input_bfd, value, hit_data);
277 return bfd_reloc_ok;
278
279 case R_MN10200_24:
280 value += addend;
281
282 if ((long) value > 0x7fffff || (long) value < -0x800000)
283 return bfd_reloc_overflow;
284
285 value &= 0xffffff;
286 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
287 bfd_put_32 (input_bfd, value, hit_data);
288 return bfd_reloc_ok;
289
290 case R_MN10200_PCREL8:
291 value -= (input_section->output_section->vma
292 + input_section->output_offset);
293 value -= (offset + 1);
294 value += addend;
295
296 if ((long) value > 0xff || (long) value < -0x100)
297 return bfd_reloc_overflow;
298
299 bfd_put_8 (input_bfd, value, hit_data);
300 return bfd_reloc_ok;
301
302 case R_MN10200_PCREL16:
303 value -= (input_section->output_section->vma
304 + input_section->output_offset);
305 value -= (offset + 2);
306 value += addend;
307
308 if ((long) value > 0xffff || (long) value < -0x10000)
309 return bfd_reloc_overflow;
310
311 bfd_put_16 (input_bfd, value, hit_data);
312 return bfd_reloc_ok;
313
314 case R_MN10200_PCREL24:
315 value -= (input_section->output_section->vma
316 + input_section->output_offset);
317 value -= (offset + 3);
318 value += addend;
319
320 if ((long) value > 0xffffff || (long) value < -0x1000000)
321 return bfd_reloc_overflow;
322
323 value &= 0xffffff;
324 value |= (bfd_get_32 (input_bfd, hit_data) & 0xff000000);
325 bfd_put_32 (input_bfd, value, hit_data);
326 return bfd_reloc_ok;
327
328 default:
329 return bfd_reloc_notsupported;
330 }
331 }
332 \f
333 /* Relocate an MN10200 ELF section. */
334 static boolean
335 mn10200_elf_relocate_section (output_bfd, info, input_bfd, input_section,
336 contents, relocs, local_syms, local_sections)
337 bfd *output_bfd;
338 struct bfd_link_info *info;
339 bfd *input_bfd;
340 asection *input_section;
341 bfd_byte *contents;
342 Elf_Internal_Rela *relocs;
343 Elf_Internal_Sym *local_syms;
344 asection **local_sections;
345 {
346 Elf_Internal_Shdr *symtab_hdr;
347 struct elf_link_hash_entry **sym_hashes;
348 Elf_Internal_Rela *rel, *relend;
349
350 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
351 sym_hashes = elf_sym_hashes (input_bfd);
352
353 rel = relocs;
354 relend = relocs + input_section->reloc_count;
355 for (; rel < relend; rel++)
356 {
357 int r_type;
358 reloc_howto_type *howto;
359 unsigned long r_symndx;
360 Elf_Internal_Sym *sym;
361 asection *sec;
362 struct elf_link_hash_entry *h;
363 bfd_vma relocation;
364 bfd_reloc_status_type r;
365
366 r_symndx = ELF32_R_SYM (rel->r_info);
367 r_type = ELF32_R_TYPE (rel->r_info);
368 howto = elf_mn10200_howto_table + r_type;
369
370 if (info->relocateable)
371 {
372 /* This is a relocateable link. We don't have to change
373 anything, unless the reloc is against a section symbol,
374 in which case we have to adjust according to where the
375 section symbol winds up in the output section. */
376 if (r_symndx < symtab_hdr->sh_info)
377 {
378 sym = local_syms + r_symndx;
379 if (ELF_ST_TYPE (sym->st_info) == STT_SECTION)
380 {
381 sec = local_sections[r_symndx];
382 rel->r_addend += sec->output_offset + sym->st_value;
383 }
384 }
385
386 continue;
387 }
388
389 /* This is a final link. */
390 h = NULL;
391 sym = NULL;
392 sec = NULL;
393 if (r_symndx < symtab_hdr->sh_info)
394 {
395 sym = local_syms + r_symndx;
396 sec = local_sections[r_symndx];
397 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
398 }
399 else
400 {
401 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
402 while (h->root.type == bfd_link_hash_indirect
403 || h->root.type == bfd_link_hash_warning)
404 h = (struct elf_link_hash_entry *) h->root.u.i.link;
405 if (h->root.type == bfd_link_hash_defined
406 || h->root.type == bfd_link_hash_defweak)
407 {
408 sec = h->root.u.def.section;
409 relocation = (h->root.u.def.value
410 + sec->output_section->vma
411 + sec->output_offset);
412 }
413 else if (h->root.type == bfd_link_hash_undefweak)
414 relocation = 0;
415 else
416 {
417 if (! ((*info->callbacks->undefined_symbol)
418 (info, h->root.root.string, input_bfd,
419 input_section, rel->r_offset, true)))
420 return false;
421 relocation = 0;
422 }
423 }
424
425 r = mn10200_elf_final_link_relocate (howto, input_bfd, output_bfd,
426 input_section,
427 contents, rel->r_offset,
428 relocation, rel->r_addend,
429 info, sec, h == NULL);
430
431 if (r != bfd_reloc_ok)
432 {
433 const char *name;
434 const char *msg = (const char *) 0;
435
436 if (h != NULL)
437 name = h->root.root.string;
438 else
439 {
440 name = (bfd_elf_string_from_elf_section
441 (input_bfd, symtab_hdr->sh_link, sym->st_name));
442 if (name == NULL || *name == '\0')
443 name = bfd_section_name (input_bfd, sec);
444 }
445
446 switch (r)
447 {
448 case bfd_reloc_overflow:
449 if (! ((*info->callbacks->reloc_overflow)
450 (info, name, howto->name, (bfd_vma) 0,
451 input_bfd, input_section, rel->r_offset)))
452 return false;
453 break;
454
455 case bfd_reloc_undefined:
456 if (! ((*info->callbacks->undefined_symbol)
457 (info, name, input_bfd, input_section,
458 rel->r_offset, true)))
459 return false;
460 break;
461
462 case bfd_reloc_outofrange:
463 msg = _("internal error: out of range error");
464 goto common_error;
465
466 case bfd_reloc_notsupported:
467 msg = _("internal error: unsupported relocation error");
468 goto common_error;
469
470 case bfd_reloc_dangerous:
471 msg = _("internal error: dangerous error");
472 goto common_error;
473
474 default:
475 msg = _("internal error: unknown error");
476 /* fall through */
477
478 common_error:
479 if (!((*info->callbacks->warning)
480 (info, msg, name, input_bfd, input_section,
481 rel->r_offset)))
482 return false;
483 break;
484 }
485 }
486 }
487
488 return true;
489 }
490
491 /* This function handles relaxing for the mn10200.
492
493 There's quite a few relaxing opportunites available on the mn10200:
494
495 * jsr:24 -> jsr:16 2 bytes
496
497 * jmp:24 -> jmp:16 2 bytes
498 * jmp:16 -> bra:8 1 byte
499
500 * If the previous instruction is a conditional branch
501 around the jump/bra, we may be able to reverse its condition
502 and change its target to the jump's target. The jump/bra
503 can then be deleted. 2 bytes
504
505 * mov abs24 -> mov abs16 2 byte savings
506
507 * Most instructions which accept imm24 can relax to imm16 2 bytes
508 - Most instructions which accept imm16 can relax to imm8 1 byte
509
510 * Most instructions which accept d24 can relax to d16 2 bytes
511 - Most instructions which accept d16 can relax to d8 1 byte
512
513 abs24, imm24, d24 all look the same at the reloc level. It
514 might make the code simpler if we had different relocs for
515 the various relaxable operand types.
516
517 We don't handle imm16->imm8 or d16->d8 as they're very rare
518 and somewhat more difficult to support. */
519
520 static boolean
521 mn10200_elf_relax_section (abfd, sec, link_info, again)
522 bfd *abfd;
523 asection *sec;
524 struct bfd_link_info *link_info;
525 boolean *again;
526 {
527 Elf_Internal_Shdr *symtab_hdr;
528 Elf_Internal_Shdr *shndx_hdr;
529 Elf_Internal_Rela *internal_relocs;
530 Elf_Internal_Rela *free_relocs = NULL;
531 Elf_Internal_Rela *irel, *irelend;
532 bfd_byte *contents = NULL;
533 bfd_byte *free_contents = NULL;
534 Elf32_External_Sym *extsyms = NULL;
535 Elf32_External_Sym *free_extsyms = NULL;
536 Elf_External_Sym_Shndx *shndx_buf = NULL;
537
538 /* Assume nothing changes. */
539 *again = false;
540
541 /* We don't have to do anything for a relocateable link, if
542 this section does not have relocs, or if this is not a
543 code section. */
544 if (link_info->relocateable
545 || (sec->flags & SEC_RELOC) == 0
546 || sec->reloc_count == 0
547 || (sec->flags & SEC_CODE) == 0)
548 return true;
549
550 /* If this is the first time we have been called for this section,
551 initialize the cooked size. */
552 if (sec->_cooked_size == 0)
553 sec->_cooked_size = sec->_raw_size;
554
555 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
556 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
557
558 /* Get a copy of the native relocations. */
559 internal_relocs = (_bfd_elf32_link_read_relocs
560 (abfd, sec, (PTR) NULL, (Elf_Internal_Rela *) NULL,
561 link_info->keep_memory));
562 if (internal_relocs == NULL)
563 goto error_return;
564 if (! link_info->keep_memory)
565 free_relocs = internal_relocs;
566
567 /* Walk through them looking for relaxing opportunities. */
568 irelend = internal_relocs + sec->reloc_count;
569 for (irel = internal_relocs; irel < irelend; irel++)
570 {
571 bfd_vma symval;
572
573 /* If this isn't something that can be relaxed, then ignore
574 this reloc. */
575 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_NONE
576 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_8
577 || ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_MAX)
578 continue;
579
580 /* Get the section contents if we haven't done so already. */
581 if (contents == NULL)
582 {
583 /* Get cached copy if it exists. */
584 if (elf_section_data (sec)->this_hdr.contents != NULL)
585 contents = elf_section_data (sec)->this_hdr.contents;
586 else
587 {
588 /* Go get them off disk. */
589 contents = (bfd_byte *) bfd_malloc (sec->_raw_size);
590 if (contents == NULL)
591 goto error_return;
592 free_contents = contents;
593
594 if (! bfd_get_section_contents (abfd, sec, contents,
595 (file_ptr) 0, sec->_raw_size))
596 goto error_return;
597 }
598 }
599
600 /* Read this BFD's symbols if we haven't done so already. */
601 if (extsyms == NULL)
602 {
603 /* Get cached copy if it exists. */
604 if (symtab_hdr->contents != NULL)
605 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
606 else
607 {
608 /* Go get them off disk. */
609 bfd_size_type amt;
610
611 amt = symtab_hdr->sh_info;
612 amt *= sizeof (Elf32_External_Sym);
613 extsyms = (Elf32_External_Sym *) bfd_malloc (amt);
614 if (extsyms == NULL)
615 goto error_return;
616 free_extsyms = extsyms;
617 if (bfd_seek (abfd, symtab_hdr->sh_offset, SEEK_SET) != 0
618 || bfd_bread ((PTR) extsyms, amt, abfd) != amt)
619 goto error_return;
620 symtab_hdr->contents = (bfd_byte *) extsyms;
621 }
622
623 if (shndx_hdr->sh_size != 0)
624 {
625 bfd_size_type amt;
626
627 amt = symtab_hdr->sh_info;
628 amt *= sizeof (Elf_External_Sym_Shndx);
629 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
630 if (shndx_buf == NULL)
631 goto error_return;
632 if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0
633 || bfd_bread ((PTR) shndx_buf, amt, abfd) != amt)
634 goto error_return;
635 shndx_hdr->contents = (bfd_byte *) shndx_buf;
636 }
637 }
638
639 /* Get the value of the symbol referred to by the reloc. */
640 if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
641 {
642 /* A local symbol. */
643 Elf32_External_Sym *esym;
644 Elf_External_Sym_Shndx *shndx;
645 Elf_Internal_Sym isym;
646 asection *sym_sec;
647
648 esym = extsyms + ELF32_R_SYM (irel->r_info);
649 shndx = shndx_buf + (shndx_buf ? ELF32_R_SYM (irel->r_info) : 0);
650 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
651
652 if (isym.st_shndx == SHN_UNDEF)
653 sym_sec = bfd_und_section_ptr;
654 else if (isym.st_shndx == SHN_ABS)
655 sym_sec = bfd_abs_section_ptr;
656 else if (isym.st_shndx == SHN_COMMON)
657 sym_sec = bfd_com_section_ptr;
658 else
659 sym_sec = bfd_section_from_elf_index (abfd, isym.st_shndx);
660 symval = (isym.st_value
661 + sym_sec->output_section->vma
662 + sym_sec->output_offset);
663 }
664 else
665 {
666 unsigned long indx;
667 struct elf_link_hash_entry *h;
668
669 /* An external symbol. */
670 indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
671 h = elf_sym_hashes (abfd)[indx];
672 BFD_ASSERT (h != NULL);
673 if (h->root.type != bfd_link_hash_defined
674 && h->root.type != bfd_link_hash_defweak)
675 {
676 /* This appears to be a reference to an undefined
677 symbol. Just ignore it--it will be caught by the
678 regular reloc processing. */
679 continue;
680 }
681
682 symval = (h->root.u.def.value
683 + h->root.u.def.section->output_section->vma
684 + h->root.u.def.section->output_offset);
685 }
686
687 /* For simplicity of coding, we are going to modify the section
688 contents, the section relocs, and the BFD symbol table. We
689 must tell the rest of the code not to free up this
690 information. It would be possible to instead create a table
691 of changes which have to be made, as is done in coff-mips.c;
692 that would be more work, but would require less memory when
693 the linker is run. */
694
695 /* Try to turn a 24bit pc-relative branch/call into a 16bit pc-relative
696 branch/call. */
697 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL24)
698 {
699 bfd_vma value = symval;
700
701 /* Deal with pc-relative gunk. */
702 value -= (sec->output_section->vma + sec->output_offset);
703 value -= (irel->r_offset + 3);
704 value += irel->r_addend;
705
706 /* See if the value will fit in 16 bits, note the high value is
707 0x7fff + 2 as the target will be two bytes closer if we are
708 able to relax. */
709 if ((long) value < 0x8001 && (long) value > -0x8000)
710 {
711 unsigned char code;
712
713 /* Get the opcode. */
714 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
715
716 if (code != 0xe0 && code != 0xe1)
717 continue;
718
719 /* Note that we've changed the relocs, section contents, etc. */
720 elf_section_data (sec)->relocs = internal_relocs;
721 free_relocs = NULL;
722
723 elf_section_data (sec)->this_hdr.contents = contents;
724 free_contents = NULL;
725
726 free_extsyms = NULL;
727
728 /* Fix the opcode. */
729 if (code == 0xe0)
730 bfd_put_8 (abfd, 0xfc, contents + irel->r_offset - 2);
731 else if (code == 0xe1)
732 bfd_put_8 (abfd, 0xfd, contents + irel->r_offset - 2);
733
734 /* Fix the relocation's type. */
735 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
736 R_MN10200_PCREL16);
737
738 /* The opcode got shorter too, so we have to fix the offset. */
739 irel->r_offset -= 1;
740
741 /* Delete two bytes of data. */
742 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
743 irel->r_offset + 1, 2))
744 goto error_return;
745
746 /* That will change things, so, we should relax again.
747 Note that this is not required, and it may be slow. */
748 *again = true;
749 }
750 }
751
752 /* Try to turn a 16bit pc-relative branch into a 8bit pc-relative
753 branch. */
754 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL16)
755 {
756 bfd_vma value = symval;
757
758 /* Deal with pc-relative gunk. */
759 value -= (sec->output_section->vma + sec->output_offset);
760 value -= (irel->r_offset + 2);
761 value += irel->r_addend;
762
763 /* See if the value will fit in 8 bits, note the high value is
764 0x7f + 1 as the target will be one bytes closer if we are
765 able to relax. */
766 if ((long) value < 0x80 && (long) value > -0x80)
767 {
768 unsigned char code;
769
770 /* Get the opcode. */
771 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
772
773 if (code != 0xfc)
774 continue;
775
776 /* Note that we've changed the relocs, section contents, etc. */
777 elf_section_data (sec)->relocs = internal_relocs;
778 free_relocs = NULL;
779
780 elf_section_data (sec)->this_hdr.contents = contents;
781 free_contents = NULL;
782
783 free_extsyms = NULL;
784
785 /* Fix the opcode. */
786 bfd_put_8 (abfd, 0xea, contents + irel->r_offset - 1);
787
788 /* Fix the relocation's type. */
789 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
790 R_MN10200_PCREL8);
791
792 /* Delete one byte of data. */
793 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
794 irel->r_offset + 1, 1))
795 goto error_return;
796
797 /* That will change things, so, we should relax again.
798 Note that this is not required, and it may be slow. */
799 *again = true;
800 }
801 }
802
803 /* Try to eliminate an unconditional 8 bit pc-relative branch
804 which immediately follows a conditional 8 bit pc-relative
805 branch around the unconditional branch.
806
807 original: new:
808 bCC lab1 bCC' lab2
809 bra lab2
810 lab1: lab1:
811
812 This happens when the bCC can't reach lab2 at assembly time,
813 but due to other relaxations it can reach at link time. */
814 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_PCREL8)
815 {
816 Elf_Internal_Rela *nrel;
817 bfd_vma value = symval;
818 unsigned char code;
819
820 /* Deal with pc-relative gunk. */
821 value -= (sec->output_section->vma + sec->output_offset);
822 value -= (irel->r_offset + 1);
823 value += irel->r_addend;
824
825 /* Do nothing if this reloc is the last byte in the section. */
826 if (irel->r_offset == sec->_cooked_size)
827 continue;
828
829 /* See if the next instruction is an unconditional pc-relative
830 branch, more often than not this test will fail, so we
831 test it first to speed things up. */
832 code = bfd_get_8 (abfd, contents + irel->r_offset + 1);
833 if (code != 0xea)
834 continue;
835
836 /* Also make sure the next relocation applies to the next
837 instruction and that it's a pc-relative 8 bit branch. */
838 nrel = irel + 1;
839 if (nrel == irelend
840 || irel->r_offset + 2 != nrel->r_offset
841 || ELF32_R_TYPE (nrel->r_info) != (int) R_MN10200_PCREL8)
842 continue;
843
844 /* Make sure our destination immediately follows the
845 unconditional branch. */
846 if (symval != (sec->output_section->vma + sec->output_offset
847 + irel->r_offset + 3))
848 continue;
849
850 /* Now make sure we are a conditional branch. This may not
851 be necessary, but why take the chance.
852
853 Note these checks assume that R_MN10200_PCREL8 relocs
854 only occur on bCC and bCCx insns. If they occured
855 elsewhere, we'd need to know the start of this insn
856 for this check to be accurate. */
857 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
858 if (code != 0xe0 && code != 0xe1 && code != 0xe2
859 && code != 0xe3 && code != 0xe4 && code != 0xe5
860 && code != 0xe6 && code != 0xe7 && code != 0xe8
861 && code != 0xe9 && code != 0xec && code != 0xed
862 && code != 0xee && code != 0xef && code != 0xfc
863 && code != 0xfd && code != 0xfe && code != 0xff)
864 continue;
865
866 /* We also have to be sure there is no symbol/label
867 at the unconditional branch. */
868 if (mn10200_elf_symbol_address_p (abfd, sec, irel->r_offset + 1))
869 continue;
870
871 /* Note that we've changed the relocs, section contents, etc. */
872 elf_section_data (sec)->relocs = internal_relocs;
873 free_relocs = NULL;
874
875 elf_section_data (sec)->this_hdr.contents = contents;
876 free_contents = NULL;
877
878 free_extsyms = NULL;
879
880 /* Reverse the condition of the first branch. */
881 switch (code)
882 {
883 case 0xfc:
884 code = 0xfd;
885 break;
886 case 0xfd:
887 code = 0xfc;
888 break;
889 case 0xfe:
890 code = 0xff;
891 break;
892 case 0xff:
893 code = 0xfe;
894 break;
895 case 0xe8:
896 code = 0xe9;
897 break;
898 case 0xe9:
899 code = 0xe8;
900 break;
901 case 0xe0:
902 code = 0xe2;
903 break;
904 case 0xe2:
905 code = 0xe0;
906 break;
907 case 0xe3:
908 code = 0xe1;
909 break;
910 case 0xe1:
911 code = 0xe3;
912 break;
913 case 0xe4:
914 code = 0xe6;
915 break;
916 case 0xe6:
917 code = 0xe4;
918 break;
919 case 0xe7:
920 code = 0xe5;
921 break;
922 case 0xe5:
923 code = 0xe7;
924 break;
925 case 0xec:
926 code = 0xed;
927 break;
928 case 0xed:
929 code = 0xec;
930 break;
931 case 0xee:
932 code = 0xef;
933 break;
934 case 0xef:
935 code = 0xee;
936 break;
937 }
938 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
939
940 /* Set the reloc type and symbol for the first branch
941 from the second branch. */
942 irel->r_info = nrel->r_info;
943
944 /* Make the reloc for the second branch a null reloc. */
945 nrel->r_info = ELF32_R_INFO (ELF32_R_SYM (nrel->r_info),
946 R_MN10200_NONE);
947
948 /* Delete two bytes of data. */
949 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
950 irel->r_offset + 1, 2))
951 goto error_return;
952
953 /* That will change things, so, we should relax again.
954 Note that this is not required, and it may be slow. */
955 *again = true;
956 }
957
958 /* Try to turn a 24bit immediate, displacement or absolute address
959 into a 16bit immediate, displacement or absolute address. */
960 if (ELF32_R_TYPE (irel->r_info) == (int) R_MN10200_24)
961 {
962 bfd_vma value = symval;
963
964 /* See if the value will fit in 16 bits.
965 We allow any 16bit match here. We prune those we can't
966 handle below. */
967 if ((long) value < 0x7fff && (long) value > -0x8000)
968 {
969 unsigned char code;
970
971 /* All insns which have 24bit operands are 5 bytes long,
972 the first byte will always be 0xf4, but we double check
973 it just in case. */
974
975 /* Get the first opcode. */
976 code = bfd_get_8 (abfd, contents + irel->r_offset - 2);
977
978 if (code != 0xf4)
979 continue;
980
981 /* Get the second opcode. */
982 code = bfd_get_8 (abfd, contents + irel->r_offset - 1);
983
984 switch (code & 0xfc)
985 {
986 /* mov imm24,dn -> mov imm16,dn */
987 case 0x70:
988 /* Not safe if the high bit is on as relaxing may
989 move the value out of high mem and thus not fit
990 in a signed 16bit value. */
991 if (value & 0x8000)
992 continue;
993
994 /* Note that we've changed the reldection contents, etc. */
995 elf_section_data (sec)->relocs = internal_relocs;
996 free_relocs = NULL;
997
998 elf_section_data (sec)->this_hdr.contents = contents;
999 free_contents = NULL;
1000
1001 free_extsyms = NULL;
1002
1003 /* Fix the opcode. */
1004 bfd_put_8 (abfd, 0xf8 + (code & 0x03),
1005 contents + irel->r_offset - 2);
1006
1007 /* Fix the relocation's type. */
1008 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1009 R_MN10200_16);
1010
1011 /* The opcode got shorter too, so we have to fix the
1012 offset. */
1013 irel->r_offset -= 1;
1014
1015 /* Delete two bytes of data. */
1016 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1017 irel->r_offset + 1, 2))
1018 goto error_return;
1019
1020 /* That will change things, so, we should relax again.
1021 Note that this is not required, and it may be slow. */
1022 *again = true;
1023 break;
1024
1025 /* mov imm24,an -> mov imm16,an
1026 cmp imm24,an -> cmp imm16,an
1027 mov (abs24),dn -> mov (abs16),dn
1028 mov dn,(abs24) -> mov dn,(abs16)
1029 movb dn,(abs24) -> movb dn,(abs16)
1030 movbu (abs24),dn -> movbu (abs16),dn */
1031 case 0x74:
1032 case 0x7c:
1033 case 0xc0:
1034 case 0x40:
1035 case 0x44:
1036 case 0xc8:
1037 /* Note that we've changed the reldection contents, etc. */
1038 elf_section_data (sec)->relocs = internal_relocs;
1039 free_relocs = NULL;
1040
1041 elf_section_data (sec)->this_hdr.contents = contents;
1042 free_contents = NULL;
1043
1044 free_extsyms = NULL;
1045
1046 if ((code & 0xfc) == 0x74)
1047 code = 0xdc + (code & 0x03);
1048 else if ((code & 0xfc) == 0x7c)
1049 code = 0xec + (code & 0x03);
1050 else if ((code & 0xfc) == 0xc0)
1051 code = 0xc8 + (code & 0x03);
1052 else if ((code & 0xfc) == 0x40)
1053 code = 0xc0 + (code & 0x03);
1054 else if ((code & 0xfc) == 0x44)
1055 code = 0xc4 + (code & 0x03);
1056 else if ((code & 0xfc) == 0xc8)
1057 code = 0xcc + (code & 0x03);
1058
1059 /* Fix the opcode. */
1060 bfd_put_8 (abfd, code, contents + irel->r_offset - 2);
1061
1062 /* Fix the relocation's type. */
1063 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1064 R_MN10200_16);
1065
1066 /* The opcode got shorter too, so we have to fix the
1067 offset. */
1068 irel->r_offset -= 1;
1069
1070 /* Delete two bytes of data. */
1071 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1072 irel->r_offset + 1, 2))
1073 goto error_return;
1074
1075 /* That will change things, so, we should relax again.
1076 Note that this is not required, and it may be slow. */
1077 *again = true;
1078 break;
1079
1080 /* cmp imm24,dn -> cmp imm16,dn
1081 mov (abs24),an -> mov (abs16),an
1082 mov an,(abs24) -> mov an,(abs16)
1083 add imm24,dn -> add imm16,dn
1084 add imm24,an -> add imm16,an
1085 sub imm24,dn -> sub imm16,dn
1086 sub imm24,an -> sub imm16,an
1087 And all d24->d16 in memory ops. */
1088 case 0x78:
1089 case 0xd0:
1090 case 0x50:
1091 case 0x60:
1092 case 0x64:
1093 case 0x68:
1094 case 0x6c:
1095 case 0x80:
1096 case 0xf0:
1097 case 0x00:
1098 case 0x10:
1099 case 0xb0:
1100 case 0x30:
1101 case 0xa0:
1102 case 0x20:
1103 case 0x90:
1104 /* Not safe if the high bit is on as relaxing may
1105 move the value out of high mem and thus not fit
1106 in a signed 16bit value. */
1107 if (((code & 0xfc) == 0x78
1108 || (code & 0xfc) == 0x60
1109 || (code & 0xfc) == 0x64
1110 || (code & 0xfc) == 0x68
1111 || (code & 0xfc) == 0x6c
1112 || (code & 0xfc) == 0x80
1113 || (code & 0xfc) == 0xf0
1114 || (code & 0xfc) == 0x00
1115 || (code & 0xfc) == 0x10
1116 || (code & 0xfc) == 0xb0
1117 || (code & 0xfc) == 0x30
1118 || (code & 0xfc) == 0xa0
1119 || (code & 0xfc) == 0x20
1120 || (code & 0xfc) == 0x90)
1121 && (value & 0x8000) != 0)
1122 continue;
1123
1124 /* Note that we've changed the reldection contents, etc. */
1125 elf_section_data (sec)->relocs = internal_relocs;
1126 free_relocs = NULL;
1127
1128 elf_section_data (sec)->this_hdr.contents = contents;
1129 free_contents = NULL;
1130
1131 free_extsyms = NULL;
1132
1133 /* Fix the opcode. */
1134 bfd_put_8 (abfd, 0xf7, contents + irel->r_offset - 2);
1135
1136 if ((code & 0xfc) == 0x78)
1137 code = 0x48 + (code & 0x03);
1138 else if ((code & 0xfc) == 0xd0)
1139 code = 0x30 + (code & 0x03);
1140 else if ((code & 0xfc) == 0x50)
1141 code = 0x20 + (code & 0x03);
1142 else if ((code & 0xfc) == 0x60)
1143 code = 0x18 + (code & 0x03);
1144 else if ((code & 0xfc) == 0x64)
1145 code = 0x08 + (code & 0x03);
1146 else if ((code & 0xfc) == 0x68)
1147 code = 0x1c + (code & 0x03);
1148 else if ((code & 0xfc) == 0x6c)
1149 code = 0x0c + (code & 0x03);
1150 else if ((code & 0xfc) == 0x80)
1151 code = 0xc0 + (code & 0x07);
1152 else if ((code & 0xfc) == 0xf0)
1153 code = 0xb0 + (code & 0x07);
1154 else if ((code & 0xfc) == 0x00)
1155 code = 0x80 + (code & 0x07);
1156 else if ((code & 0xfc) == 0x10)
1157 code = 0xa0 + (code & 0x07);
1158 else if ((code & 0xfc) == 0xb0)
1159 code = 0x70 + (code & 0x07);
1160 else if ((code & 0xfc) == 0x30)
1161 code = 0x60 + (code & 0x07);
1162 else if ((code & 0xfc) == 0xa0)
1163 code = 0xd0 + (code & 0x07);
1164 else if ((code & 0xfc) == 0x20)
1165 code = 0x90 + (code & 0x07);
1166 else if ((code & 0xfc) == 0x90)
1167 code = 0x50 + (code & 0x07);
1168
1169 bfd_put_8 (abfd, code, contents + irel->r_offset - 1);
1170
1171 /* Fix the relocation's type. */
1172 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1173 R_MN10200_16);
1174
1175 /* Delete one bytes of data. */
1176 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1177 irel->r_offset + 2, 1))
1178 goto error_return;
1179
1180 /* That will change things, so, we should relax again.
1181 Note that this is not required, and it may be slow. */
1182 *again = true;
1183 break;
1184
1185 /* movb (abs24),dn ->movbu (abs16),dn extxb bn */
1186 case 0xc4:
1187 /* Note that we've changed the reldection contents, etc. */
1188 elf_section_data (sec)->relocs = internal_relocs;
1189 free_relocs = NULL;
1190
1191 elf_section_data (sec)->this_hdr.contents = contents;
1192 free_contents = NULL;
1193
1194 free_extsyms = NULL;
1195
1196 bfd_put_8 (abfd, 0xcc + (code & 0x03),
1197 contents + irel->r_offset - 2);
1198
1199 bfd_put_8 (abfd, 0xb8 + (code & 0x03),
1200 contents + irel->r_offset - 1);
1201
1202 /* Fix the relocation's type. */
1203 irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
1204 R_MN10200_16);
1205
1206 /* The reloc will be applied one byte in front of its
1207 current location. */
1208 irel->r_offset -= 1;
1209
1210 /* Delete one bytes of data. */
1211 if (!mn10200_elf_relax_delete_bytes (abfd, sec,
1212 irel->r_offset + 2, 1))
1213 goto error_return;
1214
1215 /* That will change things, so, we should relax again.
1216 Note that this is not required, and it may be slow. */
1217 *again = true;
1218 break;
1219 }
1220 }
1221 }
1222 }
1223
1224 if (free_relocs != NULL)
1225 free (free_relocs);
1226
1227 if (free_contents != NULL)
1228 {
1229 if (! link_info->keep_memory)
1230 free (free_contents);
1231 else
1232 {
1233 /* Cache the section contents for elf_link_input_bfd. */
1234 elf_section_data (sec)->this_hdr.contents = contents;
1235 }
1236 }
1237
1238 if (shndx_buf != NULL)
1239 {
1240 shndx_hdr->contents = NULL;
1241 free (shndx_buf);
1242 }
1243
1244 if (free_extsyms != NULL)
1245 {
1246 if (! link_info->keep_memory)
1247 {
1248 symtab_hdr->contents = NULL;
1249 free (free_extsyms);
1250 }
1251 }
1252
1253 return true;
1254
1255 error_return:
1256 if (free_relocs != NULL)
1257 free (free_relocs);
1258 if (free_contents != NULL)
1259 free (free_contents);
1260 if (shndx_buf != NULL)
1261 {
1262 shndx_hdr->contents = NULL;
1263 free (shndx_buf);
1264 }
1265 if (free_extsyms != NULL)
1266 {
1267 symtab_hdr->contents = NULL;
1268 free (free_extsyms);
1269 }
1270
1271 return false;
1272 }
1273
1274 /* Delete some bytes from a section while relaxing. */
1275
1276 static boolean
1277 mn10200_elf_relax_delete_bytes (abfd, sec, addr, count)
1278 bfd *abfd;
1279 asection *sec;
1280 bfd_vma addr;
1281 int count;
1282 {
1283 Elf_Internal_Shdr *symtab_hdr;
1284 Elf_Internal_Shdr *shndx_hdr;
1285 Elf32_External_Sym *extsyms;
1286 unsigned int sec_shndx;
1287 bfd_byte *contents;
1288 Elf_Internal_Rela *irel, *irelend;
1289 Elf_Internal_Rela *irelalign;
1290 bfd_vma toaddr;
1291 Elf32_External_Sym *esym, *esymend;
1292 Elf_External_Sym_Shndx *shndx;
1293 struct elf_link_hash_entry **sym_hashes;
1294 struct elf_link_hash_entry **end_hashes;
1295 unsigned int symcount;
1296
1297 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1298 extsyms = (Elf32_External_Sym *) symtab_hdr->contents;
1299
1300 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1301
1302 contents = elf_section_data (sec)->this_hdr.contents;
1303
1304 /* The deletion must stop at the next ALIGN reloc for an aligment
1305 power larger than the number of bytes we are deleting. */
1306
1307 irelalign = NULL;
1308 toaddr = sec->_cooked_size;
1309
1310 irel = elf_section_data (sec)->relocs;
1311 irelend = irel + sec->reloc_count;
1312
1313 /* Actually delete the bytes. */
1314 memmove (contents + addr, contents + addr + count,
1315 (size_t) (toaddr - addr - count));
1316 sec->_cooked_size -= count;
1317
1318 /* Adjust all the relocs. */
1319 for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
1320 {
1321 /* Get the new reloc address. */
1322 if ((irel->r_offset > addr
1323 && irel->r_offset < toaddr))
1324 irel->r_offset -= count;
1325 }
1326
1327 /* Adjust the local symbols defined in this section. */
1328 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1329 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1330 esym = extsyms;
1331 esymend = esym + symtab_hdr->sh_info;
1332 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
1333 {
1334 Elf_Internal_Sym isym;
1335 Elf_External_Sym_Shndx dummy;
1336
1337 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
1338
1339 if (isym.st_shndx == sec_shndx
1340 && isym.st_value > addr
1341 && isym.st_value < toaddr)
1342 {
1343 isym.st_value -= count;
1344 bfd_elf32_swap_symbol_out (abfd, &isym, (PTR) esym, (PTR) &dummy);
1345 }
1346 }
1347
1348 /* Now adjust the global symbols defined in this section. */
1349 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1350 - symtab_hdr->sh_info);
1351 sym_hashes = elf_sym_hashes (abfd);
1352 end_hashes = sym_hashes + symcount;
1353 for (; sym_hashes < end_hashes; sym_hashes++)
1354 {
1355 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1356 if ((sym_hash->root.type == bfd_link_hash_defined
1357 || sym_hash->root.type == bfd_link_hash_defweak)
1358 && sym_hash->root.u.def.section == sec
1359 && sym_hash->root.u.def.value > addr
1360 && sym_hash->root.u.def.value < toaddr)
1361 {
1362 sym_hash->root.u.def.value -= count;
1363 }
1364 }
1365
1366 return true;
1367 }
1368
1369 /* Return true if a symbol exists at the given address, else return
1370 false. */
1371 static boolean
1372 mn10200_elf_symbol_address_p (abfd, sec, addr)
1373 bfd *abfd;
1374 asection *sec;
1375 bfd_vma addr;
1376 {
1377 Elf_Internal_Shdr *symtab_hdr;
1378 Elf_Internal_Shdr *shndx_hdr;
1379 unsigned int sec_shndx;
1380 Elf32_External_Sym *esym, *esymend;
1381 Elf_External_Sym_Shndx *shndx;
1382 struct elf_link_hash_entry **sym_hashes;
1383 struct elf_link_hash_entry **end_hashes;
1384 unsigned int symcount;
1385
1386 sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
1387
1388 /* Examine all the symbols. */
1389 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
1390 shndx_hdr = &elf_tdata (abfd)->symtab_shndx_hdr;
1391 shndx = (Elf_External_Sym_Shndx *) shndx_hdr->contents;
1392 esym = (Elf32_External_Sym *) symtab_hdr->contents;
1393 esymend = esym + symtab_hdr->sh_info;
1394 for (; esym < esymend; esym++, shndx = (shndx ? shndx + 1 : NULL))
1395 {
1396 Elf_Internal_Sym isym;
1397
1398 bfd_elf32_swap_symbol_in (abfd, esym, shndx, &isym);
1399
1400 if (isym.st_shndx == sec_shndx
1401 && isym.st_value == addr)
1402 return true;
1403 }
1404
1405 symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
1406 - symtab_hdr->sh_info);
1407 sym_hashes = elf_sym_hashes (abfd);
1408 end_hashes = sym_hashes + symcount;
1409 for (; sym_hashes < end_hashes; sym_hashes++)
1410 {
1411 struct elf_link_hash_entry *sym_hash = *sym_hashes;
1412 if ((sym_hash->root.type == bfd_link_hash_defined
1413 || sym_hash->root.type == bfd_link_hash_defweak)
1414 && sym_hash->root.u.def.section == sec
1415 && sym_hash->root.u.def.value == addr)
1416 return true;
1417 }
1418
1419 return false;
1420 }
1421
1422 /* This is a version of bfd_generic_get_relocated_section_contents
1423 which uses mn10200_elf_relocate_section. */
1424
1425 static bfd_byte *
1426 mn10200_elf_get_relocated_section_contents (output_bfd, link_info, link_order,
1427 data, relocateable, symbols)
1428 bfd *output_bfd;
1429 struct bfd_link_info *link_info;
1430 struct bfd_link_order *link_order;
1431 bfd_byte *data;
1432 boolean relocateable;
1433 asymbol **symbols;
1434 {
1435 Elf_Internal_Shdr *symtab_hdr;
1436 Elf_Internal_Shdr *shndx_hdr;
1437 asection *input_section = link_order->u.indirect.section;
1438 bfd *input_bfd = input_section->owner;
1439 asection **sections = NULL;
1440 Elf_Internal_Rela *internal_relocs = NULL;
1441 Elf32_External_Sym *external_syms = NULL;
1442 Elf_External_Sym_Shndx *shndx_buf = NULL;
1443 Elf_External_Sym_Shndx *shndx;
1444 Elf_Internal_Sym *internal_syms = NULL;
1445
1446 /* We only need to handle the case of relaxing, or of having a
1447 particular set of section contents, specially. */
1448 if (relocateable
1449 || elf_section_data (input_section)->this_hdr.contents == NULL)
1450 return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
1451 link_order, data,
1452 relocateable,
1453 symbols);
1454
1455 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
1456 shndx_hdr = &elf_tdata (input_bfd)->symtab_shndx_hdr;
1457
1458 memcpy (data, elf_section_data (input_section)->this_hdr.contents,
1459 (size_t) input_section->_raw_size);
1460
1461 if ((input_section->flags & SEC_RELOC) != 0
1462 && input_section->reloc_count > 0)
1463 {
1464 Elf_Internal_Sym *isymp;
1465 asection **secpp;
1466 Elf32_External_Sym *esym, *esymend;
1467 bfd_size_type amt;
1468
1469 if (symtab_hdr->contents != NULL)
1470 external_syms = (Elf32_External_Sym *) symtab_hdr->contents;
1471 else if (symtab_hdr->sh_info != 0)
1472 {
1473 amt = symtab_hdr->sh_info;
1474 amt *= sizeof (Elf32_External_Sym);
1475 external_syms = (Elf32_External_Sym *) bfd_malloc (amt);
1476 if (external_syms == NULL)
1477 goto error_return;
1478 if (bfd_seek (input_bfd, symtab_hdr->sh_offset, SEEK_SET) != 0
1479 || bfd_bread ((PTR) external_syms, amt, input_bfd) != amt)
1480 goto error_return;
1481 }
1482
1483 if (symtab_hdr->sh_info != 0 && shndx_hdr->sh_size != 0)
1484 {
1485 amt = symtab_hdr->sh_info;
1486 amt *= sizeof (Elf_External_Sym_Shndx);
1487 shndx_buf = (Elf_External_Sym_Shndx *) bfd_malloc (amt);
1488 if (shndx_buf == NULL)
1489 goto error_return;
1490 if (bfd_seek (input_bfd, shndx_hdr->sh_offset, SEEK_SET) != 0
1491 || bfd_bread ((PTR) shndx_buf, amt, input_bfd) != amt)
1492 goto error_return;
1493 }
1494
1495 internal_relocs = (_bfd_elf32_link_read_relocs
1496 (input_bfd, input_section, (PTR) NULL,
1497 (Elf_Internal_Rela *) NULL, false));
1498 if (internal_relocs == NULL)
1499 goto error_return;
1500
1501 amt = symtab_hdr->sh_info;
1502 amt *= sizeof (Elf_Internal_Sym);
1503 internal_syms = (Elf_Internal_Sym *) bfd_malloc (amt);
1504 if (internal_syms == NULL && amt != 0)
1505 goto error_return;
1506
1507 amt = symtab_hdr->sh_info;
1508 amt *= sizeof (asection *);
1509 sections = (asection **) bfd_malloc (amt);
1510 if (sections == NULL && amt != 0)
1511 goto error_return;
1512
1513 for (isymp = internal_syms, secpp = sections, shndx = shndx_buf,
1514 esym = external_syms, esymend = esym + symtab_hdr->sh_info;
1515 esym < esymend;
1516 ++esym, ++isymp, ++secpp, shndx = (shndx ? shndx + 1 : NULL))
1517 {
1518 asection *isec;
1519
1520 bfd_elf32_swap_symbol_in (input_bfd, esym, shndx, isymp);
1521
1522 if (isymp->st_shndx == SHN_UNDEF)
1523 isec = bfd_und_section_ptr;
1524 else if (isymp->st_shndx == SHN_ABS)
1525 isec = bfd_abs_section_ptr;
1526 else if (isymp->st_shndx == SHN_COMMON)
1527 isec = bfd_com_section_ptr;
1528 else
1529 isec = bfd_section_from_elf_index (input_bfd, isymp->st_shndx);
1530
1531 *secpp = isec;
1532 }
1533
1534 if (! mn10200_elf_relocate_section (output_bfd, link_info, input_bfd,
1535 input_section, data, internal_relocs,
1536 internal_syms, sections))
1537 goto error_return;
1538
1539 if (sections != NULL)
1540 free (sections);
1541 if (internal_syms != NULL)
1542 free (internal_syms);
1543 if (shndx_buf != NULL)
1544 free (shndx_buf);
1545 if (external_syms != NULL && symtab_hdr->contents == NULL)
1546 free (external_syms);
1547 if (internal_relocs != elf_section_data (input_section)->relocs)
1548 free (internal_relocs);
1549 }
1550
1551 return data;
1552
1553 error_return:
1554 if (internal_relocs != NULL
1555 && internal_relocs != elf_section_data (input_section)->relocs)
1556 free (internal_relocs);
1557 if (shndx_buf != NULL)
1558 free (shndx_buf);
1559 if (external_syms != NULL && symtab_hdr->contents == NULL)
1560 free (external_syms);
1561 if (internal_syms != NULL)
1562 free (internal_syms);
1563 if (sections != NULL)
1564 free (sections);
1565 return NULL;
1566 }
1567
1568 #define TARGET_LITTLE_SYM bfd_elf32_mn10200_vec
1569 #define TARGET_LITTLE_NAME "elf32-mn10200"
1570 #define ELF_ARCH bfd_arch_mn10200
1571 #define ELF_MACHINE_CODE EM_MN10200
1572 #define ELF_MACHINE_ALT1 EM_CYGNUS_MN10200
1573 #define ELF_MAXPAGESIZE 0x1000
1574
1575 #define elf_info_to_howto mn10200_info_to_howto
1576 #define elf_info_to_howto_rel 0
1577 #define elf_backend_relocate_section mn10200_elf_relocate_section
1578 #define bfd_elf32_bfd_relax_section mn10200_elf_relax_section
1579 #define bfd_elf32_bfd_get_relocated_section_contents \
1580 mn10200_elf_get_relocated_section_contents
1581
1582 #define elf_symbol_leading_char '_'
1583
1584 #include "elf32-target.h"
This page took 0.0729 seconds and 4 git commands to generate.