1 /* BFD back-end for National Semiconductor's CRX ELF
2 Copyright 2004 Free Software Foundation, Inc.
3 Written by Tomer Levi, NSC, Israel.
5 This file is part of BFD, the Binary File Descriptor library.
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.
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.
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. */
28 static reloc_howto_type
*elf_crx_reloc_type_lookup
29 (bfd
*, bfd_reloc_code_real_type
);
30 static void elf_crx_info_to_howto
31 (bfd
*, arelent
*, Elf_Internal_Rela
*);
32 static bfd_boolean elf32_crx_relax_delete_bytes
33 (bfd
*, asection
*, bfd_vma
, int);
34 static bfd_reloc_status_type crx_elf_final_link_relocate
35 (reloc_howto_type
*, bfd
*, bfd
*, asection
*,
36 bfd_byte
*, bfd_vma
, bfd_vma
, bfd_vma
,
37 struct bfd_link_info
*, asection
*, int);
38 static bfd_boolean elf32_crx_relocate_section
39 (bfd
*, struct bfd_link_info
*, bfd
*, asection
*, bfd_byte
*,
40 Elf_Internal_Rela
*, Elf_Internal_Sym
*, asection
**);
41 static asection
* elf32_crx_gc_mark_hook
42 (asection
*, struct bfd_link_info
*, Elf_Internal_Rela
*,
43 struct elf_link_hash_entry
*, Elf_Internal_Sym
*);
44 static bfd_boolean elf32_crx_gc_sweep_hook
45 (bfd
*, struct bfd_link_info
*, asection
*,
46 const Elf_Internal_Rela
*);
47 static bfd_boolean elf32_crx_relax_section
48 (bfd
*, asection
*, struct bfd_link_info
*, bfd_boolean
*);
49 static bfd_byte
* elf32_crx_get_relocated_section_contents
50 (bfd
*, struct bfd_link_info
*, struct bfd_link_order
*,
51 bfd_byte
*, bfd_boolean
, asymbol
**);
53 /* crx_reloc_map array maps BFD relocation enum into a CRGAS relocation type. */
57 bfd_reloc_code_real_type bfd_reloc_enum
; /* BFD relocation enum. */
58 unsigned short crx_reloc_type
; /* CRX relocation type. */
61 static const struct crx_reloc_map crx_reloc_map
[R_CRX_MAX
] =
63 {BFD_RELOC_NONE
, R_CRX_NONE
},
64 {BFD_RELOC_CRX_REL4
, R_CRX_REL4
},
65 {BFD_RELOC_CRX_REL8
, R_CRX_REL8
},
66 {BFD_RELOC_CRX_REL8_CMP
, R_CRX_REL8_CMP
},
67 {BFD_RELOC_CRX_REL16
, R_CRX_REL16
},
68 {BFD_RELOC_CRX_REL24
, R_CRX_REL24
},
69 {BFD_RELOC_CRX_REL32
, R_CRX_REL32
},
70 {BFD_RELOC_CRX_REGREL12
, R_CRX_REGREL12
},
71 {BFD_RELOC_CRX_REGREL22
, R_CRX_REGREL22
},
72 {BFD_RELOC_CRX_REGREL28
, R_CRX_REGREL28
},
73 {BFD_RELOC_CRX_REGREL32
, R_CRX_REGREL32
},
74 {BFD_RELOC_CRX_ABS16
, R_CRX_ABS16
},
75 {BFD_RELOC_CRX_ABS32
, R_CRX_ABS32
},
76 {BFD_RELOC_CRX_NUM8
, R_CRX_NUM8
},
77 {BFD_RELOC_CRX_NUM16
, R_CRX_NUM16
},
78 {BFD_RELOC_CRX_NUM32
, R_CRX_NUM32
},
79 {BFD_RELOC_CRX_IMM16
, R_CRX_IMM16
},
80 {BFD_RELOC_CRX_IMM32
, R_CRX_IMM32
},
81 {BFD_RELOC_CRX_SWITCH8
, R_CRX_SWITCH8
},
82 {BFD_RELOC_CRX_SWITCH16
, R_CRX_SWITCH16
},
83 {BFD_RELOC_CRX_SWITCH32
, R_CRX_SWITCH32
}
86 static reloc_howto_type crx_elf_howto_table
[] =
88 HOWTO (R_CRX_NONE
, /* type */
92 FALSE
, /* pc_relative */
94 complain_overflow_dont
,/* complain_on_overflow */
95 bfd_elf_generic_reloc
, /* special_function */
96 "R_CRX_NONE", /* name */
97 FALSE
, /* partial_inplace */
100 FALSE
), /* pcrel_offset */
102 HOWTO (R_CRX_REL4
, /* type */
106 TRUE
, /* pc_relative */
108 complain_overflow_bitfield
,/* complain_on_overflow */
109 bfd_elf_generic_reloc
, /* special_function */
110 "R_CRX_REL4", /* name */
111 FALSE
, /* partial_inplace */
114 FALSE
), /* pcrel_offset */
116 HOWTO (R_CRX_REL8
, /* type */
120 TRUE
, /* pc_relative */
122 complain_overflow_bitfield
,/* complain_on_overflow */
123 bfd_elf_generic_reloc
, /* special_function */
124 "R_CRX_REL8", /* name */
125 FALSE
, /* partial_inplace */
128 FALSE
), /* pcrel_offset */
130 HOWTO (R_CRX_REL8_CMP
, /* type */
134 TRUE
, /* pc_relative */
136 complain_overflow_bitfield
,/* complain_on_overflow */
137 bfd_elf_generic_reloc
, /* special_function */
138 "R_CRX_REL8_CMP", /* name */
139 FALSE
, /* partial_inplace */
142 FALSE
), /* pcrel_offset */
144 HOWTO (R_CRX_REL16
, /* type */
148 TRUE
, /* pc_relative */
150 complain_overflow_bitfield
,/* complain_on_overflow */
151 bfd_elf_generic_reloc
, /* special_function */
152 "R_CRX_REL16", /* name */
153 FALSE
, /* partial_inplace */
154 0xffff, /* src_mask */
155 0xffff, /* dst_mask */
156 FALSE
), /* pcrel_offset */
158 HOWTO (R_CRX_REL24
, /* type */
162 TRUE
, /* pc_relative */
164 complain_overflow_bitfield
,/* complain_on_overflow */
165 bfd_elf_generic_reloc
, /* special_function */
166 "R_CRX_REL24", /* name */
167 FALSE
, /* partial_inplace */
168 0xffffff, /* src_mask */
169 0xffffff, /* dst_mask */
170 FALSE
), /* pcrel_offset */
172 HOWTO (R_CRX_REL32
, /* type */
176 TRUE
, /* pc_relative */
178 complain_overflow_bitfield
,/* complain_on_overflow */
179 bfd_elf_generic_reloc
, /* special_function */
180 "R_CRX_REL32", /* name */
181 FALSE
, /* partial_inplace */
182 0xffffffff, /* src_mask */
183 0xffffffff, /* dst_mask */
184 FALSE
), /* pcrel_offset */
186 HOWTO (R_CRX_REGREL12
, /* type */
190 FALSE
, /* pc_relative */
192 complain_overflow_bitfield
,/* complain_on_overflow */
193 bfd_elf_generic_reloc
, /* special_function */
194 "R_CRX_REGREL12", /* name */
195 FALSE
, /* partial_inplace */
196 0xfff, /* src_mask */
197 0xfff, /* dst_mask */
198 FALSE
), /* pcrel_offset */
200 HOWTO (R_CRX_REGREL22
, /* type */
204 FALSE
, /* pc_relative */
206 complain_overflow_bitfield
,/* complain_on_overflow */
207 bfd_elf_generic_reloc
, /* special_function */
208 "R_CRX_REGREL22", /* name */
209 FALSE
, /* partial_inplace */
210 0x3fffff, /* src_mask */
211 0x3fffff, /* dst_mask */
212 FALSE
), /* pcrel_offset */
214 HOWTO (R_CRX_REGREL28
, /* type */
218 FALSE
, /* pc_relative */
220 complain_overflow_bitfield
,/* complain_on_overflow */
221 bfd_elf_generic_reloc
, /* special_function */
222 "R_CRX_REGREL28", /* name */
223 FALSE
, /* partial_inplace */
224 0xfffffff, /* src_mask */
225 0xfffffff, /* dst_mask */
226 FALSE
), /* pcrel_offset */
228 HOWTO (R_CRX_REGREL32
, /* type */
232 FALSE
, /* pc_relative */
234 complain_overflow_bitfield
,/* complain_on_overflow */
235 bfd_elf_generic_reloc
, /* special_function */
236 "R_CRX_REGREL32", /* name */
237 FALSE
, /* partial_inplace */
238 0xffffffff, /* src_mask */
239 0xffffffff, /* dst_mask */
240 FALSE
), /* pcrel_offset */
242 HOWTO (R_CRX_ABS16
, /* type */
246 FALSE
, /* pc_relative */
248 complain_overflow_bitfield
,/* complain_on_overflow */
249 bfd_elf_generic_reloc
, /* special_function */
250 "R_CRX_ABS16", /* name */
251 FALSE
, /* partial_inplace */
252 0xffff, /* src_mask */
253 0xffff, /* dst_mask */
254 FALSE
), /* pcrel_offset */
256 HOWTO (R_CRX_ABS32
, /* type */
260 FALSE
, /* pc_relative */
262 complain_overflow_bitfield
,/* complain_on_overflow */
263 bfd_elf_generic_reloc
, /* special_function */
264 "R_CRX_ABS32", /* name */
265 FALSE
, /* partial_inplace */
266 0xffffffff, /* src_mask */
267 0xffffffff, /* dst_mask */
268 FALSE
), /* pcrel_offset */
270 HOWTO (R_CRX_NUM8
, /* type */
274 FALSE
, /* pc_relative */
276 complain_overflow_bitfield
,/* complain_on_overflow */
277 bfd_elf_generic_reloc
, /* special_function */
278 "R_CRX_NUM8", /* name */
279 FALSE
, /* partial_inplace */
282 FALSE
), /* pcrel_offset */
284 HOWTO (R_CRX_NUM16
, /* type */
288 FALSE
, /* pc_relative */
290 complain_overflow_bitfield
,/* complain_on_overflow */
291 bfd_elf_generic_reloc
, /* special_function */
292 "R_CRX_NUM16", /* name */
293 FALSE
, /* partial_inplace */
294 0xffff, /* src_mask */
295 0xffff, /* dst_mask */
296 FALSE
), /* pcrel_offset */
298 HOWTO (R_CRX_NUM32
, /* type */
302 FALSE
, /* pc_relative */
304 complain_overflow_bitfield
,/* complain_on_overflow */
305 bfd_elf_generic_reloc
, /* special_function */
306 "R_CRX_NUM32", /* name */
307 FALSE
, /* partial_inplace */
308 0xffffffff, /* src_mask */
309 0xffffffff, /* dst_mask */
310 FALSE
), /* pcrel_offset */
312 HOWTO (R_CRX_IMM16
, /* type */
316 FALSE
, /* pc_relative */
318 complain_overflow_bitfield
,/* complain_on_overflow */
319 bfd_elf_generic_reloc
, /* special_function */
320 "R_CRX_IMM16", /* name */
321 FALSE
, /* partial_inplace */
322 0xffff, /* src_mask */
323 0xffff, /* dst_mask */
324 FALSE
), /* pcrel_offset */
326 HOWTO (R_CRX_IMM32
, /* type */
330 FALSE
, /* pc_relative */
332 complain_overflow_bitfield
,/* complain_on_overflow */
333 bfd_elf_generic_reloc
, /* special_function */
334 "R_CRX_IMM32", /* name */
335 FALSE
, /* partial_inplace */
336 0xffffffff, /* src_mask */
337 0xffffffff, /* dst_mask */
338 FALSE
), /* pcrel_offset */
340 /* An 8 bit switch table entry. This is generated for an expression
341 such as ``.byte L1 - L2''. The offset holds the difference
342 between the reloc address and L2. */
343 HOWTO (R_CRX_SWITCH8
, /* type */
345 0, /* size (0 = byte, 1 = short, 2 = long) */
347 FALSE
, /* pc_relative */
349 complain_overflow_unsigned
, /* complain_on_overflow */
350 bfd_elf_generic_reloc
, /* special_function */
351 "R_CRX_SWITCH8", /* name */
352 FALSE
, /* partial_inplace */
355 TRUE
), /* pcrel_offset */
357 /* A 16 bit switch table entry. This is generated for an expression
358 such as ``.word L1 - L2''. The offset holds the difference
359 between the reloc address and L2. */
360 HOWTO (R_CRX_SWITCH16
, /* type */
362 1, /* size (0 = byte, 1 = short, 2 = long) */
364 FALSE
, /* pc_relative */
366 complain_overflow_unsigned
, /* complain_on_overflow */
367 bfd_elf_generic_reloc
, /* special_function */
368 "R_CRX_SWITCH16", /* name */
369 FALSE
, /* partial_inplace */
370 0xffff, /* src_mask */
371 0xffff, /* dst_mask */
372 TRUE
), /* pcrel_offset */
374 /* A 32 bit switch table entry. This is generated for an expression
375 such as ``.long L1 - L2''. The offset holds the difference
376 between the reloc address and L2. */
377 HOWTO (R_CRX_SWITCH32
, /* type */
379 2, /* size (0 = byte, 1 = short, 2 = long) */
381 FALSE
, /* pc_relative */
383 complain_overflow_unsigned
, /* complain_on_overflow */
384 bfd_elf_generic_reloc
, /* special_function */
385 "R_CRX_SWITCH32", /* name */
386 FALSE
, /* partial_inplace */
387 0xffffffff, /* src_mask */
388 0xffffffff, /* dst_mask */
389 TRUE
) /* pcrel_offset */
392 /* Retrieve a howto ptr using a BFD reloc_code. */
394 static reloc_howto_type
*
395 elf_crx_reloc_type_lookup (bfd
*abfd ATTRIBUTE_UNUSED
,
396 bfd_reloc_code_real_type code
)
400 for (i
= 0; i
< R_CRX_MAX
; i
++)
401 if (code
== crx_reloc_map
[i
].bfd_reloc_enum
)
402 return &crx_elf_howto_table
[crx_reloc_map
[i
].crx_reloc_type
];
404 printf ("This relocation Type is not supported -0x%x\n", code
);
408 /* Retrieve a howto ptr using an internal relocation entry. */
411 elf_crx_info_to_howto (bfd
*abfd ATTRIBUTE_UNUSED
, arelent
*cache_ptr
,
412 Elf_Internal_Rela
*dst
)
414 unsigned int r_type
= ELF32_R_TYPE (dst
->r_info
);
415 BFD_ASSERT (r_type
< (unsigned int) R_CRX_MAX
);
416 cache_ptr
->howto
= &crx_elf_howto_table
[r_type
];
419 /* Perform a relocation as part of a final link. */
421 static bfd_reloc_status_type
422 crx_elf_final_link_relocate (reloc_howto_type
*howto
, bfd
*input_bfd
,
423 bfd
*output_bfd ATTRIBUTE_UNUSED
,
424 asection
*input_section
, bfd_byte
*contents
,
425 bfd_vma offset
, bfd_vma Rvalue
, bfd_vma addend
,
426 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
427 asection
*sec ATTRIBUTE_UNUSED
,
428 int is_local ATTRIBUTE_UNUSED
)
430 unsigned short r_type
= howto
->type
;
431 bfd_byte
*hit_data
= contents
+ offset
;
432 bfd_vma reloc_bits
, check
;
448 /* 'hit_data' is relative to the start of the instruction, not the
449 relocation offset. Advance it to account for the exact offset. */
454 /* This relocation type is used only in 'Branch if Equal to 0'
455 instructions and requires special handling. */
466 /* We only care about the addend, where the difference between
467 expressions is kept. */
474 if (howto
->pc_relative
)
476 /* Subtract the address of the section containing the location. */
477 Rvalue
-= (input_section
->output_section
->vma
478 + input_section
->output_offset
);
479 /* Subtract the position of the location within the section. */
483 /* Add in supplied addend. */
486 /* Complain if the bitfield overflows, whether it is considered
487 as signed or unsigned. */
488 check
= Rvalue
>> howto
->rightshift
;
490 /* Assumes two's complement. This expression avoids
491 overflow if howto->bitsize is the number of bits in
493 reloc_bits
= (((1 << (howto
->bitsize
- 1)) - 1) << 1) | 1;
495 if (((bfd_vma
) check
& ~reloc_bits
) != 0
496 && (((bfd_vma
) check
& ~reloc_bits
)
497 != (-(bfd_vma
) 1 & ~reloc_bits
)))
499 /* The above right shift is incorrect for a signed
500 value. See if turning on the upper bits fixes the
502 if (howto
->rightshift
&& (bfd_signed_vma
) Rvalue
< 0)
504 check
|= ((bfd_vma
) - 1
506 >> howto
->rightshift
));
507 if (((bfd_vma
) check
& ~reloc_bits
)
508 != (-(bfd_vma
) 1 & ~reloc_bits
))
509 return bfd_reloc_overflow
;
512 return bfd_reloc_overflow
;
515 /* Drop unwanted bits from the value we are relocating to. */
516 Rvalue
>>= (bfd_vma
) howto
->rightshift
;
518 /* Apply dst_mask to select only relocatable part of the insn. */
519 Rvalue
&= howto
->dst_mask
;
524 if (r_type
== R_CRX_REL4
)
527 Rvalue
|= (bfd_get_8 (input_bfd
, hit_data
) & 0x0f);
530 bfd_put_8 (input_bfd
, (unsigned char) Rvalue
, hit_data
);
534 if (r_type
== R_CRX_REGREL12
)
535 Rvalue
|= (bfd_get_16 (input_bfd
, hit_data
) & 0xf000);
537 bfd_put_16 (input_bfd
, Rvalue
, hit_data
);
541 if (r_type
== R_CRX_REL24
542 || r_type
== R_CRX_REGREL22
543 || r_type
== R_CRX_REGREL28
)
544 Rvalue
|= (((bfd_get_16 (input_bfd
, hit_data
) << 16) |
545 bfd_get_16 (input_bfd
, hit_data
+ 2)) & ~howto
->dst_mask
);
547 if (r_type
== R_CRX_NUM32
|| r_type
== R_CRX_SWITCH32
)
548 /* Relocation on DATA is purely little-endian, that is, for a
549 multi-byte datum, the lowest address in memory contains the
550 little end of the datum, that is, the least significant byte.
551 Therefore we use BFD's byte Putting functions. */
552 bfd_put_32 (input_bfd
, Rvalue
, hit_data
);
554 /* Relocation on INSTRUCTIONS is different : Instructions are
555 word-addressable, that is, each word itself is arranged according
556 to little-endian convention, whereas the words are arranged with
557 respect to one another in BIG ENDIAN fashion.
558 When there is an immediate value that spans a word boundary, it is
559 split in a big-endian way with respect to the words. */
561 bfd_put_16 (input_bfd
, (Rvalue
>> 16) & 0xffff, hit_data
);
562 bfd_put_16 (input_bfd
, Rvalue
& 0xffff, hit_data
+ 2);
567 return bfd_reloc_notsupported
;
573 /* Delete some bytes from a section while relaxing. */
576 elf32_crx_relax_delete_bytes (bfd
*abfd
, asection
*sec
,
577 bfd_vma addr
, int count
)
579 Elf_Internal_Shdr
*symtab_hdr
;
580 unsigned int sec_shndx
;
582 Elf_Internal_Rela
*irel
, *irelend
;
583 Elf_Internal_Rela
*irelalign
;
585 Elf_Internal_Sym
*isym
;
586 Elf_Internal_Sym
*isymend
;
587 struct elf_link_hash_entry
**sym_hashes
;
588 struct elf_link_hash_entry
**end_hashes
;
589 unsigned int symcount
;
591 sec_shndx
= _bfd_elf_section_from_bfd_section (abfd
, sec
);
593 contents
= elf_section_data (sec
)->this_hdr
.contents
;
595 /* The deletion must stop at the next ALIGN reloc for an aligment
596 power larger than the number of bytes we are deleting. */
601 irel
= elf_section_data (sec
)->relocs
;
602 irelend
= irel
+ sec
->reloc_count
;
604 /* Actually delete the bytes. */
605 memmove (contents
+ addr
, contents
+ addr
+ count
,
606 (size_t) (toaddr
- addr
- count
));
609 /* Adjust all the relocs. */
610 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
612 /* Get the new reloc address. */
613 if ((irel
->r_offset
> addr
614 && irel
->r_offset
< toaddr
))
615 irel
->r_offset
-= count
;
618 /* Adjust the local symbols defined in this section. */
619 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
620 isym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
621 for (isymend
= isym
+ symtab_hdr
->sh_info
; isym
< isymend
; isym
++)
623 if (isym
->st_shndx
== sec_shndx
624 && isym
->st_value
> addr
625 && isym
->st_value
< toaddr
)
627 /* Adjust the addend of SWITCH relocations in this section,
628 which reference this local symbol. */
629 for (irel
= elf_section_data (sec
)->relocs
; irel
< irelend
; irel
++)
631 unsigned long r_symndx
;
632 Elf_Internal_Sym
*rsym
;
633 bfd_vma addsym
, subsym
;
635 /* Skip if not a SWITCH relocation. */
636 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_CRX_SWITCH8
637 && ELF32_R_TYPE (irel
->r_info
) != (int) R_CRX_SWITCH16
638 && ELF32_R_TYPE (irel
->r_info
) != (int) R_CRX_SWITCH32
)
641 r_symndx
= ELF32_R_SYM (irel
->r_info
);
642 rsym
= (Elf_Internal_Sym
*) symtab_hdr
->contents
+ r_symndx
;
644 /* Skip if not the local adjusted symbol. */
648 addsym
= isym
->st_value
;
649 subsym
= addsym
- irel
->r_addend
;
651 /* Fix the addend only when -->> (addsym > addr >= subsym). */
653 irel
->r_addend
-= count
;
658 isym
->st_value
-= count
;
662 /* Now adjust the global symbols defined in this section. */
663 symcount
= (symtab_hdr
->sh_size
/ sizeof (Elf32_External_Sym
)
664 - symtab_hdr
->sh_info
);
665 sym_hashes
= elf_sym_hashes (abfd
);
666 end_hashes
= sym_hashes
+ symcount
;
668 for (; sym_hashes
< end_hashes
; sym_hashes
++)
670 struct elf_link_hash_entry
*sym_hash
= *sym_hashes
;
672 if ((sym_hash
->root
.type
== bfd_link_hash_defined
673 || sym_hash
->root
.type
== bfd_link_hash_defweak
)
674 && sym_hash
->root
.u
.def
.section
== sec
675 && sym_hash
->root
.u
.def
.value
> addr
676 && sym_hash
->root
.u
.def
.value
< toaddr
)
677 sym_hash
->root
.u
.def
.value
-= count
;
683 /* This is a version of bfd_generic_get_relocated_section_contents
684 which uses elf32_crx_relocate_section. */
687 elf32_crx_get_relocated_section_contents (bfd
*output_bfd
,
688 struct bfd_link_info
*link_info
,
689 struct bfd_link_order
*link_order
,
691 bfd_boolean relocatable
,
694 Elf_Internal_Shdr
*symtab_hdr
;
695 asection
*input_section
= link_order
->u
.indirect
.section
;
696 bfd
*input_bfd
= input_section
->owner
;
697 asection
**sections
= NULL
;
698 Elf_Internal_Rela
*internal_relocs
= NULL
;
699 Elf_Internal_Sym
*isymbuf
= NULL
;
701 /* We only need to handle the case of relaxing, or of having a
702 particular set of section contents, specially. */
704 || elf_section_data (input_section
)->this_hdr
.contents
== NULL
)
705 return bfd_generic_get_relocated_section_contents (output_bfd
, link_info
,
710 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
712 memcpy (data
, elf_section_data (input_section
)->this_hdr
.contents
,
713 (size_t) input_section
->size
);
715 if ((input_section
->flags
& SEC_RELOC
) != 0
716 && input_section
->reloc_count
> 0)
718 Elf_Internal_Sym
*isym
;
719 Elf_Internal_Sym
*isymend
;
723 internal_relocs
= (_bfd_elf_link_read_relocs
724 (input_bfd
, input_section
, (PTR
) NULL
,
725 (Elf_Internal_Rela
*) NULL
, FALSE
));
726 if (internal_relocs
== NULL
)
729 if (symtab_hdr
->sh_info
!= 0)
731 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
733 isymbuf
= bfd_elf_get_elf_syms (input_bfd
, symtab_hdr
,
734 symtab_hdr
->sh_info
, 0,
740 amt
= symtab_hdr
->sh_info
;
741 amt
*= sizeof (asection
*);
742 sections
= bfd_malloc (amt
);
743 if (sections
== NULL
&& amt
!= 0)
746 isymend
= isymbuf
+ symtab_hdr
->sh_info
;
747 for (isym
= isymbuf
, secpp
= sections
; isym
< isymend
; ++isym
, ++secpp
)
751 if (isym
->st_shndx
== SHN_UNDEF
)
752 isec
= bfd_und_section_ptr
;
753 else if (isym
->st_shndx
== SHN_ABS
)
754 isec
= bfd_abs_section_ptr
;
755 else if (isym
->st_shndx
== SHN_COMMON
)
756 isec
= bfd_com_section_ptr
;
758 isec
= bfd_section_from_elf_index (input_bfd
, isym
->st_shndx
);
763 if (! elf32_crx_relocate_section (output_bfd
, link_info
, input_bfd
,
764 input_section
, data
, internal_relocs
,
768 if (sections
!= NULL
)
771 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
773 if (elf_section_data (input_section
)->relocs
!= internal_relocs
)
774 free (internal_relocs
);
780 if (sections
!= NULL
)
783 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
785 if (internal_relocs
!= NULL
786 && elf_section_data (input_section
)->relocs
!= internal_relocs
)
787 free (internal_relocs
);
791 /* Relocate a CRX ELF section. */
794 elf32_crx_relocate_section (bfd
*output_bfd
, struct bfd_link_info
*info
,
795 bfd
*input_bfd
, asection
*input_section
,
796 bfd_byte
*contents
, Elf_Internal_Rela
*relocs
,
797 Elf_Internal_Sym
*local_syms
,
798 asection
**local_sections
)
800 Elf_Internal_Shdr
*symtab_hdr
;
801 struct elf_link_hash_entry
**sym_hashes
;
802 Elf_Internal_Rela
*rel
, *relend
;
804 if (info
->relocatable
)
807 symtab_hdr
= &elf_tdata (input_bfd
)->symtab_hdr
;
808 sym_hashes
= elf_sym_hashes (input_bfd
);
811 relend
= relocs
+ input_section
->reloc_count
;
812 for (; rel
< relend
; rel
++)
815 reloc_howto_type
*howto
;
816 unsigned long r_symndx
;
817 Elf_Internal_Sym
*sym
;
819 struct elf_link_hash_entry
*h
;
821 bfd_reloc_status_type r
;
823 r_symndx
= ELF32_R_SYM (rel
->r_info
);
824 r_type
= ELF32_R_TYPE (rel
->r_info
);
825 howto
= crx_elf_howto_table
+ (r_type
);
830 if (r_symndx
< symtab_hdr
->sh_info
)
832 sym
= local_syms
+ r_symndx
;
833 sec
= local_sections
[r_symndx
];
834 relocation
= _bfd_elf_rela_local_sym (output_bfd
, sym
, &sec
, rel
);
838 bfd_boolean unresolved_reloc
, warned
;
840 RELOC_FOR_GLOBAL_SYMBOL (info
, input_bfd
, input_section
, rel
,
841 r_symndx
, symtab_hdr
, sym_hashes
,
843 unresolved_reloc
, warned
);
846 r
= crx_elf_final_link_relocate (howto
, input_bfd
, output_bfd
,
848 contents
, rel
->r_offset
,
849 relocation
, rel
->r_addend
,
850 info
, sec
, h
== NULL
);
852 if (r
!= bfd_reloc_ok
)
855 const char *msg
= (const char *) 0;
858 name
= h
->root
.root
.string
;
861 name
= (bfd_elf_string_from_elf_section
862 (input_bfd
, symtab_hdr
->sh_link
, sym
->st_name
));
863 if (name
== NULL
|| *name
== '\0')
864 name
= bfd_section_name (input_bfd
, sec
);
869 case bfd_reloc_overflow
:
870 if (!((*info
->callbacks
->reloc_overflow
)
871 (info
, (h
? &h
->root
: NULL
), name
, howto
->name
,
872 (bfd_vma
) 0, input_bfd
, input_section
,
877 case bfd_reloc_undefined
:
878 if (!((*info
->callbacks
->undefined_symbol
)
879 (info
, name
, input_bfd
, input_section
,
880 rel
->r_offset
, TRUE
)))
884 case bfd_reloc_outofrange
:
885 msg
= _("internal error: out of range error");
888 case bfd_reloc_notsupported
:
889 msg
= _("internal error: unsupported relocation error");
892 case bfd_reloc_dangerous
:
893 msg
= _("internal error: dangerous error");
897 msg
= _("internal error: unknown error");
901 if (!((*info
->callbacks
->warning
)
902 (info
, msg
, name
, input_bfd
, input_section
,
913 /* This function handles relaxing for the CRX.
915 There's quite a few relaxing opportunites available on the CRX:
917 * bal/bcond:32 -> bal/bcond:16 2 bytes
918 * bcond:16 -> bcond:8 2 bytes
919 * cmpbcond:24 -> cmpbcond:8 2 bytes
920 * arithmetic imm32 -> arithmetic imm16 2 bytes
922 Symbol- and reloc-reading infrastructure copied from elf-m10200.c. */
925 elf32_crx_relax_section (bfd
*abfd
, asection
*sec
,
926 struct bfd_link_info
*link_info
, bfd_boolean
*again
)
928 Elf_Internal_Shdr
*symtab_hdr
;
929 Elf_Internal_Rela
*internal_relocs
;
930 Elf_Internal_Rela
*irel
, *irelend
;
931 bfd_byte
*contents
= NULL
;
932 Elf_Internal_Sym
*isymbuf
= NULL
;
934 /* Assume nothing changes. */
937 /* We don't have to do anything for a relocatable link, if
938 this section does not have relocs, or if this is not a
940 if (link_info
->relocatable
941 || (sec
->flags
& SEC_RELOC
) == 0
942 || sec
->reloc_count
== 0
943 || (sec
->flags
& SEC_CODE
) == 0)
946 symtab_hdr
= &elf_tdata (abfd
)->symtab_hdr
;
948 /* Get a copy of the native relocations. */
949 internal_relocs
= (_bfd_elf_link_read_relocs
950 (abfd
, sec
, (PTR
) NULL
, (Elf_Internal_Rela
*) NULL
,
951 link_info
->keep_memory
));
952 if (internal_relocs
== NULL
)
955 /* Walk through them looking for relaxing opportunities. */
956 irelend
= internal_relocs
+ sec
->reloc_count
;
957 for (irel
= internal_relocs
; irel
< irelend
; irel
++)
961 /* If this isn't something that can be relaxed, then ignore
963 if (ELF32_R_TYPE (irel
->r_info
) != (int) R_CRX_REL32
964 && ELF32_R_TYPE (irel
->r_info
) != (int) R_CRX_REL16
965 && ELF32_R_TYPE (irel
->r_info
) != (int) R_CRX_REL24
966 && ELF32_R_TYPE (irel
->r_info
) != (int) R_CRX_IMM32
)
969 /* Get the section contents if we haven't done so already. */
970 if (contents
== NULL
)
972 /* Get cached copy if it exists. */
973 if (elf_section_data (sec
)->this_hdr
.contents
!= NULL
)
974 contents
= elf_section_data (sec
)->this_hdr
.contents
;
975 /* Go get them off disk. */
976 else if (!bfd_malloc_and_get_section (abfd
, sec
, &contents
))
980 /* Read this BFD's local symbols if we haven't done so already. */
981 if (isymbuf
== NULL
&& symtab_hdr
->sh_info
!= 0)
983 isymbuf
= (Elf_Internal_Sym
*) symtab_hdr
->contents
;
985 isymbuf
= bfd_elf_get_elf_syms (abfd
, symtab_hdr
,
986 symtab_hdr
->sh_info
, 0,
992 /* Get the value of the symbol referred to by the reloc. */
993 if (ELF32_R_SYM (irel
->r_info
) < symtab_hdr
->sh_info
)
995 /* A local symbol. */
996 Elf_Internal_Sym
*isym
;
999 isym
= isymbuf
+ ELF32_R_SYM (irel
->r_info
);
1000 if (isym
->st_shndx
== SHN_UNDEF
)
1001 sym_sec
= bfd_und_section_ptr
;
1002 else if (isym
->st_shndx
== SHN_ABS
)
1003 sym_sec
= bfd_abs_section_ptr
;
1004 else if (isym
->st_shndx
== SHN_COMMON
)
1005 sym_sec
= bfd_com_section_ptr
;
1007 sym_sec
= bfd_section_from_elf_index (abfd
, isym
->st_shndx
);
1008 symval
= (isym
->st_value
1009 + sym_sec
->output_section
->vma
1010 + sym_sec
->output_offset
);
1015 struct elf_link_hash_entry
*h
;
1017 /* An external symbol. */
1018 indx
= ELF32_R_SYM (irel
->r_info
) - symtab_hdr
->sh_info
;
1019 h
= elf_sym_hashes (abfd
)[indx
];
1020 BFD_ASSERT (h
!= NULL
);
1022 if (h
->root
.type
!= bfd_link_hash_defined
1023 && h
->root
.type
!= bfd_link_hash_defweak
)
1024 /* This appears to be a reference to an undefined
1025 symbol. Just ignore it--it will be caught by the
1026 regular reloc processing. */
1029 symval
= (h
->root
.u
.def
.value
1030 + h
->root
.u
.def
.section
->output_section
->vma
1031 + h
->root
.u
.def
.section
->output_offset
);
1034 /* For simplicity of coding, we are going to modify the section
1035 contents, the section relocs, and the BFD symbol table. We
1036 must tell the rest of the code not to free up this
1037 information. It would be possible to instead create a table
1038 of changes which have to be made, as is done in coff-mips.c;
1039 that would be more work, but would require less memory when
1040 the linker is run. */
1042 /* Try to turn a 32bit pc-relative branch/call into
1043 a 16bit pc-relative branch/call. */
1044 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_CRX_REL32
)
1046 bfd_vma value
= symval
;
1048 /* Deal with pc-relative gunk. */
1049 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1050 value
-= irel
->r_offset
;
1051 value
+= irel
->r_addend
;
1053 /* See if the value will fit in 16 bits, note the high value is
1054 0xfffe + 2 as the target will be two bytes closer if we are
1056 if ((long) value
< 0x10000 && (long) value
> -0x10002)
1058 unsigned short code
;
1060 /* Get the opcode. */
1061 code
= (unsigned short) bfd_get_16 (abfd
, contents
+ irel
->r_offset
);
1063 /* Verify it's a 'bal'/'bcond' and fix the opcode. */
1064 if ((code
& 0xfff0) == 0x3170)
1065 bfd_put_8 (abfd
, 0x30, contents
+ irel
->r_offset
+ 1);
1066 else if ((code
& 0xf0ff) == 0x707f)
1067 bfd_put_8 (abfd
, 0x7e, contents
+ irel
->r_offset
);
1071 /* Note that we've changed the relocs, section contents, etc. */
1072 elf_section_data (sec
)->relocs
= internal_relocs
;
1073 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1074 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1076 /* Fix the relocation's type. */
1077 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1080 /* Delete two bytes of data. */
1081 if (!elf32_crx_relax_delete_bytes (abfd
, sec
,
1082 irel
->r_offset
+ 2, 2))
1085 /* That will change things, so, we should relax again.
1086 Note that this is not required, and it may be slow. */
1091 /* Try to turn a 16bit pc-relative branch into an
1092 8bit pc-relative branch. */
1093 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_CRX_REL16
)
1095 bfd_vma value
= symval
;
1097 /* Deal with pc-relative gunk. */
1098 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1099 value
-= irel
->r_offset
;
1100 value
+= irel
->r_addend
;
1102 /* See if the value will fit in 8 bits, note the high value is
1103 0xfc + 2 as the target will be two bytes closer if we are
1105 if ((long) value
< 0xfe && (long) value
> -0x100)
1107 unsigned short code
;
1109 /* Get the opcode. */
1110 code
= (unsigned short) bfd_get_16 (abfd
, contents
+ irel
->r_offset
);
1112 /* Verify it's a 'bcond' opcode. */
1113 if ((code
& 0xf0ff) != 0x707e)
1116 /* Note that we've changed the relocs, section contents, etc. */
1117 elf_section_data (sec
)->relocs
= internal_relocs
;
1118 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1119 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1121 /* Fix the relocation's type. */
1122 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1125 /* Delete two bytes of data. */
1126 if (!elf32_crx_relax_delete_bytes (abfd
, sec
,
1127 irel
->r_offset
+ 2, 2))
1130 /* That will change things, so, we should relax again.
1131 Note that this is not required, and it may be slow. */
1136 /* Try to turn a 24bit pc-relative cmp&branch into
1137 an 8bit pc-relative cmp&branch. */
1138 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_CRX_REL24
)
1140 bfd_vma value
= symval
;
1142 /* Deal with pc-relative gunk. */
1143 value
-= (sec
->output_section
->vma
+ sec
->output_offset
);
1144 value
-= irel
->r_offset
;
1145 value
+= irel
->r_addend
;
1147 /* See if the value will fit in 8 bits, note the high value is
1148 0x7e + 2 as the target will be two bytes closer if we are
1150 if ((long) value
< 0x100 && (long) value
> -0x100)
1152 unsigned short code
;
1154 /* Get the opcode. */
1155 code
= (unsigned short) bfd_get_16 (abfd
, contents
+ irel
->r_offset
);
1157 /* Verify it's a 'cmp&branch' opcode. */
1158 if ((code
& 0xfff0) != 0x3180 && (code
& 0xfff0) != 0x3190
1159 && (code
& 0xfff0) != 0x31a0 && (code
& 0xfff0) != 0x31c0
1160 && (code
& 0xfff0) != 0x31d0 && (code
& 0xfff0) != 0x31e0)
1163 /* Note that we've changed the relocs, section contents, etc. */
1164 elf_section_data (sec
)->relocs
= internal_relocs
;
1165 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1166 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1168 /* Fix the opcode. */
1169 bfd_put_8 (abfd
, 0x30, contents
+ irel
->r_offset
+ 1);
1171 /* Fix the relocation's type. */
1172 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1175 /* Delete two bytes of data. */
1176 if (!elf32_crx_relax_delete_bytes (abfd
, sec
,
1177 irel
->r_offset
+ 4, 2))
1180 /* That will change things, so, we should relax again.
1181 Note that this is not required, and it may be slow. */
1186 /* Try to turn a 32bit immediate address into
1187 a 16bit immediate address. */
1188 if (ELF32_R_TYPE (irel
->r_info
) == (int) R_CRX_IMM32
)
1190 bfd_vma value
= symval
;
1192 /* See if the value will fit in 16 bits. */
1193 if ((long) value
< 0x7fff && (long) value
> -0x8000)
1195 unsigned short code
;
1197 /* Get the opcode. */
1198 code
= (unsigned short) bfd_get_16 (abfd
, contents
+ irel
->r_offset
);
1200 /* Verify it's a 'arithmetic double'. */
1201 if ((code
& 0xf0f0) != 0x20f0)
1204 /* Note that we've changed the relocs, section contents, etc. */
1205 elf_section_data (sec
)->relocs
= internal_relocs
;
1206 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1207 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1209 /* Fix the opcode. */
1210 bfd_put_8 (abfd
, (code
& 0xff) - 0x10, contents
+ irel
->r_offset
);
1212 /* Fix the relocation's type. */
1213 irel
->r_info
= ELF32_R_INFO (ELF32_R_SYM (irel
->r_info
),
1216 /* Delete two bytes of data. */
1217 if (!elf32_crx_relax_delete_bytes (abfd
, sec
,
1218 irel
->r_offset
+ 2, 2))
1221 /* That will change things, so, we should relax again.
1222 Note that this is not required, and it may be slow. */
1229 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1231 if (! link_info
->keep_memory
)
1235 /* Cache the symbols for elf_link_input_bfd. */
1236 symtab_hdr
->contents
= (unsigned char *) isymbuf
;
1240 if (contents
!= NULL
1241 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1243 if (! link_info
->keep_memory
)
1247 /* Cache the section contents for elf_link_input_bfd. */
1248 elf_section_data (sec
)->this_hdr
.contents
= contents
;
1252 if (internal_relocs
!= NULL
1253 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1254 free (internal_relocs
);
1260 && symtab_hdr
->contents
!= (unsigned char *) isymbuf
)
1262 if (contents
!= NULL
1263 && elf_section_data (sec
)->this_hdr
.contents
!= contents
)
1265 if (internal_relocs
!= NULL
1266 && elf_section_data (sec
)->relocs
!= internal_relocs
)
1267 free (internal_relocs
);
1273 elf32_crx_gc_mark_hook (asection
*sec
,
1274 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1275 Elf_Internal_Rela
*rel ATTRIBUTE_UNUSED
,
1276 struct elf_link_hash_entry
*h
,
1277 Elf_Internal_Sym
*sym
)
1280 return bfd_section_from_elf_index (sec
->owner
, sym
->st_shndx
);
1282 switch (h
->root
.type
)
1284 case bfd_link_hash_defined
:
1285 case bfd_link_hash_defweak
:
1286 return h
->root
.u
.def
.section
;
1288 case bfd_link_hash_common
:
1289 return h
->root
.u
.c
.p
->section
;
1296 /* Update the got entry reference counts for the section being removed. */
1299 elf32_crx_gc_sweep_hook (bfd
*abfd ATTRIBUTE_UNUSED
,
1300 struct bfd_link_info
*info ATTRIBUTE_UNUSED
,
1301 asection
*sec ATTRIBUTE_UNUSED
,
1302 const Elf_Internal_Rela
*relocs ATTRIBUTE_UNUSED
)
1304 /* We don't support garbage collection of GOT and PLT relocs yet. */
1308 /* Definitions for setting CRX target vector. */
1309 #define TARGET_LITTLE_SYM bfd_elf32_crx_vec
1310 #define TARGET_LITTLE_NAME "elf32-crx"
1311 #define ELF_ARCH bfd_arch_crx
1312 #define ELF_MACHINE_CODE EM_CRX
1313 #define ELF_MAXPAGESIZE 0x1
1314 #define elf_symbol_leading_char '_'
1316 #define bfd_elf32_bfd_reloc_type_lookup elf_crx_reloc_type_lookup
1317 #define elf_info_to_howto elf_crx_info_to_howto
1318 #define elf_info_to_howto_rel 0
1319 #define elf_backend_relocate_section elf32_crx_relocate_section
1320 #define bfd_elf32_bfd_relax_section elf32_crx_relax_section
1321 #define bfd_elf32_bfd_get_relocated_section_contents \
1322 elf32_crx_get_relocated_section_contents
1323 #define elf_backend_gc_mark_hook elf32_crx_gc_mark_hook
1324 #define elf_backend_gc_sweep_hook elf32_crx_gc_sweep_hook
1325 #define elf_backend_can_gc_sections 1
1326 #define elf_backend_rela_normal 1
1328 #include "elf32-target.h"