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