1 /* DLX specific support for 32-bit ELF
2 Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2011
3 Free Software Foundation, Inc.
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 3 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., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
30 #define bfd_elf32_bfd_reloc_type_lookup elf32_dlx_reloc_type_lookup
31 #define bfd_elf32_bfd_reloc_name_lookup elf32_dlx_reloc_name_lookup
32 #define elf_info_to_howto elf32_dlx_info_to_howto
33 #define elf_info_to_howto_rel elf32_dlx_info_to_howto_rel
34 #define elf_backend_check_relocs elf32_dlx_check_relocs
36 /* The gas default behavior is not to preform the %hi modifier so that the
37 GNU assembler can have the lower 16 bits offset placed in the insn, BUT
38 we do like the gas to indicate it is %hi reloc type so when we in the link
39 loader phase we can have the corrected hi16 vale replace the buggous lo16
40 value that was placed there by gas. */
42 static int skip_dlx_elf_hi16_reloc = 0;
44 extern int set_dlx_skip_hi16_flag (int);
47 set_dlx_skip_hi16_flag (int flag)
49 skip_dlx_elf_hi16_reloc = flag;
53 static bfd_reloc_status_type
54 _bfd_dlx_elf_hi16_reloc (bfd *abfd,
58 asection *input_section,
62 bfd_reloc_status_type ret;
65 /* If the skip flag is set then we simply do the generic relocating, this
66 is more of a hack for dlx gas/gld, so we do not need to do the %hi/%lo
67 fixup like mips gld did. */
68 if (skip_dlx_elf_hi16_reloc)
69 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
70 input_section, output_bfd, error_message);
72 /* If we're relocating, and this an external symbol, we don't want
73 to change anything. */
74 if (output_bfd != (bfd *) NULL
75 && (symbol->flags & BSF_SECTION_SYM) == 0
76 && reloc_entry->addend == 0)
78 reloc_entry->address += input_section->output_offset;
84 if (bfd_is_und_section (symbol->section)
85 && output_bfd == (bfd *) NULL)
86 ret = bfd_reloc_undefined;
88 relocation = (bfd_is_com_section (symbol->section)) ? 0 : symbol->value;
89 relocation += symbol->section->output_section->vma;
90 relocation += symbol->section->output_offset;
91 relocation += reloc_entry->addend;
92 relocation += bfd_get_16 (abfd, (bfd_byte *)data + reloc_entry->address);
94 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
95 return bfd_reloc_outofrange;
97 bfd_put_16 (abfd, (short)((relocation >> 16) & 0xFFFF),
98 (bfd_byte *)data + reloc_entry->address);
103 /* ELF relocs are against symbols. If we are producing relocatable
104 output, and the reloc is against an external symbol, and nothing
105 has given us any additional addend, the resulting reloc will also
106 be against the same symbol. In such a case, we don't want to
107 change anything about the way the reloc is handled, since it will
108 all be done at final link time. Rather than put special case code
109 into bfd_perform_relocation, all the reloc types use this howto
110 function. It just short circuits the reloc if producing
111 relocatable output against an external symbol. */
113 static bfd_reloc_status_type
114 elf32_dlx_relocate16 (bfd *abfd,
115 arelent *reloc_entry,
118 asection *input_section,
120 char **error_message ATTRIBUTE_UNUSED)
122 unsigned long insn, vallo, allignment;
125 /* HACK: I think this first condition is necessary when producing
126 relocatable output. After the end of HACK, the code is identical
127 to bfd_elf_generic_reloc(). I would _guess_ the first change
128 belongs there rather than here. martindo 1998-10-23. */
130 if (skip_dlx_elf_hi16_reloc)
131 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
132 input_section, output_bfd, error_message);
134 /* Check undefined section and undefined symbols. */
135 if (bfd_is_und_section (symbol->section)
136 && output_bfd == (bfd *) NULL)
137 return bfd_reloc_undefined;
139 /* Can not support a long jump to sections other then .text. */
140 if (strcmp (input_section->name, symbol->section->output_section->name) != 0)
142 (*_bfd_error_handler) (_("BFD Link Error: branch (PC rel16) to section (%s) not supported"),
143 symbol->section->output_section->name);
144 return bfd_reloc_undefined;
147 insn = bfd_get_32 (abfd, (bfd_byte *)data + reloc_entry->address);
148 allignment = 1 << (input_section->output_section->alignment_power - 1);
149 vallo = insn & 0x0000FFFF;
152 vallo = ~(vallo | 0xFFFF0000) + 1;
154 /* vallo points to the vma of next instruction. */
155 vallo += (((unsigned long)(input_section->output_section->vma +
156 input_section->output_offset) +
157 allignment) & ~allignment);
159 /* val is the displacement (PC relative to next instruction). */
160 val = (symbol->section->output_offset +
161 symbol->section->output_section->vma +
162 symbol->value) - vallo;
164 if (abs ((int) val) > 0x00007FFF)
165 return bfd_reloc_outofrange;
167 insn = (insn & 0xFFFF0000) | (val & 0x0000FFFF);
169 bfd_put_32 (abfd, insn,
170 (bfd_byte *) data + reloc_entry->address);
175 static bfd_reloc_status_type
176 elf32_dlx_relocate26 (bfd *abfd,
177 arelent *reloc_entry,
180 asection *input_section,
182 char **error_message ATTRIBUTE_UNUSED)
184 unsigned long insn, vallo, allignment;
187 /* HACK: I think this first condition is necessary when producing
188 relocatable output. After the end of HACK, the code is identical
189 to bfd_elf_generic_reloc(). I would _guess_ the first change
190 belongs there rather than here. martindo 1998-10-23. */
192 if (skip_dlx_elf_hi16_reloc)
193 return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
194 input_section, output_bfd, error_message);
196 /* Check undefined section and undefined symbols. */
197 if (bfd_is_und_section (symbol->section)
198 && output_bfd == (bfd *) NULL)
199 return bfd_reloc_undefined;
201 /* Can not support a long jump to sections other then .text */
202 if (strcmp (input_section->name, symbol->section->output_section->name) != 0)
204 (*_bfd_error_handler) (_("BFD Link Error: jump (PC rel26) to section (%s) not supported"),
205 symbol->section->output_section->name);
206 return bfd_reloc_undefined;
209 insn = bfd_get_32 (abfd, (bfd_byte *)data + reloc_entry->address);
210 allignment = 1 << (input_section->output_section->alignment_power - 1);
211 vallo = insn & 0x03FFFFFF;
213 if (vallo & 0x03000000)
214 vallo = ~(vallo | 0xFC000000) + 1;
216 /* vallo is the vma for the next instruction. */
217 vallo += (((unsigned long) (input_section->output_section->vma +
218 input_section->output_offset) +
219 allignment) & ~allignment);
221 /* val is the displacement (PC relative to next instruction). */
222 val = (symbol->section->output_offset +
223 symbol->section->output_section->vma + symbol->value)
226 if (abs ((int) val) > 0x01FFFFFF)
227 return bfd_reloc_outofrange;
229 insn = (insn & 0xFC000000) | (val & 0x03FFFFFF);
230 bfd_put_32 (abfd, insn,
231 (bfd_byte *) data + reloc_entry->address);
236 static reloc_howto_type dlx_elf_howto_table[]=
239 HOWTO (R_DLX_NONE, /* Type. */
241 0, /* size (0 = byte, 1 = short, 2 = long). */
243 FALSE, /* PC_relative. */
245 complain_overflow_dont,/* Complain_on_overflow. */
246 bfd_elf_generic_reloc, /* Special_function. */
247 "R_DLX_NONE", /* Name. */
248 FALSE, /* Partial_inplace. */
251 FALSE), /* PCrel_offset. */
253 /* 8 bit relocation. */
254 HOWTO (R_DLX_RELOC_8, /* Type. */
256 0, /* Size (0 = byte, 1 = short, 2 = long). */
258 FALSE, /* PC_relative. */
260 complain_overflow_dont,/* Complain_on_overflow. */
261 bfd_elf_generic_reloc, /* Special_function. */
262 "R_DLX_RELOC_8", /* Name. */
263 TRUE, /* Partial_inplace. */
264 0xff, /* Src_mask. */
265 0xff, /* Dst_mask. */
266 FALSE), /* PCrel_offset. */
268 /* 16 bit relocation. */
269 HOWTO (R_DLX_RELOC_16, /* Type. */
271 1, /* Size (0 = byte, 1 = short, 2 = long). */
273 FALSE, /* PC_relative. */
275 complain_overflow_dont,/* Complain_on_overflow. */
276 bfd_elf_generic_reloc, /* Special_function. */
277 "R_DLX_RELOC_16", /* Name. */
278 TRUE, /* Partial_inplace. */
279 0xffff, /* Src_mask. */
280 0xffff, /* Dst_mask. */
281 FALSE), /* PCrel_offset. */
283 /* 32 bit relocation. */
284 HOWTO (R_DLX_RELOC_32, /* Type. */
286 2, /* Size (0 = byte, 1 = short, 2 = long). */
288 FALSE, /* PC_relative. */
290 complain_overflow_dont,/* Complain_on_overflow. */
291 bfd_elf_generic_reloc, /* Special_function. */
292 "R_DLX_RELOC_32", /* Name. */
293 TRUE, /* Partial_inplace. */
294 0xffffffff, /* Src_mask. */
295 0xffffffff, /* Dst_mask. */
296 FALSE), /* PCrel_offset. */
298 /* GNU extension to record C++ vtable hierarchy. */
299 HOWTO (R_DLX_GNU_VTINHERIT, /* Type. */
301 2, /* Size (0 = byte, 1 = short, 2 = long). */
303 FALSE, /* PC_relative. */
305 complain_overflow_dont,/* Complain_on_overflow. */
306 NULL, /* Special_function. */
307 "R_DLX_GNU_VTINHERIT", /* Name. */
308 FALSE, /* Partial_inplace. */
311 FALSE), /* PCrel_offset. */
313 /* GNU extension to record C++ vtable member usage. */
314 HOWTO (R_DLX_GNU_VTENTRY, /* Type. */
316 2, /* Size (0 = byte, 1 = short, 2 = long). */
318 FALSE, /* PC_relative. */
320 complain_overflow_dont,/* Complain_on_overflow. */
321 _bfd_elf_rel_vtable_reloc_fn,/* Special_function. */
322 "R_DLX_GNU_VTENTRY", /* Name. */
323 FALSE, /* Partial_inplace. */
326 FALSE) /* PCrel_offset. */
329 /* 16 bit offset for pc-relative branches. */
330 static reloc_howto_type elf_dlx_gnu_rel16_s2 =
331 HOWTO (R_DLX_RELOC_16_PCREL, /* Type. */
333 1, /* Size (0 = byte, 1 = short, 2 = long). */
335 TRUE, /* PC_relative. */
337 complain_overflow_signed, /* Complain_on_overflow. */
338 elf32_dlx_relocate16, /* Special_function. */
339 "R_DLX_RELOC_16_PCREL",/* Name. */
340 TRUE, /* Partial_inplace. */
341 0xffff, /* Src_mask. */
342 0xffff, /* Dst_mask. */
343 TRUE); /* PCrel_offset. */
345 /* 26 bit offset for pc-relative branches. */
346 static reloc_howto_type elf_dlx_gnu_rel26_s2 =
347 HOWTO (R_DLX_RELOC_26_PCREL, /* Type. */
349 2, /* Size (0 = byte, 1 = short, 2 = long). */
351 TRUE, /* PC_relative. */
353 complain_overflow_dont,/* Complain_on_overflow. */
354 elf32_dlx_relocate26, /* Special_function. */
355 "R_DLX_RELOC_26_PCREL",/* Name. */
356 TRUE, /* Partial_inplace. */
357 0xffff, /* Src_mask. */
358 0xffff, /* Dst_mask. */
359 TRUE); /* PCrel_offset. */
361 /* High 16 bits of symbol value. */
362 static reloc_howto_type elf_dlx_reloc_16_hi =
363 HOWTO (R_DLX_RELOC_16_HI, /* Type. */
364 16, /* Rightshift. */
365 2, /* Size (0 = byte, 1 = short, 2 = long). */
367 FALSE, /* PC_relative. */
369 complain_overflow_dont,/* Complain_on_overflow. */
370 _bfd_dlx_elf_hi16_reloc,/* Special_function. */
371 "R_DLX_RELOC_16_HI", /* Name. */
372 TRUE, /* Partial_inplace. */
373 0xFFFF, /* Src_mask. */
374 0xffff, /* Dst_mask. */
375 FALSE); /* PCrel_offset. */
377 /* Low 16 bits of symbol value. */
378 static reloc_howto_type elf_dlx_reloc_16_lo =
379 HOWTO (R_DLX_RELOC_16_LO, /* Type. */
381 1, /* Size (0 = byte, 1 = short, 2 = long). */
383 FALSE, /* PC_relative. */
385 complain_overflow_dont,/* Complain_on_overflow. */
386 bfd_elf_generic_reloc, /* Special_function. */
387 "R_DLX_RELOC_16_LO", /* Name. */
388 TRUE, /* Partial_inplace. */
389 0xffff, /* Src_mask. */
390 0xffff, /* Dst_mask. */
391 FALSE); /* PCrel_offset. */
393 /* A mapping from BFD reloc types to DLX ELF reloc types.
394 Stolen from elf32-mips.c.
396 More about this table - for dlx elf relocation we do not really
397 need this table, if we have a rtype defined in this table will
398 caused tc_gen_relocate confused and die on us, but if we remove
399 this table it will caused more problem, so for now simple solution
400 is to remove those entries which may cause problem. */
403 bfd_reloc_code_real_type bfd_reloc_val;
404 enum elf_dlx_reloc_type elf_reloc_val;
407 static const struct elf_reloc_map dlx_reloc_map[] =
409 { BFD_RELOC_NONE, R_DLX_NONE },
410 { BFD_RELOC_16, R_DLX_RELOC_16 },
411 { BFD_RELOC_32, R_DLX_RELOC_32 },
412 { BFD_RELOC_DLX_HI16_S, R_DLX_RELOC_16_HI },
413 { BFD_RELOC_DLX_LO16, R_DLX_RELOC_16_LO },
414 { BFD_RELOC_VTABLE_INHERIT, R_DLX_GNU_VTINHERIT },
415 { BFD_RELOC_VTABLE_ENTRY, R_DLX_GNU_VTENTRY }
418 /* Look through the relocs for a section during the first phase.
419 Since we don't do .gots or .plts, we just need to consider the
420 virtual table relocs for gc. */
423 elf32_dlx_check_relocs (bfd *abfd,
424 struct bfd_link_info *info,
426 const Elf_Internal_Rela *relocs)
428 Elf_Internal_Shdr *symtab_hdr;
429 struct elf_link_hash_entry **sym_hashes;
430 const Elf_Internal_Rela *rel;
431 const Elf_Internal_Rela *rel_end;
433 if (info->relocatable)
436 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
437 sym_hashes = elf_sym_hashes (abfd);
439 rel_end = relocs + sec->reloc_count;
440 for (rel = relocs; rel < rel_end; rel++)
442 struct elf_link_hash_entry *h;
443 unsigned long r_symndx;
445 r_symndx = ELF32_R_SYM (rel->r_info);
446 if (r_symndx < symtab_hdr->sh_info)
450 h = sym_hashes[r_symndx - symtab_hdr->sh_info];
451 while (h->root.type == bfd_link_hash_indirect
452 || h->root.type == bfd_link_hash_warning)
453 h = (struct elf_link_hash_entry *) h->root.u.i.link;
455 /* PR15323, ref flags aren't set for references in the same
457 h->root.non_ir_ref = 1;
460 switch (ELF32_R_TYPE (rel->r_info))
462 /* This relocation describes the C++ object vtable hierarchy.
463 Reconstruct it for later use during GC. */
464 case R_DLX_GNU_VTINHERIT:
465 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
469 /* This relocation describes which C++ vtable entries are actually
470 used. Record for later use during GC. */
471 case R_DLX_GNU_VTENTRY:
472 BFD_ASSERT (h != NULL);
474 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
483 /* Given a BFD reloc type, return a howto structure. */
485 static reloc_howto_type *
486 elf32_dlx_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
487 bfd_reloc_code_real_type code)
491 for (i = 0; i < sizeof (dlx_reloc_map) / sizeof (struct elf_reloc_map); i++)
492 if (dlx_reloc_map[i].bfd_reloc_val == code)
493 return &dlx_elf_howto_table[(int) dlx_reloc_map[i].elf_reloc_val];
498 bfd_set_error (bfd_error_bad_value);
500 case BFD_RELOC_16_PCREL_S2:
501 return &elf_dlx_gnu_rel16_s2;
502 case BFD_RELOC_DLX_JMP26:
503 return &elf_dlx_gnu_rel26_s2;
504 case BFD_RELOC_HI16_S:
505 return &elf_dlx_reloc_16_hi;
507 return &elf_dlx_reloc_16_lo;
511 static reloc_howto_type *
512 elf32_dlx_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
518 i < sizeof (dlx_elf_howto_table) / sizeof (dlx_elf_howto_table[0]);
520 if (dlx_elf_howto_table[i].name != NULL
521 && strcasecmp (dlx_elf_howto_table[i].name, r_name) == 0)
522 return &dlx_elf_howto_table[i];
524 if (strcasecmp (elf_dlx_gnu_rel16_s2.name, r_name) == 0)
525 return &elf_dlx_gnu_rel16_s2;
526 if (strcasecmp (elf_dlx_gnu_rel26_s2.name, r_name) == 0)
527 return &elf_dlx_gnu_rel26_s2;
528 if (strcasecmp (elf_dlx_reloc_16_hi.name, r_name) == 0)
529 return &elf_dlx_reloc_16_hi;
530 if (strcasecmp (elf_dlx_reloc_16_lo.name, r_name) == 0)
531 return &elf_dlx_reloc_16_lo;
536 static reloc_howto_type *
537 dlx_rtype_to_howto (unsigned int r_type)
541 case R_DLX_RELOC_16_PCREL:
542 return & elf_dlx_gnu_rel16_s2;
543 case R_DLX_RELOC_26_PCREL:
544 return & elf_dlx_gnu_rel26_s2;
545 case R_DLX_RELOC_16_HI:
546 return & elf_dlx_reloc_16_hi;
547 case R_DLX_RELOC_16_LO:
548 return & elf_dlx_reloc_16_lo;
550 BFD_ASSERT (r_type < (unsigned int) R_DLX_max);
551 return & dlx_elf_howto_table[r_type];
556 elf32_dlx_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
557 arelent * cache_ptr ATTRIBUTE_UNUSED,
558 Elf_Internal_Rela * dst ATTRIBUTE_UNUSED)
564 elf32_dlx_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
566 Elf_Internal_Rela *dst)
570 r_type = ELF32_R_TYPE (dst->r_info);
571 cache_ptr->howto = dlx_rtype_to_howto (r_type);
575 #define TARGET_BIG_SYM bfd_elf32_dlx_big_vec
576 #define TARGET_BIG_NAME "elf32-dlx"
577 #define ELF_ARCH bfd_arch_dlx
578 #define ELF_MACHINE_CODE EM_DLX
579 #define ELF_MAXPAGESIZE 1 /* FIXME: This number is wrong, It should be the page size in bytes. */
581 #include "elf32-target.h"