bfd/
[deliverable/binutils-gdb.git] / bfd / elf32-xc16x.c
1 /* Infineon XC16X-specific support for 16-bit ELF.
2 Copyright 2006, 2007 Free Software Foundation, Inc.
3 Contributed by KPIT Cummins Infosystems
4
5 This file is part of BFD, the Binary File Descriptor library.
6
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
20
21 #include "sysdep.h"
22 #include "bfd.h"
23 #include "libbfd.h"
24 #include "elf-bfd.h"
25 #include "elf/xc16x.h"
26 #include "elf/dwarf2.h"
27 #include "libiberty.h"
28
29 static reloc_howto_type xc16x_elf_howto_table [] =
30 {
31 /* This reloc does nothing. */
32 HOWTO (R_XC16X_NONE, /* type */
33 0, /* rightshift */
34 1, /* size (0 = byte, 1 = short, 2 = long) */
35 16, /* bitsize */
36 FALSE, /* pc_relative */
37 0, /* bitpos */
38 complain_overflow_bitfield, /* complain_on_overflow */
39 bfd_elf_generic_reloc, /* special_function */
40 "R_XC16X_NONE", /* name */
41 FALSE, /* partial_inplace */
42 0, /* src_mask */
43 0, /* dst_mask */
44 FALSE), /* pcrel_offset */
45
46 /* An 8 bit absolute relocation. */
47 HOWTO (R_XC16X_ABS_8, /* type */
48 0, /* rightshift */
49 0, /* size (0 = byte, 1 = short, 2 = long) */
50 8, /* bitsize */
51 FALSE, /* pc_relative */
52 8, /* bitpos */
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 */
60
61 /* A 16 bit absolute relocation. */
62 HOWTO (R_XC16X_ABS_16, /* type */
63 0, /* rightshift */
64 1, /* size (0 = byte, 1 = short, 2 = long) */
65 16, /* bitsize */
66 FALSE, /* pc_relative */
67 0, /* bitpos */
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 */
75
76 HOWTO (R_XC16X_ABS_32, /* type */
77 0, /* rightshift */
78 2, /* size (0 = byte, 1 = short, 2 = long) */
79 32, /* bitsize */
80 FALSE, /* pc_relative */
81 0, /* bitpos */
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 */
89
90
91 /* A PC relative 8 bit relocation. */
92 HOWTO (R_XC16X_8_PCREL, /* type */
93 0, /* rightshift */
94 0, /* size (0 = byte, 1 = short, 2 = long) */
95 8, /* bitsize */
96 TRUE, /* pc_relative */
97 8, /* bitpos */
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 */
105
106 /* Relocation regarding page number. */
107 HOWTO (R_XC16X_PAG, /* type */
108 0, /* rightshift */
109 1, /* size (0 = byte, 1 = short, 2 = long) */
110 16, /* bitsize */
111 FALSE, /* pc_relative */
112 0, /* bitpos */
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 */
120
121
122 /* Relocation regarding page number. */
123 HOWTO (R_XC16X_POF, /* type */
124 0, /* rightshift */
125 1, /* size (0 = byte, 1 = short, 2 = long) */
126 16, /* bitsize */
127 FALSE, /* pc_relative */
128 0, /* bitpos */
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 */
136
137
138 /* Relocation regarding segment number. */
139 HOWTO (R_XC16X_SEG, /* type */
140 0, /* rightshift */
141 1, /* size (0 = byte, 1 = short, 2 = long) */
142 16, /* bitsize */
143 FALSE, /* pc_relative */
144 0, /* bitpos */
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 */
152
153 /* Relocation regarding segment offset. */
154 HOWTO (R_XC16X_SOF, /* type */
155 0, /* rightshift */
156 1, /* size (0 = byte, 1 = short, 2 = long) */
157 16, /* bitsize */
158 FALSE, /* pc_relative */
159 0, /* bitpos */
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 */
167 };
168
169
170 /* Map BFD reloc types to XC16X ELF reloc types. */
171
172 struct xc16x_reloc_map
173 {
174 bfd_reloc_code_real_type bfd_reloc_val;
175 unsigned int xc16x_reloc_val;
176 };
177
178 static const struct xc16x_reloc_map xc16x_reloc_map [] =
179 {
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},
189 };
190
191
192 /* This function is used to search for correct relocation type from
193 howto structure. */
194
195 static reloc_howto_type *
196 xc16x_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
197 bfd_reloc_code_real_type code)
198 {
199 unsigned int i;
200
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];
204
205 return NULL;
206 }
207
208 static reloc_howto_type *
209 xc16x_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
210 const char *r_name)
211 {
212 unsigned int i;
213
214 for (i = 0;
215 i < sizeof (xc16x_elf_howto_table) / sizeof (xc16x_elf_howto_table[0]);
216 i++)
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];
220
221 return NULL;
222 }
223
224 /* For a particular operand this function is
225 called to finalise the type of relocation. */
226
227 static void
228 elf32_xc16x_info_to_howto (bfd *abfd ATTRIBUTE_UNUSED, arelent *bfd_reloc,
229 Elf_Internal_Rela *elf_reloc)
230 {
231 unsigned int r;
232 unsigned int i;
233
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)
237 {
238 bfd_reloc->howto = &xc16x_elf_howto_table[i];
239 return;
240 }
241 abort ();
242 }
243
244 static bfd_reloc_status_type
245 elf32_xc16x_final_link_relocate (unsigned long r_type,
246 bfd *input_bfd,
247 bfd *output_bfd ATTRIBUTE_UNUSED,
248 asection *input_section ATTRIBUTE_UNUSED,
249 bfd_byte *contents,
250 bfd_vma offset,
251 bfd_vma value,
252 bfd_vma addend,
253 struct bfd_link_info *info ATTRIBUTE_UNUSED,
254 asection *sym_sec ATTRIBUTE_UNUSED,
255 int is_local ATTRIBUTE_UNUSED)
256 {
257 bfd_byte *hit_data = contents + offset;
258 bfd_vma val1;
259
260 switch (r_type)
261 {
262 case R_XC16X_NONE:
263 return bfd_reloc_ok;
264
265 case R_XC16X_ABS_16:
266 value += addend;
267 bfd_put_16 (input_bfd, value, hit_data);
268 return bfd_reloc_ok;
269
270 case R_XC16X_8_PCREL:
271 bfd_put_8 (input_bfd, value, hit_data);
272 return bfd_reloc_ok;
273
274 /* Following case is to find page number from actual
275 address for this divide value by 16k i.e. page size. */
276
277 case R_XC16X_PAG:
278 value += addend;
279 value /= 0x4000;
280 bfd_put_16 (input_bfd, value, hit_data);
281 return bfd_reloc_ok;
282
283 /* Following case is to find page offset from actual address
284 for this take modulo of value by 16k i.e. page size. */
285
286 case R_XC16X_POF:
287 value += addend;
288 value %= 0x4000;
289 bfd_put_16 (input_bfd, value, hit_data);
290 return bfd_reloc_ok;
291
292 /* Following case is to find segment number from actual
293 address for this divide value by 64k i.e. segment size. */
294
295 case R_XC16X_SEG:
296 value += addend;
297 value /= 0x10000;
298 bfd_put_16 (input_bfd, value, hit_data);
299 return bfd_reloc_ok;
300
301 /* Following case is to find segment offset from actual address
302 for this take modulo of value by 64k i.e. segment size. */
303
304 case R_XC16X_SOF:
305 value += addend;
306 value %= 0x10000;
307 bfd_put_16 (input_bfd, value, hit_data);
308 return bfd_reloc_ok;
309
310 case R_XC16X_ABS_32:
311 if (!strstr (input_section->name,".debug"))
312 {
313 value += addend;
314 val1 = value;
315 value %= 0x4000;
316 val1 /= 0x4000;
317 val1 = val1 << 16;
318 value += val1;
319 bfd_put_32 (input_bfd, value, hit_data);
320 }
321 else
322 {
323 value += addend;
324 bfd_put_32 (input_bfd, value, hit_data);
325 }
326 return bfd_reloc_ok;
327
328 default:
329 return bfd_reloc_notsupported;
330 }
331 }
332
333 static bfd_boolean
334 elf32_xc16x_relocate_section (bfd *output_bfd,
335 struct bfd_link_info *info,
336 bfd *input_bfd,
337 asection *input_section,
338 bfd_byte *contents,
339 Elf_Internal_Rela *relocs,
340 Elf_Internal_Sym *local_syms,
341 asection **local_sections)
342 {
343 Elf_Internal_Shdr *symtab_hdr;
344 struct elf_link_hash_entry **sym_hashes;
345 Elf_Internal_Rela *rel, *relend;
346
347 symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
348 sym_hashes = elf_sym_hashes (input_bfd);
349
350 rel = relocs;
351 relend = relocs + input_section->reloc_count;
352 for (; rel < relend; rel++)
353 {
354 unsigned int r_type;
355 unsigned long r_symndx;
356 Elf_Internal_Sym *sym;
357 asection *sec;
358 struct elf_link_hash_entry *h;
359 bfd_vma relocation;
360 bfd_reloc_status_type r;
361
362 /* This is a final link. */
363 r_symndx = ELF32_R_SYM (rel->r_info);
364 r_type = ELF32_R_TYPE (rel->r_info);
365 h = NULL;
366 sym = NULL;
367 sec = NULL;
368 if (r_symndx < symtab_hdr->sh_info)
369 {
370 sym = local_syms + r_symndx;
371 sec = local_sections[r_symndx];
372 relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
373 }
374 else
375 {
376 bfd_boolean unresolved_reloc, warned;
377
378 RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
379 r_symndx, symtab_hdr, sym_hashes,
380 h, sec, relocation,
381 unresolved_reloc, warned);
382 }
383
384 if (sec != NULL && elf_discarded_section (sec))
385 {
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);
392 rel->r_info = 0;
393 rel->r_addend = 0;
394 continue;
395 }
396
397 if (info->relocatable)
398 continue;
399
400 r = elf32_xc16x_final_link_relocate (r_type, input_bfd, output_bfd,
401 input_section,
402 contents, rel->r_offset,
403 relocation, rel->r_addend,
404 info, sec, h == NULL);
405 }
406
407 return TRUE;
408 }
409
410
411 static void
412 elf32_xc16x_final_write_processing (bfd *abfd,
413 bfd_boolean linker ATTRIBUTE_UNUSED)
414 {
415 unsigned long val;
416
417 switch (bfd_get_mach (abfd))
418 {
419 default:
420 case bfd_mach_xc16x:
421 val = 0x1000;
422 break;
423
424 case bfd_mach_xc16xl:
425 val = 0x1001;
426 break;
427
428 case bfd_mach_xc16xs:
429 val = 0x1002;
430 break;
431 }
432
433 elf_elfheader (abfd)->e_flags |= val;
434 }
435
436 static unsigned long
437 elf32_xc16x_mach (flagword flags)
438 {
439 switch (flags)
440 {
441 case 0x1000:
442 default:
443 return bfd_mach_xc16x;
444
445 case 0x1001:
446 return bfd_mach_xc16xl;
447
448 case 0x1002:
449 return bfd_mach_xc16xs;
450 }
451 }
452
453
454 static bfd_boolean
455 elf32_xc16x_object_p (bfd *abfd)
456 {
457 bfd_default_set_arch_mach (abfd, bfd_arch_xc16x,
458 elf32_xc16x_mach (elf_elfheader (abfd)->e_flags));
459 return TRUE;
460 }
461
462
463 #define ELF_ARCH bfd_arch_xc16x
464 #define ELF_MACHINE_CODE EM_XC16X
465 #define ELF_MAXPAGESIZE 0x100
466
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
478
479 #include "elf32-target.h"
This page took 0.040281 seconds and 5 git commands to generate.