1 /* Infineon XC16X-specific support for 16-bit ELF.
2 Copyright 2006, 2007 Free Software Foundation, Inc.
3 Contributed by KPIT Cummins Infosystems
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, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
25 #include "elf/xc16x.h"
26 #include "elf/dwarf2.h"
27 #include "libiberty.h"
29 static reloc_howto_type xc16x_elf_howto_table [] =
31 /* This reloc does nothing. */
32 HOWTO (R_XC16X_NONE, /* type */
34 1, /* size (0 = byte, 1 = short, 2 = long) */
36 FALSE, /* pc_relative */
38 complain_overflow_bitfield, /* complain_on_overflow */
39 bfd_elf_generic_reloc, /* special_function */
40 "R_XC16X_NONE", /* name */
41 FALSE, /* partial_inplace */
44 FALSE), /* pcrel_offset */
46 /* An 8 bit absolute relocation. */
47 HOWTO (R_XC16X_ABS_8, /* type */
49 0, /* size (0 = byte, 1 = short, 2 = long) */
51 FALSE, /* pc_relative */
53 complain_overflow_bitfield, /* complain_on_overflow */
54 bfd_elf_generic_reloc, /* special_function */
55 "R_XC16X_ABS_8", /* name */
56 TRUE, /* partial_inplace */
57 0x0000, /* src_mask */
58 0x00ff, /* dst_mask */
59 FALSE), /* pcrel_offset */
61 /* A 16 bit absolute relocation. */
62 HOWTO (R_XC16X_ABS_16, /* type */
64 1, /* size (0 = byte, 1 = short, 2 = long) */
66 FALSE, /* pc_relative */
68 complain_overflow_dont, /* complain_on_overflow */
69 bfd_elf_generic_reloc, /* special_function */
70 "R_XC16X_ABS_16", /* name */
71 TRUE, /* partial_inplace */
72 0x00000000, /* src_mask */
73 0x0000ffff, /* dst_mask */
74 FALSE), /* pcrel_offset */
76 HOWTO (R_XC16X_ABS_32, /* type */
78 2, /* size (0 = byte, 1 = short, 2 = long) */
80 FALSE, /* pc_relative */
82 complain_overflow_bitfield, /* complain_on_overflow */
83 bfd_elf_generic_reloc, /* special_function */
84 "R_XC16X_ABS_32", /* name */
85 TRUE, /* partial_inplace */
86 0x00000000, /* src_mask */
87 0xffffffff, /* dst_mask */
88 FALSE), /* pcrel_offset */
91 /* A PC relative 8 bit relocation. */
92 HOWTO (R_XC16X_8_PCREL, /* type */
94 0, /* size (0 = byte, 1 = short, 2 = long) */
96 TRUE, /* pc_relative */
98 complain_overflow_signed, /* complain_on_overflow */
99 bfd_elf_generic_reloc, /* special_function */
100 "R_XC16X_8_PCREL", /* name */
101 FALSE, /* partial_inplace */
102 0x0000, /* src_mask */
103 0x00ff, /* dst_mask */
104 TRUE), /* pcrel_offset */
106 /* Relocation regarding page number. */
107 HOWTO (R_XC16X_PAG, /* type */
109 1, /* size (0 = byte, 1 = short, 2 = long) */
111 FALSE, /* pc_relative */
113 complain_overflow_signed, /* complain_on_overflow */
114 bfd_elf_generic_reloc, /* special_function */
115 "R_XC16X_PAG", /* name */
116 TRUE, /* partial_inplace */
117 0x00000000, /* src_mask */
118 0x0000ffff, /* dst_mask */
119 FALSE), /* pcrel_offset */
122 /* Relocation regarding page number. */
123 HOWTO (R_XC16X_POF, /* type */
125 1, /* size (0 = byte, 1 = short, 2 = long) */
127 FALSE, /* pc_relative */
129 complain_overflow_signed, /* complain_on_overflow */
130 bfd_elf_generic_reloc, /* special_function */
131 "R_XC16X_POF", /* name */
132 TRUE, /* partial_inplace */
133 0x00000000, /* src_mask */
134 0x0000ffff, /* dst_mask */
135 FALSE), /* pcrel_offset */
138 /* Relocation regarding segment number. */
139 HOWTO (R_XC16X_SEG, /* type */
141 1, /* size (0 = byte, 1 = short, 2 = long) */
143 FALSE, /* pc_relative */
145 complain_overflow_signed, /* complain_on_overflow */
146 bfd_elf_generic_reloc, /* special_function */
147 "R_XC16X_SEG", /* name */
148 TRUE, /* partial_inplace */
149 0x00000000, /* src_mask */
150 0x0000ffff, /* dst_mask */
151 FALSE), /* pcrel_offset */
153 /* Relocation regarding segment offset. */
154 HOWTO (R_XC16X_SOF, /* type */
156 1, /* size (0 = byte, 1 = short, 2 = long) */
158 FALSE, /* pc_relative */
160 complain_overflow_signed, /* complain_on_overflow */
161 bfd_elf_generic_reloc, /* special_function */
162 "R_XC16X_SOF", /* name */
163 TRUE, /* partial_inplace */
164 0x00000000, /* src_mask */
165 0x0000ffff, /* dst_mask */
166 FALSE) /* pcrel_offset */
170 /* Map BFD reloc types to XC16X ELF reloc types. */
172 struct xc16x_reloc_map
174 bfd_reloc_code_real_type bfd_reloc_val;
175 unsigned int xc16x_reloc_val;
178 static const struct xc16x_reloc_map xc16x_reloc_map [] =
180 { BFD_RELOC_NONE, R_XC16X_NONE },
181 { BFD_RELOC_8, R_XC16X_ABS_8 },
182 { BFD_RELOC_16, R_XC16X_ABS_16 },
183 { BFD_RELOC_32, R_XC16X_ABS_32 },
184 { BFD_RELOC_8_PCREL, R_XC16X_8_PCREL },
185 { BFD_RELOC_XC16X_PAG, R_XC16X_PAG},
186 { BFD_RELOC_XC16X_POF, R_XC16X_POF},
187 { BFD_RELOC_XC16X_SEG, R_XC16X_SEG},
188 { BFD_RELOC_XC16X_SOF, R_XC16X_SOF},
192 /* This function is used to search for correct relocation type from
195 static reloc_howto_type *
196 xc16x_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
197 bfd_reloc_code_real_type code)
201 for (i = ARRAY_SIZE (xc16x_reloc_map); --i;)
202 if (xc16x_reloc_map [i].bfd_reloc_val == code)
203 return & xc16x_elf_howto_table [xc16x_reloc_map[i].xc16x_reloc_val];
208 static reloc_howto_type *
209 xc16x_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
215 i < sizeof (xc16x_elf_howto_table) / sizeof (xc16x_elf_howto_table[0]);
217 if (xc16x_elf_howto_table[i].name != NULL
218 && strcasecmp (xc16x_elf_howto_table[i].name, r_name) == 0)
219 return &xc16x_elf_howto_table[i];
224 /* For a particular operand this function is
225 called to finalise the type of relocation. */
228 elf32_xc16x_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
229 Elf_Internal_Rela *elf_reloc)
234 r = ELF32_R_TYPE (elf_reloc->r_info);
235 for (i = 0; i < ARRAY_SIZE (xc16x_elf_howto_table); i++)
236 if (xc16x_elf_howto_table[i].type == r)
238 bfd_reloc->howto = &xc16x_elf_howto_table[i];
244 static bfd_reloc_status_type
245 elf32_xc16x_final_link_relocate (unsigned long r_type,
247 bfd *output_bfd ATTRIBUTE_UNUSED,
248 asection *input_section ATTRIBUTE_UNUSED,
253 struct bfd_link_info *info ATTRIBUTE_UNUSED,
254 asection *sym_sec ATTRIBUTE_UNUSED,
255 int is_local ATTRIBUTE_UNUSED)
257 bfd_byte *hit_data = contents + offset;
267 bfd_put_16 (input_bfd, value, hit_data);
270 case R_XC16X_8_PCREL:
271 bfd_put_8 (input_bfd, value, hit_data);
274 /* Following case is to find page number from actual
275 address for this divide value by 16k i.e. page size. */
280 bfd_put_16 (input_bfd, value, hit_data);
283 /* Following case is to find page offset from actual address
284 for this take modulo of value by 16k i.e. page size. */
289 bfd_put_16 (input_bfd, value, hit_data);
292 /* Following case is to find segment number from actual
293 address for this divide value by 64k i.e. segment size. */
298 bfd_put_16 (input_bfd, value, hit_data);
301 /* Following case is to find segment offset from actual address
302 for this take modulo of value by 64k i.e. segment size. */
307 bfd_put_16 (input_bfd, value, hit_data);
311 if (!strstr (input_section->name,".debug"))
319 bfd_put_32 (input_bfd, value, hit_data);
324 bfd_put_32 (input_bfd, value, hit_data);
329 return bfd_reloc_notsupported;
334 elf32_xc16x_relocate_section (bfd *output_bfd,
335 struct bfd_link_info *info,
337 asection *input_section,
339 Elf_Internal_Rela *relocs,
340 Elf_Internal_Sym *local_syms,
341 asection **local_sections)
343 Elf_Internal_Shdr *symtab_hdr;
344 struct elf_link_hash_entry **sym_hashes;
345 Elf_Internal_Rela *rel, *relend;
347 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
348 sym_hashes = elf_sym_hashes (input_bfd);
351 relend = relocs + input_section->reloc_count;
352 for (; rel < relend; rel++)
355 unsigned long r_symndx;
356 Elf_Internal_Sym *sym;
358 struct elf_link_hash_entry *h;
360 bfd_reloc_status_type r;
362 /* This is a final link. */
363 r_symndx = ELF32_R_SYM (rel->r_info);
364 r_type = ELF32_R_TYPE (rel->r_info);
368 if (r_symndx < symtab_hdr->sh_info)
370 sym = local_syms + r_symndx;
371 sec = local_sections[r_symndx];
372 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
376 bfd_boolean unresolved_reloc, warned;
378 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
379 r_symndx, symtab_hdr, sym_hashes,
381 unresolved_reloc, warned);
384 if (sec != NULL && elf_discarded_section (sec))
386 /* For relocs against symbols from removed linkonce sections,
387 or sections discarded by a linker script, we just want the
388 section contents zeroed. Avoid any special processing. */
389 reloc_howto_type *howto;
390 howto = xc16x_reloc_type_lookup (input_bfd, r_type);
391 _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
397 if (info->relocatable)
400 r = elf32_xc16x_final_link_relocate (r_type, input_bfd, output_bfd,
402 contents, rel->r_offset,
403 relocation, rel->r_addend,
404 info, sec, h == NULL);
412 elf32_xc16x_final_write_processing (bfd *abfd,
413 bfd_boolean linker ATTRIBUTE_UNUSED)
417 switch (bfd_get_mach (abfd))
424 case bfd_mach_xc16xl:
428 case bfd_mach_xc16xs:
433 elf_elfheader (abfd)->e_flags |= val;
437 elf32_xc16x_mach (flagword flags)
443 return bfd_mach_xc16x;
446 return bfd_mach_xc16xl;
449 return bfd_mach_xc16xs;
455 elf32_xc16x_object_p (bfd *abfd)
457 bfd_default_set_arch_mach (abfd, bfd_arch_xc16x,
458 elf32_xc16x_mach (elf_elfheader (abfd)->e_flags));
463 #define ELF_ARCH bfd_arch_xc16x
464 #define ELF_MACHINE_CODE EM_XC16X
465 #define ELF_MAXPAGESIZE 0x100
467 #define TARGET_LITTLE_SYM bfd_elf32_xc16x_vec
468 #define TARGET_LITTLE_NAME "elf32-xc16x"
469 #define elf_backend_final_write_processing elf32_xc16x_final_write_processing
470 #define elf_backend_object_p elf32_xc16x_object_p
471 #define elf_backend_can_gc_sections 1
472 #define bfd_elf32_bfd_reloc_type_lookup xc16x_reloc_type_lookup
473 #define bfd_elf32_bfd_reloc_name_lookup xc16x_reloc_name_lookup
474 #define elf_info_to_howto elf32_xc16x_info_to_howto
475 #define elf_info_to_howto_rel elf32_xc16x_info_to_howto
476 #define elf_backend_relocate_section elf32_xc16x_relocate_section
477 #define elf_backend_rela_normal 1
479 #include "elf32-target.h"