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