* bfd.c (bfd_perror): Flush stdout before and stderr after printing
[deliverable/binutils-gdb.git] / bfd / elf32-dlx.c
1 /* DLX specific support for 32-bit ELF
2 Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2011
3 Free Software Foundation, Inc.
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 3 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, Inc., 51 Franklin Street - Fifth Floor, Boston,
20 MA 02110-1301, USA. */
21
22 #include "sysdep.h"
23 #include "bfd.h"
24 #include "libbfd.h"
25 #include "elf-bfd.h"
26 #include "elf/dlx.h"
27
28 #define USE_REL 1
29
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
35
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. */
41
42 static int skip_dlx_elf_hi16_reloc = 0;
43
44 extern int set_dlx_skip_hi16_flag (int);
45
46 int
47 set_dlx_skip_hi16_flag (int flag)
48 {
49 skip_dlx_elf_hi16_reloc = flag;
50 return flag;
51 }
52
53 static bfd_reloc_status_type
54 _bfd_dlx_elf_hi16_reloc (bfd *abfd,
55 arelent *reloc_entry,
56 asymbol *symbol,
57 void * data,
58 asection *input_section,
59 bfd *output_bfd,
60 char **error_message)
61 {
62 bfd_reloc_status_type ret;
63 bfd_vma relocation;
64
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);
71
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)
77 {
78 reloc_entry->address += input_section->output_offset;
79 return bfd_reloc_ok;
80 }
81
82 ret = bfd_reloc_ok;
83
84 if (bfd_is_und_section (symbol->section)
85 && output_bfd == (bfd *) NULL)
86 ret = bfd_reloc_undefined;
87
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);
93
94 if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
95 return bfd_reloc_outofrange;
96
97 bfd_put_16 (abfd, (short)((relocation >> 16) & 0xFFFF),
98 (bfd_byte *)data + reloc_entry->address);
99
100 return ret;
101 }
102
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. */
112
113 static bfd_reloc_status_type
114 elf32_dlx_relocate16 (bfd *abfd,
115 arelent *reloc_entry,
116 asymbol *symbol,
117 void * data,
118 asection *input_section,
119 bfd *output_bfd,
120 char **error_message ATTRIBUTE_UNUSED)
121 {
122 unsigned long insn, vallo, allignment;
123 int val;
124
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. */
129
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);
133
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;
138
139 /* Can not support a long jump to sections other then .text. */
140 if (strcmp (input_section->name, symbol->section->output_section->name) != 0)
141 {
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;
145 }
146
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;
150
151 if (vallo & 0x8000)
152 vallo = ~(vallo | 0xFFFF0000) + 1;
153
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);
158
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;
163
164 if (abs ((int) val) > 0x00007FFF)
165 return bfd_reloc_outofrange;
166
167 insn = (insn & 0xFFFF0000) | (val & 0x0000FFFF);
168
169 bfd_put_32 (abfd, insn,
170 (bfd_byte *) data + reloc_entry->address);
171
172 return bfd_reloc_ok;
173 }
174
175 static bfd_reloc_status_type
176 elf32_dlx_relocate26 (bfd *abfd,
177 arelent *reloc_entry,
178 asymbol *symbol,
179 void * data,
180 asection *input_section,
181 bfd *output_bfd,
182 char **error_message ATTRIBUTE_UNUSED)
183 {
184 unsigned long insn, vallo, allignment;
185 int val;
186
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. */
191
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);
195
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;
200
201 /* Can not support a long jump to sections other then .text */
202 if (strcmp (input_section->name, symbol->section->output_section->name) != 0)
203 {
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;
207 }
208
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;
212
213 if (vallo & 0x03000000)
214 vallo = ~(vallo | 0xFC000000) + 1;
215
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);
220
221 /* val is the displacement (PC relative to next instruction). */
222 val = (symbol->section->output_offset +
223 symbol->section->output_section->vma + symbol->value)
224 - vallo;
225
226 if (abs ((int) val) > 0x01FFFFFF)
227 return bfd_reloc_outofrange;
228
229 insn = (insn & 0xFC000000) | (val & 0x03FFFFFF);
230 bfd_put_32 (abfd, insn,
231 (bfd_byte *) data + reloc_entry->address);
232
233 return bfd_reloc_ok;
234 }
235
236 static reloc_howto_type dlx_elf_howto_table[]=
237 {
238 /* No relocation. */
239 HOWTO (R_DLX_NONE, /* Type. */
240 0, /* Rightshift. */
241 0, /* size (0 = byte, 1 = short, 2 = long). */
242 0, /* Bitsize. */
243 FALSE, /* PC_relative. */
244 0, /* Bitpos. */
245 complain_overflow_dont,/* Complain_on_overflow. */
246 bfd_elf_generic_reloc, /* Special_function. */
247 "R_DLX_NONE", /* Name. */
248 FALSE, /* Partial_inplace. */
249 0, /* Src_mask. */
250 0, /* Dst_mask. */
251 FALSE), /* PCrel_offset. */
252
253 /* 8 bit relocation. */
254 HOWTO (R_DLX_RELOC_8, /* Type. */
255 0, /* Rightshift. */
256 0, /* Size (0 = byte, 1 = short, 2 = long). */
257 8, /* Bitsize. */
258 FALSE, /* PC_relative. */
259 0, /* Bitpos. */
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. */
267
268 /* 16 bit relocation. */
269 HOWTO (R_DLX_RELOC_16, /* Type. */
270 0, /* Rightshift. */
271 1, /* Size (0 = byte, 1 = short, 2 = long). */
272 16, /* Bitsize. */
273 FALSE, /* PC_relative. */
274 0, /* Bitpos. */
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. */
282
283 /* 32 bit relocation. */
284 HOWTO (R_DLX_RELOC_32, /* Type. */
285 0, /* Rightshift. */
286 2, /* Size (0 = byte, 1 = short, 2 = long). */
287 32, /* Bitsize. */
288 FALSE, /* PC_relative. */
289 0, /* Bitpos. */
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. */
297
298 /* GNU extension to record C++ vtable hierarchy. */
299 HOWTO (R_DLX_GNU_VTINHERIT, /* Type. */
300 0, /* Rightshift. */
301 2, /* Size (0 = byte, 1 = short, 2 = long). */
302 0, /* Bitsize. */
303 FALSE, /* PC_relative. */
304 0, /* Bitpos. */
305 complain_overflow_dont,/* Complain_on_overflow. */
306 NULL, /* Special_function. */
307 "R_DLX_GNU_VTINHERIT", /* Name. */
308 FALSE, /* Partial_inplace. */
309 0, /* Src_mask. */
310 0, /* Dst_mask. */
311 FALSE), /* PCrel_offset. */
312
313 /* GNU extension to record C++ vtable member usage. */
314 HOWTO (R_DLX_GNU_VTENTRY, /* Type. */
315 0, /* Rightshift. */
316 2, /* Size (0 = byte, 1 = short, 2 = long). */
317 0, /* Bitsize. */
318 FALSE, /* PC_relative. */
319 0, /* Bitpos. */
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. */
324 0, /* Src_mask. */
325 0, /* Dst_mask. */
326 FALSE) /* PCrel_offset. */
327 };
328
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. */
332 0, /* Rightshift. */
333 1, /* Size (0 = byte, 1 = short, 2 = long). */
334 16, /* Bitsize. */
335 TRUE, /* PC_relative. */
336 0, /* Bitpos. */
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. */
344
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. */
348 0, /* Rightshift. */
349 2, /* Size (0 = byte, 1 = short, 2 = long). */
350 26, /* Bitsize. */
351 TRUE, /* PC_relative. */
352 0, /* Bitpos. */
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. */
360
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). */
366 32, /* Bitsize. */
367 FALSE, /* PC_relative. */
368 0, /* Bitpos. */
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. */
376
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. */
380 0, /* Rightshift. */
381 1, /* Size (0 = byte, 1 = short, 2 = long). */
382 16, /* Bitsize. */
383 FALSE, /* PC_relative. */
384 0, /* Bitpos. */
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. */
392
393 /* A mapping from BFD reloc types to DLX ELF reloc types.
394 Stolen from elf32-mips.c.
395
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. */
401 struct elf_reloc_map
402 {
403 bfd_reloc_code_real_type bfd_reloc_val;
404 enum elf_dlx_reloc_type elf_reloc_val;
405 };
406
407 static const struct elf_reloc_map dlx_reloc_map[] =
408 {
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 }
416 };
417
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. */
421
422 static bfd_boolean
423 elf32_dlx_check_relocs (bfd *abfd,
424 struct bfd_link_info *info,
425 asection *sec,
426 const Elf_Internal_Rela *relocs)
427 {
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;
432
433 if (info->relocatable)
434 return TRUE;
435
436 symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
437 sym_hashes = elf_sym_hashes (abfd);
438
439 rel_end = relocs + sec->reloc_count;
440 for (rel = relocs; rel < rel_end; rel++)
441 {
442 struct elf_link_hash_entry *h;
443 unsigned long r_symndx;
444
445 r_symndx = ELF32_R_SYM (rel->r_info);
446 if (r_symndx < symtab_hdr->sh_info)
447 h = NULL;
448 else
449 {
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;
454 }
455
456 switch (ELF32_R_TYPE (rel->r_info))
457 {
458 /* This relocation describes the C++ object vtable hierarchy.
459 Reconstruct it for later use during GC. */
460 case R_DLX_GNU_VTINHERIT:
461 if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
462 return FALSE;
463 break;
464
465 /* This relocation describes which C++ vtable entries are actually
466 used. Record for later use during GC. */
467 case R_DLX_GNU_VTENTRY:
468 BFD_ASSERT (h != NULL);
469 if (h != NULL
470 && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
471 return FALSE;
472 break;
473 }
474 }
475
476 return TRUE;
477 }
478
479 /* Given a BFD reloc type, return a howto structure. */
480
481 static reloc_howto_type *
482 elf32_dlx_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
483 bfd_reloc_code_real_type code)
484 {
485 unsigned int i;
486
487 for (i = 0; i < sizeof (dlx_reloc_map) / sizeof (struct elf_reloc_map); i++)
488 if (dlx_reloc_map[i].bfd_reloc_val == code)
489 return &dlx_elf_howto_table[(int) dlx_reloc_map[i].elf_reloc_val];
490
491 switch (code)
492 {
493 default:
494 bfd_set_error (bfd_error_bad_value);
495 return NULL;
496 case BFD_RELOC_16_PCREL_S2:
497 return &elf_dlx_gnu_rel16_s2;
498 case BFD_RELOC_DLX_JMP26:
499 return &elf_dlx_gnu_rel26_s2;
500 case BFD_RELOC_HI16_S:
501 return &elf_dlx_reloc_16_hi;
502 case BFD_RELOC_LO16:
503 return &elf_dlx_reloc_16_lo;
504 }
505 }
506
507 static reloc_howto_type *
508 elf32_dlx_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
509 const char *r_name)
510 {
511 unsigned int i;
512
513 for (i = 0;
514 i < sizeof (dlx_elf_howto_table) / sizeof (dlx_elf_howto_table[0]);
515 i++)
516 if (dlx_elf_howto_table[i].name != NULL
517 && strcasecmp (dlx_elf_howto_table[i].name, r_name) == 0)
518 return &dlx_elf_howto_table[i];
519
520 if (strcasecmp (elf_dlx_gnu_rel16_s2.name, r_name) == 0)
521 return &elf_dlx_gnu_rel16_s2;
522 if (strcasecmp (elf_dlx_gnu_rel26_s2.name, r_name) == 0)
523 return &elf_dlx_gnu_rel26_s2;
524 if (strcasecmp (elf_dlx_reloc_16_hi.name, r_name) == 0)
525 return &elf_dlx_reloc_16_hi;
526 if (strcasecmp (elf_dlx_reloc_16_lo.name, r_name) == 0)
527 return &elf_dlx_reloc_16_lo;
528
529 return NULL;
530 }
531
532 static reloc_howto_type *
533 dlx_rtype_to_howto (unsigned int r_type)
534 {
535 switch (r_type)
536 {
537 case R_DLX_RELOC_16_PCREL:
538 return & elf_dlx_gnu_rel16_s2;
539 case R_DLX_RELOC_26_PCREL:
540 return & elf_dlx_gnu_rel26_s2;
541 case R_DLX_RELOC_16_HI:
542 return & elf_dlx_reloc_16_hi;
543 case R_DLX_RELOC_16_LO:
544 return & elf_dlx_reloc_16_lo;
545 default:
546 BFD_ASSERT (r_type < (unsigned int) R_DLX_max);
547 return & dlx_elf_howto_table[r_type];
548 }
549 }
550
551 static void
552 elf32_dlx_info_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
553 arelent * cache_ptr ATTRIBUTE_UNUSED,
554 Elf_Internal_Rela * dst ATTRIBUTE_UNUSED)
555 {
556 abort ();
557 }
558
559 static void
560 elf32_dlx_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
561 arelent *cache_ptr,
562 Elf_Internal_Rela *dst)
563 {
564 unsigned int r_type;
565
566 r_type = ELF32_R_TYPE (dst->r_info);
567 cache_ptr->howto = dlx_rtype_to_howto (r_type);
568 return;
569 }
570
571 #define TARGET_BIG_SYM bfd_elf32_dlx_big_vec
572 #define TARGET_BIG_NAME "elf32-dlx"
573 #define ELF_ARCH bfd_arch_dlx
574 #define ELF_MACHINE_CODE EM_DLX
575 #define ELF_MAXPAGESIZE 1 /* FIXME: This number is wrong, It should be the page size in bytes. */
576
577 #include "elf32-target.h"
This page took 0.059633 seconds and 5 git commands to generate.